Skip to content

Commit

Permalink
document native plugins within the Sway book (FuelLabs#1582)
Browse files Browse the repository at this point in the history
* Refactoring to get ready for plugin documentation integration

* Check for errors when updating doucmentation

* Fix formatting of error text

* Remove unused BorrowMut

* Add plugin docs

* strip whitespace to remove version no. from plugin command header

* Better naming for commands/plugins within preprocessor

* extract inject_content

* handle parsing subcommand

* Refactor

* Install plugins within ci and gh-pages

* order plugins alphabetically

* official_plugin_commands -> plugin_commands, since they mean the same

* Handle error messages better

* subcommand_is_parsed -> has_parsed_subcommand_header for consistency

* More consistent expect message

* Add additional white spaces for test_format_subcommand_line

* cargo install --debug to speed things up

* Update input from &Vec<String> to &[String]

* Move forc-explore, forc-fmt, forc-lsp, into forc-plugins dir

* Update CI ymls with new plugins path

* Update all references to forc plugins in ci

* Programatically get plugins from forc-plugins

* Update ci.yml

* Properly install plugins

* Re-add removed cancel-previous-run

* Remove unused actions-rs/cargo

* Update plugins installation for gh-pages.yml

* Improve plugins.rs

* Improved find_forc_plugins_dir

* Remove return line within format_header_line

* Fix bug where preprocessor would panic if not in Sway root (FuelLabs#1618)

While testing, I noticed that the build would panic if you attempted to
`mdbook build` the book while in the `docs` directory. I then realised
we had a misuse of the `current_dir` function which must have slipped
through in a previous PR review. I've fixed this by adding a
`find_sway_repo_root` fn and implementing functions to find the plugins
and examples directories in terms of it.

Co-authored-by: mitchmindtree <[email protected]>
  • Loading branch information
bing and mitchmindtree authored May 23, 2022
1 parent 381a080 commit 17b9e24
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 133 deletions.
33 changes: 19 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
args: --debug --path ./forc-plugins/forc-fmt
- name: Build Sway examples
uses: actions-rs/cargo@v1
with:
Expand All @@ -101,7 +101,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
args: --debug --path ./forc-plugins/forc-fmt
- name: Check Sway examples formatting
uses: actions-rs/cargo@v1
with:
Expand All @@ -123,6 +123,11 @@ jobs:
with:
command: install
args: --debug --path ./forc
- name: Install Forc plugins
run: |
cargo install --debug --path ./forc-plugins/forc-fmt
cargo install --debug --path ./forc-plugins/forc-lsp
cargo install --debug --path ./forc-plugins/forc-explore
- name: Install mdbook-forc-documenter
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -161,7 +166,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
args: --debug --path ./forc-plugins/forc-fmt
- name: Initialize test project
run: forc init test-proj
- name: Update project forc manifest to use local sway-lib-std
Expand Down Expand Up @@ -195,7 +200,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
args: --debug --path ./forc-plugins/forc-fmt
- name: Build Sway examples
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -227,7 +232,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
args: --debug --path ./forc-plugins/forc-fmt
- name: Build Sway examples
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -315,7 +320,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
args: --debug --path ./forc-plugins/forc-fmt
- name: Build Sway examples
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -348,7 +353,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
args: --debug --path ./forc-plugins/forc-fmt
- name: Build Sway examples
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -380,7 +385,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
args: --debug --path ./forc-plugins/forc-fmt
- name: Build Sway examples
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -460,9 +465,9 @@ jobs:
run: |
cargo install toml-cli
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc-explore/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc-fmt/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc-lsp/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc-plugins/forc-explore/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc-plugins/forc-fmt/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc-plugins/forc-lsp/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc-pkg/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} forc-util/Cargo.toml
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} sway-core/Cargo.toml
Expand Down Expand Up @@ -695,19 +700,19 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: --profile=release --path ./forc-fmt
args: --profile=release --path ./forc-plugins/forc-fmt

- name: Install Forc-LSP
uses: actions-rs/cargo@v1
with:
command: install
args: --profile=release --path ./forc-lsp
args: --profile=release --path ./forc-plugins/forc-lsp

- name: Install Forc-Explore
uses: actions-rs/cargo@v1
with:
command: install
args: --profile=release --path ./forc-explore
args: --profile=release --path ./forc-plugins/forc-explore

