Skip to content

Commit

Permalink
Use $DOTNET_ROOT/sdk as inventory dir and sync installed versions
Browse files Browse the repository at this point in the history
As a bonus this allows uninstallation of the SDK.
  • Loading branch information
Phault committed Mar 16, 2024
1 parent 9299b98 commit ae4f995
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ use crate::{
#[host_fn]
extern "ExtismHost" {
fn exec_command(input: Json<ExecCommandInput>) -> Json<ExecCommandOutput>;
fn to_virtual_path(input: String) -> String;
}

static NAME: &str = ".NET";
static BIN: &str = "dotnet";

#[plugin_fn]
pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMetadataOutput>> {
let env = get_host_environment()?;
let dotnet_sdk_dir = virtual_path!(buf, get_dotnet_root(&env)?.join("sdk"));

Ok(Json(ToolMetadataOutput {
name: NAME.into(),
type_of: PluginType::Language,
plugin_version: Some(env!("CARGO_PKG_VERSION").into()),
inventory: ToolInventoryMetadata {
override_dir: Some(dotnet_sdk_dir),
// we'll stream the output from the dotnet-install script instead
disable_progress_bars: true,
..Default::default()
Expand Down Expand Up @@ -175,10 +180,10 @@ pub fn native_install(
pub fn native_uninstall(
Json(_input): Json<NativeUninstallInput>,
) -> FnResult<Json<NativeUninstallOutput>> {
warn!("Uninstalling .NET sdks is not currently supported, as they all share their installation folder.");
warn!("This will only uninstall the SDK itself, not the runtime nor any installed workloads.");

Ok(Json(NativeUninstallOutput {
uninstalled: false,
uninstalled: true,
..NativeUninstallOutput::default()
}))
}
Expand All @@ -204,3 +209,42 @@ pub fn locate_executables(
..LocateExecutablesOutput::default()
}))
}

#[plugin_fn]
pub fn sync_manifest(Json(_): Json<SyncManifestInput>) -> FnResult<Json<SyncManifestOutput>> {
let env = get_host_environment()?;
let dotnet_sdk_dir = virtual_path!(buf, get_dotnet_root(&env)?.join("sdk"));

let mut output = SyncManifestOutput::default();
let mut versions = vec![];

// Path may not be whitelisted, so exit early instead of failing
let Ok(dirs) = fs::read_dir(dotnet_sdk_dir) else {
warn!("dotnet root is not whitelisted");
return Ok(Json(output));
};

for dir in dirs {
let dir = dir?.path();

if !dir.is_dir() {
continue;
}

let name = dir.file_name().unwrap_or_default().to_string_lossy();

let Ok(spec) = UnresolvedVersionSpec::parse(name) else {
continue;
};

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

if !versions.is_empty() {
output.versions = Some(versions);
}

Ok(Json(output))
}

0 comments on commit ae4f995

Please sign in to comment.