Skip to content

Commit

Permalink
clean everything up a bit (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
bend-n authored Mar 10, 2023
1 parent 8ceafa6 commit 3aed028
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 306 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "godot-package-manager"
version = "1.2.0"
version = "1.2.1"
edition = "2021"
authors = ["bendn <[email protected]>"]
description = "A package manager for godot"
Expand Down Expand Up @@ -29,6 +29,8 @@ reqwest = { version = "0.11", features = [] }
tokio = { version = "1", features = ["full"] }
async-recursion = "1.0.2"
futures = "0.3"
semver_rs = { version = "0.2", features = ["serde"] }
async-trait = "0.1.66"

[dev-dependencies]
glob = "0.3.0"
Expand Down
25 changes: 6 additions & 19 deletions godot.lock
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
[
{
"name": "@bendn/splitter",
"version": "1.0.6",
"integrity": "sha512-HT7q5qv6OEpX95e5r+kAsasoAvH0Mgf+aT4SdKQ18fyDIn1dW02WqbulF0AMwHufgRZkMf9SnQGiAq79P5ZIKQ=="
},
{
"name": "@bendn/test",
"version": "2.0.10",
"integrity": "sha512-hyPGxDG8poa2ekmWr1BeTCUa7YaZYfhsN7jcLJ3q2cQVlowcTnzqmz4iV3t21QFyabE5R+rV+y6d5dAItrJeDw=="
"integrity": "sha512-hyPGxDG8poa2ekmWr1BeTCUa7YaZYfhsN7jcLJ3q2cQVlowcTnzqmz4iV3t21QFyabE5R+rV+y6d5dAItrJeDw==",
"tarball": "https://registry.npmjs.org/@bendn/test/-/test-2.0.10.tgz",
"version": "2.0.10"
},
{
"name": "@bendn/gdcli",
"version": "1.2.5",
"integrity": "sha512-/YOAd1+K4JlKvPTmpX8B7VWxGtFrxKq4R0A6u5qOaaVPK6uGsl4dGZaIHpxuqcurEcwPEOabkoShXKZaOXB0lw=="
},
{
"name": "prettier",
"version": "2.8.4",
"integrity": "sha512-vIS4Rlc2FNh0BySk3Wkd6xmwxB0FpOndW5fisM5H8hsZSxU2VWVB5CWIkIjWvrHjIhxk2g3bfMKM87zNTrZddw=="
},
{
"name": "stockfish",
"version": "15.0.0",
"integrity": "sha512-ze3vTgMrnCmMdtC8qpONBdXkSHWwv4Dx+GFV1V5TIi0qP1HF+Vwa4lArgqvHDooU8Mwfs1cgRzkW3SM8x/7TJg=="
"integrity": "sha512-/YOAd1+K4JlKvPTmpX8B7VWxGtFrxKq4R0A6u5qOaaVPK6uGsl4dGZaIHpxuqcurEcwPEOabkoShXKZaOXB0lw==",
"tarball": "https://registry.npmjs.org/@bendn/gdcli/-/gdcli-1.2.5.tgz",
"version": "1.2.5"
}
]
65 changes: 39 additions & 26 deletions src/config_file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::package::parsing::IntoPackageList;
use crate::package::Package;
use anyhow::Result;
use console::style;
use futures::stream::{self, StreamExt};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand Down Expand Up @@ -43,7 +44,7 @@ impl From<ConfigFile> for ParsedConfig {
packages: from
.packages
.into_iter()
.map(|p| (p.name, p.version))
.map(|p| (p.name, p.version.to_string()))
.collect(),
}
}
Expand All @@ -58,13 +59,12 @@ impl ParsedConfig {
})
}

pub async fn into_configfile(self) -> ConfigFile {
let packages = stream::iter(self.packages.into_iter())
.map(|(name, version)| async { Package::new(name, version).await.unwrap() })
.buffer_unordered(crate::PARALLEL)
.collect::<Vec<Package>>()
.await;
ConfigFile { packages }
pub async fn into_configfile(self, client: Client) -> ConfigFile {
let mut packages = self.packages.into_package_list(client).await.unwrap();
for mut p in &mut packages {
p.indirect = false
}
ConfigFile { packages: packages }
}
}

