Skip to content

Commit

Permalink
internal: Split up code into flows. (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Aug 21, 2024
1 parent 554daec commit 41275f4
Show file tree
Hide file tree
Showing 18 changed files with 1,620 additions and 1,476 deletions.
75 changes: 54 additions & 21 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ default-members = ["crates/cli"]
anyhow = "1.0.86"
async-trait = "0.1.81"
clap = "4.5.16"
clap_complete = "4.5.16"
clap_complete = "4.5.20"
dirs = "5.0.1"
extism = "1.0.0" # Lower for consumers
extism-pdk = "1.2.1"
Expand All @@ -16,14 +16,14 @@ indexmap = "2.4.0"
miette = "7.2.0"
once_cell = "1.19.0"
regex = { version = "1.10.6", default-features = false, features = ["std"] }
reqwest = { version = "0.12.5", default-features = false, features = [
reqwest = { version = "0.12.7", default-features = false, features = [
"charset",
"http2",
"macos-system-configuration",
] }
rustc-hash = "2.0.0"
scc = "2.1.15"
schematic = { version = "0.17.2", default-features = false }
scc = "2.1.16"
schematic = { version = "0.17.3", default-features = false }
semver = { version = "1.0.23", features = ["serde"] }
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
Expand All @@ -50,7 +50,7 @@ starbase_utils = { version = "0.8.7", default-features = false, features = [
"toml",
] }
thiserror = "1.0.63"
tokio = { version = "1.39.2", features = ["full", "tracing"] }
tokio = { version = "1.39.3", features = ["full", "tracing"] }
tracing = "0.1.40"
uuid = { version = "1.10.0", features = ["v4"] }

Expand Down
11 changes: 4 additions & 7 deletions crates/cli/src/commands/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,13 @@ pub async fn activate(session: ProtoSession, args: ActivateArgs) -> AppResult {

// Resolve the version and locate executables
if tool.is_setup(&version).await? {
tool.locate_exes_dir().await?;
tool.locate_globals_dirs().await?;

// Higher priority over globals
if let Some(exe_dir) = tool.get_exes_dir() {
item.add_path(exe_dir);
if let Some(exes_dir) = tool.locate_exes_dir().await? {
item.add_path(&exes_dir);
}

for global_dir in tool.get_globals_dirs() {
item.add_path(global_dir);
for globals_dir in tool.locate_globals_dirs().await? {
item.add_path(&globals_dir);
}
}

Expand Down
11 changes: 7 additions & 4 deletions crates/cli/src/commands/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ pub async fn bin(session: ProtoSession, args: BinArgs) -> AppResult {
let version = detect_version(&tool, args.spec.clone()).await?;

tool.resolve_version(&version, true).await?;
tool.create_executables(args.shim, args.bin).await?;

if args.bin {
for bin in tool.get_bin_locations().await? {
tool.symlink_bins(true).await?;

for bin in tool.resolve_bin_locations().await? {
if bin.primary {
println!("{}", bin.path.display());
return Ok(());
Expand All @@ -48,15 +49,17 @@ pub async fn bin(session: ProtoSession, args: BinArgs) -> AppResult {
}

if args.shim {
for shim in tool.get_shim_locations().await? {
tool.generate_shims(true).await?;

for shim in tool.resolve_shim_locations().await? {
if shim.primary {
println!("{}", shim.path.display());
return Ok(());
}
}
}

println!("{}", tool.get_exe_path()?.display());
println!("{}", tool.locate_exe_file().await?.display());

Ok(())
}
4 changes: 2 additions & 2 deletions crates/cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ pub async fn purge_tool(session: &ProtoSession, id: &Id, yes: bool) -> miette::R
fs::remove_dir_all(inventory_dir)?;

// Delete binaries
for bin in tool.get_bin_locations().await? {
for bin in tool.resolve_bin_locations().await? {
session.env.store.unlink_bin(&bin.path)?;
}

// Delete shims
for shim in tool.get_shim_locations().await? {
for shim in tool.resolve_shim_locations().await? {
session.env.store.remove_shim(&shim.path)?;
}

Expand Down
44 changes: 23 additions & 21 deletions crates/cli/src/commands/plugin/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::printer::{format_env_var, format_value, Printer};
use crate::session::ProtoSession;
use clap::Args;
use proto_core::{
detect_version, EnvVar, ExecutableLocation, Id, PluginLocator, ProtoToolConfig, ToolManifest,
UnresolvedVersionSpec,
detect_version, flow::locate::ExecutableLocation, EnvVar, Id, PluginLocator, ProtoToolConfig,
ToolManifest, UnresolvedVersionSpec,
};
use proto_pdk_api::ToolMetadataOutput;
use serde::Serialize;
Expand All @@ -16,8 +16,8 @@ use std::path::PathBuf;
pub struct PluginInfo {
bins: Vec<ExecutableLocation>,
config: ProtoToolConfig,
exe_file: PathBuf,
exes_dir: Option<PathBuf>,
exe_path: PathBuf,
globals_dirs: Vec<PathBuf>,
globals_prefix: Option<String>,
id: Id,
Expand Down Expand Up @@ -46,23 +46,20 @@ pub async fn info(session: ProtoSession, args: InfoPluginArgs) -> AppResult {
.unwrap_or_else(|_| UnresolvedVersionSpec::parse("*").unwrap());

tool.resolve_version(&version, false).await?;
tool.create_executables(false, false).await?;
tool.locate_exes_dir().await?;
tool.locate_globals_dirs().await?;

let mut config = session.env.load_config()?.to_owned();
let tool_config = config.tools.remove(&tool.id).unwrap_or_default();
let bins = tool.get_bin_locations().await?;
let shims = tool.get_shim_locations().await?;
let bins = tool.resolve_bin_locations().await?;
let shims = tool.resolve_shim_locations().await?;

if args.json {
let info = PluginInfo {
bins,
config: tool_config,
exes_dir: tool.get_exes_dir().map(|dir| dir.to_path_buf()),
exe_path: tool.get_exe_path()?.to_path_buf(),
globals_dirs: tool.get_globals_dirs().to_owned(),
globals_prefix: tool.get_globals_prefix().map(|p| p.to_owned()),
exe_file: tool.locate_exe_file().await?,
exes_dir: tool.locate_exes_dir().await?,
globals_dirs: tool.locate_globals_dirs().await?,
globals_prefix: tool.locate_globals_prefix().await?,
inventory_dir: tool.get_inventory_dir(),
shims,
id: tool.id,
Expand All @@ -77,11 +74,6 @@ pub async fn info(session: ProtoSession, args: InfoPluginArgs) -> AppResult {
return Ok(());
}

let mut version_resolver = tool
.load_version_resolver(&UnresolvedVersionSpec::default())
.await?;
version_resolver.aliases.extend(tool_config.aliases.clone());

let mut printer = Printer::new();
printer.header(&tool.id, &tool.metadata.name);

Expand All @@ -101,22 +93,32 @@ pub async fn info(session: ProtoSession, args: InfoPluginArgs) -> AppResult {

// INVENTORY

let exe_file = tool.locate_exe_file().await?;
let exes_dir = tool.locate_exes_dir().await?;
let globals_dirs = tool.locate_globals_dir().await?;
let globals_prefix = tool.locate_globals_prefix().await?;

let mut version_resolver = tool
.load_version_resolver(&UnresolvedVersionSpec::default())
.await?;
version_resolver.aliases.extend(tool_config.aliases.clone());

printer.named_section("Inventory", |p| {
p.entry("Store", color::path(tool.get_inventory_dir()));

p.entry("Executable", color::path(tool.get_exe_path()?));
p.entry("Executable", color::path(exe_file));

if let Some(dir) = tool.get_exes_dir() {
if let Some(dir) = exes_dir {
p.entry("Executables directory", color::path(dir));
}

if let Some(prefix) = tool.get_globals_prefix() {
if let Some(prefix) = globals_prefix {
p.entry("Global packages prefix", color::property(prefix));
}

p.entry_list(
"Global packages directories",
tool.get_globals_dirs().iter().map(color::path),
globals_dirs.iter().map(color::path),
Some(color::failure("None")),
);

Expand Down
Loading

0 comments on commit 41275f4

Please sign in to comment.