-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
22 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,32 @@ | ||
use clap::{Args, ValueEnum}; | ||
use moon_config::PlatformType; | ||
use moon_node_tool::NodeTool; | ||
use moon_platform::PlatformManager; | ||
use clap::Args; | ||
use miette::IntoDiagnostic; | ||
use moon_terminal::safe_exit; | ||
use moon_tool::Tool; | ||
use moon_tool::{get_proto_paths, prepend_path_env_var}; | ||
use proto_core::ProtoEnvironment; | ||
use starbase::system; | ||
|
||
#[derive(ValueEnum, Clone, Debug)] | ||
#[value(rename_all = "lowercase")] | ||
pub enum BinTool { | ||
// JavaScript | ||
Bun, | ||
Node, | ||
Npm, | ||
Pnpm, | ||
Yarn, | ||
// Rust | ||
Rust, | ||
} | ||
use tokio::process::Command; | ||
|
||
#[derive(Args, Clone, Debug)] | ||
pub struct BinArgs { | ||
#[arg(value_enum, help = "The tool to query")] | ||
tool: BinTool, | ||
} | ||
|
||
enum BinExitCodes { | ||
NotConfigured = 1, | ||
NotInstalled = 2, | ||
} | ||
|
||
fn is_installed(tool: &dyn Tool) { | ||
// TODO | ||
// if let Some(shim_path) = tool.get_shim_path() { | ||
// println!("{}", shim_path.display()); | ||
// } else { | ||
// match tool.get_bin_path() { | ||
// Ok(path) => { | ||
// println!("{}", path.display()); | ||
// } | ||
// Err(_) => { | ||
// safe_exit(BinExitCodes::NotInstalled as i32); | ||
// } | ||
// } | ||
// } | ||
} | ||
|
||
fn not_configured() -> ! { | ||
safe_exit(BinExitCodes::NotConfigured as i32); | ||
#[arg(help = "The tool to query")] | ||
tool: String, | ||
} | ||
|
||
#[system] | ||
pub async fn bin(args: ArgsRef<BinArgs>) { | ||
match &args.tool { | ||
BinTool::Bun => { | ||
let bun = PlatformManager::read().get(PlatformType::Bun)?.get_tool()?; | ||
|
||
is_installed(*bun); | ||
} | ||
BinTool::Node => { | ||
let node = PlatformManager::read() | ||
.get(PlatformType::Node)? | ||
.get_tool()?; | ||
|
||
is_installed(*node); | ||
} | ||
BinTool::Npm | BinTool::Pnpm | BinTool::Yarn => { | ||
let node = PlatformManager::read() | ||
.get(PlatformType::Node)? | ||
.get_tool()? | ||
.as_any(); | ||
let node = node.downcast_ref::<NodeTool>().unwrap(); | ||
|
||
match &args.tool { | ||
BinTool::Npm => match node.get_npm() { | ||
Ok(npm) => is_installed(npm), | ||
Err(_) => not_configured(), | ||
}, | ||
BinTool::Pnpm => match node.get_pnpm() { | ||
Ok(pnpm) => is_installed(pnpm), | ||
Err(_) => not_configured(), | ||
}, | ||
BinTool::Yarn => match node.get_yarn() { | ||
Ok(yarn) => is_installed(yarn), | ||
Err(_) => not_configured(), | ||
}, | ||
_ => {} | ||
}; | ||
} | ||
BinTool::Rust => { | ||
let rust = PlatformManager::read() | ||
.get(PlatformType::Rust)? | ||
.get_tool()?; | ||
|
||
is_installed(*rust); | ||
} | ||
}; | ||
let proto = ProtoEnvironment::new()?; | ||
|
||
let result = Command::new("proto") | ||
.arg("bin") | ||
.arg(&args.tool) | ||
.env("PATH", prepend_path_env_var(get_proto_paths(&proto))) | ||
.spawn() | ||
.into_diagnostic()? | ||
.wait() | ||
.await | ||
.into_diagnostic()?; | ||
|
||
if !result.success() { | ||
safe_exit(result.code().unwrap_or(1)); | ||
} | ||
} |