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.
document native plugins within the Sway book (FuelLabs#1582)
* 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
1 parent
381a080
commit 17b9e24
Showing
17 changed files
with
336 additions
and
133 deletions.
There are no files selected for viewing
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
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
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 @@ | ||
# forc explore |
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 @@ | ||
# forc fmt |
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 @@ | ||
# forc lsp |
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
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
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,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")), | ||
); | ||
} | ||
} |
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
Oops, something went wrong.