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

Commit

Permalink
new: Support proto v0.15. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Aug 21, 2023
1 parent 0fdb360 commit a33adc0
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 78 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 6 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "schema_plugin"
version = "0.0.1"
version = "0.1.0"
edition = "2021"
license = "MIT"
publish = false
Expand All @@ -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"
114 changes: 88 additions & 26 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -19,15 +18,13 @@ fn get_schema() -> Result<Schema, Error> {

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");
}

Expand All @@ -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()
}

Expand All @@ -56,6 +53,7 @@ pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMeta
SchemaType::DependencyManager => PluginType::DependencyManager,
SchemaType::Language => PluginType::Language,
},
plugin_version: Some(env!("CARGO_PKG_VERSION").into()),
..ToolMetadataOutput::default()
}))
}
Expand All @@ -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);

Expand All @@ -97,23 +100,38 @@ fn interpolate_tokens(value: &str, schema: &Schema, env: &Environment) -> String
pub fn download_prebuilt(
Json(input): Json<DownloadPrebuiltInput>,
) -> FnResult<Json<DownloadPrebuiltOutput>> {
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 {
Expand All @@ -126,12 +144,13 @@ pub fn download_prebuilt(
}

#[plugin_fn]
pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBinsOutput>> {
pub fn locate_bins(Json(_): Json<LocateBinsInput>) -> FnResult<Json<LocateBinsOutput>> {
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,
Expand Down Expand Up @@ -162,7 +181,7 @@ pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVers
})
.collect::<Vec<_>>();

return Ok(Json(LoadVersionsOutput::from_tags(&tags)?));
return Ok(Json(LoadVersionsOutput::from(tags)?));
}

if let Some(endpoint) = schema.resolve.manifest_url {
Expand All @@ -184,7 +203,7 @@ pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVers
}
}

return Ok(Json(LoadVersionsOutput::from_tags(&versions)?));
return Ok(Json(LoadVersionsOutput::from(versions)?));
}

err!(
Expand All @@ -206,17 +225,20 @@ pub fn detect_version_files(_: ()) -> FnResult<Json<DetectVersionOutput>> {
}

#[plugin_fn]
pub fn create_shims(Json(input): Json<CreateShimsInput>) -> FnResult<Json<CreateShimsOutput>> {
let mut output = CreateShimsOutput::default();
pub fn create_shims(Json(_): Json<CreateShimsInput>) -> FnResult<Json<CreateShimsOutput>> {
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 {
Expand All @@ -227,3 +249,43 @@ pub fn create_shims(Json(input): Json<CreateShimsInput>) -> FnResult<Json<Create

Ok(Json(output))
}

#[plugin_fn]
pub fn install_global(
Json(input): Json<InstallGlobalInput>,
) -> FnResult<Json<InstallGlobalOutput>> {
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::<Vec<_>>();

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<UninstallGlobalInput>,
) -> FnResult<Json<UninstallGlobalOutput>> {
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::<Vec<_>>();

let result = exec_command!(inherit, get_tool_id(), args);

return Ok(Json(UninstallGlobalOutput::from_exec_command(result)));
}

Ok(Json(UninstallGlobalOutput::default()))
}
Loading

0 comments on commit a33adc0

Please sign in to comment.