Skip to content

Commit

Permalink
feat: format manifest with forc fmt command
Browse files Browse the repository at this point in the history
  • Loading branch information
iqdecay committed Jan 11, 2022
1 parent 5d85d7d commit 168fe87
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
105 changes: 105 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 forc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sway-fmt = { version = "0.2.1", path = "../sway-fmt" }
sway-server = { version = "0.2.1", path = "../sway-server" }
sway-utils = { version = "0.2.1", path = "../sway-utils" }
sway-types = { version = "0.2.1", path = "../sway-types" }
taplo = "0.7.0"
tar = "0.4.35"
term-table = "1.3"
termcolor = "1.1"
Expand Down
27 changes: 24 additions & 3 deletions forc/src/ops/forc_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::ops::forc_build;
use crate::utils::helpers::{println_green, println_red};
use prettydiff::{basic::DiffOp, diff_lines};
use std::{fmt, fs, io, path::Path, sync::Arc};
use taplo::formatter as taplo_fmt;
use sway_fmt::get_formatted_data;
use sway_utils::{find_manifest_dir, get_sway_files};
use sway_utils::{find_manifest_dir, get_sway_files, constants};

pub fn format(command: FormatCommand) -> Result<(), FormatError> {
let build_command = BuildCommand {
Expand All @@ -30,6 +31,8 @@ fn format_after_build(command: FormatCommand) -> Result<(), FormatError> {

match find_manifest_dir(&curr_dir) {
Some(path) => {
let mut manifest_file = path.clone();
manifest_file.push(constants::MANIFEST_FILE_NAME);
let files = get_sway_files(path);
let mut contains_edits = false;

Expand All @@ -48,7 +51,7 @@ fn format_after_build(command: FormatCommand) -> Result<(), FormatError> {
display_file_diff(&file_content, &formatted_content)?;
}
} else {
format_sway_file(&file, &formatted_content)?;
format_file(&file, &formatted_content)?;
}
}
Err(err) => {
Expand All @@ -60,6 +63,24 @@ fn format_after_build(command: FormatCommand) -> Result<(), FormatError> {
}
}

// format manifest using taplo formatter
if let Ok(file_content) = fs::read_to_string(&manifest_file) {
let formatted_content = taplo_fmt::format(&file_content,
taplo_fmt::Options { ..Default::default() });
if command.check {
if formatted_content != file_content {
if !contains_edits {
contains_edits = true;
}
eprintln!("\nManifest Forc.toml improperly formatted");
display_file_diff(&file_content, &formatted_content)?;
} else {
format_file(&manifest_file, &formatted_content)?;
}
}
}


if command.check {
if contains_edits {
// One or more files are not formatted, exit with error
Expand Down Expand Up @@ -117,7 +138,7 @@ fn display_file_diff(file_content: &str, formatted_content: &str) -> Result<(),
Result::Ok(())
}

fn format_sway_file(file: &Path, formatted_content: &str) -> Result<(), FormatError> {
fn format_file(file: &Path, formatted_content: &str) -> Result<(), FormatError> {
fs::write(file, formatted_content)?;

Ok(())
Expand Down

0 comments on commit 168fe87

Please # to comment.