Skip to content

Commit

Permalink
Update detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 28, 2023
1 parent 3b38810 commit 54fa5cf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@
- [Rust](https://github.com/moonrepo/rust-plugin/blob/master/CHANGELOG.md)
- [TOML schema](https://github.com/moonrepo/schema-plugin/blob/master/CHANGELOG.md)

## Unreleased

#### 💥 Breaking

> To ease the migration process, we've added a new migrate command. Simply run `proto migrate v0.24` after upgrading proto!
- Reworked user configured aliases and default/global version.
- Moved to `~/.proto/config.toml` (user managed) from `~/.proto/tools/<name>/manifest.json` (internally managed).
- Are now stored in the `tools` table (object), indexed by tool name.
```toml
[tools.node]
default-version = "20.10.0"
aliases = { work = "^18" }
```

#### 🚀 Updates

- Added a `proto migrate v0.24` command for migrating aliases/versions. We'll also log a warning if we detect the old configuration.

## 0.23.2

#### 🐞 Fixes
Expand Down
7 changes: 2 additions & 5 deletions crates/cli/tests/run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ mod run {
fs::remove_file(temp.path().join(".prototools")).unwrap();

// Global version
temp.create_file(
"tools/node/manifest.json",
r#"{ "default_version": "19.0.0", "installed_versions": ["19.0.0"] }"#,
);
temp.create_file("config.toml", "[tools.node]\ndefault-version = \"19.0.0\"");

let mut cmd = create_proto_command(temp.path());
let assert = cmd
Expand All @@ -121,7 +118,7 @@ mod run {

assert.stdout(predicate::str::contains("19.0.0"));

fs::remove_file(temp.path().join("tools/node/manifest.json")).unwrap();
fs::remove_file(temp.path().join("config.toml")).unwrap();
}

// This test fails in Windows for some reason, but works fine with `cargo run`...
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ impl ProtoEnvironment {

pub fn load_user_config(&self) -> miette::Result<&UserConfig> {
self.user_config.get_or_try_init(|| {
if self.test_mode || env::var("PROTO_TEST_USER_CONFIG").is_ok() {
Ok(UserConfig::default())
} else {
UserConfig::load_from(&self.root)
}
// if self.test_mode || env::var("PROTO_TEST_USER_CONFIG").is_ok() {
// Ok(UserConfig::default())
// } else {
UserConfig::load_from(&self.root)
// }
})
}

Expand Down
30 changes: 20 additions & 10 deletions crates/core/src/tool_manifest.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::helpers::{read_json_file_with_lock, write_json_file_with_lock};
use serde::{Deserialize, Serialize};
use starbase_styles::color;
use std::{
collections::{BTreeMap, HashSet},
env,
path::{Path, PathBuf},
time::SystemTime,
};
use tracing::{debug, info};
use tracing::{debug, info, warn};
use version_spec::*;

fn now() -> u128 {
Expand Down Expand Up @@ -73,6 +74,22 @@ impl ToolManifest {

manifest.path = path.to_owned();

#[allow(deprecated)]
if !manifest.aliases.is_empty() {
warn!(
"Found legacy aliases in tool manifest, please run {} to migrate them",
color::shell("proto migrate v0.24")
);
}

#[allow(deprecated)]
if manifest.default_version.is_some() {
warn!(
"Found legacy default version in tool manifest, please run {} to migrate it",
color::shell("proto migrate v0.24")
);
}

Ok(manifest)
}

Expand Down Expand Up @@ -125,14 +142,7 @@ impl ToolManifest {
}

pub fn track_used_at(&mut self, version: VersionSpec) {
self.versions
.entry(version)
.and_modify(|v| {
v.last_used_at = Some(now());
})
.or_insert(ToolManifestVersion {
last_used_at: Some(now()),
..ToolManifestVersion::default()
});
let entry = self.versions.entry(version).or_default();
entry.last_used_at = Some(now());
}
}
24 changes: 14 additions & 10 deletions crates/core/src/version_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub async fn detect_version(
return Ok(candidate);
}

// Env var takes highest priorit
// Env var takes highest priority
let env_var = format!("{}_VERSION", tool.get_env_var_prefix());

if let Ok(session_version) = env::var(&env_var) {
Expand Down Expand Up @@ -170,18 +170,22 @@ pub async fn detect_version(
// If still no version, load the global version
trace!(
tool = tool.id.as_str(),
"Attempting to use global version from manifest",
"Attempting to use global version from user config",
);

if let Some(global_version) = &tool.manifest.default_version {
debug!(
tool = tool.id.as_str(),
version = global_version.to_string(),
file = ?tool.manifest.path,
"Detected global version from manifest",
);
let user_config = tool.proto.load_user_config()?;

if let Some(tool_user_config) = user_config.tools.get(&tool.id) {
if let Some(global_version) = &tool_user_config.default_version {
debug!(
tool = tool.id.as_str(),
version = global_version.to_string(),
file = ?user_config.path,
"Detected global version from user config",
);

return Ok(global_version.to_owned());
return Ok(global_version.to_owned());
}
}

// We didn't find anything!
Expand Down

0 comments on commit 54fa5cf

Please sign in to comment.