Skip to content

Commit

Permalink
Add a forc plugins command for listing all plugins
Browse files Browse the repository at this point in the history
Closes #1198.

Lists the full path to every `forc-*` plugin discovered under the user's
`PATH`.

For example:

```sh
[mindtree@minddesk:~]$ forc plugins
/home/mindtree/.cargo/bin/forc-fmt
/home/mindtree/.cargo/bin/forc-explore
/home/mindtree/.cargo/bin/forc-lsp
```

We could potentially add flags to this command in the future for:

- only emitting executable names (not full paths)
- printing versions and descriptions in a table
- outputting via JSON or TOML for easier machine digestion.

For now, this PR just aims to add initial support.

The logic included in this should make it easier to include available
plugin commands in a future `forc --list` command too (ala #702).
  • Loading branch information
mitchmindtree committed Apr 8, 2022
1 parent 800c33c commit 9645b3e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions forc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ toml_edit = "0.13"
ureq = { version = "2.4", features = ["json"] }
url = "2.2"
uwuify = { version = "^0.2", optional = true }
walkdir = "2"
whoami = "1.1"

[features]
Expand Down
1 change: 1 addition & 0 deletions forc/src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod format;
pub mod init;
pub mod json_abi;
pub mod parse_bytecode;
pub mod plugins;
pub mod run;
pub mod test;
pub mod update;
8 changes: 8 additions & 0 deletions forc/src/cli/commands/plugins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use anyhow::Result;

pub(crate) fn exec() -> Result<()> {
for path in crate::cli::plugin::find_all() {
println!("{}", path.display());
}
Ok(())
}
9 changes: 7 additions & 2 deletions forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use self::commands::{
addr2line, build, clean, completions, deploy, format, init, json_abi, parse_bytecode, run,
test, update,
addr2line, build, clean, completions, deploy, format, init, json_abi, parse_bytecode, plugins,
run, test, update,
};
use addr2line::Command as Addr2LineCommand;
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -45,6 +45,10 @@ enum Forc {
Test(TestCommand),
Update(UpdateCommand),
JsonAbi(JsonAbiCommand),
/// Find all forc plugins available via `PATH`.
///
/// Prints the absolute path to each discovered plugin on a new line.
Plugins,
/// This is a catch-all for unknown subcommands and their arguments.
///
/// When we receive an unknown subcommand, we check for a plugin exe named
Expand All @@ -68,6 +72,7 @@ pub async fn run_cli() -> Result<()> {
Forc::Format(command) => format::exec(command),
Forc::Init(command) => init::exec(command),
Forc::ParseBytecode(command) => parse_bytecode::exec(command),
Forc::Plugins => plugins::exec(),
Forc::Run(command) => run::exec(command).await,
Forc::Test(command) => test::exec(command),
Forc::Update(command) => update::exec(command).await,
Expand Down
20 changes: 20 additions & 0 deletions forc/src/cli/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ fn is_executable(path: &Path) -> bool {
fn is_executable(path: &Path) -> bool {
path.is_file()
}

/// Whether or not the given path points to a valid forc plugin.
fn is_plugin(path: &Path) -> bool {
if let Some(stem) = path.file_name().and_then(|os_str| os_str.to_str()) {
if stem.starts_with("forc-") && is_executable(path) {
return true;
}
}
false
}

/// Find all forc plugins available via `PATH`.
pub(crate) fn find_all() -> impl Iterator<Item = PathBuf> {
search_directories()
.into_iter()
.flat_map(walkdir::WalkDir::new)
.filter_map(Result::ok)
.map(|entry| entry.path().to_path_buf())
.filter(|p| is_plugin(p))
}

0 comments on commit 9645b3e

Please sign in to comment.