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

Commit

Permalink
new: Support manifest syncing. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Aug 11, 2023
1 parent 68b2514 commit 50fcd2e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.0.2

#### 🚀 Updates

- Supports automatic manifest syncing (keeps track of installed versions).

## 0.0.1

#### 🎉 Release
Expand Down
12 changes: 5 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust_plugin"
version = "0.0.1"
version = "0.0.2"
edition = "2021"
license = "MIT"
publish = false
Expand All @@ -10,13 +10,11 @@ crate-type = ['cdylib']

[dependencies]
extism-pdk = "0.3.3"
proto_pdk = { version = "0.4.2" }
proto_pdk = { version = "0.4.3" }
serde = "1.0.183"
starbase_utils = { version = "0.2.17", default-features = false, features = [
"toml",
] }
toml = "0.7.6"

[dev-dependencies]
proto_pdk_test_utils = { version = "0.3.4" }
proto_pdk_test_utils = { version = "0.3.6" }
starbase_sandbox = "0.1.8"
tokio = "1.29.1"
tokio = "1.30.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Build the plugin:
cargo build --target wasm32-wasi
```

Test the plugin by running `proto` commands. Requires proto >= v0.12.
Test the plugin by running `proto` commands. Requires proto >= v0.14.

```shell
proto install rust-test
Expand Down
48 changes: 42 additions & 6 deletions src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::toolchain_toml::ToolchainToml;
use extism_pdk::*;
use proto_pdk::*;
use starbase_utils::toml;
use std::fs;

#[host_fn]
extern "ExtismHost" {
Expand Down Expand Up @@ -125,17 +125,18 @@ pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVers
Ok(Json(LoadVersionsOutput::from_tags(&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>,
) -> FnResult<Json<ResolveVersionOutput>> {
let mut output = ResolveVersionOutput::default();

if input.initial == "stable"
|| input.initial == "beta"
|| input.initial == "nightly"
|| input.initial.starts_with("nightly")
{
// Allow channels as explicit aliases
if is_non_version_channel(&input.initial) {
output.version = Some(input.initial);
}

Expand Down Expand Up @@ -177,3 +178,38 @@ pub fn create_shims(Json(_): Json<CreateShimsInput>) -> FnResult<Json<CreateShim
..CreateShimsOutput::default()
}))
}

#[plugin_fn]
pub fn sync_manifest(Json(input): Json<SyncManifestInput>) -> FnResult<Json<SyncManifestOutput>> {
let triple = get_triple_target(&input.env)?;
let mut output = SyncManifestOutput::default();
let mut versions = vec![];

for dir in fs::read_dir(input.home_dir.join(".rustup/toolchains"))? {
let dir = dir?.path();

if !dir.is_dir() {
continue;
}

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

if !name.ends_with(&triple) {
continue;
}

let name = name.replace(&format!("-{triple}"), "");

if is_non_version_channel(&name) {
continue;
}

versions.push(Version::parse(&name)?);
}

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

Ok(Json(output))
}
2 changes: 1 addition & 1 deletion tests/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn registers_metadata() {

assert_eq!(metadata.name, "Rust");
assert_eq!(metadata.default_version, Some("stable".to_owned()));
assert_eq!(metadata.inventory.disable_progress_bars, true);
assert!(metadata.inventory.disable_progress_bars);
assert_eq!(
metadata.inventory.override_dir,
Some(PathBuf::from("/home/.rustup/toolchains"))
Expand Down

0 comments on commit 50fcd2e

Please sign in to comment.