Skip to content

Commit

Permalink
<>=||~*x^ syntax (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
bend-n authored May 14, 2023
1 parent 3aed028 commit 7907a61
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 110 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ Cargo.lock
addons/

# Performance data
perf.data*
perf.data*

# cache
.gpm_cache
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "godot-package-manager"
version = "1.2.1"
version = "1.3.0"
edition = "2021"
authors = ["bendn <[email protected]>"]
description = "A package manager for godot"
Expand All @@ -24,13 +24,15 @@ sha1 = "0.10.5"
console = "0.15.4"
indicatif = "0.17.2"
anyhow = "1.0.68"
dialoguer = { version = "0.10.3", features = [] }
reqwest = { version = "0.11", features = [] }
tokio = { version = "1", features = ["full"] }
dialoguer = { version = "0.10.3", default-features = false, features = [] }
reqwest = "0.11"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread"] }
async-recursion = "1.0.2"
futures = "0.3"
semver_rs = { version = "0.2", features = ["serde"] }
semver_rs = "0.2"
async-trait = "0.1.66"
http-cache-reqwest = "0.8.0"
reqwest-middleware = "0.2.1"

[dev-dependencies]
glob = "0.3.0"
Expand Down
22 changes: 15 additions & 7 deletions godot.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
[
{
"name": "@bendn/test",
"integrity": "sha512-hyPGxDG8poa2ekmWr1BeTCUa7YaZYfhsN7jcLJ3q2cQVlowcTnzqmz4iV3t21QFyabE5R+rV+y6d5dAItrJeDw==",
"tarball": "https://registry.npmjs.org/@bendn/test/-/test-2.0.10.tgz",
"version": "2.0.10"
},
{
"name": "@bendn/gdcli",
"integrity": "sha512-/YOAd1+K4JlKvPTmpX8B7VWxGtFrxKq4R0A6u5qOaaVPK6uGsl4dGZaIHpxuqcurEcwPEOabkoShXKZaOXB0lw==",
"tarball": "https://registry.npmjs.org/@bendn/gdcli/-/gdcli-1.2.5.tgz",
"version": "1.2.5"
},
{
"name": "@bendn/splitter",
"tarball": "https://registry.npmjs.org/@bendn/splitter/-/splitter-1.0.6.tgz",
"version": "1.0.6"
},
{
"name": "@bendn/stockfish.gd",
"tarball": "https://registry.npmjs.org/@bendn/stockfish.gd/-/stockfish.gd-1.2.6.tgz",
"version": "1.2.6"
},
{
"name": "@bendn/test",
"tarball": "https://registry.npmjs.org/@bendn/test/-/test-2.0.10.tgz",
"version": "2.0.10"
}
]
4 changes: 3 additions & 1 deletion godot.package
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
packages: {
@bendn/test: 2.0.10
@bendn/test: "^2.0.0"
@bendn/splitter: "1.0.x"
@bendn/stockfish.gd: "1.*"
}
16 changes: 8 additions & 8 deletions src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::package::parsing::IntoPackageList;
use crate::package::Package;
use anyhow::Result;
use console::style;
use reqwest::Client;
use reqwest_middleware::ClientWithMiddleware;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand Down Expand Up @@ -59,12 +59,12 @@ impl ParsedConfig {
})
}

pub async fn into_configfile(self, client: Client) -> ConfigFile {
pub async fn into_configfile(self, client: ClientWithMiddleware) -> ConfigFile {
let mut packages = self.packages.into_package_list(client).await.unwrap();
for mut p in &mut packages {
p.indirect = false
}
ConfigFile { packages: packages }
ConfigFile { packages }
}
}

