Skip to content

Commit 7907a61

Browse files
authored
<>=||~*x^ syntax (#28)
1 parent 3aed028 commit 7907a61

File tree

8 files changed

+186
-110
lines changed

8 files changed

+186
-110
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ Cargo.lock
1717
addons/
1818

1919
# Performance data
20-
perf.data*
20+
perf.data*
21+
22+
# cache
23+
.gpm_cache

Cargo.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "godot-package-manager"
3-
version = "1.2.1"
3+
version = "1.3.0"
44
edition = "2021"
55
authors = ["bendn <[email protected]>"]
66
description = "A package manager for godot"
@@ -24,13 +24,15 @@ sha1 = "0.10.5"
2424
console = "0.15.4"
2525
indicatif = "0.17.2"
2626
anyhow = "1.0.68"
27-
dialoguer = { version = "0.10.3", features = [] }
28-
reqwest = { version = "0.11", features = [] }
29-
tokio = { version = "1", features = ["full"] }
27+
dialoguer = { version = "0.10.3", default-features = false, features = [] }
28+
reqwest = "0.11"
29+
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread"] }
3030
async-recursion = "1.0.2"
3131
futures = "0.3"
32-
semver_rs = { version = "0.2", features = ["serde"] }
32+
semver_rs = "0.2"
3333
async-trait = "0.1.66"
34+
http-cache-reqwest = "0.8.0"
35+
reqwest-middleware = "0.2.1"
3436

3537
[dev-dependencies]
3638
glob = "0.3.0"

godot.lock

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
[
2-
{
3-
"name": "@bendn/test",
4-
"integrity": "sha512-hyPGxDG8poa2ekmWr1BeTCUa7YaZYfhsN7jcLJ3q2cQVlowcTnzqmz4iV3t21QFyabE5R+rV+y6d5dAItrJeDw==",
5-
"tarball": "https://registry.npmjs.org/@bendn/test/-/test-2.0.10.tgz",
6-
"version": "2.0.10"
7-
},
82
{
93
"name": "@bendn/gdcli",
10-
"integrity": "sha512-/YOAd1+K4JlKvPTmpX8B7VWxGtFrxKq4R0A6u5qOaaVPK6uGsl4dGZaIHpxuqcurEcwPEOabkoShXKZaOXB0lw==",
114
"tarball": "https://registry.npmjs.org/@bendn/gdcli/-/gdcli-1.2.5.tgz",
125
"version": "1.2.5"
6+
},
7+
{
8+
"name": "@bendn/splitter",
9+
"tarball": "https://registry.npmjs.org/@bendn/splitter/-/splitter-1.0.6.tgz",
10+
"version": "1.0.6"
11+
},
12+
{
13+
"name": "@bendn/stockfish.gd",
14+
"tarball": "https://registry.npmjs.org/@bendn/stockfish.gd/-/stockfish.gd-1.2.6.tgz",
15+
"version": "1.2.6"
16+
},
17+
{
18+
"name": "@bendn/test",
19+
"tarball": "https://registry.npmjs.org/@bendn/test/-/test-2.0.10.tgz",
20+
"version": "2.0.10"
1321
}
1422
]

godot.package

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
packages: {
2-
@bendn/test: 2.0.10
2+
@bendn/test: "^2.0.0"
3+
@bendn/splitter: "1.0.x"
4+
@bendn/stockfish.gd: "1.*"
35
}

src/config_file.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::package::parsing::IntoPackageList;
22
use crate::package::Package;
33
use anyhow::Result;
44
use console::style;
5-
use reqwest::Client;
5+
use reqwest_middleware::ClientWithMiddleware;
66
use serde::{Deserialize, Serialize};
77
use std::collections::HashMap;
88

@@ -59,12 +59,12 @@ impl ParsedConfig {
5959
})
6060
}
6161

