forked from matter-labs/zksync-era
-
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.
feat(zk_toolbox): Run formatters and linterrs (matter-labs#2675)
## What ❔ Adding an ability to run linters and formatters for zk supervisor ## Why ❔ <!-- Why are these changes done? What goal do they contribute to? What are the principles behind them? --> <!-- Example: PR templates ensure PR reviewers, observers, and future iterators are in context about the evolution of repos. --> ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zk fmt` and `zk lint`. --------- Signed-off-by: Danil <[email protected]> Co-authored-by: Alexander Melnikov <[email protected]>
- Loading branch information
1 parent
fa866cd
commit caedd1c
Showing
11 changed files
with
274 additions
and
80 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
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
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,127 @@ | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
use common::{cmd::Cmd, logger, spinner::Spinner}; | ||
use config::EcosystemConfig; | ||
use xshell::{cmd, Shell}; | ||
|
||
use crate::{ | ||
commands::lint_utils::{get_unignored_files, Extension}, | ||
messages::{ | ||
msg_running_fmt_for_extension_spinner, msg_running_fmt_for_extensions_spinner, | ||
msg_running_rustfmt_for_dir_spinner, MSG_RUNNING_CONTRACTS_FMT_SPINNER, | ||
}, | ||
}; | ||
|
||
async fn prettier(shell: Shell, extension: Extension, check: bool) -> anyhow::Result<()> { | ||
let spinner = Spinner::new(&msg_running_fmt_for_extension_spinner(extension)); | ||
let files = get_unignored_files(&shell, &extension)?; | ||
|
||
if files.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
spinner.freeze(); | ||
let mode = if check { "--check" } else { "--write" }; | ||
let config = format!("etc/prettier-config/{extension}.js"); | ||
Ok( | ||
Cmd::new(cmd!(shell, "yarn --silent prettier {mode} --config {config}").args(files)) | ||
.run()?, | ||
) | ||
} | ||
|
||
async fn prettier_contracts(shell: Shell, check: bool) -> anyhow::Result<()> { | ||
let spinner = Spinner::new(MSG_RUNNING_CONTRACTS_FMT_SPINNER); | ||
spinner.freeze(); | ||
let prettier_command = cmd!(shell, "yarn --silent --cwd contracts") | ||
.arg(format!("prettier:{}", if check { "check" } else { "fix" })); | ||
|
||
Ok(Cmd::new(prettier_command).run()?) | ||
} | ||
|
||
async fn rustfmt(shell: Shell, check: bool, link_to_code: PathBuf) -> anyhow::Result<()> { | ||
for dir in [".", "prover", "zk_toolbox"] { | ||
let spinner = Spinner::new(&msg_running_rustfmt_for_dir_spinner(dir)); | ||
let _dir = shell.push_dir(link_to_code.join(dir)); | ||
let mut cmd = cmd!(shell, "cargo fmt -- --config imports_granularity=Crate --config group_imports=StdExternalCrate"); | ||
if check { | ||
cmd = cmd.arg("--check"); | ||
} | ||
spinner.freeze(); | ||
Cmd::new(cmd).run()?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn run_all_rust_formatters( | ||
shell: Shell, | ||
check: bool, | ||
link_to_code: PathBuf, | ||
) -> anyhow::Result<()> { | ||
rustfmt(shell.clone(), check, link_to_code).await?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub enum Formatter { | ||
Rustfmt, | ||
Contract, | ||
Prettier { | ||
#[arg(short, long)] | ||
extensions: Vec<Extension>, | ||
}, | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct FmtArgs { | ||
#[clap(long, short = 'c')] | ||
pub check: bool, | ||
#[clap(subcommand)] | ||
pub formatter: Option<Formatter>, | ||
} | ||
|
||
pub async fn run(shell: Shell, args: FmtArgs) -> anyhow::Result<()> { | ||
let ecosystem = EcosystemConfig::from_file(&shell)?; | ||
match args.formatter { | ||
None => { | ||
let mut tasks = vec![]; | ||
let extensions: Vec<_> = | ||
vec![Extension::Js, Extension::Ts, Extension::Md, Extension::Sol]; | ||
let spinner = Spinner::new(&msg_running_fmt_for_extensions_spinner(&extensions)); | ||
spinner.freeze(); | ||
for ext in extensions { | ||
tasks.push(tokio::spawn(prettier(shell.clone(), ext, args.check))); | ||
} | ||
tasks.push(tokio::spawn(rustfmt( | ||
shell.clone(), | ||
args.check, | ||
ecosystem.link_to_code, | ||
))); | ||
tasks.push(tokio::spawn(prettier_contracts(shell.clone(), args.check))); | ||
|
||
futures::future::join_all(tasks) | ||
.await | ||
.iter() | ||
.for_each(|res| { | ||
if let Err(err) = res { | ||
logger::error(err) | ||
} | ||
}); | ||
} | ||
Some(Formatter::Prettier { mut extensions }) => { | ||
if extensions.is_empty() { | ||
extensions = vec![Extension::Js, Extension::Ts, Extension::Md, Extension::Sol]; | ||
} | ||
let spinner = Spinner::new(&msg_running_fmt_for_extensions_spinner(&extensions)); | ||
for ext in extensions { | ||
prettier(shell.clone(), ext, args.check).await? | ||
} | ||
spinner.finish() | ||
} | ||
Some(Formatter::Rustfmt) => { | ||
run_all_rust_formatters(shell.clone(), args.check, ".".into()).await? | ||
} | ||
Some(Formatter::Contract) => prettier_contracts(shell.clone(), args.check).await?, | ||
} | ||
Ok(()) | ||
} |
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.