Expand All @@ -80,7 +80,7 @@ impl ConfigFile {

/// Creates a new [ConfigFile] from the given text
/// Panics if the file cant be parsed as toml, hjson or yaml.
pub async fn new(contents: &String, client: Client) -> Self {
pub async fn new(contents: &String, client: ClientWithMiddleware) -> Self {
if contents.is_empty() {
panic!("Empty CFG");
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl ConfigFile {
cfg
}

pub async fn parse(txt: &str, t: ConfigType, client: Client) -> Result<Self> {
pub async fn parse(txt: &str, t: ConfigType, client: ClientWithMiddleware) -> Result<Self> {
Ok(ParsedConfig::parse(txt, t)?.into_configfile(client).await)
}

Expand All @@ -131,6 +131,7 @@ impl ConfigFile {
pkgs.push(p)
};
}
pkgs.sort();
serde_json::to_string_pretty(&pkgs).unwrap()
}

Expand Down Expand Up @@ -168,7 +169,7 @@ mod tests {
#[tokio::test]
async fn parse() {
let _t = crate::test_utils::mktemp();
let c = Client::new();
let c = crate::mkclient();
let cfgs: [&mut ConfigFile; 3] = [
&mut ConfigFile::new(
&r#"dependencies: { "@bendn/test": 2.0.10 }"#.into(),
Expand All @@ -190,10 +191,9 @@ mod tests {
struct LockFileEntry {
pub name: String,
pub version: String,
pub integrity: String,
}
let wanted_lockfile = serde_json::from_str::<Vec<LockFileEntry>>(
r#"[{"name":"@bendn/test","version":"2.0.10","integrity":"sha512-hyPGxDG8poa2ekmWr1BeTCUa7YaZYfhsN7jcLJ3q2cQVlowcTnzqmz4iV3t21QFyabE5R+rV+y6d5dAItrJeDw=="},{"name":"@bendn/gdcli","version":"1.2.5","integrity":"sha512-/YOAd1+K4JlKvPTmpX8B7VWxGtFrxKq4R0A6u5qOaaVPK6uGsl4dGZaIHpxuqcurEcwPEOabkoShXKZaOXB0lw=="}]"#,
r#"[{"name":"@bendn/gdcli","version":"1.2.5"},{"name":"@bendn/test","version":"2.0.10"}]"#,
).unwrap();
for cfg in cfgs {
assert_eq!(cfg.packages.len(), 1);
Expand Down
62 changes: 53 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ use clap::{ColorChoice, Parser, Subcommand, ValueEnum};
use config_file::{ConfigFile, ConfigType};
use console::{self, Term};
use futures::stream::{self, StreamExt};
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache};
use indicatif::{HumanCount, HumanDuration, ProgressBar, ProgressIterator};
use lazy_static::lazy_static;
use package::parsing::ParsedPackage;
use reqwest::Client;
use reqwest::{header::HeaderMap, ClientBuilder as NormalClientBuilder};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use std::fs::{create_dir, read_dir, read_to_string, remove_dir, write};
use std::io::{stdin, Read};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -146,7 +148,7 @@ async fn main() {
ColorChoice::Never => set_colors(false),
ColorChoice::Auto => set_colors(Term::stdout().is_term() && Term::stderr().is_term()),
}
async fn get_cfg(path: PathBuf, client: Client) -> ConfigFile {
async fn get_cfg(path: PathBuf, client: ClientWithMiddleware) -> ConfigFile {
let mut contents = String::from("");
if path == Path::new("-") {
let bytes = stdin()
Expand All @@ -168,8 +170,18 @@ async fn main() {
write(path, lockfile).expect("Writing lock file should be ok");
}
}
let mut headers = HeaderMap::new();
headers.insert(
"User-Agent",
format!(
"gpm/{} (godot-package-manager/cli on GitHub)",
env!("CARGO_PKG_VERSION")
)
.parse()
.unwrap(),
);
let client = mkclient();
let _ = BEGIN.elapsed(); // needed to initialize the instant for whatever reason
let client = Client::new();
match args.action {
Actions::Update => {
let c = &mut get_cfg(args.config_file, client.clone()).await;
Expand Down Expand Up @@ -207,7 +219,39 @@ async fn main() {
}
}

async fn update(cfg: &mut ConfigFile, modify: bool, v: Verbosity, client: Client) {
pub fn mkclient() -> ClientWithMiddleware {
let mut headers = HeaderMap::new();
headers.insert(
"User-Agent",
format!(
"gpm/{} (godot-package-manager/cli on GitHub)",
env!("CARGO_PKG_VERSION")
)
.parse()
.unwrap(),
);
let path = if Path::new(".import").is_dir() {
".import/gpm_cache"
} else if Path::new(".godot").is_dir() {
".godot/gpm_cache"
} else {
".gpm_cache"
};
ClientBuilder::new(
NormalClientBuilder::new()
.default_headers(headers)
.build()
.unwrap(),
)
.with(Cache(HttpCache {
mode: CacheMode::Default,
manager: CACacheManager { path: path.into() },
options: None,
}))
.build()
}

async fn update(cfg: &mut ConfigFile, modify: bool, v: Verbosity, client: ClientWithMiddleware) {
if !Path::new("./addons/").exists() {
create_dir("./addons/").expect("Should be able to create addons folder");
}
Expand Down Expand Up @@ -248,7 +292,7 @@ async fn update(cfg: &mut ConfigFile, modify: bool, v: Verbosity, client: Client
Finished(String),
}
let bar_or_info = v.bar() || v.info();
let (tx, rx) = bar_or_info.then(|| channel()).unzip();
let (tx, rx) = bar_or_info.then(channel).unzip();
let buf = stream::iter(packages)
.map(|mut p| async {
let p_name = p.to_string();
Expand Down Expand Up @@ -394,7 +438,7 @@ async fn tree(
charset: CharSet,
prefix: PrefixType,
print_tarballs: bool,
client: Client,
client: ClientWithMiddleware,
) -> String {
let mut tree: String = if let Ok(s) = current_dir() {
format!("{}\n", s.to_string_lossy())
Expand Down Expand Up @@ -434,7 +478,7 @@ async fn tree(
print_tarballs: bool,
depth: u32,
count: &mut u64,
client: Client,
client: ClientWithMiddleware,
) {
// the index is used to decide if the package is the last package,
// so we can use a L instead of a T.
Expand Down Expand Up @@ -484,7 +528,7 @@ async fn tree(
tree
}

async fn init(mut packages: Vec<Package>, client: Client) -> Result<()> {
async fn init(mut packages: Vec<Package>, client: ClientWithMiddleware) -> Result<()> {
let mut c = ConfigFile::default();
if packages.is_empty() {
let mut has_asked = false;
Expand Down Expand Up @@ -594,7 +638,7 @@ mod test_utils {
#[tokio::test]
async fn gpm() {
let _t = test_utils::mktemp();
let c = Client::new();
let c = mkclient();
let cfg_file =
&mut config_file::ConfigFile::new(&r#"packages: {"@bendn/test":2.0.10}"#.into(), c.clone())
.await;
Expand Down
Loading

0 comments on commit 7907a61

Please sign in to comment.