Skip to content

Commit

Permalink
new: Add pin-latest setting. (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 29, 2023
1 parent 5d0650f commit 9d6a880
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 165 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#### 🚀 Updates

- Added a `proto pin` command, which is a merge of the old `proto global` and `proto local` commands.
- Added a `pin-latest` setting to `~/.proto/config.toml` that'll automatically pin tools when they're being installed with the "latest" version.
- Updated `proto install` to auto-clean stale plugins after a successful installation.

## 0.18.5
Expand Down
107 changes: 0 additions & 107 deletions Cargo.lock

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

44 changes: 33 additions & 11 deletions crates/cli/src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::clean::clean_plugins;
use super::pin::{internal_pin, PinArgs};
use crate::helpers::{create_progress_bar, disable_progress_bars};
use crate::shell;
use clap::Args;
use miette::IntoDiagnostic;
use proto_core::{load_tool, Id, Tool, UnresolvedVersionSpec};
use proto_core::{load_tool, Id, PinType, Tool, UnresolvedVersionSpec, UserConfig};
use proto_pdk_api::{InstallHook, SyncShellProfileInput, SyncShellProfileOutput};
use starbase::{system, SystemResult};
use starbase_styles::color;
Expand Down Expand Up @@ -37,9 +38,34 @@ pub struct InstallArgs {
pub passthrough: Vec<String>,
}

pub fn pin_global(tool: &mut Tool) -> SystemResult {
tool.manifest.default_version = Some(tool.get_resolved_version().to_unresolved_spec());
tool.manifest.save()?;
fn pin_version(
tool: &mut Tool,
initial_version: &UnresolvedVersionSpec,
global: bool,
) -> SystemResult {
let mut args = PinArgs {
id: tool.id.clone(),
spec: tool.get_resolved_version().to_unresolved_spec(),
global: false,
};

// via `--pin` arg
if global {
args.global = true;

return internal_pin(tool, &args);
}

// via `pin-latest` setting
if initial_version.is_latest() {
let user_config = UserConfig::load()?;

if let Some(pin_type) = user_config.pin_latest {
args.global = matches!(pin_type, PinType::Global);

return internal_pin(tool, &args);
}
}

Ok(())
}
Expand All @@ -56,9 +82,7 @@ pub async fn internal_install(args: InstallArgs) -> SystemResult {
tool.disable_caching();

if !version.is_canary() && tool.is_setup(&version).await? {
if args.pin {
pin_global(&mut tool)?;
}
pin_version(&mut tool, &version, args.pin)?;

info!(
"{} has already been installed at {}",
Expand Down Expand Up @@ -110,9 +134,7 @@ pub async fn internal_install(args: InstallArgs) -> SystemResult {
return Ok(());
}

if args.pin {
pin_global(&mut tool)?;
}
pin_version(&mut tool, &version, args.pin)?;

info!(
"{} has been installed to {}!",
Expand All @@ -134,7 +156,7 @@ pub async fn internal_install(args: InstallArgs) -> SystemResult {
update_shell(tool, args.passthrough.clone())?;

// Clean plugins
debug!("Auto-cleaning old plugins");
debug!("Auto-cleaning plugins");

clean_plugins(7).await?;

Expand Down
42 changes: 21 additions & 21 deletions crates/cli/src/commands/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ use std::env;
use std::path::PathBuf;

use clap::Args;
use proto_core::{load_tool, Id, ToolsConfig, UnresolvedVersionSpec};
use starbase::system;
use proto_core::{load_tool, Id, Tool, ToolsConfig, UnresolvedVersionSpec};
use starbase::{system, SystemResult};
use starbase_styles::color;
use tracing::{debug, info};

#[derive(Args, Clone, Debug)]
pub struct PinArgs {
#[arg(required = true, help = "ID of tool")]
id: Id,
pub id: Id,

#[arg(required = true, help = "Version or alias of tool")]
spec: UnresolvedVersionSpec,
pub spec: UnresolvedVersionSpec,

#[arg(
long,
help = "Add to the global user config instead of local .prototools"
)]
global: bool,
pub global: bool,
}

#[system]
pub async fn pin(args: ArgsRef<PinArgs>) -> SystemResult {
let mut tool = load_tool(&args.id).await?;

pub fn internal_pin(tool: &mut Tool, args: &PinArgs) -> SystemResult {
if args.global {
tool.manifest.default_version = Some(args.spec.clone());
tool.manifest.save()?;
Expand All @@ -35,12 +32,6 @@ pub async fn pin(args: ArgsRef<PinArgs>) -> SystemResult {
manifest = ?tool.manifest.path,
"Wrote the global version",
);

info!(
"Set the global {} version to {}",
tool.get_name(),
color::hash(args.spec.to_string())
);
} else {
let local_path = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

Expand All @@ -53,11 +44,20 @@ pub async fn pin(args: ArgsRef<PinArgs>) -> SystemResult {
config = ?config.path,
"Wrote the local version",
);

info!(
"Set the local {} version to {}",
tool.get_name(),
color::hash(args.spec.to_string())
);
}

Ok(())
}

#[system]
pub async fn pin(args: ArgsRef<PinArgs>) -> SystemResult {
let mut tool = load_tool(&args.id).await?;

internal_pin(&mut tool, args)?;

info!(
"Set the {} version to {}",
tool.get_name(),
color::hash(args.spec.to_string())
);
}
Loading

0 comments on commit 9d6a880

Please sign in to comment.