Skip to content

Commit

Permalink
new: Create a registry of all shims. (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Dec 13, 2023
1 parent 2bd1b0f commit 66fea2d
Show file tree
Hide file tree
Showing 26 changed files with 328 additions and 66 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
- [Rust](https://github.com/moonrepo/rust-plugin/blob/master/CHANGELOG.md)
- [TOML schema](https://github.com/moonrepo/schema-plugin/blob/master/CHANGELOG.md)

## Unreleased

#### 🚀 Updates

- WASM API
- Added a `ExecutableConfig.shim_env_vars` field.
- Updated `ExecutableConfig.shim_before_args` and `ExecutableConfig.shim_after_args` to support a list of strings.

## 0.25.0

#### 🚀 Updates
Expand Down
3 changes: 2 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ semver = "1.0.20"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
sha2 = "0.10.8"
shell-words = "1.1.0"
starbase = "0.2.10"
starbase_archive = { version = "0.2.5", features = [
"tar-gz",
Expand Down
5 changes: 4 additions & 1 deletion crates/cli-shim/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "proto_cli_shim"
version = "0.0.1"
version = "0.25.0"
edition = "2021"
publish = false

Expand All @@ -16,3 +16,6 @@ serde_json = { workspace = true }
shared_child = "1.0.0"
sigpipe = "0.1.3"
starbase = { workspace = true }

[package.metadata.dist]
dist = true
4 changes: 4 additions & 0 deletions crates/cli-shim/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use std::process::{Command, Stdio};
use std::sync::Arc;
use std::{env, fs, io, process};

// Keep in sync with crates/core/src/shim_registry.rs
#[derive(Default, Deserialize)]
#[serde(default)]
struct Shim {
after_args: Vec<String>,
alt_for: Option<String>,
before_args: Vec<String>,
env_vars: HashMap<String, String>,
}

fn get_proto_home() -> Result<PathBuf> {
Expand Down Expand Up @@ -75,6 +77,8 @@ fn create_command(mut args: VecDeque<String>, shim_name: &str) -> Result<Command
command.args(passthrough_args);
}

command.envs(shim.env_vars);

Ok(command)
}

Expand Down
3 changes: 3 additions & 0 deletions crates/cli/tests/install_uninstall_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ mod install_uninstall {
} else {
assert!(sandbox.path().join(".proto/shims/node").exists());
}

// Check that the registry was created also
assert!(sandbox.path().join(".proto/shims/registry.json").exists());
}

#[test]
Expand Down
44 changes: 40 additions & 4 deletions crates/cli/tests/plugins_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use futures::Future;
use proto_core::{
load_tool_from_locator, Id, PluginLocator, ProtoEnvironment, Tool, UnresolvedVersionSpec,
};
use starbase_sandbox::assert_snapshot;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use utils::*;

Expand Down Expand Up @@ -127,6 +129,10 @@ mod plugins {
.arg("--version")
.assert()
.success();

assert_snapshot!(
fs::read_to_string(sandbox.path().join(".proto/shims/registry.json")).unwrap()
);
}

#[test]
Expand All @@ -143,6 +149,10 @@ mod plugins {
.arg("--version")
.assert()
.success();

assert_snapshot!(
fs::read_to_string(sandbox.path().join(".proto/shims/registry.json")).unwrap()
);
}

#[test]
Expand All @@ -159,6 +169,10 @@ mod plugins {
.arg("version")
.assert()
.success();

assert_snapshot!(
fs::read_to_string(sandbox.path().join(".proto/shims/registry.json")).unwrap()
);
}

#[test]
Expand All @@ -177,6 +191,10 @@ mod plugins {
.arg("--version")
.assert()
.success();

assert_snapshot!(
fs::read_to_string(sandbox.path().join(".proto/shims/registry.json")).unwrap()
);
}

#[test]
Expand All @@ -203,6 +221,10 @@ mod plugins {
.arg("--version")
.assert()
.success();

assert_snapshot!(
fs::read_to_string(sandbox.path().join(".proto/shims/registry.json")).unwrap()
);
}

#[test]
Expand All @@ -229,6 +251,10 @@ mod plugins {
.arg("--version")
.assert()
.success();

assert_snapshot!(
fs::read_to_string(sandbox.path().join(".proto/shims/registry.json")).unwrap()
);
}

#[test]
Expand All @@ -255,6 +281,10 @@ mod plugins {
.arg("--version")
.assert()
.success();

assert_snapshot!(
fs::read_to_string(sandbox.path().join(".proto/shims/registry.json")).unwrap()
);
}

#[test]
Expand All @@ -264,27 +294,31 @@ mod plugins {
create_proto_command(sandbox.path())
.arg("install")
.arg("python")
.arg("3.12.0") // Latest doesn't always work
.assert()
.success();

create_shim_command(sandbox.path(), "python")
.arg("--version")
.env("PROTO_PYTHON_VERSION", "3.12.0")
.assert()
.success();

assert_snapshot!(
fs::read_to_string(sandbox.path().join(".proto/shims/registry.json")).unwrap()
);
}

#[test]
fn supports_rust() {
let sandbox = create_empty_sandbox();

let assert = create_proto_command(sandbox.path())
create_proto_command(sandbox.path())
.arg("install")
.arg("rust")
.assert();

starbase_sandbox::debug_process_output(assert.get_output());

assert.success();
// Doesn't create shims
}

#[test]
Expand All @@ -296,6 +330,8 @@ mod plugins {
.arg("moon-test")
.assert()
.success();

// Doesn't create shims
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/cli/tests/plugins_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".proto/shims/registry.json\")).unwrap()"
---
{
"bun": {},
"bunx": {
"alt_for": "bun",
"before_args": [
"x"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/cli/tests/plugins_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".proto/shims/registry.json\")).unwrap()"
---
{
"deno": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/cli/tests/plugins_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".proto/shims/registry.json\")).unwrap()"
---
{
"go": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/cli/tests/plugins_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".proto/shims/registry.json\")).unwrap()"
---
{
"node": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/cli/tests/plugins_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".proto/shims/registry.json\")).unwrap()"
---
{
"node": {},
"node-gyp": {
"alt_for": "npm"
},
"npm": {},
"npx": {
"alt_for": "npm"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/cli/tests/plugins_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".proto/shims/registry.json\")).unwrap()"
---
{
"node": {},
"pnpm": {},
"pnpx": {
"alt_for": "pnpm",
"before_args": [
"dlx"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/cli/tests/plugins_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".proto/shims/registry.json\")).unwrap()"
---
{
"pip": {
"alt_for": "python",
"before_args": [
"-m",
"pip"
]
},
"python": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/cli/tests/plugins_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".proto/shims/registry.json\")).unwrap()"
---
{
"node": {},
"yarn": {},
"yarnpkg": {
"alt_for": "yarn"
}
}
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ semver = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sha2 = { workspace = true }
shell-words = { workspace = true }
starbase_archive = { workspace = true }
starbase_events = { workspace = true }
starbase_styles = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod helpers;
mod host_funcs;
mod proto;
mod proto_config;
mod shim_registry;
mod shimmer;
mod tool;
mod tool_loader;
Expand Down
62 changes: 62 additions & 0 deletions crates/core/src/shim_registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::helpers::{read_json_file_with_lock, write_json_file_with_lock};
use crate::proto::ProtoEnvironment;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};

// Keep in sync with crates/cli-shim/src/main.rs
#[derive(Default, Deserialize, PartialEq, Serialize)]
#[serde(default)]
pub struct Shim {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub after_args: Vec<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub alt_for: Option<String>,

#[serde(skip_serializing_if = "Vec::is_empty")]
pub before_args: Vec<String>,

#[serde(skip_serializing_if = "HashMap::is_empty")]
pub env_vars: HashMap<String, String>,
}

pub type ShimsMap = BTreeMap<String, Shim>;

pub struct ShimRegistry;

impl ShimRegistry {
pub fn update<P: AsRef<ProtoEnvironment>>(proto: P, entries: ShimsMap) -> miette::Result<()> {
if entries.is_empty() {
return Ok(());
}

let file = proto.as_ref().shims_dir.join("registry.json");

let mut config: ShimsMap = if file.exists() {
read_json_file_with_lock(&file)?
} else {
BTreeMap::default()
};

let mut mutated = false;

for (key, value) in entries {
// Don't write the file if nothing has changed
if config
.get(&key)
.is_some_and(|current_value| current_value == &value)
{
continue;
}

config.insert(key, value);
mutated = true;
}

if mutated {
write_json_file_with_lock(file, &config)?;
}

Ok(())
}
}
Loading

0 comments on commit 66fea2d

Please sign in to comment.