Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
internal: Migrate to proto v0.21 version types. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Oct 30, 2023
1 parent 48f11af commit 186e56d
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 175 deletions.
166 changes: 76 additions & 90 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ members = ["crates/*"]

[workspace.dependencies]
extism-pdk = "0.3.4"
proto_pdk = { version = "0.8.1" } # , path = "../../proto/crates/pdk" }
proto_pdk_api = { version = "0.8.1" } # , path = "../../proto/crates/pdk-api" }
proto_pdk_test_utils = { version = "0.8.4" } # , path = "../../proto/crates/pdk-test-utils" }
regex = "1.10.2"
serde = "1.0.189"
serde_json = "1.0.107"
starbase_sandbox = "0.1.11"
tokio = "1.33.0"
proto_pdk = { version = "0.9.0" } # , path = "../../proto/crates/pdk" }
proto_pdk_api = { version = "0.9.0" } # , path = "../../proto/crates/pdk-api" }
proto_pdk_test_utils = { version = "0.9.1" } # , path = "../../proto/crates/pdk-test-utils" }
regex = { version = "1.10.2", default-features = false, features = ["unicode"] }
serde = "1.0.190"
serde_json = "1.0.108"
starbase_sandbox = "0.1.12"
tokio = { version = "1.33.0", features = ["full"] }
version_spec = "0.1.3"

[profile.release]
codegen-units = 1
Expand Down
1 change: 1 addition & 0 deletions crates/node-depman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extism-pdk = { workspace = true }
proto_pdk = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
version_spec = { workspace = true }

[dev-dependencies]
proto_pdk_test_utils = { workspace = true }
Expand Down
69 changes: 43 additions & 26 deletions crates/node-depman/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,32 @@ impl PackageManager {
}
}

