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. (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Nov 2, 2023
1 parent 52e4682 commit b76050e
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 141 deletions.
175 changes: 80 additions & 95 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ crate-type = ['cdylib']

[dependencies]
extism-pdk = "0.3.4"
proto_pdk = { version = "0.8.0" } # , path = "../../proto/crates/pdk" }
serde = "1.0.189"
toml = "0.8.2"
proto_pdk = { version = "0.9.0" } # , path = "../../proto/crates/pdk" }
serde = "1.0.190"
toml = "0.8.6"

[dev-dependencies]
proto_pdk_test_utils = { version = "0.8.2" } # , path = "../../proto/crates/pdk-test-utils" }
starbase_sandbox = "0.1.11"
tokio = "1.33.0"
proto_pdk_test_utils = { version = "0.9.1" } # , path = "../../proto/crates/pdk-test-utils" }
starbase_sandbox = "0.1.12"
tokio = { version = "1.33.0", features = ["full"] }

[profile.release]
codegen-units = 1
Expand Down
37 changes: 37 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use extism_pdk::*;
use proto_pdk::*;
use std::path::PathBuf;

#[host_fn]
extern "ExtismHost" {
fn get_env_var(name: &str) -> String;
}

pub fn get_rustup_home(env: &HostEnvironment) -> Result<PathBuf, Error> {
// Variable returns a real path
Ok(host_env!("RUSTUP_HOME")
.map(PathBuf::from)
// So we need our fallback to also be a real path
.unwrap_or_else(|| env.home_dir.real_path().join(".rustup")))
}

pub fn get_channel_from_version(spec: &VersionSpec) -> String {
if spec.is_canary() {
"nightly".to_owned()
} else {
spec.to_string()
}
}

pub fn is_non_version_channel(spec: &UnresolvedVersionSpec) -> bool {
match spec {
UnresolvedVersionSpec::Canary => true,
UnresolvedVersionSpec::Alias(value) => {
value == "stable"
|| value == "beta"
|| value == "nightly"
|| value.starts_with("nightly")
}
_ => false,
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod toolchain_toml;
// WASM cannot be executed through the test runner and we need to avoid building
// WASM code for non-WASM targets. We can solve both of these with a cfg flag.

#[cfg(not(test))]
mod helpers;

#[cfg(not(test))]
mod proto;

Expand Down
53 changes: 19 additions & 34 deletions src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::helpers::*;
use crate::toolchain_toml::ToolchainToml;
use extism_pdk::*;
use proto_pdk::*;
Expand All @@ -7,28 +8,19 @@ use std::path::PathBuf;
#[host_fn]
extern "ExtismHost" {
fn exec_command(input: Json<ExecCommandInput>) -> Json<ExecCommandOutput>;
fn get_env_var(name: &str) -> String;
fn host_log(input: Json<HostLogInput>);
}

static NAME: &str = "Rust";

fn get_rustup_home(env: &HostEnvironment) -> Result<PathBuf, Error> {
// Variable returns a real path
Ok(host_env!("RUSTUP_HOME")
.map(PathBuf::from)
// So we need our fallback to also be a real path
.unwrap_or_else(|| env.home_dir.real_path().join(".rustup")))
}

#[plugin_fn]
pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMetadataOutput>> {
let env = get_proto_environment()?;

Ok(Json(ToolMetadataOutput {
name: NAME.into(),
type_of: PluginType::Language,
default_version: Some("stable".into()),
default_version: Some(UnresolvedVersionSpec::Alias("stable".into())),
inventory: ToolInventoryMetadata {
disable_progress_bars: true,
override_dir: Some(get_rustup_home(&env)?.join("toolchains")),
Expand Down Expand Up @@ -76,11 +68,8 @@ pub fn native_install(
});
}

let channel = if input.context.version == "canary" {
"nightly"
} else {
&input.context.version
};
let version = &input.context.version;
let channel = get_channel_from_version(version);

let triple = format!("{}-{}", channel, get_target_triple(&env, NAME)?);

Expand All @@ -105,15 +94,15 @@ pub fn native_install(
} else {
host_log!("Detected a broken toolchain, uninstalling it");

exec_command!(inherit, "rustup", ["toolchain", "uninstall", channel]);
exec_command!(inherit, "rustup", ["toolchain", "uninstall", &channel]);
}
}

if do_install {
exec_command!(
inherit,
"rustup",
["toolchain", "install", channel, "--force"]
["toolchain", "install", &channel, "--force"]
);
}

Expand All @@ -128,11 +117,9 @@ pub fn native_install(
pub fn native_uninstall(
Json(input): Json<NativeUninstallInput>,
) -> FnResult<Json<NativeUninstallOutput>> {
exec_command!(
inherit,
"rustup",
["toolchain", "uninstall", &input.context.version]
);
let channel = get_channel_from_version(&input.context.version);

exec_command!(inherit, "rustup", ["toolchain", "uninstall", &channel]);

Ok(Json(NativeUninstallOutput {
uninstalled: true,
Expand Down Expand Up @@ -170,10 +157,6 @@ pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVers
Ok(Json(LoadVersionsOutput::from(tags)?))
}

fn is_non_version_channel(value: &str) -> bool {
value == "stable" || value == "beta" || value == "nightly" || value.starts_with("nightly")
}

#[plugin_fn]
pub fn resolve_version(
Json(input): Json<ResolveVersionInput>,
Expand All @@ -182,9 +165,9 @@ pub fn resolve_version(

// Allow channels as explicit aliases
if is_non_version_channel(&input.initial) {
output.version = Some(input.initial);
} else if input.initial == "canary" {
output.version = Some("nightly".into());
output.version = Some(VersionSpec::Alias(input.initial.into()));
} else if input.initial.is_canary() {
output.version = Some(VersionSpec::Alias("nightly".into()));
}

Ok(Json(output))
Expand All @@ -205,13 +188,13 @@ pub fn parse_version_file(

if input.file == "rust-toolchain" {
if !input.content.is_empty() {
output.version = Some(input.content);
output.version = Some(UnresolvedVersionSpec::parse(input.content)?);
}
} else if input.file == "rust-toolchain.toml" {
let config: ToolchainToml = toml::from_str(&input.content)?;

if let Some(channel) = config.toolchain.channel {
output.version = Some(channel);
output.version = Some(UnresolvedVersionSpec::parse(channel)?);
}
}

Expand Down Expand Up @@ -269,13 +252,15 @@ pub fn sync_manifest(Json(_): Json<SyncManifestInput>) -> FnResult<Json<SyncMani
continue;
}

let name = name.replace(&format!("-{triple}"), "");
let spec = UnresolvedVersionSpec::parse(name.replace(&format!("-{triple}"), ""))?;

if is_non_version_channel(&name) {
if is_non_version_channel(&spec) {
continue;
}

versions.push(Version::parse(&name)?);
if let UnresolvedVersionSpec::Version(version) = spec {
versions.push(version);
}
}

if !versions.is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions tests/download_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn locates_linux_bin() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "1.69.0".into(),
version: VersionSpec::parse("1.69.0").unwrap(),
..Default::default()
},
})
Expand All @@ -43,7 +43,7 @@ fn locates_macos_bin() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "1.69.0".into(),
version: VersionSpec::parse("1.69.0").unwrap(),
..Default::default()
},
})
Expand All @@ -67,7 +67,7 @@ fn locates_windows_bin() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "1.69.0".into(),
version: VersionSpec::parse("1.69.0").unwrap(),
..Default::default()
},
})
Expand Down
5 changes: 4 additions & 1 deletion tests/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ fn registers_metadata() {
});

assert_eq!(metadata.name, "Rust");
assert_eq!(metadata.default_version, Some("stable".to_owned()));
assert_eq!(
metadata.default_version,
Some(UnresolvedVersionSpec::Alias("stable".to_owned()))
);
assert!(metadata.inventory.disable_progress_bars);
assert!(metadata.inventory.override_dir.is_some());
assert!(metadata.inventory.version_suffix.is_some());
Expand Down
4 changes: 2 additions & 2 deletions tests/versions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn parses_rust_toolchain() {
file: "rust-toolchain".into(),
}),
ParseVersionFileOutput {
version: Some("1.60.0".into()),
version: Some(UnresolvedVersionSpec::parse("1.60.0").unwrap()),
}
);
}
Expand Down Expand Up @@ -72,7 +72,7 @@ fn parses_rust_toolchain_toml() {
file: "rust-toolchain.toml".into(),
}),
ParseVersionFileOutput {
version: Some("1.70.0".into()),
version: Some(UnresolvedVersionSpec::parse("1.70.0").unwrap()),
}
);
}
Expand Down

0 comments on commit b76050e

Please sign in to comment.