Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: Move version spec into its own crate. #214

Merged
merged 7 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
- [Rust](https://github.com/moonrepo/rust-plugin/blob/master/CHANGELOG.md)
- [Schema](https://github.com/moonrepo/schema-plugin/blob/master/CHANGELOG.md)

## Unreleased

#### 🐞 Fixes

- Another attempt at fixing WASM memory issues.
- Fixed an issue where binaries sometimes could not be located for "installed" tools.

## 0.18.2

#### 🐞 Fixes
Expand Down
28 changes: 19 additions & 9 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ miette = "5.10.0"
once_cell = "1.18.0"
once_map = "0.4.8"
regex = "1.9.5"
reqwest = { version = "0.11.20", default-features = false, features = [
"rustls-tls-native-roots",
] }
reqwest = { version = "0.11.20", default-features = false }
semver = "1.0.18"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ futures = "0.3.28"
human-sort = { workspace = true }
indicatif = "0.17.6"
miette = { workspace = true }
reqwest = { workspace = true, features = ["stream"] }
reqwest = { workspace = true, features = ["rustls-tls-native-roots", "stream"] }
semver = { workspace = true }
serde = { workspace = true }
starbase = "0.2.6"
Expand Down
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
8 changes: 1 addition & 7 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.1.0", path = "../version-spec" }
warpgate = { version = "0.5.7", path = "../warpgate" }
cached = { workspace = true }
extism = { workspace = true }
Expand All @@ -18,9 +19,6 @@ miette = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
reqwest = { workspace = true }
schematic = { version = "0.11.6", default-features = false, features = [
"json",
], optional = true }
semver = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand All @@ -36,7 +34,3 @@ url = "2.4.1"

[dev-dependencies]
starbase_sandbox = { workspace = true }

[features]
default = []
schematic = ["dep:schematic"]
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
35 changes: 0 additions & 35 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,40 +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_v_prefix<T: AsRef<str>>(value: T) -> String {
let value = value.as_ref();

if value.starts_with('v') || value.starts_with('V') {
return value[1..].to_owned();
}

value.to_owned()
}

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::*;
51 changes: 42 additions & 9 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 @@ -377,7 +384,13 @@ impl Tool {

let mut versions = LoadVersionsOutput::default();
let mut cached = false;
let cache_path = self.get_inventory_dir().join("remote-versions.json");

// Don't use the overridden inventory path
let cache_path = self
.proto
.tools_dir
.join(self.id.as_str())
.join("remote-versions.json");

// Attempt to read from the cache first
if cache_path.exists() && (is_cache_enabled() || is_offline()) {
Expand Down Expand Up @@ -448,7 +461,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 +503,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 +519,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 +604,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 @@ -1164,7 +1188,9 @@ impl Tool {
self.version
.as_ref()
// Canary can be overwritten so treat as not-installed
.is_some_and(|v| !v.is_latest() && !v.is_canary())
.is_some_and(|v| {
!v.is_latest() && !v.is_canary() && self.manifest.installed_versions.contains(v)
})
&& dir.exists()
&& !fs::is_dir_locked(dir)
}
Expand Down Expand Up @@ -1218,7 +1244,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
Loading
Loading