Skip to content

Commit

Permalink
Clean up usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 21, 2023
1 parent 683bc73 commit 3df2a92
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 157 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 5 additions & 2 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, Id, Tool, ToolsConfig, VersionSpec,
get_plugins_dir, get_shim_file_name, load_tool, Id, ProtoError, Tool, ToolsConfig, VersionSpec,
};
use proto_pdk_api::{CreateShimsInput, CreateShimsOutput};
use starbase::diagnostics::IntoDiagnostic;
Expand Down Expand Up @@ -70,7 +70,10 @@ pub async fn clean_tool(mut tool: Tool, now: u128, days: u8, yes: bool) -> miett
continue;
}

let version = VersionSpec::parse(&dir_name)?;
let version = VersionSpec::parse(&dir_name).map_err(|error| ProtoError::Semver {
version: dir_name,
error,
})?;

if !tool.manifest.versions.contains_key(&version) {
debug!(
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/moonrepo/proto"
[dependencies]
proto_pdk_api = { version = "0.7.2", path = "../pdk-api" }
proto_wasm_plugin = { version = "0.6.6", path = "../wasm-plugin" }
version_spec = { version = "0.0.1", path = "../version-spec" }
warpgate = { version = "0.5.7", path = "../warpgate" }
cached = { workspace = true }
extism = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::version::*;
use starbase_events::Event;
use version_spec::*;

macro_rules! impl_event {
($name:ident, $impl:tt) => {
Expand Down
25 changes: 0 additions & 25 deletions crates/core/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::path::Path;
use std::{env, path::PathBuf};
use tracing::trace;

pub static CLEAN_VERSION: Lazy<Regex> = Lazy::new(|| Regex::new(r"([><]=?)\s+(\d)").unwrap());
pub static ENV_VAR: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$([A-Z0-9_]+)").unwrap());

#[deprecated = "Use `get_proto_home` instead."]
Expand Down Expand Up @@ -54,30 +53,6 @@ pub fn get_plugins_dir() -> miette::Result<PathBuf> {
Ok(get_proto_home()?.join("plugins"))
}

// Aliases are words that map to version. For example, "latest" -> "1.2.3".
pub fn is_alias_name<T: AsRef<str>>(value: T) -> bool {
let value = value.as_ref();

value.chars().enumerate().all(|(i, c)| {
if i == 0 {
char::is_ascii_alphabetic(&c)
} else {
char::is_ascii_alphanumeric(&c)
|| c == '-'
|| c == '_'
|| c == '/'
|| c == '.'
|| c == '*'
}
})
}

pub fn remove_space_after_gtlt<T: AsRef<str>>(value: T) -> String {
CLEAN_VERSION
.replace_all(value.as_ref(), "$1$2")
.to_string()
}

#[cached(time = 300)]
#[tracing::instrument]
pub fn is_offline() -> bool {
Expand Down
3 changes: 1 addition & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod tool_loader;
mod tool_manifest;
mod tools_config;
mod user_config;
mod version;
mod version_detector;
mod version_resolver;

Expand All @@ -24,7 +23,7 @@ pub use tool_loader::*;
pub use tool_manifest::*;
pub use tools_config::*;
pub use user_config::*;
pub use version::*;
pub use version_detector::*;
pub use version_resolver::*;
pub use version_spec::*;
pub use warpgate::*;
39 changes: 32 additions & 7 deletions crates/core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::shimmer::{
create_global_shim, create_local_shim, get_shim_file_name, ShimContext, SHIM_VERSION,
};
use crate::tool_manifest::ToolManifest;
use crate::version::{UnresolvedVersionSpec, VersionSpec};
use crate::version_resolver::VersionResolver;
use extism::{manifest::Wasm, Manifest as PluginManifest};
use miette::IntoDiagnostic;
Expand All @@ -27,6 +26,7 @@ use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
use tracing::{debug, trace};
use version_spec::*;
use warpgate::{download_from_url_to_file, Id, PluginContainer, VirtualPath};

pub struct Tool {
Expand Down Expand Up @@ -330,7 +330,14 @@ impl Tool {

if let Some(default) = sync_changes.default_version {
modified = true;
self.manifest.default_version = Some(UnresolvedVersionSpec::parse(&default)?);

self.manifest.default_version =
Some(UnresolvedVersionSpec::parse(&default).map_err(|error| {
ProtoError::Semver {
version: default,
error,
}
})?);
}

if let Some(versions) = sync_changes.versions {
Expand Down Expand Up @@ -448,7 +455,7 @@ impl Tool {
if is_offline() && matches!(initial_version, UnresolvedVersionSpec::Version(_))
|| matches!(initial_version, UnresolvedVersionSpec::Canary)
{
let version = initial_version.to_spec();
let version = initial_version.to_resolved_spec();

debug!(
tool = self.id.as_str(),
Expand Down Expand Up @@ -490,7 +497,12 @@ impl Tool {
);

resolved = true;
version = resolver.resolve(&UnresolvedVersionSpec::parse(candidate)?)?;
version = resolver.resolve(&UnresolvedVersionSpec::parse(&candidate).map_err(
|error| ProtoError::Semver {
version: candidate,
error,
},
)?)?;
}

if let Some(candidate) = result.version {
Expand All @@ -501,7 +513,10 @@ impl Tool {
);

resolved = true;
version = VersionSpec::parse(candidate)?;
version = VersionSpec::parse(&candidate).map_err(|error| ProtoError::Semver {
version: candidate,
error,
})?;
}
}

Expand Down Expand Up @@ -583,7 +598,10 @@ impl Tool {
"Detected a version"
);

return Ok(Some(UnresolvedVersionSpec::parse(version)?));
return Ok(Some(
UnresolvedVersionSpec::parse(&version)
.map_err(|error| ProtoError::Semver { version, error })?,
));
}

Ok(None)
Expand Down Expand Up @@ -1218,7 +1236,14 @@ impl Tool {
let mut default = None;

if let Some(default_version) = &self.metadata.default_version {
default = Some(UnresolvedVersionSpec::parse(default_version)?);
default = Some(
UnresolvedVersionSpec::parse(default_version).map_err(|error| {
ProtoError::Semver {
version: default_version.to_owned(),
error,
}
})?,
);
}

self.manifest
Expand Down
6 changes: 2 additions & 4 deletions crates/core/src/tool_manifest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
helpers::{read_json_file_with_lock, write_json_file_with_lock},
version::{UnresolvedVersionSpec, VersionSpec},
};
use crate::helpers::{read_json_file_with_lock, write_json_file_with_lock};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashSet},
Expand All @@ -10,6 +7,7 @@ use std::{
time::SystemTime,
};
use tracing::{debug, info};
use version_spec::*;

fn now() -> u128 {
SystemTime::now()
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/tools_config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::version::UnresolvedVersionSpec;
use miette::IntoDiagnostic;
use serde::{Deserialize, Serialize};
use starbase_utils::{fs, toml};
use std::collections::BTreeMap;
use std::env;
use std::path::{Path, PathBuf};
use tracing::{debug, trace};
use version_spec::*;
use warpgate::{Id, PluginLocator};

pub const TOOLS_CONFIG_NAME: &str = ".prototools";
Expand Down
112 changes: 0 additions & 112 deletions crates/core/src/version.rs

This file was deleted.

11 changes: 9 additions & 2 deletions crates/core/src/version_detector.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::error::ProtoError;
use crate::tool::Tool;
use crate::tools_config::ToolsConfig;
use crate::version::UnresolvedVersionSpec;
use std::{env, path::Path};
use tracing::{debug, trace};
use version_spec::*;

pub async fn detect_version(
tool: &Tool,
Expand All @@ -23,7 +23,14 @@ pub async fn detect_version(
"Detected version from environment variable",
);

candidate = Some(UnresolvedVersionSpec::parse(session_version)?);
candidate = Some(
UnresolvedVersionSpec::parse(&session_version).map_err(|error| {
ProtoError::Semver {
version: session_version,
error,
}
})?,
);
} else {
trace!(
tool = tool.id.as_str(),
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/version_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::error::ProtoError;
use crate::tool_manifest::ToolManifest;
use crate::version::UnresolvedVersionSpec;
use crate::VersionSpec;
use proto_pdk_api::LoadVersionsOutput;
use semver::{Version, VersionReq};
use std::collections::{BTreeMap, HashSet};
use version_spec::*;

#[derive(Debug, Default)]
pub struct VersionResolver<'tool> {
Expand Down

0 comments on commit 3df2a92

Please sign in to comment.