62-
pub async fn into_configfile(self, client: Client) -> ConfigFile {
62+
pub async fn into_configfile(self, client: ClientWithMiddleware) -> ConfigFile {
6363
let mut packages = self.packages.into_package_list(client).await.unwrap();
6464
for mut p in &mut packages {
6565
p.indirect = false
6666
}
67-
ConfigFile { packages: packages }
67+
ConfigFile { packages }
6868
}
6969
}
7070

@@ -80,7 +80,7 @@ impl ConfigFile {
8080

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

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

@@ -131,6 +131,7 @@ impl ConfigFile {
131131
pkgs.push(p)
132132
};
133133
}
134+
pkgs.sort();
134135
serde_json::to_string_pretty(&pkgs).unwrap()
135136
}
136137

@@ -168,7 +169,7 @@ mod tests {
168169
#[tokio::test]
169170
async fn parse() {
170171
let _t = crate::test_utils::mktemp();
171-
let c = Client::new();
172+
let c = crate::mkclient();
172173
let cfgs: [&mut ConfigFile; 3] = [
173174
&mut ConfigFile::new(
174175
&r#"dependencies: { "@bendn/test": 2.0.10 }"#.into(),
@@ -190,10 +191,9 @@ mod tests {
190191
struct LockFileEntry {
191192
pub name: String,
192193
pub version: String,
193-
pub integrity: String,
194194
}
195195
let wanted_lockfile = serde_json::from_str::<Vec<LockFileEntry>>(
196-
r#"[{"name":"@bendn/test","version":"2.0.10","integrity":"sha512-hyPGxDG8poa2ekmWr1BeTCUa7YaZYfhsN7jcLJ3q2cQVlowcTnzqmz4iV3t21QFyabE5R+rV+y6d5dAItrJeDw=="},{"name":"@bendn/gdcli","version":"1.2.5","integrity":"sha512-/YOAd1+K4JlKvPTmpX8B7VWxGtFrxKq4R0A6u5qOaaVPK6uGsl4dGZaIHpxuqcurEcwPEOabkoShXKZaOXB0lw=="}]"#,
196+
r#"[{"name":"@bendn/gdcli","version":"1.2.5"},{"name":"@bendn/test","version":"2.0.10"}]"#,
197197
).unwrap();
198198
for cfg in cfgs {
199199
assert_eq!(cfg.packages.len(), 1);

src/main.rs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ use clap::{ColorChoice, Parser, Subcommand, ValueEnum};
1010
use config_file::{ConfigFile, ConfigType};
1111
use console::{self, Term};
1212
use futures::stream::{self, StreamExt};
13+
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache};
1314
use indicatif::{HumanCount, HumanDuration, ProgressBar, ProgressIterator};
1415
use lazy_static::lazy_static;
1516
use package::parsing::ParsedPackage;
16-
use reqwest::Client;
17+
use reqwest::{header::HeaderMap, ClientBuilder as NormalClientBuilder};
18+
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
1719
use std::fs::{create_dir, read_dir, read_to_string, remove_dir, write};
1820
use std::io::{stdin, Read};
1921
use std::path::{Path, PathBuf};
@@ -146,7 +148,7 @@ async fn main() {
146148
ColorChoice::Never => set_colors(false),
147149
ColorChoice::Auto => set_colors(Term::stdout().is_term() && Term::stderr().is_term()),
148150
}
149-
async fn get_cfg(path: PathBuf, client: Client) -> ConfigFile {
151+
async fn get_cfg(path: PathBuf, client: ClientWithMiddleware) -> ConfigFile {
150152
let mut contents = String::from("");
151153
if path == Path::new("-") {
152154
let bytes = stdin()
@@ -168,8 +170,18 @@ async fn main() {
168170
write(path, lockfile).expect("Writing lock file should be ok");
169171
}
170172
}
173+
let mut headers = HeaderMap::new();
174+
headers.insert(
175+
"User-Agent",
176+
format!(
177+
"gpm/{} (godot-package-manager/cli on GitHub)",
178+
env!("CARGO_PKG_VERSION")
179+
)
180+
.parse()
181+
.unwrap(),
182+
);
183+
let client = mkclient();
171184
let _ = BEGIN.elapsed(); // needed to initialize the instant for whatever reason
172-
let client = Client::new();
173185
match args.action {
174186
Actions::Update => {
175187
let c = &mut get_cfg(args.config_file, client.clone()).await;
@@ -207,7 +219,39 @@ async fn main() {
207219
}
208220
}
209221

210-
async fn update(cfg: &mut ConfigFile, modify: bool, v: Verbosity, client: Client) {
222+
pub fn mkclient() -> ClientWithMiddleware {
223+
let mut headers = HeaderMap::new();
224+
headers.insert(
225+
"User-Agent",
226+
format!(
227+
"gpm/{} (godot-package-manager/cli on GitHub)",
228+
env!("CARGO_PKG_VERSION")
229+
)
230+
.parse()
231+
.unwrap(),
232+
);
233+
let path = if Path::new(".import").is_dir() {
234+
".import/gpm_cache"
235+
} else if Path::new(".godot").is_dir() {
236+
".godot/gpm_cache"
237+
} else {
238+
".gpm_cache"
239+
};
240+
ClientBuilder::new(
241+
NormalClientBuilder::new()
242+
.default_headers(headers)
243+
.build()
244+
.unwrap(),
245+
)
246+
.with(Cache(HttpCache {
247+
mode: CacheMode::Default,
248+
manager: CACacheManager { path: path.into() },
249+
options: None,
250+
}))
251+
.build()
252+
}
253+
254+
async fn update(cfg: &mut ConfigFile, modify: bool, v: Verbosity, client: ClientWithMiddleware) {
211255
if !Path::new("./addons/").exists() {
212256
create_dir("./addons/").expect("Should be able to create addons folder");
213257
}
@@ -248,7 +292,7 @@ async fn update(cfg: &mut ConfigFile, modify: bool, v: Verbosity, client: Client
248292
Finished(String),
249293
}
250294
let bar_or_info = v.bar() || v.info();
251-
let (tx, rx) = bar_or_info.then(|| channel()).unzip();
295+
let (tx, rx) = bar_or_info.then(channel).unzip();
252296
let buf = stream::iter(packages)
253297
.map(|mut p| async {
254298
let p_name = p.to_string();
@@ -394,7 +438,7 @@ async fn tree(
394438
charset: CharSet,
395439
prefix: PrefixType,
396440
print_tarballs: bool,
397-
client: Client,
441+
client: ClientWithMiddleware,
398442
) -> String {
399443
let mut tree: String = if let Ok(s) = current_dir() {
400444
format!("{}\n", s.to_string_lossy())
@@ -434,7 +478,7 @@ async fn tree(
434478
print_tarballs: bool,
435479
depth: u32,
436480
count: &mut u64,
437-
client: Client,
481+
client: ClientWithMiddleware,
438482
) {
439483
// the index is used to decide if the package is the last package,
440484
// so we can use a L instead of a T.
@@ -484,7 +528,7 @@ async fn tree(
484528
tree
485529
}
486530

487-
async fn init(mut packages: Vec<Package>, client: Client) -> Result<()> {
531+
async fn init(mut packages: Vec<Package>, client: ClientWithMiddleware) -> Result<()> {
488532
let mut c = ConfigFile::default();
489533
if packages.is_empty() {
490534
let mut has_asked = false;
@@ -594,7 +638,7 @@ mod test_utils {
594638
#[tokio::test]
595639
async fn gpm() {
596640
let _t = test_utils::mktemp();
597-
let c = Client::new();
641+
let c = mkclient();
598642
let cfg_file =
599643
&mut config_file::ConfigFile::new(&r#"packages: {"@bendn/test":2.0.10}"#.into(), c.clone())
600644
.await;

0 commit comments

Comments
 (0)