From 0a3e061b50a66e9d8357bfb3afb1d68db62ceae8 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 13 Sep 2023 10:27:22 -0700 Subject: [PATCH] new: Allow pin even if already installed. (#200) --- CHANGELOG.md | 6 ++++ crates/cli/src/commands/install.rs | 14 +++++++-- crates/cli/tests/install_uninstall_test.rs | 35 ++++++++++++++++++++++ crates/core/src/events.rs | 2 +- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 961d85b50..2a5e834b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ - [Rust](https://github.com/moonrepo/rust-plugin/blob/master/CHANGELOG.md) - [Schema](https://github.com/moonrepo/schema-plugin/blob/master/CHANGELOG.md) +## Unreleased + +#### 🚀 Updates + +- Updated `proto install --pin` to also pin even if the tool has already been installed. + ## 0.17.0 #### 💥 Breaking diff --git a/crates/cli/src/commands/install.rs b/crates/cli/src/commands/install.rs index 9ef2cfb4d..d8743c270 100644 --- a/crates/cli/src/commands/install.rs +++ b/crates/cli/src/commands/install.rs @@ -36,6 +36,13 @@ pub struct InstallArgs { pub passthrough: Vec, } +pub fn pin_global(tool: &mut Tool) -> SystemResult { + tool.manifest.default_version = Some(tool.get_resolved_version().to_unresolved_spec()); + tool.manifest.save()?; + + Ok(()) +} + pub async fn internal_install(args: InstallArgs) -> SystemResult { let mut tool = load_tool(&args.id).await?; let version = if args.canary { @@ -45,6 +52,10 @@ pub async fn internal_install(args: InstallArgs) -> SystemResult { }; if !version.is_canary() && tool.is_setup(&version).await? { + if args.pin { + pin_global(&mut tool)?; + } + info!( "{} has already been installed at {}", tool.get_name(), @@ -91,8 +102,7 @@ pub async fn internal_install(args: InstallArgs) -> SystemResult { tool.cleanup().await?; if args.pin { - tool.manifest.default_version = Some(tool.get_resolved_version().to_unresolved_spec()); - tool.manifest.save()?; + pin_global(&mut tool)?; } pb.finish_and_clear(); diff --git a/crates/cli/tests/install_uninstall_test.rs b/crates/cli/tests/install_uninstall_test.rs index 4dc5598cf..f277a904b 100644 --- a/crates/cli/tests/install_uninstall_test.rs +++ b/crates/cli/tests/install_uninstall_test.rs @@ -196,4 +196,39 @@ mod install_uninstall { ]) ); } + + #[test] + fn can_pin_when_already_installed() { + let temp = create_empty_sandbox(); + let manifest_file = temp.path().join("tools/node/manifest.json"); + + let mut cmd = create_proto_command(temp.path()); + cmd.arg("install") + .arg("node") + .arg("19.0.0") + .arg("--") + .arg("--no-bundled-npm") + .assert(); + + // Manually change it to something else + let mut manifest = ToolManifest::load(&manifest_file).unwrap(); + manifest.default_version = Some(UnresolvedVersionSpec::parse("18.0.0").unwrap()); + manifest.save().unwrap(); + + let mut cmd = create_proto_command(temp.path()); + cmd.arg("install") + .arg("node") + .arg("19.0.0") + .arg("--pin") + .arg("--") + .arg("--no-bundled-npm") + .assert(); + + let manifest = ToolManifest::load(&manifest_file).unwrap(); + + assert_eq!( + manifest.default_version, + Some(UnresolvedVersionSpec::parse("19.0.0").unwrap()) + ); + } } diff --git a/crates/core/src/events.rs b/crates/core/src/events.rs index 2404ae1ff..7c11cf168 100644 --- a/crates/core/src/events.rs +++ b/crates/core/src/events.rs @@ -6,7 +6,7 @@ macro_rules! impl_event { impl_event!($name, (), $impl); }; - ($name:ident, $data:ty, $impl:tt) => { + ($name:ident, $data:ty, $impl:tt) => { pub struct $name $impl impl Event for $name {