forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forc-gm
starter plugin (FuelLabs#1373)
* 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
Showing
6 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ members = [ | |
"forc", | ||
"forc-explore", | ||
"forc-fmt", | ||
"forc-gm", | ||
"forc-lsp", | ||
"forc-pkg", | ||
"forc-util", | ||
|
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
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 |
---|---|---|
@@ -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. |
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 |
---|---|---|
@@ -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"] } |
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 |
---|---|---|
@@ -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!"); | ||
} |