Skip to content

Commit

Permalink
Update resolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 28, 2023
1 parent 54fa5cf commit f2d13b9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 39 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
#### 🚀 Updates

- Added a `proto migrate v0.24` command for migrating aliases/versions. We'll also log a warning if we detect the old configuration.
- Updated non-latest plugins to be cached for 30 days, instead of forever.

#### 🐞 Fixes

- Fixed an issue where resolving canary versions wouldn't work correctly.

#### ⚙️ Internal

- We now load and cache the user config, instead of continually loading it.

## 0.23.2

Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/commands/migrate/v0_24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub async fn migrate() -> SystemResult {
updated_manifest = true;
}

if !tool.manifest.default_version.is_some() {
debug!("Found a default version");
if tool.manifest.default_version.is_some() {
debug!("Found default version");

let entry = user_config.tools.entry(tool.id.clone()).or_default();
entry.default_version = manifest.default_version.take();
Expand Down
4 changes: 1 addition & 3 deletions crates/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ pub async fn run(args: ArgsRef<RunArgs>) -> SystemResult {

// Check if installed or install
if !tool.is_setup(&version).await? {
let user_config = tool.proto.load_user_config()?;

if !user_config.auto_install {
if !tool.proto.load_user_config()?.auto_install {
return Err(ProtoError::MissingToolForRun {
tool: tool.get_name().to_owned(),
version: version.to_string(),
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/commands/tool/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ pub async fn list(args: ArgsRef<ListToolsArgs>) {
.map(|t| {
let user_config = user_config.tools.remove(&t.id);

return (
(
t.id,
ToolItem {
manifest: t.manifest,
user_config,
},
);
)
})
.collect::<HashMap<_, _>>();

Expand Down
47 changes: 29 additions & 18 deletions crates/core/tests/version_resolver_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use proto_core::{resolve_version, ToolManifest, UnresolvedVersionSpec, VersionSpec};
use proto_core::{
resolve_version, ToolManifest, UnresolvedVersionSpec, UserToolConfig, VersionSpec,
};
use semver::Version;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -43,15 +45,6 @@ mod version_resolver {
fn create_manifest() -> ToolManifest {
let mut manifest = ToolManifest::default();

manifest.aliases.insert(
"latest-manifest".into(),
UnresolvedVersionSpec::Version(Version::new(8, 0, 0)),
);
manifest.aliases.insert(
"stable-manifest".into(),
UnresolvedVersionSpec::Alias("stable".into()),
);

manifest
.installed_versions
.insert(VersionSpec::parse("3.0.0").unwrap());
Expand All @@ -62,6 +55,21 @@ mod version_resolver {
manifest
}

fn create_tool_config() -> UserToolConfig {
let mut config = UserToolConfig::default();

config.aliases.insert(
"latest-manifest".into(),
UnresolvedVersionSpec::Version(Version::new(8, 0, 0)),
);
config.aliases.insert(
"stable-manifest".into(),
UnresolvedVersionSpec::Alias("stable".into()),
);

config
}

#[test]
fn resolves_aliases() {
let versions = create_versions();
Expand Down Expand Up @@ -93,18 +101,19 @@ mod version_resolver {
}

#[test]
fn resolves_aliases_from_manifest() {
fn resolves_aliases_from_config() {
let versions = create_versions();
let aliases = create_aliases();
let manifest = create_manifest();
let config = create_tool_config();

assert_eq!(
resolve_version(
&UnresolvedVersionSpec::Alias("latest-manifest".into()),
&versions,
&aliases,
Some(&manifest),
None,
Some(&config),
)
.unwrap(),
Version::new(8, 0, 0)
Expand All @@ -116,7 +125,7 @@ mod version_resolver {
&versions,
&aliases,
Some(&manifest),
None,
Some(&config),
)
.unwrap(),
Version::new(10, 0, 0)
Expand Down Expand Up @@ -202,18 +211,19 @@ mod version_resolver {
}

#[test]
fn resolves_versions_from_manifest() {
fn resolves_versions_from_config() {
let versions = create_versions();
let aliases = create_aliases();
let manifest = create_manifest();
let config = create_tool_config();

assert_eq!(
resolve_version(
&UnresolvedVersionSpec::Version(Version::new(3, 0, 0)),
&versions,
&aliases,
Some(&manifest),
None,
Some(&config),
)
.unwrap(),
Version::new(3, 0, 0)
Expand Down Expand Up @@ -263,18 +273,19 @@ mod version_resolver {
}

#[test]
fn resolves_partial_versions_with_manifest() {
fn resolves_partial_versions_with_config() {
let versions = create_versions();
let aliases = create_aliases();
let manifest = create_manifest();
let config = create_tool_config();

assert_eq!(
resolve_version(
&UnresolvedVersionSpec::parse("3.3").unwrap(),
&versions,
&aliases,
Some(&manifest),
None,
Some(&config),
)
.unwrap(),
Version::new(3, 3, 3)
Expand All @@ -286,7 +297,7 @@ mod version_resolver {
&versions,
&aliases,
Some(&manifest),
None,
Some(&config),
)
.unwrap(),
Version::new(3, 3, 3)
Expand Down
29 changes: 15 additions & 14 deletions crates/warpgate/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,26 @@ impl PluginLoader {
return Ok(false);
}

let mut cached = true;
let metadata = fs::metadata(path)?;

// If latest, cache only lasts for 7 days
if fs::file_name(path).contains("-latest-") {
let metadata = fs::metadata(path)?;

cached = if let Ok(filetime) = metadata.created().or_else(|_| metadata.modified()) {
filetime > SystemTime::now() - Duration::from_secs(86400 * 7)
let mut cached = if let Ok(filetime) = metadata.created().or_else(|_| metadata.modified()) {
let days = if fs::file_name(path).contains("-latest-") {
7
} else {
false
30
};

if !cached && self.is_offline() {
cached = true;
}
filetime > SystemTime::now() - Duration::from_secs(86400 * days)
} else {
false
};

if !cached {
fs::remove_file(path)?;
}
if !cached && self.is_offline() {
cached = true;
}

if !cached {
fs::remove_file(path)?;
}

if cached {
Expand Down

0 comments on commit f2d13b9

Please sign in to comment.