Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ them (where it lands, in what mode); a language with no block takes the \
Defaults: out `generated`, layout `{{language}}/{{namespace}}.{{ext}}`, mode \
`code` (`lib` emits a buildable package instead; `dylib` is not built yet).

`--dry-run` prints the tree each target would write and writes nothing.
Each target's files are drawn as a tree. `--list` prints them one per line instead, with the schema each came from. `--dry-run` writes nothing, and reports the same way.

Examples:
comline generate
Expand Down Expand Up @@ -123,6 +123,10 @@ Examples:
#[arg(long)]
dry_run: bool,

/// List each file on its own line instead of drawing a tree
#[arg(long)]
list: bool,

/// Regenerate automatically when a schema or `config.idp` changes
#[arg(short, long)]
watch: bool,
Expand Down
79 changes: 46 additions & 33 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ pub(crate) fn generator_registry() -> Registry {
registry
}

pub fn run(work_dir: &Path, overrides: &Overrides, dry_run: bool, watching: bool) -> Result<()> {
pub fn run(
work_dir: &Path,
overrides: &Overrides,
dry_run: bool,
list: bool,
watching: bool,
) -> Result<()> {
ensure_project(work_dir)?;

if watching {
watch::run(work_dir, "generate", || {
generate_once(work_dir, overrides, dry_run)
generate_once(work_dir, overrides, dry_run, list)
})
} else {
generate_once(work_dir, overrides, dry_run)
generate_once(work_dir, overrides, dry_run, list)
}
}

Expand All @@ -54,7 +60,7 @@ struct GenVersion {
schemas: Vec<(String, Vec<FrozenUnit>)>, // (namespace path, frozen units)
}

fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool) -> Result<()> {
fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool, list: bool) -> Result<()> {
ui::step(format!(
"Generating code{}{}",
ui::at_path(work_dir),
Expand Down Expand Up @@ -224,12 +230,13 @@ fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool) -> Resul
let files =
generator(&request).map_err(|e| miette!("`{}` generator: {e}", t.language))?;

// A dry run lists destinations instead of writing them, then
// renders them as one tree per target. Everything above has
// already happened for real - the generators are pure, and
// `write_file` is the only side effect in this command - so the
// tree is what *would* be written rather than a guess at it.
let mut planned: Vec<PathBuf> = Vec::new();
// Every destination, whether or not it gets written: the tree
// is drawn from these once the loop is done. A dry run differs
// only in skipping `write_file` - everything above has already
// happened for real (the generators are pure, and `write_file`
// is the only side effect in this command), so the tree reports
// what *would* be written rather than guessing at it.
let mut destinations: Vec<PathBuf> = Vec::new();

match mode {
// One file per schema, placed by `layout` - whichever schemas
Expand All @@ -241,21 +248,23 @@ fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool) -> Resul
let namespace = f.path.with_extension("").to_string_lossy().into_owned();
let dest =
t.dest_for(&namespace, ext, &spec_version, &gv.package_version)?;
if dry_run {
planned.push(dest);
written += 1;
continue;
if !dry_run {
write_file(&dest, &f.contents)?;
}
write_file(&dest, &f.contents)?;

let src_name = format!("{namespace}.{SCHEMA_EXTENSION}");
let shown = dest.strip_prefix(work_dir).unwrap_or(&dest).display();
let tag = if multi {
format!("[{}] ", gv.package_version)
} else {
String::new()
};
ui::detail(format!(" {tag}{src_name} {} {shown}", ui::arrow()));
if list {
// The schema each file came from, which the tree
// can't show - the one thing `--list` has that
// the default doesn't.
let src_name = format!("{namespace}.{SCHEMA_EXTENSION}");
let shown = dest.strip_prefix(work_dir).unwrap_or(&dest).display();
let tag = if multi {
format!("[{}] ", gv.package_version)
} else {
String::new()
};
ui::detail(format!(" {tag}{src_name} {} {shown}", ui::arrow()));
}
destinations.push(dest);
written += 1;
}
}
Expand All @@ -270,21 +279,25 @@ fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool) -> Resul
};
for f in &files {
let dest = root.join(&f.path);
if dry_run {
planned.push(dest);
written += 1;
continue;
if !dry_run {
write_file(&dest, &f.contents)?;
}
if list {
let shown = dest.strip_prefix(work_dir).unwrap_or(&dest).display();
ui::detail(format!(
" {} {} {shown}",
f.path.display(),
ui::arrow()
));
}
write_file(&dest, &f.contents)?;
let shown = dest.strip_prefix(work_dir).unwrap_or(&dest).display();
ui::detail(format!(" {} {} {shown}", f.path.display(), ui::arrow()));
destinations.push(dest);
written += 1;
}
}
}