- name: Prep Assets
id: prep_assets
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ jobs:
with:
command: install
args: --debug --path ./forc
- name: Install Forc plugins
run: |
cargo install --debug --path ./forc-plugins/forc-fmt
cargo install --debug --path ./forc-plugins/forc-lsp
cargo install --debug --path ./forc-plugins/forc-explore
- name: Install mdbook-forc-documenter
uses: actions-rs/cargo@v1
with:
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ resolver = "2"
members = [
"docstrings",
"forc",
"forc-explore",
"forc-fmt",
"forc-gm",
"forc-lsp",
"forc-pkg",
"forc-plugins/forc-explore",
"forc-plugins/forc-fmt",
"forc-plugins/forc-lsp",
"forc-util",
"scripts/examples-checker",
"scripts/mdbook-forc-documenter",
Expand Down
3 changes: 3 additions & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
- [forc test](./forc/commands/forc_test.md)
- [forc update](./forc/commands/forc_update.md)
- [Plugins](./forc/plugins.md)
- [forc explore](./forc_explore.md)
- [forc fmt](./forc_fmt.md)
- [forc lsp](./forc_lsp.md)
- [Testing](./testing/index.md)
- [Testing with Rust](./testing/testing-with-rust.md)
- [Advanced Concepts](./advanced/index.md)
Expand Down
1 change: 1 addition & 0 deletions docs/src/forc_explore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# forc explore
1 change: 1 addition & 0 deletions docs/src/forc_fmt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# forc fmt
1 change: 1 addition & 0 deletions docs/src/forc_lsp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# forc lsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description = "A `forc` plugin for running the fuel block explorer."
[dependencies]
anyhow = "1"
clap = { version = "3", features = ["derive"] }
forc-util = { version = "0.13.0", path = "../forc-util" }
forc-util = { version = "0.13.0", path = "../../forc-util" }
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }
serde = { version = "1.0", features = ["derive"] }
tar = "0.4"
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions forc-fmt/Cargo.toml → forc-plugins/forc-fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ description = "A `forc` plugin for running the Sway code formatter."
[dependencies]
anyhow = "1"
clap = { version = "3", features = ["derive"] }
forc-util = { version = "0.13.0", path = "../forc-util" }
forc-util = { version = "0.13.0", path = "../../forc-util" }
prettydiff = "0.5"
sway-core = { version = "0.13.0", path = "../sway-core" }
sway-fmt = { version = "0.13.0", path = "../sway-fmt" }
sway-utils = { version = "0.13.0", path = "../sway-utils" }
sway-core = { version = "0.13.0", path = "../../sway-core" }
sway-fmt = { version = "0.13.0", path = "../../sway-fmt" }
sway-utils = { version = "0.13.0", path = "../../sway-utils" }
taplo = "0.7"
tracing = "0.1"
File renamed without changes.
2 changes: 1 addition & 1 deletion forc-lsp/Cargo.toml → forc-plugins/forc-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ description = "A simple `forc` plugin for starting the sway language server."
[dependencies]
anyhow = "1"
clap = { version = "3", features = ["derive"] }
sway-lsp = { version = "0.13.0", path = "../sway-lsp" }
sway-lsp = { version = "0.13.0", path = "../../sway-lsp" }
tokio = { version = "1.8" }
File renamed without changes.
108 changes: 108 additions & 0 deletions scripts/mdbook-forc-documenter/src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use crate::formatter::{format_header_line, format_line};
use anyhow::{anyhow, Result};
use std::collections::HashMap;
use std::ffi::OsString;
use std::process;

pub fn possible_forc_commands() -> Vec<String> {
let mut possible_commands = Vec::new();
let output = process::Command::new("forc")
.arg("--help")
.output()
.expect("Failed running forc --help");

let output_str = String::from_utf8_lossy(&output.stdout);
let lines = output_str.lines();

let mut has_parsed_subcommand_header = false;

for line in lines {
if has_parsed_subcommand_header {
let (command, _) = line.trim().split_once(' ').unwrap_or(("", ""));
possible_commands.push(command.to_string());
}
if line == "SUBCOMMANDS:" {
has_parsed_subcommand_header = true;
}
}

possible_commands
}

