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
10 changes: 5 additions & 5 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ toml = "0.8"
# needs real I/O (git fetch, filesystem), so it's off by default for
# `comline-core`'s wasm32 consumers (the playground editor, the language
# server); the CLI is the one consumer that actually runs `comline build`.
comline-core = { git = "https://github.com/ComlineProject/core", rev = "5c1f3474b462ed912f4f2f5a518b87a5f6576aaa", features = ["deps"] }
comline-core = { git = "https://github.com/ComlineProject/core", rev = "016e38603152444f9b51fddfd46b1e6b1d73f1d8", features = ["deps"] }
# All three pinned to revs that agree on one `comline-codegen` - bumped
# together: generation#40's branch-tip commit (GenRequest.external_std -
# comline-rust/comline-typescript's own codegen crates consume
# `comline-codegen`'s types, so this pins the exact same generation rev
# they do, since Cargo only unifies a git dependency across byte-identical
# revs), then comline-rust#30 and comline-typescript#25 (both branch-tip,
# pending those PRs' merge).
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "52803df6436f49aec5f2266a94c89d4ced4b229c" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "eb8396a337e0efe452cea32cdb0e6a50dd56f2d5", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "d8a1df17b83752314a34d9c0b7817e4feb16820e", optional = true }
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "69a3e37ae788a91c5f72dd047d6196aa4c95fdef" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "5b1ba2b099cc295c6768935623f4c084f134e6b8", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "c484b764b4341d8a8529eb52613e4ce2fb6c35d5", optional = true }

[features]
# Which language generators are compiled in. Drop one to shed its whole
Expand Down
20 changes: 15 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ lists none — from `code_generation.languages` in `config.idp`. Output location
and layout live in `comline.toml`'s `[generate]` table. `--out` / `--layout` / \
`--mode` override one target and need `--target` when several are configured. \
Defaults: out `generated`, layout `{{language}}/{{namespace}}.{{ext}}`, mode \
`code` (the only one implemented).
`code` (`lib` emits a buildable package instead; `dylib` is not built yet).

`--dry-run` prints the tree each target would write and writes nothing.

Examples:
comline generate
Expand All @@ -101,15 +103,23 @@ Examples:
#[arg(short, long, value_name = "TEMPLATE")]
layout: Option<String>,

/// Emit form for the selected target: `code` (only one supported today), `lib`, `dylib`
/// Emit form for the selected target: `code` or `lib` (`dylib` is not built yet)
#[arg(short, long, value_name = "MODE")]
mode: Option<String>,

/// Reference `comline_std` as its own crate instead of inlining it
/// (Rust only; overrides `comline.toml`'s `[generate] external_std`)
#[arg(long)]
/// Reference `comline_std` as its own package instead of inlining it
/// (Rust only; the default in `lib` mode, off in `code` mode)
#[arg(long, conflicts_with = "no_external_std")]
external_std: bool,

/// Inline `std` into the generated output instead of referencing it
#[arg(long)]
no_external_std: bool,

/// Print what would be written, without writing anything
#[arg(long)]
dry_run: bool,

/// Regenerate automatically when a schema or `config.idp` changes
#[arg(short, long)]
watch: bool,
Expand Down
49 changes: 41 additions & 8 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! `compile_package` — no `.comline/` write, no version bump. `"all"` or an
//! explicit list reads committed versions from the CAS chain instead.

use std::path::Path;
use std::path::{Path, PathBuf};

use comline_codegen::{GenRequest, Mode, PackageMeta, Registry};
use comline_core::package::build::{self, cas::ObjectStore};
Expand All @@ -36,13 +36,13 @@ pub(crate) fn generator_registry() -> Registry {
registry
}

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

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

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

fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
ui::step(format!("Generating code{}", ui::at_path(work_dir)));
fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool) -> Result<()> {
ui::step(format!(
"Generating code{}{}",
ui::at_path(work_dir),
if dry_run { " - dry run, nothing written" } else { "" },
));

let cfg = ComlineToml::load(work_dir)?;

Expand Down Expand Up @@ -169,7 +173,10 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
}
let multi = versions.len() > 1;

