Skip to content

Commit

Permalink
Integrate formatters
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Aug 16, 2024
1 parent 846d422 commit 93c4ab6
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 110 deletions.
26 changes: 26 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@
bellman-cuda
sdk/zksync-rs/CHANGELOG.md
CHANGELOG.md
core/lib/dal/.sqlx
prover/lib/dal/.sqlx
node_modules

# Ignore contract submodules
contracts

**/target/**
**/node_modules
volumes
**/build/**
dist
.git
generated
grafonnet-lib
prettier-config
lint-config
**/cache
**/artifacts
**/typechain
binaryen
system-contracts
artifacts-zk
cache-zk
// Ignore directories with OZ and forge submodules.
contracts/l1-contracts/lib

**/.git
**/node_modules
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ contract HeapBenchmark {
mstore(add(array, sub(n, 1)), 4242)

let j := 0
for {} lt(j, n) {} {
for {

} lt(j, n) {

} {
v1 := mload(add(array, mod(mul(j, j), n)))
v2 := mload(add(array, j))
mstore(add(array, j), add(add(v1, v2), 42))
j := add(j, 1)
j := add(j, 1)
if gt(j, sub(n, 1)) {
j := 0
}
Expand Down
6 changes: 3 additions & 3 deletions etc/contracts-test-data/counter/counter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ contract Counter {
value += x;
}

function incrementWithRevertPayable(uint256 x, bool shouldRevert) payable public returns (uint256) {
function incrementWithRevertPayable(uint256 x, bool shouldRevert) public payable returns (uint256) {
return incrementWithRevert(x, shouldRevert);
}

function incrementWithRevert(uint256 x, bool shouldRevert) public returns (uint256) {
value += x;
if(shouldRevert) {
if (shouldRevert) {
revert("This method always reverts");
}
return value;
Expand All @@ -24,4 +24,4 @@ contract Counter {
function get() public view returns (uint256) {
return value;
}
}
}
1 change: 1 addition & 0 deletions zk_toolbox/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion zk_toolbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ tokio = { version = "1.37", features = ["full"] }
toml = "0.8.12"
url = { version = "2.5.0", features = ["serde"] }
xshell = "0.2.6"
clap-markdown = "0.1.4"
clap-markdown = "0.1.4"
1 change: 1 addition & 0 deletions zk_toolbox/crates/zk_supervisor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ url.workspace = true
xshell.workspace = true
serde.workspace = true
clap-markdown.workspace = true
futures.workspace = true
144 changes: 42 additions & 102 deletions zk_toolbox/crates/zk_supervisor/src/commands/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
use std::env::args;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::Command;
use std::{env::args, ffi::OsStr, future, path::PathBuf, process::Command};

use clap::Parser;
use common::{cmd::Cmd, logger};
use common::{cmd::Cmd, logger, spinner::Spinner};
use xshell::{cmd, Shell};

use crate::commands::lint::{Extension, IGNORED_DIRS, IGNORED_FILES};
use crate::commands::lint::Extension;

const CONFIG_PATH: &str = "etc/prettier-config";

fn prettier(shell: &Shell, extension: Extension, check: bool) -> anyhow::Result<()> {
let command = if check { "check" } else { "write" };
let files = get_unignored_files(shell, extension)?;
logger::info(format!("Got {} files for {extension}", files.len()));

if files.is_empty() {
logger::info(format!("No files of extension {extension} to format"));
return Ok(());
}

let mut prettier_command = cmd!(shell, "yarn --silent prettier --config ")
.arg(format!("{}/{}.js", CONFIG_PATH, extension))
.arg(format!("--{}", command))
.arg(format!("{}", files.join(" ")));

// if !check {
// prettier_command = prettier_command.arg("> /dev/null");
// }

Ok(prettier_command.run()?)
async fn prettier(shell: Shell, extension: Extension, check: bool) -> anyhow::Result<()> {
let spinner = Spinner::new(&format!("Running prettier for: {extension}"));
let mode = if check { "--check" } else { "--write" };
let glob = format!("**/*.{extension}");
let config = format!("etc/prettier-config/{extension}.js");
Ok(Cmd::new(cmd!(
shell,
"yarn --silent prettier {glob} {mode} --config {config}"
))
.run()?)
}

fn prettier_contracts(shell: &Shell, check: bool) -> anyhow::Result<()> {
let mut prettier_command = cmd!(shell, "yarn --silent --cwd contracts")
async fn prettier_contracts(shell: Shell, check: bool) -> anyhow::Result<()> {
let prettier_command = cmd!(shell, "yarn --silent --cwd contracts")
.arg(format!("prettier:{}", if check { "check" } else { "fix" }));

// if !check {
// prettier_command = prettier_command.arg("> /dev/null");
// }

Ok(Cmd::new(prettier_command).run()?)
}

fn rustfmt(shell: &Shell, check: bool, link_to_code: PathBuf) -> anyhow::Result<()> {
async fn rustfmt(shell: Shell, check: bool, link_to_code: PathBuf) -> anyhow::Result<()> {
for dir in vec![".", "prover", "zk_toolbox"] {
logger::info(format!("Running rustfmt for: {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 {
Expand All @@ -56,13 +40,13 @@ fn rustfmt(shell: &Shell, check: bool, link_to_code: PathBuf) -> anyhow::Result<
Ok(())
}

fn run_all_rust_formatters(
shell: &Shell,
async fn run_all_rust_formatters(
shell: Shell,
check: bool,
link_to_code: PathBuf,
) -> anyhow::Result<()> {
rustfmt(shell, check, link_to_code)?;
format_sqlx_queries(check)?;
rustfmt(shell.clone(), check, link_to_code).await?;
format_sqlx_queries(shell, check).await?;
Ok(())
}

Expand All @@ -84,89 +68,45 @@ pub struct FmtArgs {
pub formatter: Option<Formatter>,
}

pub fn run(shell: &Shell, args: FmtArgs) -> anyhow::Result<()> {
pub async fn run(shell: Shell, args: FmtArgs) -> anyhow::Result<()> {
match args.formatter {
None => {
let mut tasks = vec![];
let extensions: Vec<_> =
vec![Extension::Js, Extension::Ts, Extension::Md, Extension::Sol];
for ext in extensions {
prettier(shell, ext, args.check)?
tasks.push(tokio::spawn(prettier(shell.clone(), ext, args.check)));
}
run_all_rust_formatters(shell, args.check, ".".into())?;
prettier_contracts(shell, args.check)?
tasks.push(tokio::spawn(rustfmt(shell.clone(), args.check, ".".into())));
tasks.push(tokio::spawn(format_sqlx_queries(shell.clone(), args.check)));
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];
}
for ext in extensions {
prettier(shell, ext, args.check)?
prettier(shell.clone(), ext, args.check).await?
}
}
Some(Formatter::Rustfmt) => run_all_rust_formatters(shell, args.check, ".".into())?,
Some(Formatter::Contract) => prettier_contracts(shell, args.check)?,
Some(Formatter::Rustfmt) => {
run_all_rust_formatters(shell.clone(), args.check, ".".into()).await?
}
Some(Formatter::Contract) => prettier_contracts(shell.clone(), args.check).await?,
}
Ok(())
}

fn get_unignored_files(shell: &Shell, extension: Extension) -> anyhow::Result<Vec<String>> {
let root = if let Extension::Sol = extension {
"contracts"
} else {
"."
};

let ignored_dirs: Vec<_> = IGNORED_DIRS
.iter()
.map(|dir| {
vec![
"-o".to_string(),
"-path".to_string(),
format!("'*{dir}'"),
"-prune".to_string(),
]
})
.flatten()
.collect();

let ignored_files: Vec<_> = IGNORED_FILES
.iter()
.map(|file| {
vec![
"-a".to_string(),
"!".to_string(),
"-name".to_ascii_lowercase(),
format!("'{file}'"),
]
})
.flatten()
.collect();

dbg!(shell.current_dir());
// let output = Cmd::new(
// cmd!(shell, "find {root} -type f -name").arg(format!("'*.{extension}'")), // .args(ignored_files)
// // .arg("-print"),
// )
// .run_with_output()?;
// dbg!(&output);
let output = Cmd::new(
cmd!(shell, "find {root} -type f -name '*.js'")
.args(ignored_files)
.arg("-print")
.args(ignored_dirs),
)
.run_with_output()?;
dbg!(&output);

let files = String::from_utf8(output.stdout)?
.lines()
.map(|line| line.to_string())
.collect();

Ok(files)
}

fn format_sqlx_queries(check: bool) -> anyhow::Result<()> {
async fn format_sqlx_queries(shell: Shell, check: bool) -> anyhow::Result<()> {
// Implement your SQLx query formatting logic here.
Ok(())
}
2 changes: 1 addition & 1 deletion zk_toolbox/crates/zk_supervisor/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn lint(
&["--config".to_string(), config_path],
files.as_slice(),
]
.concat();
.concat();

Cmd::new(cmd.args(&args)).run()?;
spinner.finish();
Expand Down
2 changes: 1 addition & 1 deletion zk_toolbox/crates/zk_supervisor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async fn run_subcommand(args: Supervisor, shell: &Shell) -> anyhow::Result<()> {
clap_markdown::print_help_markdown::<Supervisor>();
}
SupervisorSubcommands::Lint(args) => commands::lint::run(shell, args)?,
SupervisorSubcommands::Fmt(args) => commands::fmt::run(shell, args)?,
SupervisorSubcommands::Fmt(args) => commands::fmt::run(shell.clone(), args).await?,
}
Ok(())
}
Expand Down

0 comments on commit 93c4ab6

Please sign in to comment.