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: 3 additions & 3 deletions Cargo.lock

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

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ toml = "0.8"
# `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 = "6431e61a4f1073aceeaee7be7df83e6b77b2acd5", features = ["deps"] }
# All three pinned to revs that already agree on comline-core@6431e61
# (core#73 - settings hover for .ids and .idp) - bumped together:
# generation#38's branch-tip commit (comline-rust/comline-typescript's own
# codegen crates consume `comline-codegen`'s types, so this pins the exact
# same generation rev they do - 2b5c46e, not generation's later merge
# commit from step 3, since Cargo only unifies a git dependency across
# byte-identical revs), then comline-rust#27 and comline-typescript#24.
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "2b5c46eefab186d24f5e883cef3fa79e4cb83ffd" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "8844c3e8ab61cd6f3edb8c17082c93f35db6f943", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "1e934a6e98e6f447d1cfe78dc5b22dff1e491f8f", optional = true }
# 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 = "efbd9824eb4ee28bd91b1aaa04a52883d4e1ab45" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "d2999a728d9000b2345d04a4352c05016af6de64", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "86ddc4a016d5d6251179a140c808e4109d65895c", optional = true }

[features]
# Which language generators are compiled in. Drop one to shed its whole
Expand Down
26 changes: 19 additions & 7 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ comline build --watch
### `comline generate`

```
comline generate [--target <lang>] [--out <dir>] [--layout <template>] [--mode <mode>] [--watch]
comline generate [--target <lang>] [--out <dir>] [--layout <template>] [--mode <mode>] [--external-std] [--watch]
```

Validate the project, then write generated code for each target. Unlike `build`,
Expand All @@ -232,6 +232,7 @@ none, from `code_generation.languages` in `config.idp`.
out = "generated" # output root, relative to comline.toml
layout = "{{language}}/{{namespace}}.{{ext}}" # path of each file under the root
mode = "code" # code | lib | dylib (only `code` today)
external_std = false # reference comline_std instead of inlining it (rust only)
package_versions = "latest" # latest | all | ["0.3.0", "0.4.0"]

# optional, one per target you want to pin or override:
Expand Down Expand Up @@ -273,14 +274,25 @@ paths — the `layout` must contain `{{package_version}}`. For `latest`,