Expand All @@ -80,24 +80,24 @@ 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) -> Self {
pub async fn new(contents: &String, client: Client) -> Self {
if contents.is_empty() {
panic!("Empty CFG");
}

// definetly not going to backfire
let mut cfg = if contents.as_bytes()[0] == b'{' {
// json gets brute forced first so this isnt really needed
Self::parse(contents, ConfigType::JSON)
Self::parse(contents, ConfigType::JSON, client)
.await
.expect("Parsing CFG from JSON should work")
} else if contents.len() > 3 && contents[..3] == *"---" {
Self::parse(contents, ConfigType::YAML)
Self::parse(contents, ConfigType::YAML, client)
.await
.expect("Parsing CFG from YAML should work")
} else {
for i in [ConfigType::JSON, ConfigType::YAML, ConfigType::TOML].into_iter() {
let res = Self::parse(contents, i).await;
let res = Self::parse(contents, i, client.clone()).await;

// im sure theres some kind of idiomatic rust way to do this that i dont know of
if res.is_ok() {
Expand All @@ -118,16 +118,16 @@ impl ConfigFile {
cfg
}

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

/// Creates a lockfile for this config file.
/// note: Lockfiles are currently unused.
pub async fn lock(&mut self) -> String {
pub fn lock(&mut self) -> String {
let mut pkgs = vec![];
for mut p in self.collect() {
if p.is_installed() && p.get_manifest().await.is_ok() {
for p in self.collect() {
if p.is_installed() {
pkgs.push(p)
};
}
Expand All @@ -140,7 +140,7 @@ impl ConfigFile {
for p in pkgs {
cb(p);
if p.has_deps() {
inner(&mut p.dependencies, cb);
inner(&mut p.manifest.dependencies, cb);
}
}
}
Expand Down Expand Up @@ -168,10 +168,23 @@ mod tests {
#[tokio::test]
async fn parse() {
let _t = crate::test_utils::mktemp();
let c = Client::new();
let cfgs: [&mut ConfigFile; 3] = [
&mut ConfigFile::new(&r#"dependencies: { "@bendn/test": 2.0.10 }"#.into()).await, // quoteless fails as a result of https://github.com/Canop/deser-hjson/issues/9
&mut ConfigFile::new(&"dependencies:\n \"@bendn/test\": 2.0.10".into()).await,
&mut ConfigFile::new(&"[dependencies]\n\"@bendn/test\" = \"2.0.10\"".into()).await,
&mut ConfigFile::new(
&r#"dependencies: { "@bendn/test": 2.0.10 }"#.into(),
c.clone(),
)
.await, // quoteless fails as a result of https://github.com/Canop/deser-hjson/issues/9
&mut ConfigFile::new(
&"dependencies:\n \"@bendn/test\": 2.0.10".into(),
c.clone(),
)
.await,
&mut ConfigFile::new(
&"[dependencies]\n\"@bendn/test\" = \"2.0.10\"".into(),
c.clone(),
)
.await,
];
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
struct LockFileEntry {
Expand All @@ -185,16 +198,16 @@ mod tests {
for cfg in cfgs {
assert_eq!(cfg.packages.len(), 1);
assert_eq!(cfg.packages[0].to_string(), "@bendn/[email protected]");
assert_eq!(cfg.packages[0].dependencies.len(), 1);
assert_eq!(cfg.packages[0].manifest.dependencies.len(), 1);
assert_eq!(
cfg.packages[0].dependencies[0].to_string(),
cfg.packages[0].manifest.dependencies[0].to_string(),
"@bendn/[email protected]"
);
for mut p in cfg.collect() {
p.download().await
p.download(c.clone()).await
}
assert_eq!(
serde_json::from_str::<Vec<LockFileEntry>>(cfg.lock().await.as_str()).unwrap(),
serde_json::from_str::<Vec<LockFileEntry>>(cfg.lock().as_str()).unwrap(),
wanted_lockfile
);
}
Expand Down
Loading

0 comments on commit 3aed028

Please sign in to comment.