let label = format!("language: {}, version: {}", t.language, t.lang_version);
let label = format!(
"language: {}, version: {}, mode: {}",
t.language, t.lang_version, t.mode,
);
ui::detail(if ui::plain() {
label
} else {
Expand All @@ -190,6 +197,7 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
gv.package_version.clone()
},
},
lang_version: t.lang_version.clone(),
default_framing: t.default_framing.clone(),
external_std: t.external_std,
};
Expand All @@ -201,6 +209,13 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
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();

match mode {
// One file per schema, placed by `layout` - whichever schemas
// the generator actually produced a file for (it may
Expand All @@ -211,6 +226,11 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
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;
}
write_file(&dest, &f.contents)?;

let src_name = format!("{namespace}.{SCHEMA_EXTENSION}");
Expand All @@ -235,17 +255,30 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
};
for f in &files {
let dest = root.join(&f.path);
if dry_run {
planned.push(dest);
written += 1;
continue;
}
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()));
written += 1;
}
}
}

if dry_run {
ui::tree(work_dir, &planned);
}
}
}

ui::success(format!("Generated {written} file(s)"));
ui::success(if dry_run {
format!("Would write {written} file(s)")
} else {
format!("Generated {written} file(s)")
});
Ok(())
}

Expand Down
7 changes: 4 additions & 3 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use miette::{IntoDiagnostic, Result, WrapErr};

use crate::ui;

