Skip to content

Commit

Permalink
Rename enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 7, 2023
1 parent f5940a1 commit a37fca6
Show file tree
Hide file tree
Showing 29 changed files with 339 additions and 242 deletions.
10 changes: 5 additions & 5 deletions crates/cli/src/commands/alias.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Args;
use proto_core::{is_alias_name, load_tool, Id, ProtoError, VersionType};
use proto_core::{is_alias_name, load_tool, Id, ProtoError, UnresolvedVersionSpec};
use starbase::system;
use starbase_styles::color;
use tracing::info;
Expand All @@ -13,12 +13,12 @@ pub struct AliasArgs {
alias: String,

#[arg(required = true, help = "Version or alias to associate with")]
semver: VersionType,
spec: UnresolvedVersionSpec,
}

#[system]
pub async fn alias(args: ArgsRef<AliasArgs>) {
if let VersionType::Alias(inner_alias) = &args.semver {
if let UnresolvedVersionSpec::Alias(inner_alias) = &args.spec {
if &args.alias == inner_alias {
return Err(ProtoError::Message("Cannot map an alias to itself.".into()))?;
}
Expand All @@ -34,13 +34,13 @@ pub async fn alias(args: ArgsRef<AliasArgs>) {

tool.manifest
.aliases
.insert(args.alias.clone(), args.semver.clone());
.insert(args.alias.clone(), args.spec.clone());
tool.manifest.save()?;

info!(
"Added alias {} ({}) for {}",
color::id(&args.alias),
color::muted_light(args.semver.to_string()),
color::muted_light(args.spec.to_string()),
tool.get_name(),
);
}
6 changes: 3 additions & 3 deletions crates/cli/src/commands/bin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Args;
use proto_core::{detect_version, load_tool, Id, VersionType};
use proto_core::{detect_version, load_tool, Id, UnresolvedVersionSpec};
use starbase::system;

#[derive(Args, Clone, Debug)]
Expand All @@ -8,7 +8,7 @@ pub struct BinArgs {
id: Id,

#[arg(help = "Version or alias of tool")]
semver: Option<VersionType>,
spec: Option<UnresolvedVersionSpec>,

#[arg(long, help = "Display shim path when available")]
shim: bool,
Expand All @@ -17,7 +17,7 @@ pub struct BinArgs {
#[system]
pub async fn bin(args: ArgsRef<BinArgs>) {
let mut tool = load_tool(&args.id).await?;
let version = detect_version(&tool, args.semver.clone()).await?;
let version = detect_version(&tool, args.spec.clone()).await?;

tool.resolve_version(&version).await?;
tool.locate_bins().await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Args;
use dialoguer::Confirm;
use proto_core::{
get_plugins_dir, get_shim_file_name, load_tool, AliasOrVersion, Id, Tool, ToolsConfig,
get_plugins_dir, get_shim_file_name, load_tool, Id, Tool, ToolsConfig, VersionSpec,
};
use proto_pdk_api::{CreateShimsInput, CreateShimsOutput};
use starbase::diagnostics::IntoDiagnostic;
Expand Down Expand Up @@ -52,7 +52,7 @@ pub async fn clean_tool(mut tool: Tool, now: u128, days: u8, yes: bool) -> miett
return Ok(0);
}

let mut versions_to_clean = HashSet::<AliasOrVersion>::new();
let mut versions_to_clean = HashSet::<VersionSpec>::new();

debug!("Scanning file system for stale and untracked versions");

Expand All @@ -70,7 +70,7 @@ pub async fn clean_tool(mut tool: Tool, now: u128, days: u8, yes: bool) -> miett
continue;
}

let version = AliasOrVersion::parse(&dir_name)?;
let version = VersionSpec::parse(&dir_name)?;

if !tool.manifest.versions.contains_key(&version) {
debug!(
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/src/commands/global.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Args;
use proto_core::{load_tool, Id, VersionType};
use proto_core::{load_tool, Id, UnresolvedVersionSpec};
use starbase::system;
use starbase_styles::color;
use tracing::{debug, info};
Expand All @@ -10,25 +10,25 @@ pub struct GlobalArgs {
id: Id,

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

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

tool.manifest.default_version = Some(args.semver.clone());
tool.manifest.default_version = Some(args.spec.clone());
tool.manifest.save()?;

debug!(
version = args.semver.to_string(),
version = args.spec.to_string(),
manifest = ?tool.manifest.path,
"Wrote the global version",
);

info!(
"Set the global {} version to {}",
tool.get_name(),
color::hash(args.semver.to_string())
color::hash(args.spec.to_string())
);
}
6 changes: 3 additions & 3 deletions crates/cli/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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, VersionType};
use proto_core::{load_tool, Id, Tool, UnresolvedVersionSpec};
use proto_pdk_api::{InstallHook, SyncShellProfileInput, SyncShellProfileOutput};
use starbase::{system, SystemResult};
use starbase_styles::color;
Expand All @@ -15,7 +15,7 @@ pub struct InstallArgs {
pub id: Id,

#[arg(default_value = "latest", help = "Version or alias of tool")]
pub semver: Option<VersionType>,
pub spec: Option<UnresolvedVersionSpec>,

#[arg(long, help = "Pin version as the global default")]
pub pin: bool,
Expand All @@ -26,7 +26,7 @@ pub struct InstallArgs {
}

pub async fn internal_install(args: InstallArgs) -> SystemResult {
let version = args.semver.clone().unwrap_or_default();
let version = args.spec.clone().unwrap_or_default();
let mut tool = load_tool(&args.id).await?;

if tool.is_setup(&version).await? {
Expand Down
10 changes: 7 additions & 3 deletions crates/cli/src/commands/install_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::{
helpers::create_progress_bar,
};
use futures::future::try_join_all;
use proto_core::{load_tool_from_locator, ProtoEnvironment, ToolsConfig, UserConfig, VersionType};
use proto_core::{
load_tool_from_locator, ProtoEnvironment, ToolsConfig, UnresolvedVersionSpec, UserConfig,
};
use starbase::system;
use starbase_styles::color;
use std::env;
Expand Down Expand Up @@ -38,7 +40,9 @@ pub async fn install_all() {

debug!("Detected version {} for {}", version, tool.get_name());

config.tools.insert(name, VersionType::Version(version));
config
.tools
.insert(name, UnresolvedVersionSpec::Version(version));
}
}

Expand All @@ -63,9 +67,9 @@ pub async fn install_all() {
for (id, version) in config.tools {
futures.push(internal_install(InstallArgs {
id,
semver: Some(version),
pin: false,
passthrough: vec![],
spec: Some(version),
}));
}

Expand Down
6 changes: 4 additions & 2 deletions crates/cli/src/commands/list_remote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Args;
use proto_core::{load_tool, Id, VersionType};
use proto_core::{load_tool, Id, UnresolvedVersionSpec};
use starbase::system;
use std::process;
use tracing::debug;
Expand All @@ -16,7 +16,9 @@ pub async fn list_remote(args: ArgsRef<ListRemoteArgs>) {

debug!("Loading versions");

let resolver = tool.load_version_resolver(&VersionType::default()).await?;
let resolver = tool
.load_version_resolver(&UnresolvedVersionSpec::default())
.await?;
let mut versions = resolver.versions;

if versions.is_empty() {
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/src/commands/local.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Args;
use proto_core::{load_tool, Id, ToolsConfig, VersionType};
use proto_core::{load_tool, Id, ToolsConfig, UnresolvedVersionSpec};
use starbase::system;
use starbase_styles::color;
use std::{env, path::PathBuf};
Expand All @@ -11,7 +11,7 @@ pub struct LocalArgs {
id: Id,

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

#[system]
Expand All @@ -20,18 +20,18 @@ pub async fn local(args: ArgsRef<LocalArgs>) {
let local_path = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

let mut config = ToolsConfig::load_from(local_path)?;
config.tools.insert(args.id.clone(), args.semver.clone());
config.tools.insert(args.id.clone(), args.spec.clone());
config.save()?;

debug!(
version = args.semver.to_string(),
version = args.spec.to_string(),
config = ?config.path,
"Wrote the local version",
);

info!(
"Set the local {} version to {}",
tool.get_name(),
color::hash(args.semver.to_string())
color::hash(args.spec.to_string())
);
}
8 changes: 4 additions & 4 deletions crates/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::commands::install::{internal_install, InstallArgs};
use clap::Args;
use miette::IntoDiagnostic;
use proto_core::{detect_version, load_tool, Id, ProtoError, UserConfig, VersionType};
use proto_core::{detect_version, load_tool, Id, ProtoError, UnresolvedVersionSpec, UserConfig};
use proto_pdk_api::RunHook;
use starbase::system;
use starbase_styles::color;
Expand All @@ -16,7 +16,7 @@ pub struct RunArgs {
id: Id,

#[arg(help = "Version or alias of tool")]
semver: Option<VersionType>,
spec: Option<UnresolvedVersionSpec>,

#[arg(long, help = "Path to an alternate binary to run")]
bin: Option<String>,
Expand All @@ -32,7 +32,7 @@ pub struct RunArgs {
#[system]
pub async fn run(args: ArgsRef<RunArgs>) -> SystemResult {
let mut tool = load_tool(&args.id).await?;
let version = detect_version(&tool, args.semver.clone()).await?;
let version = detect_version(&tool, args.spec.clone()).await?;
let user_config = UserConfig::load()?;

// Check if installed or install
Expand All @@ -51,7 +51,7 @@ pub async fn run(args: ArgsRef<RunArgs>) -> SystemResult {

internal_install(InstallArgs {
id: args.id.clone(),
semver: Some(tool.get_resolved_version().to_implicit_type()),
spec: Some(tool.get_resolved_version().to_implicit_type()),
pin: false,
passthrough: vec![],
})
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/commands/uninstall.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::helpers::{create_progress_bar, disable_progress_bars};
use clap::Args;
use proto_core::{load_tool, Id, VersionType};
use proto_core::{load_tool, Id, UnresolvedVersionSpec};
use starbase::system;
use tracing::{debug, info};

Expand All @@ -10,7 +10,7 @@ pub struct UninstallArgs {
id: Id,

#[arg(required = true, help = "Version or alias of tool")]
semver: VersionType,
semver: UnresolvedVersionSpec,
}

#[system]
Expand Down
19 changes: 13 additions & 6 deletions crates/cli/tests/alias_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod utils;

use proto_core::{ToolManifest, VersionType};
use proto_core::{ToolManifest, UnresolvedVersionSpec};
use starbase_sandbox::predicates::prelude::*;
use std::collections::BTreeMap;
use utils::*;
Expand Down Expand Up @@ -44,7 +44,10 @@ mod alias {

assert_eq!(
manifest.aliases,
BTreeMap::from_iter([("example".into(), VersionType::parse("19.0.0").unwrap())])
BTreeMap::from_iter([(
"example".into(),
UnresolvedVersionSpec::parse("19.0.0").unwrap()
)])
);
}

Expand All @@ -54,9 +57,10 @@ mod alias {
let manifest_file = sandbox.path().join("tools/node/manifest.json");

let mut manifest = ToolManifest::load(&manifest_file).unwrap();
manifest
.aliases
.insert("example".into(), VersionType::parse("19.0.0").unwrap());
manifest.aliases.insert(
"example".into(),
UnresolvedVersionSpec::parse("19.0.0").unwrap(),
);
manifest.save().unwrap();

let mut cmd = create_proto_command(sandbox.path());
Expand All @@ -71,7 +75,10 @@ mod alias {

assert_eq!(
manifest.aliases,
BTreeMap::from_iter([("example".into(), VersionType::parse("20.0.0").unwrap())])
BTreeMap::from_iter([(
"example".into(),
UnresolvedVersionSpec::parse("20.0.0").unwrap()
)])
);
}

Expand Down
26 changes: 23 additions & 3 deletions crates/cli/tests/global_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod utils;

use proto_core::{ToolManifest, VersionType};
use proto_core::{ToolManifest, UnresolvedVersionSpec};
use utils::*;

mod global {
Expand All @@ -26,7 +26,7 @@ mod global {

assert_eq!(
manifest.default_version,
Some(VersionType::parse("19.0.0").unwrap())
Some(UnresolvedVersionSpec::parse("19.0.0").unwrap())
);
}

Expand All @@ -50,7 +50,27 @@ mod global {

assert_eq!(
manifest.default_version,
Some(VersionType::Alias("bundled".into()))
Some(UnresolvedVersionSpec::Alias("bundled".into()))
);
}

#[test]
fn can_set_partial_version_as_default() {
let temp = create_empty_sandbox();
let manifest_file = temp.path().join("tools/npm/manifest.json");

assert!(!manifest_file.exists());

let mut cmd = create_proto_command(temp.path());
cmd.arg("global").arg("npm").arg("1.2").assert().success();

assert!(manifest_file.exists());

let manifest = ToolManifest::load(manifest_file).unwrap();

assert_eq!(
manifest.default_version,
Some(UnresolvedVersionSpec::parse("1.2").unwrap())
);
}
}
Loading

0 comments on commit a37fca6

Please sign in to comment.