if dry_run {
ui::tree(work_dir, &planned);
if !list {
ui::tree(work_dir, &destinations);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn run(cli: Cli) -> Result<()> {
external_std,
no_external_std,
dry_run,
list,
watch,
} => {
let overrides = gen_config::Overrides {
Expand All @@ -138,7 +139,7 @@ fn run(cli: Cli) -> Result<()> {
.then_some(true)
.or(no_external_std.then_some(false)),
};
commands::generate::run(&work_dir, &overrides, dry_run, watch)
commands::generate::run(&work_dir, &overrides, dry_run, list, watch)
}
Commands::Diff { old, new } => commands::diff::run(&work_dir, &old, &new),
Commands::Clean { dry_run } => commands::clean::run(&work_dir, dry_run),
Expand Down
78 changes: 73 additions & 5 deletions tests/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ fn writes_one_file_per_schema_under_the_default_layout() {
.assert()
.success()
.stderr(predicate::str::contains("language: rust, version:"))
.stderr(predicate::str::contains("api.ids → generated/rust/api.rs"))
// Drawn as a tree, the same way `--dry-run` reports.
.stderr(predicate::str::contains("generated/rust/"))
.stderr(predicate::str::contains("├─ api.rs").or(predicate::str::contains("└─ api.rs")))
.stderr(predicate::str::contains("Generated"));

// default layout: generated/{language}/{namespace}.{ext}
Expand Down Expand Up @@ -124,17 +126,28 @@ fn env_out_applies_in_a_multi_target_config() {
}

#[test]
fn plain_uses_ascii_arrow() {
fn plain_uses_ascii_output() {
let temp = tempfile::tempdir().unwrap();
let project = fixture_project(temp.path());

// The default tree, drawn with ASCII branches.
comline_cmd()
.current_dir(&project)
.args(["generate", "--target", "rust", "--plain"])
.assert()
.success()
.stderr(predicate::str::contains("api.ids -> generated/rust/api.rs"))
.stderr(predicate::str::contains("|- api.rs").or(predicate::str::contains("\\- api.rs")))
.stderr(predicate::str::contains('├').not())
.stderr(predicate::str::contains('⚙').not());

// And `--list`'s arrow.
comline_cmd()
.current_dir(&project)
.args(["generate", "--target", "rust", "--plain", "--list"])
.assert()
.success()
.stderr(predicate::str::contains("api.ids -> generated/rust/api.rs"))
.stderr(predicate::str::contains('→').not());
}

#[test]
Expand Down Expand Up @@ -350,6 +363,49 @@ fn latest_stamps_the_last_committed_version() {
assert!(project.join("generated/rust/0.0.1/api.rs").exists());
}

/// The tree shows where files land; only `--list` shows which schema each
/// came from, which is the reason to keep it.
#[test]
fn list_names_the_schema_each_file_came_from() {
let temp = tempfile::tempdir().unwrap();
let project = fixture_project(temp.path());

comline_cmd()
.current_dir(&project)
.args(["generate", "--target", "rust", "--list"])
.assert()
.success()
.stderr(predicate::str::contains("api.ids → generated/rust/api.rs"))
.stderr(predicate::str::contains(
"other.ids → generated/rust/other.rs",
))
.stderr(predicate::str::contains('├').not());

assert!(project.join("generated/rust/api.rs").exists());
}

/// `--dry-run` and `--list` are independent: one decides whether anything is
/// written, the other how it is reported.
#[test]
fn dry_run_and_list_combine() {
let temp = tempfile::tempdir().unwrap();
let project = fixture_project(temp.path());

comline_cmd()
.current_dir(&project)
.args(["generate", "--target", "rust", "--dry-run", "--list"])
.assert()
.success()
.stderr(predicate::str::contains("api.ids → generated/rust/api.rs"))
.stderr(predicate::str::contains('├').not())
.stderr(predicate::str::contains("Would write"));

assert!(
!project.join("generated").exists(),
"a dry run writes nothing"
);
}

#[test]
fn package_versions_all_generates_every_committed_version() {
let temp = tempfile::tempdir().unwrap();
Expand All @@ -367,11 +423,23 @@ fn package_versions_all_generates_every_committed_version() {
.args(["generate", "--target", "rust"])
.assert()
.success()
.stderr(predicate::str::contains("[0.0.1]"))
.stderr(predicate::str::contains("[0.1.0]"));
// One tree per version. `layout` has to carry `{{package_version}}`
// for multi-version output, so each tree's root names its version -
// no `[0.0.1]` tag needed to tell them apart.
.stderr(predicate::str::contains("generated/rust/0.0.1/"))
.stderr(predicate::str::contains("generated/rust/0.1.0/"));

assert!(project.join("generated/rust/0.0.1/api.rs").exists());
assert!(project.join("generated/rust/0.1.0/api.rs").exists());

// `--list` tags each line with the version it belongs to.
comline_cmd()
.current_dir(&project)
.args(["generate", "--target", "rust", "--list"])
.assert()
.success()
.stderr(predicate::str::contains("[0.0.1]"))
.stderr(predicate::str::contains("[0.1.0]"));
}

#[test]
Expand Down
Loading