pub fn get_package_name(&self, version: &str) -> String {
if self.is_yarn_berry(version) {
pub fn get_package_name(&self, version: impl AsRef<UnresolvedVersionSpec>) -> String {
if self.is_yarn_berry(version.as_ref()) {
"@yarnpkg/cli-dist".into()
} else {
self.to_string()
}
}

pub fn is_yarn_classic(&self, version: &str) -> bool {
self == &PackageManager::Yarn
&& (version.starts_with('1') || version == "legacy" || version == "classic")
pub fn is_yarn_classic(&self, version: impl AsRef<UnresolvedVersionSpec>) -> bool {
matches!(self, PackageManager::Yarn)
&& match version.as_ref() {
UnresolvedVersionSpec::Alias(alias) => alias == "legacy" || alias == "classic",
UnresolvedVersionSpec::Version(ver) => ver.major == 1,
UnresolvedVersionSpec::Req(req) => req.comparators.iter().any(|c| c.major == 1),
_ => false,
}
}

pub fn is_yarn_berry(&self, version: &str) -> bool {
self == &PackageManager::Yarn
&& (!version.starts_with('1') || version == "berry" || version == "latest")
pub fn is_yarn_berry(&self, version: impl AsRef<UnresolvedVersionSpec>) -> bool {
matches!(self, PackageManager::Yarn)
&& match version.as_ref() {
UnresolvedVersionSpec::Alias(alias) => alias == "berry" || alias == "latest",
UnresolvedVersionSpec::Version(ver) => ver.major > 1,
UnresolvedVersionSpec::Req(req) => req.comparators.iter().any(|c| c.major > 1),
_ => false,
}
}
}

Expand All @@ -71,7 +81,7 @@ pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMeta
name: manager.to_string(),
type_of: PluginType::DependencyManager,
default_version: if manager == PackageManager::Npm {
Some("bundled".into())
Some(UnresolvedVersionSpec::Alias("bundled".into()))
} else {
None
},
Expand All @@ -87,17 +97,17 @@ pub fn download_prebuilt(
let version = &input.context.version;
let manager = PackageManager::detect();

if version == "canary" {
if version.is_canary() {
return err!(PluginError::UnsupportedCanary {
tool: manager.to_string()
}
.into());
}

let package_name = manager.get_package_name(version);
let package_name = manager.get_package_name(version.to_unresolved_spec());

// Derive values based on package manager
let archive_prefix = if manager.is_yarn_classic(version) {
let archive_prefix = if manager.is_yarn_classic(version.to_unresolved_spec()) {
format!("yarn-v{version}")
} else {
"package".into()
Expand Down Expand Up @@ -261,7 +271,7 @@ pub fn resolve_version(
PackageManager::Npm => {
// When the alias "bundled" is provided, we should install the npm
// version that comes bundled with the current Node.js version.
if input.initial == "bundled" {
if input.initial.is_alias("bundled") {
let response: Vec<NodeDistVersion> =
fetch_url("https://nodejs.org/download/release/index.json")?;
let mut found_version = false;
Expand All @@ -271,7 +281,8 @@ pub fn resolve_version(
for node_release in &response {
// Theirs starts with v, ours does not
if node_release.version[1..] == node_version {
output.version = node_release.npm.clone();
output.version =
Some(VersionSpec::parse(node_release.npm.as_ref().unwrap())?);
found_version = true;
break;
}
Expand All @@ -287,7 +298,9 @@ pub fn resolve_version(
for node_release in &response {
// Both start with v
if node_release.version == node_version {
output.version = node_release.npm.clone();
output.version = Some(VersionSpec::parse(
node_release.npm.as_ref().unwrap(),
)?);
found_version = true;
break;
}
Expand All @@ -301,16 +314,18 @@ pub fn resolve_version(
"Could not find a bundled npm version for Node.js, falling back to latest"
);

output.candidate = Some("latest".into());
output.candidate = Some(UnresolvedVersionSpec::Alias("latest".into()));
}
}
}

PackageManager::Yarn => {
if input.initial == "berry" || input.initial == "latest" {
output.candidate = Some("~4".into());
} else if input.initial == "legacy" || input.initial == "classic" {
output.candidate = Some("~1".into());
if let UnresolvedVersionSpec::Alias(alias) = input.initial {
if alias == "berry" || alias == "latest" {
output.candidate = Some(UnresolvedVersionSpec::parse("~4")?);
} else if alias == "legacy" || alias == "classic" {
output.candidate = Some(UnresolvedVersionSpec::parse("~1")?);
}
}
}

Expand Down Expand Up @@ -397,23 +412,25 @@ pub fn parse_version_file(
let name = parts.next().unwrap_or_default();

if name == manager_name {
if let Some(value) = parts.next() {
let value = if let Some(value) = parts.next() {
// Remove corepack build metadata hash
if let Some(index) = value.find('+') {
version = Some(value[0..index].to_owned());
&value[0..index]
} else {
version = Some(value.to_owned());
value
}
} else {
version = Some("latest".into());
}
"latest"
};

version = Some(UnresolvedVersionSpec::parse(value)?);
}
}

if version.is_none() {
if let Some(engines) = package_json.engines {
if let Some(constraint) = engines.get(&manager_name) {
version = Some(constraint.to_owned());
version = Some(UnresolvedVersionSpec::parse(constraint)?);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions crates/node-depman/tests/download_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod npm {
assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
context: ToolContext {
version: "9.0.0".into(),
version: VersionSpec::parse("9.0.0").unwrap(),
..Default::default()
},
..Default::default()
Expand Down Expand Up @@ -48,7 +48,7 @@ mod npm {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "9.0.0".into(),
version: VersionSpec::parse("9.0.0").unwrap(),
..Default::default()
},
})
Expand Down Expand Up @@ -77,7 +77,7 @@ mod pnpm {
assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
context: ToolContext {
version: "8.0.0".into(),
version: VersionSpec::parse("8.0.0").unwrap(),
..Default::default()
},
..Default::default()
Expand Down Expand Up @@ -105,7 +105,7 @@ mod pnpm {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "8.0.0".into(),
version: VersionSpec::parse("8.0.0").unwrap(),
..Default::default()
},
})
Expand Down Expand Up @@ -134,7 +134,7 @@ mod yarn {
assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
context: ToolContext {
version: "1.22.0".into(),
version: VersionSpec::parse("1.22.0").unwrap(),
..Default::default()
},
..Default::default()
Expand Down Expand Up @@ -162,7 +162,7 @@ mod yarn {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "1.22.0".into(),
version: VersionSpec::parse("1.22.0").unwrap(),
..Default::default()
},
})
Expand Down Expand Up @@ -191,7 +191,7 @@ mod yarn_berry {
assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
context: ToolContext {
version: "3.6.1".into(),
version: VersionSpec::parse("3.6.1").unwrap(),
..Default::default()
},
..Default::default()
Expand Down Expand Up @@ -220,7 +220,7 @@ mod yarn_berry {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "3.6.1".into(),
version: VersionSpec::parse("3.6.1").unwrap(),
..Default::default()
},
})
Expand Down Expand Up @@ -254,7 +254,7 @@ fn locates_bin_from_package_json_bin() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "20.0.0".into(),
version: VersionSpec::parse("20.0.0").unwrap(),
..Default::default()
},
})
Expand Down Expand Up @@ -284,7 +284,7 @@ fn locates_bin_from_package_json_bin() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "9.0.0".into(),
version: VersionSpec::parse("9.0.0").unwrap(),
..Default::default()
},
})
Expand Down Expand Up @@ -316,7 +316,7 @@ fn locates_bin_from_package_json_main() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "8.0.0".into(),
version: VersionSpec::parse("8.0.0").unwrap(),
..Default::default()
},
})
Expand Down
2 changes: 1 addition & 1 deletion crates/node-depman/tests/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod npm {
ToolMetadataOutput {
name: "npm".into(),
type_of: PluginType::DependencyManager,
default_version: Some("bundled".into()),
default_version: Some(UnresolvedVersionSpec::Alias("bundled".into())),
plugin_version: Some(env!("CARGO_PKG_VERSION").into()),
..ToolMetadataOutput::default()
}
Expand Down
22 changes: 11 additions & 11 deletions crates/node-depman/tests/versions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod npm {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("1.2.3".into()),
version: Some(UnresolvedVersionSpec::parse("1.2.3").unwrap()),
}
);
}
Expand All @@ -51,7 +51,7 @@ mod npm {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("1.2.3".into()),
version: Some(UnresolvedVersionSpec::parse("1.2.3").unwrap()),
}
);
}
Expand All @@ -67,7 +67,7 @@ mod npm {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("latest".into()),
version: Some(UnresolvedVersionSpec::parse("latest").unwrap()),
}
);
}
Expand All @@ -83,7 +83,7 @@ mod npm {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("1.2.3".into()),
version: Some(UnresolvedVersionSpec::parse("1.2.3").unwrap()),
}
);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ mod pnpm {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("1.2.3".into()),
version: Some(UnresolvedVersionSpec::parse("1.2.3").unwrap()),
}
);
}
Expand All @@ -139,7 +139,7 @@ mod pnpm {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("latest".into()),
version: Some(UnresolvedVersionSpec::parse("latest").unwrap()),
}
);
}
Expand All @@ -155,7 +155,7 @@ mod pnpm {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("1.2.3".into()),
version: Some(UnresolvedVersionSpec::parse("1.2.3").unwrap()),
}
);
}
Expand All @@ -168,7 +168,7 @@ mod yarn {
"1" => "1.22.19",
"2" => "2.4.3",
"3" => "3.6.4",
"berry" => "4.0.0",
"berry" => "4.0.1",
});

#[test]
Expand Down Expand Up @@ -196,7 +196,7 @@ mod yarn {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("1.2.3".into()),
version: Some(UnresolvedVersionSpec::parse("1.2.3").unwrap()),
}
);
}
Expand All @@ -212,7 +212,7 @@ mod yarn {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("latest".into()),
version: Some(UnresolvedVersionSpec::parse("latest").unwrap()),
}
);
}
Expand All @@ -228,7 +228,7 @@ mod yarn {
file: "package.json".into(),
}),
ParseVersionFileOutput {
version: Some("1.2.3".into()),
version: Some(UnresolvedVersionSpec::parse("1.2.3").unwrap()),
}
);
}
Expand Down
Loading

0 comments on commit 186e56d

Please sign in to comment.