Skip to content

Commit

Permalink
new: Add crate for installing system dependencies. (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Sep 29, 2023
1 parent ad82e7b commit 3d2a28b
Show file tree
Hide file tree
Showing 28 changed files with 1,572 additions and 210 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
os: [ubuntu-latest, windows-latest]
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: moonrepo/setup-rust@v1
with:
components: rustfmt
Expand All @@ -30,7 +30,7 @@ jobs:
os: [ubuntu-latest, windows-latest]
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: moonrepo/setup-rust@v1
with:
components: clippy
Expand All @@ -44,7 +44,7 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: moonrepo/setup-proto@v1
- uses: moonrepo/setup-rust@v1
with:
Expand All @@ -71,7 +71,7 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: moonrepo/setup-rust@v1
with:
bins: cargo-wasi
Expand Down
35 changes: 23 additions & 12 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ default-members = ["crates/cli"]

[workspace.dependencies]
cached = "0.46.0"
clap = "4.4.5"
clap_complete = "4.4.2"
clap = "4.4.6"
clap_complete = "4.4.3"
convert_case = "0.6.0"
extism = "0.5.2"
extism-pdk = "0.3.4"
Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ path = "src/main.rs"
proto_core = { version = "0.18.6", path = "../core" }
proto_pdk_api = { version = "0.7.2", path = "../pdk-api" }
proto_wasm_plugin = { version = "0.6.7", path = "../wasm-plugin" }
system_env = { version = "0.0.1", path = "../system-env" }
chrono = "0.4.31"
clap = { workspace = true, features = ["derive", "env"] }
clap_complete = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub async fn internal_install(args: InstallArgs) -> SystemResult {
tool.get_resolved_version()
));

let installed = tool.setup(&version).await?;
let installed = tool.setup(&version, false).await?;

pb.finish_and_clear();

Expand Down
8 changes: 3 additions & 5 deletions crates/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::commands::install::{internal_install, InstallArgs};
use clap::Args;
use miette::IntoDiagnostic;
use proto_core::{
detect_version, is_command_on_path, load_tool, Id, ProtoError, UnresolvedVersionSpec,
UserConfig,
};
use proto_core::{detect_version, load_tool, Id, ProtoError, UnresolvedVersionSpec, UserConfig};
use proto_pdk_api::RunHook;
use starbase::system;
use starbase_styles::color;
use std::env;
use std::process::exit;
use system_env::is_command_on_path;
use tokio::process::Command;
use tracing::debug;

Expand Down Expand Up @@ -103,7 +101,7 @@ pub async fn run(args: ArgsRef<RunArgs>) -> SystemResult {
// Run the command
let mut command = match bin_path.extension().map(|e| e.to_str().unwrap()) {
Some("ps1") => {
let mut cmd = Command::new(if is_command_on_path("pwsh".into()) {
let mut cmd = Command::new(if is_command_on_path("pwsh") {
"pwsh"
} else {
"powershell"
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/plugins_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where

env::set_var("PROTO_HOME", fixture.path().to_string_lossy().to_string());

tool.setup(&UnresolvedVersionSpec::parse("1.0.0").unwrap())
tool.setup(&UnresolvedVersionSpec::parse("1.0.0").unwrap(), false)
.await
.unwrap();

Expand Down
8 changes: 8 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub enum ProtoError {
#[error("Failed to install {tool}. {error}")]
InstallFailed { tool: String, error: String },

#[diagnostic(code(proto::tool::build_failed))]
#[error("Failed to build tool from {}: {status}", .url.style(Style::Url))]
BuildFailed { url: String, status: String },

#[diagnostic(code(proto::misc::offline))]
#[error("Internet connection required, unable to download and install tools.")]
InternetConnectionRequired,
Expand Down Expand Up @@ -60,6 +64,10 @@ pub enum ProtoError {
)]
UnknownTool { id: Id },

#[diagnostic(code(proto::build::unsupported))]
#[error("Build from source is not supported for {}.", .tool.style(Style::Id))]
UnsupportedBuildFromSource { tool: Id },

#[diagnostic(code(proto::unsupported::shell))]
#[error("Unable to detect shell.")]
UnsupportedShell,
Expand Down
47 changes: 8 additions & 39 deletions crates/core/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::ProtoError;
use cached::proc_macro::cached;
use miette::IntoDiagnostic;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -92,45 +93,6 @@ pub fn is_cache_enabled() -> bool {
})
}

#[cached]
#[cfg(windows)]
pub fn is_command_on_path(name: String) -> bool {
let Ok(system_path) = env::var("PATH") else {
return false;
};
let Ok(path_ext) = env::var("PATHEXT") else {
return false;
};
let exts = path_ext.split(';').collect::<Vec<_>>();

for path_dir in env::split_paths(&system_path) {
for ext in &exts {
if path_dir.join(format!("{name}{ext}")).exists() {
return true;
}
}
}

false
}

#[cached]
#[cfg(not(windows))]
pub fn is_command_on_path(name: String) -> bool {
let Ok(system_path) = env::var("PATH") else {
return false;
};

for path_dir in env::split_paths(&system_path) {
#[allow(clippy::needless_borrow)]
if path_dir.join(&name).exists() {
return true;
}
}

false
}

pub fn is_archive_file<P: AsRef<Path>>(path: P) -> bool {
is_supported_archive_extension(path.as_ref())
}
Expand All @@ -155,6 +117,13 @@ pub fn hash_file_contents<P: AsRef<Path>>(path: P) -> miette::Result<String> {
Ok(hash)
}

pub fn extract_filename_from_url<U: AsRef<str>>(url: U) -> miette::Result<String> {
let url = url::Url::parse(url.as_ref()).into_diagnostic()?;
let segments = url.path_segments().unwrap();

Ok(segments.last().unwrap().to_owned())
}

pub fn read_json_file_with_lock<T: DeserializeOwned>(path: impl AsRef<Path>) -> miette::Result<T> {
let path = path.as_ref();
let mut content = fs::read_file_with_lock(path)?;
Expand Down
Loading

0 comments on commit 3d2a28b

Please sign in to comment.