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

Commit

Permalink
Update prefix usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 22, 2023
1 parent a8312fd commit ea6e90d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 49 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Changelog

## Unreleased
## 0.5.3

#### 🐞 Fixes

- Fixed an incorrect globals directory on Windows.

#### ⚙️ Internal

- Updated dependencies.
- Updated globals install to use a `--prefix` arg instead of `PREFIX` env var.

## 0.5.2

#### 🚀 Updates
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

47 changes: 30 additions & 17 deletions crates/common/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
use proto_pdk_api::ExecCommandInput;
use proto_pdk_api::{ExecCommandInput, HostEnvironment};
use std::path::Path;

pub fn install_global(dependency: &str, globals_dir: &Path) -> ExecCommandInput {
pub fn get_global_prefix<T: AsRef<Path>>(env: &HostEnvironment, globals_dir: T) -> String {
let prefix = globals_dir.as_ref().to_string_lossy().to_string();

// On Windows, globals will be installed into the prefix as-is,
// so binaries will exist in the root of the prefix.
if env.os.is_windows() {
return prefix;
}

// On Unix, globals are nested within a /bin directory, and since our
// fixed globals dir ends in /bin, we must remove it and set the prefix
// to the parent directory. This way everything resolves correctly.
prefix.replace("/bin", "")
}

pub fn install_global(dependency: &str, globals_prefix: String) -> ExecCommandInput {
let mut cmd = ExecCommandInput::inherit(
"npm",
[
"install",
dependency,
"--global",
"--loglevel",
"warn",
"--no-audit",
"--no-update-notifier",
dependency,
"--prefix",
&globals_prefix,
],
);

cmd.env_vars
.insert("PROTO_INSTALL_GLOBAL".into(), "true".into());

// Remove the /bin component
cmd.env_vars.insert(
"PREFIX".into(),
globals_dir.parent().unwrap().to_string_lossy().to_string(),
);

cmd
}

pub fn uninstall_global(dependency: &str, globals_dir: &Path) -> ExecCommandInput {
pub fn uninstall_global(dependency: &str, globals_prefix: String) -> ExecCommandInput {
let mut cmd = ExecCommandInput::inherit(
"npm",
["uninstall", "--global", "--loglevel", "warn", dependency],
[
"uninstall",
dependency,
"--global",
"--loglevel",
"warn",
"--prefix",
&globals_prefix,
],
);

cmd.env_vars
.insert("PROTO_INSTALL_GLOBAL".into(), "true".into());

// Remove the /bin component
cmd.env_vars.insert(
"PREFIX".into(),
globals_dir.parent().unwrap().to_string_lossy().to_string(),
);

cmd
}
16 changes: 0 additions & 16 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,3 @@ mod package_json;

pub use node_dist::*;
pub use package_json::*;

use proto_pdk_api::HostEnvironment;

pub fn get_globals_dirs(env: &HostEnvironment) -> Vec<String> {
let mut dirs = vec![];

// Windows for some reason removes the /bin suffix when installing into it,
// so we also need to account for the path without /bin. But keep the /bin path
// as the final path and for the install to trigger correctly.
if env.os.is_windows() {
dirs.push("$PROTO_HOME/tools/node/globals".into());
}

dirs.push("$PROTO_HOME/tools/node/globals/bin".into());
dirs
}
2 changes: 1 addition & 1 deletion crates/node-depman/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node_depman_plugin"
version = "0.5.2"
version = "0.5.3"
edition = "2021"
license = "MIT"
publish = false
Expand Down
18 changes: 12 additions & 6 deletions crates/node-depman/src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::npm_registry::parse_registry_response;
use crate::package_manager::PackageManager;
use extism_pdk::*;
use node_common::{commands, get_globals_dirs, BinField, NodeDistVersion, PackageJson};
use node_common::{
commands::{self, get_global_prefix},
BinField, NodeDistVersion, PackageJson,
};
use proto_pdk::*;
use std::collections::HashMap;
use std::fs;
Expand Down Expand Up @@ -310,7 +313,7 @@ pub fn locate_executables(
};

Ok(Json(LocateExecutablesOutput {
globals_lookup_dirs: get_globals_dirs(&env),
globals_lookup_dirs: vec!["$PROTO_HOME/tools/node/globals/bin".into()],
primary: Some(primary),
secondary,
..LocateExecutablesOutput::default()
Expand All @@ -321,9 +324,11 @@ pub fn locate_executables(
pub fn install_global(
Json(input): Json<InstallGlobalInput>,
) -> FnResult<Json<InstallGlobalOutput>> {
let env = get_proto_environment()?;

let result = exec_command!(commands::install_global(
&input.dependency,
&input.globals_dir.real_path(),
get_global_prefix(&env, input.globals_dir.real_path()),
));

Ok(Json(InstallGlobalOutput::from_exec_command(result)))
Expand All @@ -333,9 +338,11 @@ pub fn install_global(
pub fn uninstall_global(
Json(input): Json<UninstallGlobalInput>,
) -> FnResult<Json<UninstallGlobalOutput>> {
let env = get_proto_environment()?;

let result = exec_command!(commands::uninstall_global(
&input.dependency,
&input.globals_dir.real_path(),
get_global_prefix(&env, input.globals_dir.real_path()),
));

Ok(Json(UninstallGlobalOutput::from_exec_command(result)))
Expand Down Expand Up @@ -393,7 +400,6 @@ pub fn pre_run(Json(input): Json<RunHook>) -> FnResult<()> {

#[plugin_fn]
pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBinsOutput>> {
let env = get_proto_environment()?;
let mut bin_path = None;
let package_path = input.context.tool_dir.join("package.json");
let manager = PackageManager::detect();
Expand Down Expand Up @@ -437,7 +443,7 @@ pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBi
Ok(Json(LocateBinsOutput {
bin_path: bin_path.map(PathBuf::from),
fallback_last_globals_dir: true,
globals_lookup_dirs: get_globals_dirs(&env),
globals_lookup_dirs: vec!["$PROTO_HOME/tools/node/globals/bin".into()],
..LocateBinsOutput::default()
}))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node_plugin"
version = "0.5.2"
version = "0.5.3"
edition = "2021"
license = "MIT"
publish = false
Expand Down
17 changes: 12 additions & 5 deletions crates/node/src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use extism_pdk::*;
use node_common::{commands, get_globals_dirs, NodeDistLTS, NodeDistVersion, PackageJson};
use node_common::{
commands::{self, get_global_prefix},
NodeDistLTS, NodeDistVersion, PackageJson,
};
use proto_pdk::*;

#[host_fn]
Expand Down Expand Up @@ -206,7 +209,7 @@ pub fn locate_executables(
let env = get_proto_environment()?;

Ok(Json(LocateExecutablesOutput {
globals_lookup_dirs: get_globals_dirs(&env),
globals_lookup_dirs: vec!["$PROTO_HOME/tools/node/globals/bin".into()],
primary: Some(ExecutableConfig::new(if env.os == HostOS::Windows {
format!("{}.exe", BIN)
} else {
Expand All @@ -220,9 +223,11 @@ pub fn locate_executables(
pub fn install_global(
Json(input): Json<InstallGlobalInput>,
) -> FnResult<Json<InstallGlobalOutput>> {
let env = get_proto_environment()?;

let result = exec_command!(commands::install_global(
&input.dependency,
&input.globals_dir.real_path(),
get_global_prefix(&env, input.globals_dir.real_path()),
));

Ok(Json(InstallGlobalOutput::from_exec_command(result)))
Expand All @@ -232,9 +237,11 @@ pub fn install_global(
pub fn uninstall_global(
Json(input): Json<UninstallGlobalInput>,
) -> FnResult<Json<UninstallGlobalOutput>> {
let env = get_proto_environment()?;

let result = exec_command!(commands::uninstall_global(
&input.dependency,
&input.globals_dir.real_path(),
get_global_prefix(&env, input.globals_dir.real_path()),
));

Ok(Json(UninstallGlobalOutput::from_exec_command(result)))
Expand Down Expand Up @@ -288,7 +295,7 @@ pub fn locate_bins(Json(_): Json<LocateBinsInput>) -> FnResult<Json<LocateBinsOu
format!("bin/{}", BIN).into()
}),
fallback_last_globals_dir: true,
globals_lookup_dirs: get_globals_dirs(&env),
globals_lookup_dirs: vec!["$PROTO_HOME/tools/node/globals/bin".into()],
..LocateBinsOutput::default()
}))
}

0 comments on commit ea6e90d

Please sign in to comment.