- `--target <lang>` — only this language (case-insensitive). Errors if nothing
matches.
- `--out <dir>` / `--layout <template>` / `--mode <mode>` — override one target's
`[generate]` values. When more than one target is configured they require
`--target` (otherwise it is ambiguous which they apply to).
- `COMLINE_GENERATE_OUT` / `COMLINE_GENERATE_LAYOUT` / `COMLINE_GENERATE_MODE` —
the same overrides from the environment, sitting just below the flags but
applied to **every** target (handy in CI). A flag still wins for its target.
- `--out <dir>` / `--layout <template>` / `--mode <mode>` / `--external-std` —
override one target's `[generate]` values. When more than one target is
configured they require `--target` (otherwise it is ambiguous which they
apply to).
- `COMLINE_GENERATE_OUT` / `COMLINE_GENERATE_LAYOUT` / `COMLINE_GENERATE_MODE` /
`COMLINE_GENERATE_EXTERNAL_STD` — the same overrides from the environment,
sitting just below the flags but applied to **every** target (handy in CI).
A flag still wins for its target.
- `--watch` — regenerate on change; see [Watch mode](#watch-mode).

**`external_std` (Rust only).** When on, `std` is referenced as its own
`comline_std` crate (a git dependency `lib` mode adds to the generated
`Cargo.toml` automatically) instead of being re-emitted into the output on
every run — a reference qualifies to `comline_std::http::Request` instead of
a locally-inlined `crate::std::http::Request`. `code` mode has no manifest to
edit on your behalf, so a file that ends up referencing it carries a comment
naming the dependency line to add by hand. Default `false` (inlined,
unchanged).

Currently `rust` and `typescript` are the generators; `code` mode is
implemented for both and `lib` mode for `rust`. `dylib`, and `lib` for other
languages, fail with a clear message.
Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ Examples:
#[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)]
external_std: bool,

/// Regenerate automatically when a schema or `config.idp` changes
#[arg(short, long)]
watch: bool,
Expand Down
14 changes: 14 additions & 0 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
},
},
default_framing: t.default_framing.clone(),
external_std: t.external_std,
};
// Deliberately not `core_err`: a generator's errors are about
// schema/package *structure* (e.g. a namespace that's both a
Expand All @@ -211,6 +212,12 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
})
.collect();
for (namespace, _) in &gv.schemas {
// `external_std` deliberately drops `std`'s own
// schemas from a generator's output (comline-rust#30)
// - expecting a file for them here would defeat that.
if t.external_std && is_std_namespace(namespace) {
continue;
}
let contents = by_namespace.get(namespace.as_str()).ok_or_else(|| {
miette!(
"`{}` generator produced no file for `{namespace}`",
Expand Down Expand Up @@ -257,6 +264,13 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> {
Ok(())
}

/// Whether a `/`-joined namespace is `std` or nested under it -
/// `package::stdlib`'s own namespace-prefixing convention (the same check
/// `comline-rust`'s generator makes to decide what `external_std` drops).
fn is_std_namespace(namespace: &str) -> bool {
namespace == "std" || namespace.starts_with("std/")
}

fn write_file(dest: &Path, contents: &str) -> Result<()> {
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent)
Expand Down
102 changes: 99 additions & 3 deletions src/gen_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub const DEFAULT_OUT: &str = "generated";
pub const DEFAULT_LAYOUT: &str = "{{language}}/{{namespace}}.{{ext}}";
/// Default emit form. Only `"code"` is implemented today.
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;

/// Which package versions to generate bindings for.
///
Expand Down Expand Up @@ -83,6 +86,10 @@ pub struct Generate {
/// (`"jsonrpc"`, `"datagram"`, …); an unknown one leaves the generator's
/// built-in default in place.
pub default_framing: Option<String>,
/// `true`: reference `comline_std` as its own crate instead of inlining
/// it, shared by every target unless overridden. Only a generator that
/// knows how (today, Rust's) acts on it. Default `false`.
pub external_std: Option<bool>,
/// `[[generate.target]]` blocks.
#[serde(rename = "target")]
pub targets: Vec<Target>,
Expand All @@ -103,6 +110,7 @@ pub struct Target {
pub mode: Option<String>,
pub package_versions: Option<VersionSpec>,
pub default_framing: Option<String>,
pub external_std: Option<bool>,
/// `lib` mode only: write the crate straight into `out` instead of
/// `<out>/<language>/`. Target-only (no `[generate]` default, no flag/env)
/// — a shared `out` across several `lib` targets needs the per-language
Expand All @@ -117,6 +125,16 @@ fn env_var(name: &str) -> Option<String> {
std::env::var(name).ok().filter(|s| !s.is_empty())
}

/// Same as [`env_var`], parsed as a bool (`"true"`/`"1"` ⇒ on,
/// `"false"`/`"0"` ⇒ off; anything else unset, same as absent).
fn env_var_bool(name: &str) -> Option<bool> {
match env_var(name)?.as_str() {
"true" | "1" => Some(true),
"false" | "0" => Some(false),
_ => None,
}
}

impl ComlineToml {
/// Load `<work_dir>/comline.toml`; a missing file yields defaults.
pub fn load(work_dir: &Path) -> Result<Self> {
Expand Down Expand Up @@ -147,6 +165,9 @@ 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.
pub external_std: Option<bool>,
}

/// A fully-resolved target: every field decided, `out` made absolute.
Expand All @@ -162,6 +183,8 @@ pub struct ResolvedTarget {
pub default_framing: Option<String>,
/// `lib` mode only — see [`Target::flatten`].
pub flatten: bool,
/// See [`Generate::external_std`].
pub external_std: bool,
}

/// Variables a `layout` template can reference.
Expand Down Expand Up @@ -227,6 +250,7 @@ pub fn resolve(
mode: None,
package_versions: None,
default_framing: None,
external_std: None,
flatten: false,
})
.collect()
Expand All @@ -253,11 +277,12 @@ pub fn resolve(
};
}

let field_override = ov.out.is_some() || ov.layout.is_some() || ov.mode.is_some();
let field_override =
ov.out.is_some() || ov.layout.is_some() || ov.mode.is_some() || ov.external_std.is_some();
if field_override && selected.len() > 1 && ov.target.is_none() {
return Err(miette!(
"`--out` / `--layout` / `--mode` need `--target <lang>` when more \
than one target is configured ({} here)",
"`--out` / `--layout` / `--mode` / `--external-std` need `--target <lang>` \
when more than one target is configured ({} here)",
selected.len()
));
}
Expand All @@ -268,6 +293,7 @@ pub fn resolve(
let env_out = env_var("COMLINE_GENERATE_OUT");
let env_layout = env_var("COMLINE_GENERATE_LAYOUT");
let env_mode = env_var("COMLINE_GENERATE_MODE");
let env_external_std = env_var_bool("COMLINE_GENERATE_EXTERNAL_STD");

let mut resolved = Vec::with_capacity(selected.len());
for t in selected {
Expand All @@ -285,6 +311,19 @@ pub fn resolve(
.unwrap_or_else(|| default.to_owned())
};

// Same precedence, for a bool field.
let pick_bool = |flag: Option<bool>,
env: Option<bool>,
target: Option<bool>,
section: Option<bool>,
default: bool| {
flag.filter(|_| apply_flag)
.or(env)
.or(target)
.or(section)
.unwrap_or(default)
};

let out = pick(ov.out, &env_out, &t.out, &cfg.generate.out, DEFAULT_OUT);
let layout = pick(
ov.layout,
Expand All @@ -300,6 +339,13 @@ pub fn resolve(
&cfg.generate.mode,
DEFAULT_MODE,
);
let external_std = pick_bool(
ov.external_std,
env_external_std,
t.external_std,
cfg.generate.external_std,
DEFAULT_EXTERNAL_STD,
);

let lang_version = t
.lang_version
Expand Down Expand Up @@ -341,6 +387,7 @@ pub fn resolve(
versions,
default_framing,
flatten: t.flatten,
external_std,
});
}

Expand Down Expand Up @@ -446,4 +493,53 @@ mod tests {
let resolved = resolve(&cfg, &declared, Path::new("/x"), &Overrides::default()).unwrap();
assert!(!resolved[0].flatten);
}

#[test]
fn external_std_defaults_to_false() {
let cfg = parse("[generate]\n");
let declared = [DeclaredLang {
language: "rust".into(),
lang_version: "1.75".into(),
}];
let resolved = resolve(&cfg, &declared, Path::new("/x"), &Overrides::default()).unwrap();
assert!(!resolved[0].external_std);
}

#[test]
fn external_std_section_applies_to_every_target() {
let cfg = parse("[generate]\nexternal_std = true\n");
let declared = [DeclaredLang {
language: "rust".into(),
lang_version: "1.75".into(),
}];
let resolved = resolve(&cfg, &declared, Path::new("/x"), &Overrides::default()).unwrap();
assert!(resolved[0].external_std);
}

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

#[test]
fn flag_wins_over_everything_for_external_std() {
let cfg = parse("[generate]\nexternal_std = false\n");
let declared = [DeclaredLang {
language: "rust".into(),
lang_version: "1.75".into(),
}];
let ov = Overrides {
external_std: Some(true),
..Default::default()
};
let resolved = resolve(&cfg, &declared, Path::new("/x"), &ov).unwrap();
assert!(resolved[0].external_std);
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,15 @@ fn run(cli: Cli) -> Result<()> {
out,
layout,
mode,
external_std,
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),
};
commands::generate::run(&work_dir, &overrides, watch)
}
Expand Down
Loading
Loading