Skip to content

Commit

Permalink
forc-gm starter plugin (FuelLabs#1373)
Browse files Browse the repository at this point in the history
* Add mimimal forc-gm example

* Update docs with forc-gm example

* Remove comment in Cargo.toml

* Bump version down, use major clap version, add publish = false

* Reorder plugins page in SUMMARY.md

* Update introduction text in plugins.md

* Revert version to 0.10.3 and remove publish = false

* Alphabetize forc-gm within Cargo.toml

* Bump forc-gm to v0.11.3

* Fix versioning, v0.11.0

Co-authored-by: bing <[email protected]>
  • Loading branch information
bing and bing authored Apr 28, 2022
1 parent d8a1e01 commit ac992fd
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
7 changes: 7 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"forc",
"forc-explore",
"forc-fmt",
"forc-gm",
"forc-lsp",
"forc-pkg",
"forc-util",
Expand Down
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- [forc run](./forc/commands/forc_run.md)
- [forc test](./forc/commands/forc_test.md)
- [forc update](./forc/commands/forc_update.md)
- [Plugins](./forc/plugins.md)
- [Testing](./testing/index.md)
- [Testing with Rust](./testing/testing-with-rust.md)
- [Advanced Concepts](./advanced/index.md)
Expand Down
37 changes: 37 additions & 0 deletions docs/src/forc/plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Plugins

Plugins can be used to extend `forc` with new commands that go beyond the native commands mentioned in the previous chapter. While the Fuel ecosystem provides a few commonly useful plugins (`forc-fmt`, `forc-lsp`, `forc-explore`), anyone can write their own!

Let's install a starter plugin, `forc-gm`, and take a look at how it works underneath:

```sh
cargo install forc-gm
```

Check that we have installed `forc-gm`:

```console
$ forc plugins
/Users/<USER>/.cargo/bin/forc-gm
```

Underneath, `forc-gm` is a simple CLI app, with [clap](https://docs.rs/clap/latest/clap/) as the only dependency:

```rust
{{#include ../../../forc-gm/src/main.rs}}
```

You can say gm, or you can greet Fuel:

```console
$ forc gm
gn!
$ forc gm fuel
gn from Fuel!
```

## Writing your own plugin

We encourage anyone to write and publish their own `forc` plugin to enhance their development experience.

Your plugin must be named in the format `forc-<MY_PLUGIN>` and you may use the above template as a starting point. You can use [clap](https://docs.rs/clap/latest/clap/) and add more subcommands, options and configurations to suit your plugin's needs.
12 changes: 12 additions & 0 deletions forc-gm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "forc-gm"
version = "0.11.0"
authors = ["Fuel Labs <[email protected]>"]
edition = "2021"
homepage = "https://fuel.network/"
license = "Apache-2.0"
repository = "https://github.com/FuelLabs/sway"
description = "A sample `forc` plugin."

[dependencies]
clap = { version = "3.1", features = ["derive"] }
39 changes: 39 additions & 0 deletions forc-gm/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! A sample `forc` plugin that greets you!
//!
//! Once installed and available via `PATH`, can be executed via `forc gm`.
use clap::Parser;

#[derive(Debug, Parser)]
#[clap(
name = "forc-gm",
about = "Sample Forc plugin that greets you!",
version
)]
struct App {
#[clap(subcommand)]
pub subcmd: Option<Subcommand>,
}

#[derive(Debug, Parser)]
enum Subcommand {
/// Say 'gm' to Fuel!
Fuel,
}

fn main() {
let app = App::parse();

match app.subcmd {
Some(Subcommand::Fuel) => greet_fuel(),
None => greet(),
}
}

fn greet_fuel() {
println!("gn from Fuel!");
}

fn greet() {
println!("gn!");
}

0 comments on commit ac992fd

Please sign in to comment.