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 committed Sep 1, 2024
1 parent f9e4e94 commit 2d69b41
Show file tree
Hide file tree
Showing 16 changed files with 1,549 additions and 1,438 deletions.
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
18 changes: 9 additions & 9 deletions crates/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ async fn get_executable(tool: &Tool, args: &RunArgs) -> miette::Result<Executabl

// Run an alternate executable (via shim)
if let Some(alt_name) = &args.alt {
for location in tool.get_shim_locations().await? {
if location.name == *alt_name {
for location in tool.resolve_shim_locations().await? {
if &location.name == alt_name {
let Some(exe_path) = &location.config.exe_path else {
continue;
};
Expand Down Expand Up @@ -88,11 +88,13 @@ async fn get_executable(tool: &Tool, args: &RunArgs) -> miette::Result<Executabl

// Otherwise use the primary
let mut config = tool
.get_exe_location()
.resolve_primary_exe_location()
.await?
.expect("Required executable information missing!")
.config;

// We don't use `locate_exe_file` here because we need to handle
// tools whose primary file is not executable, like JavaScript!
config.exe_path = Some(tool_dir.join(config.exe_path.as_ref().unwrap()));

Ok(config)
Expand Down Expand Up @@ -212,18 +214,16 @@ pub async fn run(session: ProtoSession, args: RunArgs) -> AppResult {

// Run before hook
let hook_result = if tool.plugin.has_func("pre_run").await {
tool.locate_globals_dirs().await?;

let globals_dir = tool.get_globals_dir();
let globals_prefix = tool.get_globals_prefix();
let globals_dir = tool.locate_globals_dir().await?;
let globals_prefix = tool.locate_globals_prefix().await?;

tool.plugin
.call_func_with(
"pre_run",
RunHook {
context: tool.create_context(),
globals_dir: globals_dir.map(|dir| tool.to_virtual_path(dir)),
globals_prefix: globals_prefix.map(|p| p.to_owned()),
globals_dir: globals_dir.map(|dir| tool.to_virtual_path(&dir)),
globals_prefix,
passthrough_args: args.passthrough.clone(),
},
)
Expand Down
10 changes: 8 additions & 2 deletions crates/cli/tests/plugins_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ where
let base_dir = proto.store.inventory_dir.join("moon/1.0.0");

if cfg!(windows) {
assert_eq!(tool.get_exe_path().unwrap(), &base_dir.join("moon.exe"));
assert_eq!(
&tool.locate_exe_file().await.unwrap(),
&base_dir.join("moon.exe")
);
assert!(proto.store.shims_dir.join("moon.exe").exists());
} else {
assert_eq!(tool.get_exe_path().unwrap(), &base_dir.join("moon"));
assert_eq!(
&tool.locate_exe_file().await.unwrap(),
&base_dir.join("moon")
);
assert!(proto.store.shims_dir.join("moon").exists());
}
}
Expand Down
Loading

0 comments on commit 2d69b41

Please sign in to comment.