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

Commit

Permalink
new: Changes for proto v0.18. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Sep 18, 2023
1 parent afe5a99 commit 2405f8d
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 62 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 0.3.0

#### 🚀 Breaking

- We updated the schema internally to be represented as JSON instead of TOML, which may cause breakages depending on a version mismatch between proto and the plugin.

#### 🐞 Fixes

- Fixed version parsing from tags to be more accurate. Will now properly include prerelease/build metadata.

## 0.2.0

#### 🚀 Updates
Expand Down
67 changes: 15 additions & 52 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "schema_plugin"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
license = "MIT"
publish = false
Expand All @@ -10,15 +10,13 @@ crate-type = ['cdylib']

[dependencies]
extism-pdk = "0.3.4"
proto_pdk = { version = "0.7.1" } # , path = "../../proto/crates/pdk" }
proto_schema_plugin = { version = "0.11.4" } # , path = "../../proto/crates/schema-plugin" }
proto_pdk = { version = "0.7.2" } # , path = "../../proto/crates/pdk" }
regex = "1.9.5"
serde = "1.0.188"
serde_json = "1.0.105"
toml = "0.7.7"
serde_json = "1.0.107"

[dev-dependencies]
proto_pdk_test_utils = { version = "0.6.2" } # , path = "../../proto/crates/pdk-test-utils" }
proto_pdk_test_utils = { version = "0.7.0" } # , path = "../../proto/crates/pdk-test-utils" }
starbase_sandbox = "0.1.10"
tokio = "1.32.0"

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod schema;

// 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.

Expand Down
10 changes: 6 additions & 4 deletions src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use crate::schema::{PlatformMapper, Schema, SchemaType};
use extism_pdk::*;
use proto_pdk::*;
use proto_schema_plugin::{PlatformMapper, Schema, SchemaType};
use serde_json::Value as JsonValue;
use std::path::PathBuf;

#[host_fn]
extern "ExtismHost" {
fn host_log(input: Json<HostLogInput>);
fn exec_command(input: Json<ExecCommandInput>) -> Json<ExecCommandOutput>;
}

fn get_schema() -> Result<Schema, Error> {
let data = config::get("schema").expect("Missing schema!");
let schema: Schema = toml::from_str(&data)?;
let schema: Schema = json::from_str(&data)?;

Ok(schema)
}
Expand Down Expand Up @@ -174,12 +175,13 @@ pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVers
if let Some(repository) = schema.resolve.git_url {
let pattern = regex::Regex::new(&schema.resolve.git_tag_pattern)?;

let tags = load_git_tags(repository)?
let tags = load_git_tags(repository)?;
let tags = tags
.into_iter()
.filter_map(|t| {
pattern
.captures(&t)
.map(|captures| remove_v_prefix(captures.get(1).unwrap().as_str()).to_string())
.map(|captures| captures.get(1).unwrap().as_str().to_string())
})
.collect::<Vec<_>>();

Expand Down
101 changes: 101 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct PlatformMapper {
pub archive_prefix: Option<String>,
pub bin_path: Option<String>,
pub checksum_file: Option<String>,
pub download_file: String,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct DetectSchema {
pub version_files: Option<Vec<String>>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct InstallSchema {
pub arch: HashMap<String, String>,
pub checksum_url: Option<String>,
pub checksum_url_canary: Option<String>,
pub download_url: String,
pub download_url_canary: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct GlobalsSchema {
pub install_args: Option<Vec<String>>,
pub lookup_dirs: Vec<String>,
pub package_prefix: Option<String>,
pub uninstall_args: Option<Vec<String>>,
}

#[derive(Debug, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct ResolveSchema {
// Manifest
pub manifest_url: Option<String>,
pub manifest_version_key: String,
// Tags
pub git_url: Option<String>,
pub git_tag_pattern: String,
}

impl Default for ResolveSchema {
fn default() -> Self {
ResolveSchema {
manifest_url: None,
manifest_version_key: "version".to_string(),
git_url: None,
git_tag_pattern: r"^v?((\d+)\.(\d+)\.(\d+)(-[a-zA-Z\.\d]+)?(+[-a-zA-Z\.\d]+)?)$"
.to_string(),
}
}
}

#[derive(Debug, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct ShimSchema {
pub local: bool,
pub global: bool,
pub parent_bin: Option<String>,
}

impl Default for ShimSchema {
fn default() -> Self {
ShimSchema {
local: false,
global: true,
parent_bin: None,
}
}
}

#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum SchemaType {
#[default]
Language,
DependencyManager,
Cli,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct Schema {
pub name: String,
#[serde(rename = "type")]
pub type_of: SchemaType,
pub platform: HashMap<String, PlatformMapper>,

pub detect: DetectSchema,
pub install: InstallSchema,
pub globals: GlobalsSchema,
pub resolve: ResolveSchema,
pub shim: ShimSchema,
}

0 comments on commit 2405f8d

Please sign in to comment.