diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dec2d5..ad1bf13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.1.0 + +#### 🚀 Updates + +- Added support for `install_global` and `uninstall_global`. +- Updated to support proto v0.15 release. + ## 0.0.1 #### 🎉 Release diff --git a/Cargo.toml b/Cargo.toml index 8fbc9d3..6aa6139 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "schema_plugin" -version = "0.0.1" +version = "0.1.0" edition = "2021" license = "MIT" publish = false @@ -10,16 +10,14 @@ crate-type = ['cdylib'] [dependencies] extism-pdk = "0.3.3" -proto_pdk = { version = "0.4.3" } -proto_schema_plugin = { version = "0.9.1" } +proto_pdk = { version = "0.6.2" } # , path = "../../proto/crates/pdk" } +proto_schema_plugin = { version = "0.11.0" } # , path = "../../proto/crates/schema-plugin" } regex = "1.9.3" serde = "1.0.183" serde_json = "1.0.104" -starbase_utils = { version = "0.2.17", default-features = false, features = [ - "toml", -] } +toml = "0.7.6" [dev-dependencies] -proto_pdk_test_utils = { version = "0.3.5" } +proto_pdk_test_utils = { version = "0.5.0" } # , path = "../../proto/crates/pdk-test-utils" } starbase_sandbox = "0.1.8" -tokio = "1.30.0" +tokio = "1.31.0" diff --git a/src/proto.rs b/src/proto.rs index f6332af..3085fc1 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -2,7 +2,6 @@ use extism_pdk::*; use proto_pdk::*; use proto_schema_plugin::{PlatformMapper, Schema, SchemaType}; use serde_json::Value as JsonValue; -use starbase_utils::toml; use std::path::PathBuf; #[host_fn] @@ -19,15 +18,13 @@ fn get_schema() -> Result { fn get_platform<'schema>( schema: &'schema Schema, - env: &Environment, + env: &HostEnvironment, ) -> Result<&'schema PlatformMapper, PluginError> { let os = env.os.to_string(); let mut platform = schema.platform.get(&os); // Fallback to linux for other OSes - if platform.is_none() - && (env.os == HostOS::FreeBSD || env.os == HostOS::NetBSD || env.os == HostOS::OpenBSD) - { + if platform.is_none() && env.os.is_bsd() { platform = schema.platform.get("linux"); } @@ -37,11 +34,11 @@ fn get_platform<'schema>( }) } -fn get_bin_path(platform: &PlatformMapper, env: &Environment) -> PathBuf { +fn get_bin_path(platform: &PlatformMapper, env: &HostEnvironment) -> PathBuf { platform .bin_path .clone() - .unwrap_or_else(|| format_bin_name(&env.id, env.os)) + .unwrap_or_else(|| format_bin_name(&get_tool_id(), env.os)) .into() } @@ -56,6 +53,7 @@ pub fn register_tool(Json(_): Json) -> FnResult PluginType::DependencyManager, SchemaType::Language => PluginType::Language, }, + plugin_version: Some(env!("CARGO_PKG_VERSION").into()), ..ToolMetadataOutput::default() })) } @@ -69,12 +67,17 @@ fn is_musl() -> bool { } } -fn interpolate_tokens(value: &str, schema: &Schema, env: &Environment) -> String { +fn interpolate_tokens( + value: &str, + version: &str, + schema: &Schema, + env: &HostEnvironment, +) -> String { let arch = env.arch.to_rust_arch(); let os = env.os.to_string(); let mut value = value - .replace("{version}", &env.version) + .replace("{version}", version) .replace("{arch}", schema.install.arch.get(&arch).unwrap_or(&arch)) .replace("{os}", &os); @@ -97,23 +100,38 @@ fn interpolate_tokens(value: &str, schema: &Schema, env: &Environment) -> String pub fn download_prebuilt( Json(input): Json, ) -> FnResult> { + let env = get_proto_environment()?; let schema = get_schema()?; - let platform = get_platform(&schema, &input.env)?; + let platform = get_platform(&schema, &env)?; - let download_file = interpolate_tokens(&platform.download_file, &schema, &input.env); - let download_url = interpolate_tokens(&schema.install.download_url, &schema, &input.env) - .replace("{download_file}", &download_file); + let download_file = interpolate_tokens( + &platform.download_file, + &input.context.version, + &schema, + &env, + ); + + let download_url = interpolate_tokens( + &schema.install.download_url, + &input.context.version, + &schema, + &env, + ) + .replace("{download_file}", &download_file); let checksum_file = interpolate_tokens( platform .checksum_file .as_ref() .unwrap_or(&"CHECKSUM.txt".to_string()), + &input.context.version, &schema, - &input.env, + &env, ); + let checksum_url = schema.install.checksum_url.as_ref().map(|url| { - interpolate_tokens(url, &schema, &input.env).replace("{checksum_file}", &checksum_file) + interpolate_tokens(url, &input.context.version, &schema, &env) + .replace("{checksum_file}", &checksum_file) }); Ok(Json(DownloadPrebuiltOutput { @@ -126,12 +144,13 @@ pub fn download_prebuilt( } #[plugin_fn] -pub fn locate_bins(Json(input): Json) -> FnResult> { +pub fn locate_bins(Json(_): Json) -> FnResult> { + let env = get_proto_environment()?; let schema = get_schema()?; - let platform = get_platform(&schema, &input.env)?; + let platform = get_platform(&schema, &env)?; Ok(Json(LocateBinsOutput { - bin_path: Some(get_bin_path(platform, &input.env)), + bin_path: Some(get_bin_path(platform, &env)), fallback_last_globals_dir: true, globals_lookup_dirs: schema.globals.lookup_dirs, globals_prefix: schema.globals.package_prefix, @@ -162,7 +181,7 @@ pub fn load_versions(Json(_): Json) -> FnResult>(); - return Ok(Json(LoadVersionsOutput::from_tags(&tags)?)); + return Ok(Json(LoadVersionsOutput::from(tags)?)); } if let Some(endpoint) = schema.resolve.manifest_url { @@ -184,7 +203,7 @@ pub fn load_versions(Json(_): Json) -> FnResult FnResult> { } #[plugin_fn] -pub fn create_shims(Json(input): Json) -> FnResult> { - let mut output = CreateShimsOutput::default(); +pub fn create_shims(Json(_): Json) -> FnResult> { + let env = get_proto_environment()?; let schema = get_schema()?; - let platform = get_platform(&schema, &input.env)?; - let bin_path = get_bin_path(platform, &input.env); + let platform = get_platform(&schema, &env)?; + let bin_path = get_bin_path(platform, &env); - output.no_primary_global = !schema.shim.global; + let mut output = CreateShimsOutput { + no_primary_global: !schema.shim.global, + ..CreateShimsOutput::default() + }; if schema.shim.local { output.local_shims.insert( - input.env.id, + get_tool_id(), if let Some(parent_bin) = schema.shim.parent_bin { ShimConfig::local_with_parent(bin_path, parent_bin) } else { @@ -227,3 +249,43 @@ pub fn create_shims(Json(input): Json) -> FnResult, +) -> FnResult> { + let schema = get_schema()?; + + if let Some(install_args) = schema.globals.install_args { + let args = install_args + .into_iter() + .map(|arg| arg.replace("{dependency}", &input.dependency)) + .collect::>(); + + let result = exec_command!(inherit, get_tool_id(), args); + + return Ok(Json(InstallGlobalOutput::from_exec_command(result))); + } + + Ok(Json(InstallGlobalOutput::default())) +} + +#[plugin_fn] +pub fn uninstall_global( + Json(input): Json, +) -> FnResult> { + let schema = get_schema()?; + + if let Some(uninstall_args) = schema.globals.uninstall_args { + let args = uninstall_args + .into_iter() + .map(|arg| arg.replace("{dependency}", &input.dependency)) + .collect::>(); + + let result = exec_command!(inherit, get_tool_id(), args); + + return Ok(Json(UninstallGlobalOutput::from_exec_command(result))); + } + + Ok(Json(UninstallGlobalOutput::default())) +} diff --git a/tests/download_test.rs b/tests/download_test.rs index cb1591d..d7dbdd9 100644 --- a/tests/download_test.rs +++ b/tests/download_test.rs @@ -1,6 +1,5 @@ use proto_pdk_test_utils::*; use starbase_sandbox::{create_empty_sandbox, locate_fixture}; -use std::path::PathBuf; generate_download_install_tests!( "schema-test", @@ -11,17 +10,21 @@ generate_download_install_tests!( #[test] fn supports_linux_arm64() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::Arm64, + os: HostOS::Linux, + ..Default::default() + }); + assert_eq!( plugin.download_prebuilt(DownloadPrebuiltInput { - env: Environment { - arch: HostArch::Arm64, - os: HostOS::Linux, + context: ToolContext { version: "20.0.0".into(), ..Default::default() } @@ -39,17 +42,21 @@ fn supports_linux_arm64() { #[test] fn supports_linux_x64() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::X64, + os: HostOS::Linux, + ..Default::default() + }); + assert_eq!( plugin.download_prebuilt(DownloadPrebuiltInput { - env: Environment { - arch: HostArch::X64, - os: HostOS::Linux, + context: ToolContext { version: "20.0.0".into(), ..Default::default() } @@ -67,17 +74,21 @@ fn supports_linux_x64() { #[test] fn supports_macos_arm64() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::Arm64, + os: HostOS::MacOS, + ..Default::default() + }); + assert_eq!( plugin.download_prebuilt(DownloadPrebuiltInput { - env: Environment { - arch: HostArch::Arm64, - os: HostOS::MacOS, + context: ToolContext { version: "20.0.0".into(), ..Default::default() } @@ -95,17 +106,21 @@ fn supports_macos_arm64() { #[test] fn supports_macos_x64() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::X64, + os: HostOS::MacOS, + ..Default::default() + }); + assert_eq!( plugin.download_prebuilt(DownloadPrebuiltInput { - env: Environment { - arch: HostArch::X64, - os: HostOS::MacOS, + context: ToolContext { version: "20.0.0".into(), ..Default::default() } @@ -123,17 +138,21 @@ fn supports_macos_x64() { #[test] fn supports_windows_arm64() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::Arm64, + os: HostOS::Windows, + ..Default::default() + }); + assert_eq!( plugin.download_prebuilt(DownloadPrebuiltInput { - env: Environment { - arch: HostArch::Arm64, - os: HostOS::Windows, + context: ToolContext { version: "20.0.0".into(), ..Default::default() } @@ -151,17 +170,21 @@ fn supports_windows_arm64() { #[test] fn supports_windows_x86() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::X86, + os: HostOS::Windows, + ..Default::default() + }); + assert_eq!( plugin.download_prebuilt(DownloadPrebuiltInput { - env: Environment { - arch: HostArch::X86, - os: HostOS::Windows, + context: ToolContext { version: "20.0.0".into(), ..Default::default() } @@ -179,23 +202,25 @@ fn supports_windows_x86() { #[test] fn locates_linux_bin() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::Arm64, + os: HostOS::Linux, + ..Default::default() + }); + assert_eq!( plugin .locate_bins(LocateBinsInput { - env: Environment { - arch: HostArch::Arm64, - os: HostOS::Linux, + context: ToolContext { version: "20.0.0".into(), ..Default::default() }, - home_dir: PathBuf::new(), - tool_dir: PathBuf::new(), }) .bin_path, Some("lin/moon".into()) @@ -205,23 +230,25 @@ fn locates_linux_bin() { #[test] fn locates_macos_bin() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::X64, + os: HostOS::MacOS, + ..Default::default() + }); + assert_eq!( plugin .locate_bins(LocateBinsInput { - env: Environment { - arch: HostArch::X64, - os: HostOS::MacOS, + context: ToolContext { version: "20.0.0".into(), ..Default::default() }, - home_dir: PathBuf::new(), - tool_dir: PathBuf::new(), }) .bin_path, Some("mac/moon".into()) @@ -231,23 +258,25 @@ fn locates_macos_bin() { #[test] fn locates_windows_bin() { let sandbox = create_empty_sandbox(); - let plugin = create_schema_plugin( + let mut plugin = create_schema_plugin( "schema-test", sandbox.path(), locate_fixture("schemas").join("bins.toml"), ); + plugin.set_environment(HostEnvironment { + arch: HostArch::X64, + os: HostOS::Windows, + ..Default::default() + }); + assert_eq!( plugin .locate_bins(LocateBinsInput { - env: Environment { - arch: HostArch::X64, - os: HostOS::Windows, + context: ToolContext { version: "20.0.0".into(), ..Default::default() }, - home_dir: PathBuf::new(), - tool_dir: PathBuf::new(), }) .bin_path, Some("win/moon.exe".into()) diff --git a/tests/metadata_test.rs b/tests/metadata_test.rs index 1a60451..4e68c8a 100644 --- a/tests/metadata_test.rs +++ b/tests/metadata_test.rs @@ -15,6 +15,7 @@ fn registers_metadata() { ToolMetadataOutput { name: "moon-test".into(), type_of: PluginType::CLI, + plugin_version: Some(env!("CARGO_PKG_VERSION").into()), ..ToolMetadataOutput::default() } ); diff --git a/tests/versions_test.rs b/tests/versions_test.rs index 4ae0c34..a2c2779 100644 --- a/tests/versions_test.rs +++ b/tests/versions_test.rs @@ -7,7 +7,7 @@ generate_resolve_versions_tests!( "1.0" => "1.0.3", "1.4" => "1.4.0", "1.5" => "1.5.1", - "1" => "1.11.1", + "1" => "1.12.0", }, Some(locate_fixture("schemas").join("base.toml")) );