Skip to content

Commit

Permalink
Add support for plugins using stellar prefix. (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Oct 22, 2024
1 parent 3f06e1d commit fb6be31
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ members = [
"cmd/crates/*",
"cmd/crates/soroban-test/tests/fixtures/test-wasms/*",
"cmd/crates/soroban-test/tests/fixtures/hello",
"cmd/crates/soroban-test/tests/fixtures/bye",
]
default-members = ["cmd/soroban-cli", "cmd/crates/soroban-spec-tools", "cmd/crates/soroban-test"]
exclude = ["cmd/crates/soroban-test/tests/fixtures/hello"]
exclude = ["cmd/crates/soroban-test/tests/fixtures/hello", "cmd/crates/soroban-test/tests/fixtures/bye"]

[workspace.package]
version = "21.5.0"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ install_rust: install
install:
cargo install --force --locked --path ./cmd/stellar-cli --debug
cargo install --force --locked --path ./cmd/crates/soroban-test/tests/fixtures/hello --root ./target --debug --quiet
cargo install --force --locked --path ./cmd/crates/soroban-test/tests/fixtures/bye --root ./target --debug --quiet

# regenerate the example lib in `cmd/crates/soroban-spec-typsecript/fixtures/ts`
build-snapshot: typescript-bindings-fixtures
Expand Down
7 changes: 7 additions & 0 deletions cmd/crates/soroban-test/tests/fixtures/bye/Cargo.lock

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

9 changes: 9 additions & 0 deletions cmd/crates/soroban-test/tests/fixtures/bye/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "stellar-bye"
version = "21.5.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions cmd/crates/soroban-test/tests/fixtures/bye/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Bye, world!");
}
34 changes: 24 additions & 10 deletions cmd/crates/soroban-test/tests/it/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This function calls the soroban executable via cargo and checks that the output
This function calls the stellar executable via cargo and checks that the output
is correct. The PATH environment variable is set to include the target/bin
directory, so that the soroban executable can be found.
directory, so that the stellar executable can be found.
*/

use std::{ffi::OsString, path::PathBuf};
Expand All @@ -11,31 +11,45 @@ fn soroban_hello() {
// Add the target/bin directory to the iterator of paths
let paths = get_paths();
// Call soroban with the PATH variable set to include the target/bin directory
assert_cmd::Command::cargo_bin("soroban")
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("soroban"))
.arg("hello")
.env("PATH", &paths)
.assert()
.stdout("Hello, world!\n");
}

#[test]
fn stellar_bye() {
// Add the target/bin directory to the iterator of paths
let paths = get_paths();
// Call soroban with the PATH variable set to include the target/bin directory
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("stellar"))
.arg("bye")
.env("PATH", &paths)
.assert()
.stdout("Bye, world!\n");
}

#[test]
fn list() {
// Call `soroban --list` with the PATH variable set to include the target/bin directory
assert_cmd::Command::cargo_bin("soroban")
.unwrap_or_else(|_| assert_cmd::Command::new("soroban"))
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("stellar"))
.arg("--list")
.env("PATH", get_paths())
.assert()
.stdout(predicates::str::contains("hello"));
.stdout(predicates::str::contains("hello"))
.stdout(predicates::str::contains("bye"));
}

#[test]
#[cfg(not(unix))]
fn has_no_path() {
// Call soroban with the PATH variable set to include just target/bin directory
assert_cmd::Command::cargo_bin("soroban")
.unwrap_or_else(|_| assert_cmd::Command::new("soroban"))
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("stellar"))
.arg("hello")
.env("PATH", target_bin())
.assert()
Expand All @@ -45,8 +59,8 @@ fn has_no_path() {
#[test]
fn has_no_path_failure() {
// Call soroban with the PATH variable set to include just target/bin directory
assert_cmd::Command::cargo_bin("soroban")
.unwrap_or_else(|_| assert_cmd::Command::new("soroban"))
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("stellar"))
.arg("hello")
.assert()
.stderr(predicates::str::contains("error: no such command: `hello`"));
Expand Down
23 changes: 18 additions & 5 deletions cmd/soroban-cli/src/commands/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::process::Command;
use std::{path::PathBuf, process::Command};

use clap::CommandFactory;
use which::which;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn run() -> Result<(), Error> {
return Ok(());
}

let bin = which(format!("soroban-{name}")).map_err(|_| {
let bin = find_bin(&name).map_err(|_| {
let suggestion = if let Ok(bins) = list() {
let suggested_name = bins
.iter()
Expand All @@ -53,6 +53,7 @@ pub fn run() -> Result<(), Error> {
.min_by(|a, b| a.1.total_cmp(&b.1))
.map(|(a, _)| a.to_string())
.unwrap_or_default();

if suggested_name.is_empty() {
suggested_name
} else {
Expand All @@ -64,8 +65,10 @@ pub fn run() -> Result<(), Error> {
} else {
String::new()
};

Error::ExecutableNotFound(name, suggestion)
})?;

std::process::exit(
Command::new(bin)
.args(args)
Expand All @@ -78,19 +81,29 @@ pub fn run() -> Result<(), Error> {

const MAX_HEX_LENGTH: usize = 10;

fn find_bin(name: &str) -> Result<PathBuf, which::Error> {
if let Ok(path) = which(format!("stellar-{name}")) {
Ok(path)
} else {
which(format!("soroban-{name}"))
}
}

pub fn list() -> Result<Vec<String>, Error> {
let re_str = if cfg!(target_os = "windows") {
r"^soroban-.*.exe$"
r"^(soroban|stellar)-.*.exe$"
} else {
r"^soroban-.*"
r"^(soroban|stellar)-.*"
};

let re = regex::Regex::new(re_str)?;

Ok(which::which_re(re)?
.filter_map(|b| {
let s = b.file_name()?.to_str()?;
Some(s.strip_suffix(".exe").unwrap_or(s).to_string())
})
.filter(|s| !(utils::is_hex_string(s) && s.len() > MAX_HEX_LENGTH))
.map(|s| s.replace("soroban-", ""))
.map(|s| s.replace("soroban-", "").replace("stellar-", ""))
.collect())
}

0 comments on commit fb6be31

Please sign in to comment.