Skip to content

Commit

Permalink
new: Pass proto version in plugin context. (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Dec 19, 2023
1 parent b7f9249 commit 05f8c12
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Handles stdin, pipes, and redirects efficiently and correctly.
- Better compatibility and portability.
- WASM API
- Added a `ToolContext.proto_version` field.
- Added a `ExecutableConfig.shim_env_vars` field.
- Updated `ExecutableConfig.shim_before_args` and `ExecutableConfig.shim_after_args` to support a list of strings.

Expand Down
6 changes: 6 additions & 0 deletions crates/core/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cached::proc_macro::cached;
use miette::IntoDiagnostic;
use once_cell::sync::Lazy;
use regex::Regex;
use semver::Version;
use serde::de::DeserializeOwned;
use serde::Serialize;
use sha2::{Digest, Sha256};
Expand All @@ -19,6 +20,11 @@ use tracing::trace;

pub static ENV_VAR: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$([A-Z0-9_]+)").unwrap());

#[cached]
pub fn get_proto_version() -> Version {
Version::parse(env!("CARGO_PKG_VERSION")).unwrap()
}

pub fn get_proto_home() -> miette::Result<PathBuf> {
// if cfg!(debug_assertions) {
// return Ok(get_home_dir()?.join(".proto-debug"));
Expand Down
5 changes: 3 additions & 2 deletions crates/core/src/tool.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::error::ProtoError;
use crate::events::*;
use crate::helpers::{
extract_filename_from_url, hash_file_contents, is_archive_file, is_cache_enabled, is_offline,
remove_bin_file, ENV_VAR,
extract_filename_from_url, get_proto_version, hash_file_contents, is_archive_file,
is_cache_enabled, is_offline, remove_bin_file, ENV_VAR,
};
use crate::host_funcs::{create_host_functions, HostData};
use crate::proto::ProtoEnvironment;
Expand Down Expand Up @@ -258,6 +258,7 @@ impl Tool {
/// Return contextual information to pass to WASM plugin functions.
pub fn create_context(&self) -> ToolContext {
ToolContext {
proto_version: Some(get_proto_version()),
tool_dir: self.to_virtual_path(&self.get_tool_dir()),
version: self.get_resolved_version(),
}
Expand Down
5 changes: 4 additions & 1 deletion crates/pdk-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ json_struct!(
json_struct!(
/// Information about the current state of the tool.
pub struct ToolContext {
/// The version of proto (the core crate) calling plugin functions.
pub proto_version: Option<Version>,

/// Virtual path to the tool's installation directory.
pub tool_dir: VirtualPath,

/// Current version. Will be "latest" if not resolved.
/// Current version. Will be a "latest" alias if not resolved.
pub version: VersionSpec,
}
);
Expand Down

0 comments on commit 05f8c12

Please sign in to comment.