const MAIN_IDS: &str = "\
const GREETING_IDS: &str = "\
/// The language a greeting is written in.
enum Language {
English
Expand Down Expand Up @@ -38,7 +38,8 @@ const COMLINE_TOML: &str = "\
# [generate]
# out = \"generated\" # output root
# layout = \"{{language}}/{{namespace}}.{{ext}}\" # path under the root
# mode = \"code\" # code | lib | dylib (only `code` today)
# mode = \"code\" # code (loose source) | lib (a buildable package); dylib not built yet
# external_std = false # reference `comline_std` rather than inlining std; defaults on in `lib`
# package_versions = \"latest\" # latest | all | [\"0.3.0\", \"0.4.0\"]
# default_framing = \"jsonrpc\" # wire framing for protocols with no `@framing` (else datagram)
#
Expand Down Expand Up @@ -69,7 +70,7 @@ pub fn run(work_dir: &Path, name: &str, git: bool) -> Result<()> {

write(&root.join("config.idp"), &config_idp(&package))?;
write(&root.join("comline.toml"), COMLINE_TOML)?;
write(&root.join("src/main.ids"), MAIN_IDS)?;
write(&root.join("src/greeting.ids"), GREETING_IDS)?;
write(&root.join(".gitignore"), GITIGNORE)?;

if git {
Expand Down
84 changes: 77 additions & 7 deletions src/gen_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ pub const FILE_NAME: &str = "comline.toml";
pub const DEFAULT_OUT: &str = "generated";
/// Default on-disk layout under a target's root.
pub const DEFAULT_LAYOUT: &str = "{{language}}/{{namespace}}.{{ext}}";
/// Default emit form. Only `"code"` is implemented today.
/// Default emit form. `"code"` and `"lib"` are implemented; `"dylib"` is not.
pub const DEFAULT_MODE: &str = "code";
/// Default: `std` stays inlined into the generated output, unchanged from
/// before this existed.
pub const DEFAULT_EXTERNAL_STD: bool = false;

/// Whether `std` is referenced as its own package rather than inlined, when
/// nothing says either way.
///
/// It follows the mode, because the two modes differ in whether there is
/// anywhere to put the dependency. `lib` writes the package manifest, so the
/// generator adds `comline_std` to it and the crate links one shared std.
/// `code` emits loose source files with no manifest to add anything to - all
/// a generator can do there is leave a comment naming the dependency line to
/// write by hand, so inlining stays the honest default.
fn default_external_std(mode: &str) -> bool {
mode == "lib"
}

/// Which package versions to generate bindings for.
///
Expand Down Expand Up @@ -165,8 +175,11 @@ pub struct Overrides<'a> {
pub out: Option<&'a str>,
pub layout: Option<&'a str>,
pub mode: Option<&'a str>,
/// `--external-std` is presence-only (no `--no-external-std`, since the
/// default is already off) - `Some(true)` when passed, `None` otherwise.
/// `Some(true)` from `--external-std`, `Some(false)` from
/// `--no-external-std`, `None` when neither was passed. Both spellings
/// exist because the default is no longer a fixed `false` - it follows
/// the mode (see [`default_external_std`]), so either direction can be
/// the one a given run needs to override.
pub external_std: Option<bool>,
}

Expand Down Expand Up @@ -344,7 +357,7 @@ pub fn resolve(
env_external_std,
t.external_std,
cfg.generate.external_std,
DEFAULT_EXTERNAL_STD,
default_external_std(&mode),
);

let lang_version = t
Expand Down Expand Up @@ -466,6 +479,63 @@ mod tests {
assert_eq!(resolved[0].default_framing.as_deref(), Some("datagram"));
}

/// `lib` writes the package manifest, so the generator can add
/// `comline_std` to it; `code` emits loose files with nowhere to put a
/// dependency, so inlining stays the honest default there.
#[test]
fn external_std_follows_the_mode() {
for (mode, expected) in [("code", false), ("lib", true)] {
let cfg = parse(&format!(
"[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n\
mode = \"{mode}\"\n",
));
let resolved = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap();
assert_eq!(resolved[0].external_std, expected, "mode {mode}");
}
}

/// Following the mode is a *default*, not a rule: a crate that wants a
/// self-contained copy of std can still say so.
#[test]
fn an_explicit_false_still_inlines_std_in_lib_mode() {
let cfg = parse(
"[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n\
mode = \"lib\"\nexternal_std = false\n",
);
let resolved = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap();
assert!(!resolved[0].external_std);
}

#[test]
fn the_section_default_applies_to_a_code_target_too() {
let cfg = parse(
"[generate]\nexternal_std = true\n\n\
[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n",
);
let resolved = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap();
assert!(resolved[0].external_std, "`code`, but asked for explicitly");
}

/// Both flag spellings exist because the default is no longer a fixed
/// `false` - either direction can be the one a run needs to override.
#[test]
fn either_flag_spelling_overrides_the_mode_default() {
let cfg = parse(
"[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n\
mode = \"lib\"\n",
);
let off = Overrides { external_std: Some(false), ..Default::default() };
let resolved = resolve(&cfg, &[], Path::new("/x"), &off).unwrap();
assert!(!resolved[0].external_std, "--no-external-std");

let cfg = parse(
"[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n",
);
let on = Overrides { external_std: Some(true), ..Default::default() };
let resolved = resolve(&cfg, &[], Path::new("/x"), &on).unwrap();
assert!(resolved[0].external_std, "--external-std");
}

#[test]
fn default_framing_absent_is_none() {
let cfg = parse("[generate]\n");
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,23 @@ fn run(cli: Cli) -> Result<()> {
layout,
mode,
external_std,
no_external_std,
dry_run,
watch,
} => {
let overrides = gen_config::Overrides {
target: target.as_deref(),
out: out.as_deref().map(|p| p.to_str().unwrap_or_default()),
layout: layout.as_deref(),
mode: mode.as_deref(),
external_std: external_std.then_some(true),
// Two presence-only flags rather than one `Option<bool>`:
// clap rejects passing both (`conflicts_with`), so at most
// one is ever set.
external_std: external_std
.then_some(true)
.or(no_external_std.then_some(false)),
};
commands::generate::run(&work_dir, &overrides, watch)
commands::generate::run(&work_dir, &overrides, dry_run, 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
Loading
Loading