pub fn get_contents_from_commands(commands: &[String]) -> HashMap<String, String> {
let mut contents: HashMap<String, String> = HashMap::new();

for command in commands {
let result = match generate_documentation(command) {
Ok(output) => output,
Err(_) => continue,
};
contents.insert("forc ".to_owned() + command, result);
}

contents
}

fn generate_documentation(subcommand: &str) -> Result<String> {
let mut result = String::new();
let mut has_parsed_subcommand_header = false;

let output = process::Command::new("forc")
.args([subcommand, "--help"])
.output()
.expect("Failed running forc --help");

if !output.status.success() {
return Err(anyhow!("Failed to run forc {} --help", subcommand));
}

let s = String::from_utf8_lossy(&output.stdout) + String::from_utf8_lossy(&output.stderr);

for (index, line) in s.lines().enumerate() {
let mut formatted_line = String::new();
let line = line.trim();

if line == "SUBCOMMANDS:" {
has_parsed_subcommand_header = true;
}

if index == 0 {
formatted_line.push_str(&format_header_line(line));
} else if index == 1 {
formatted_line.push_str(line);
} else {
formatted_line.push_str(&format_line(line, has_parsed_subcommand_header))
}

result.push_str(&formatted_line);

if !formatted_line.ends_with('\n') {
result.push('\n');
}
}
result = result.trim().to_string();
Ok(result)
}

pub fn get_forc_command_from_file_name(file_name: OsString) -> String {
file_name
.into_string()
.unwrap()
.split('.')
.next()
.unwrap()
.to_string()
.replace('_', " ")
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_forc_command_from_file_name() {
assert_eq!(
"forc gm",
get_forc_command_from_file_name(OsString::from("forc_gm.md")),
);
}
}
32 changes: 28 additions & 4 deletions scripts/mdbook-forc-documenter/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ pub enum LineKind {
SubHeader,
Arg,
Option,
Subcommand,
Text,
}

fn get_line_kind(line: &str) -> LineKind {
fn get_line_kind(line: &str, has_parsed_subcommand_header: bool) -> LineKind {
if SUBHEADERS.contains(&line) {
LineKind::SubHeader
} else if is_args_line(line) {
LineKind::Arg
} else if is_options_line(line) {
LineKind::Option
} else if has_parsed_subcommand_header {
LineKind::Subcommand
} else {
LineKind::Text
}
Expand All @@ -37,14 +40,15 @@ pub fn is_arg(token: &str) -> bool {
}

pub fn format_header_line(header_line: &str) -> String {
"\n# ".to_owned() + header_line + "\n"
"\n# ".to_owned() + header_line.split_whitespace().next().unwrap() + "\n"
}

pub fn format_line(line: &str) -> String {
match get_line_kind(line) {
pub fn format_line(line: &str, has_parsed_subcommand_header: bool) -> String {
match get_line_kind(line, has_parsed_subcommand_header) {
LineKind::SubHeader => format_subheader_line(line),
LineKind::Option => format_option_line(line),
LineKind::Arg => format_arg_line(line),
LineKind::Subcommand => format_subcommand_line(line),
LineKind::Text => line.to_string(),
}
}
Expand All @@ -53,6 +57,13 @@ fn format_subheader_line(subheader_line: &str) -> String {
"\n## ".to_owned() + subheader_line + "\n"
}

fn format_subcommand_line(line: &str) -> String {
let mut line_iter = line.trim().splitn(2, ' ');
let name = "`".to_owned() + line_iter.next().unwrap() + "`\n\n";
let text = line_iter.collect::<String>().trim_start().to_owned() + "\n\n";
name + &text
}

fn format_arg_line(arg_line: &str) -> String {
let mut formatted_arg_line = String::new();

Expand Down Expand Up @@ -214,4 +225,17 @@ mod tests {
format_option_line(example_option_line_2)
);
}

#[test]
fn test_format_subcommand_line() {
let example_subcommand =
" clean Cleans up any existing state associated with the fuel block explorer";
let expected_subcommand =
"`clean`\n\nCleans up any existing state associated with the fuel block explorer\n\n";

assert_eq!(
expected_subcommand,
format_subcommand_line(example_subcommand)
);
}
}
Loading

0 comments on commit 17b9e24

Please sign in to comment.