Skip to content

Commit

Permalink
internal: support extra_args for moon fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunchen committed Oct 21, 2024
1 parent 9f6059d commit 735551e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
11 changes: 8 additions & 3 deletions crates/moon/src/cli/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
// For inquiries, you can contact us via e-mail at [email protected].

use anyhow::bail;
use moonbuild::dry_run;
use mooncake::pkg::sync::auto_sync;
use moonutil::{
common::{FileLock, FmtOpt, MoonbuildOpt, MooncOpt, RunMode},
Expand All @@ -36,6 +36,8 @@ pub struct FmtSubcommand {
/// Sort input files
#[clap(long)]
pub sort_input: bool,

pub args: Vec<String>,
}

pub fn run_fmt(cli: &UniversalFlags, cmd: FmtSubcommand) -> anyhow::Result<i32> {
Expand Down Expand Up @@ -64,7 +66,10 @@ pub fn run_fmt(cli: &UniversalFlags, cmd: FmtSubcommand) -> anyhow::Result<i32>
target_dir: target_dir.clone(),
sort_input: cmd.sort_input,
run_mode,
fmt_opt: Some(FmtOpt { check: cmd.check }),
fmt_opt: Some(FmtOpt {
check: cmd.check,
extra_args: cmd.args,
}),
build_graph: cli.build_graph,
test_opt: None,
args: vec![],
Expand All @@ -83,7 +88,7 @@ pub fn run_fmt(cli: &UniversalFlags, cmd: FmtSubcommand) -> anyhow::Result<i32>
)?;

if cli.dry_run {
bail!("dry-run is not implemented for fmt");
return dry_run::print_commands(&module, &moonc_opt, &moonbuild_opt);
}
moonbuild::entry::run_fmt(&module, &moonc_opt, &moonbuild_opt)
}
10 changes: 0 additions & 10 deletions crates/moon/src/cli/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
//
// For inquiries, you can contact us via e-mail at [email protected].

use std::path::PathBuf;

pub mod embed;
pub mod format_and_diff;

Expand All @@ -36,14 +34,6 @@ pub enum ToolSubcommands {
Embed(Embed),
}

#[derive(Debug, clap::Parser)]
pub struct FormatAndWriteSubcommand {
#[clap(long)]
old: PathBuf,
#[clap(long)]
new: PathBuf,
}

pub fn run_tool(cmd: ToolSubcommand) -> anyhow::Result<i32> {
match cmd.subcommand {
ToolSubcommands::FormatAndDiff(subcmd) => run_format_and_diff(subcmd),
Expand Down
3 changes: 3 additions & 0 deletions crates/moon/src/cli/tool/format_and_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct FormatAndDiffSubcommand {
/// The target path of the formatted code
#[clap(long)]
new: PathBuf,

pub args: Vec<String>,
}

pub fn run_format_and_diff(cmd: FormatAndDiffSubcommand) -> anyhow::Result<i32> {
Expand All @@ -36,6 +38,7 @@ pub fn run_format_and_diff(cmd: FormatAndDiffSubcommand) -> anyhow::Result<i32>
.arg(cmd.old.to_str().unwrap())
.arg("-o")
.arg(cmd.new.to_str().unwrap())
.args(&cmd.args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
Expand Down
26 changes: 20 additions & 6 deletions crates/moonbuild/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ pub fn load_moon_proj(
moonc_opt: &MooncOpt,
moonbuild_opt: &MoonbuildOpt,
) -> anyhow::Result<State> {
let n2_input = gen_fmt(m, moonc_opt, moonbuild_opt);
let state = gen_n2_fmt_state(&n2_input?, moonc_opt, moonbuild_opt)?;
let n2_input = super::fmt::gen_fmt(m, moonc_opt, moonbuild_opt)?;
let state = if moonbuild_opt.fmt_opt.as_ref().unwrap().check {
super::fmt::gen_n2_fmt_check_state(&n2_input, moonc_opt, moonbuild_opt)?
} else {
super::fmt::gen_n2_fmt_state(&n2_input, moonc_opt, moonbuild_opt)?
};
Ok(state)
}

Expand Down Expand Up @@ -89,7 +93,11 @@ pub fn gen_fmt(
Ok(N2FmtInput { items })
}

fn gen_inplace_fmt_command(graph: &mut n2graph::Graph, item: &FmtItem) -> (Build, FileId) {
fn gen_inplace_fmt_command(
graph: &mut n2graph::Graph,
item: &FmtItem,
moonbuild_opt: &MoonbuildOpt,
) -> (Build, FileId) {
let loc = FileLoc {
filename: Rc::new(PathBuf::from("format")),
line: 0,
Expand Down Expand Up @@ -119,6 +127,7 @@ fn gen_inplace_fmt_command(graph: &mut n2graph::Graph, item: &FmtItem) -> (Build
.arg("-w")
.arg("-o")
.arg(&item.phony_out)
.args(&moonbuild_opt.fmt_opt.as_ref().unwrap().extra_args)
.build();
build.cmdline = Some(command);
build.desc = Some(format!("moonfmt {}", item.input));
Expand Down Expand Up @@ -160,7 +169,7 @@ pub fn gen_n2_fmt_state(
let mut builds = vec![];

for item in input.items.iter() {
let (build, fid) = gen_inplace_fmt_command(&mut graph, item);
let (build, fid) = gen_inplace_fmt_command(&mut graph, item, moonbuild_opt);
graph.add_build(build)?;
builds.push(fid);
}
Expand All @@ -185,7 +194,11 @@ pub fn gen_n2_fmt_state(
})
}

fn gen_fmt_to_command(graph: &mut n2graph::Graph, item: &FmtItem) -> (Build, FileId) {
fn gen_fmt_to_command(
graph: &mut n2graph::Graph,
item: &FmtItem,
moonbuild_opt: &MoonbuildOpt,
) -> (Build, FileId) {
let loc = FileLoc {
filename: Rc::new(PathBuf::from("format")),
line: 0,
Expand Down Expand Up @@ -220,6 +233,7 @@ fn gen_fmt_to_command(graph: &mut n2graph::Graph, item: &FmtItem) -> (Build, Fil
.arg(&item.input)
.arg("--new")
.arg(&item.output)
.args(&moonbuild_opt.fmt_opt.as_ref().unwrap().extra_args)
.build();
build.cmdline = Some(command);
build.desc = Some(format!("moonfmt {}", item.input));
Expand Down Expand Up @@ -261,7 +275,7 @@ pub fn gen_n2_fmt_check_state(
let mut builds = vec![];

for item in input.items.iter() {
let (bs, fs) = gen_fmt_to_command(&mut graph, item);
let (bs, fs) = gen_fmt_to_command(&mut graph, item, moonbuild_opt);
graph.add_build(bs)?;
builds.push(fs);
}
Expand Down
1 change: 1 addition & 0 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ pub struct TestArtifacts {
#[derive(Debug, Clone, Default)]
pub struct FmtOpt {
pub check: bool,
pub extra_args: Vec<String>,
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 735551e

Please sign in to comment.