crepuscularity-cli 0.10.10

crepus CLI — scaffolding and builds for Crepuscularity (UNSTABLE; in active development).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use std::collections::HashMap;
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};

use clap::{Args, ValueEnum};
use crepuscularity_core::context::{TemplateContext, TemplateValue};
use crepuscularity_core::{DriverCache, Fingerprint};
use crepuscularity_native::{
    generate_native_source, render_component_file_to_ir, render_from_files, render_template_to_ir,
    to_json, to_json_pretty, NativeCodegenTarget,
};
use serde::Deserialize;
use serde_json::Value;

use super::{check_template_size, prepend_kotlin_package};
use crate::error::CrepusCliError;
use crate::ui;

#[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq)]
pub enum CodegenPlatform {
    #[value(name = "swiftui", alias = "swift", alias = "ios")]
    SwiftUi,
    #[value(name = "compose", alias = "kotlin", alias = "android")]
    Compose,
}

impl From<CodegenPlatform> for NativeCodegenTarget {
    fn from(p: CodegenPlatform) -> Self {
        match p {
            CodegenPlatform::SwiftUi => NativeCodegenTarget::SwiftUi,
            CodegenPlatform::Compose => NativeCodegenTarget::Compose,
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IrEnvelope {
    entry: Option<String>,
    files: Option<HashMap<String, String>>,
    template: Option<String>,
    context: Option<Value>,
    component: Option<String>,
    pretty: Option<bool>,
    base_dir: Option<PathBuf>,
}

#[derive(Args, Debug, Clone)]
pub struct IrArgs {
    pub file: Option<PathBuf>,
    #[arg(long)]
    pub component: Option<String>,
    #[arg(long)]
    pub ctx: Option<PathBuf>,
    #[arg(long = "var", value_name = "KEY=VALUE")]
    pub vars: Vec<String>,
    #[arg(long)]
    pub pretty: bool,
    #[arg(long)]
    pub stdin: bool,
    #[arg(long = "stdin-json")]
    pub stdin_json: bool,
    #[arg(long)]
    pub base_dir: Option<PathBuf>,
}

#[derive(Args, Debug, Clone)]
pub struct SyncArgs {
    pub template: PathBuf,
    #[arg(long, default_value = ".")]
    pub dir: PathBuf,
    #[arg(long)]
    pub out: Vec<PathBuf>,
    #[arg(long = "no-defaults")]
    pub no_defaults: bool,
    #[arg(long)]
    pub component: Option<String>,
    #[arg(long)]
    pub ctx: Option<PathBuf>,
    #[arg(long = "var", value_name = "KEY=VALUE")]
    pub vars: Vec<String>,
    #[arg(long)]
    pub pretty: bool,
}

#[derive(Args, Debug, Clone)]
pub struct CodegenArgs {
    pub template: Option<PathBuf>,
    #[arg(long)]
    pub platform: Option<CodegenPlatform>,
    #[arg(long)]
    pub out: Option<PathBuf>,
    #[arg(long)]
    pub view_name: Option<String>,
    #[arg(long)]
    pub component: Option<String>,
    #[arg(long)]
    pub ctx: Option<PathBuf>,
    #[arg(long = "var", value_name = "KEY=VALUE")]
    pub vars: Vec<String>,
}

pub fn parse_kv_vars(vars: &[String]) -> Vec<(String, String)> {
    vars.iter()
        .map(|raw| {
            raw.split_once('=')
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .ok_or_else(|| format!("--var expects key=value, got: {raw}"))
        })
        .collect::<Result<Vec<_>, _>>()
        .unwrap_or_else(|e| ui::error(&e))
}

pub fn run_ir_parsed(parsed: IrArgs) -> Result<String, CrepusCliError> {
    if parsed.stdin && parsed.stdin_json {
        return Err(CrepusCliError::context(
            "--stdin and --stdin-json are mutually exclusive",
        ));
    }

    let mut ctx = TemplateContext::new();
    if let Some(path) = &parsed.ctx {
        load_json_ctx(path, &mut ctx)?;
    }
    for (key, raw) in parse_kv_vars(&parsed.vars) {
        ctx.set(key, parse_var_value(&raw));
    }

    if parsed.stdin_json {
        let mut raw = String::new();
        std::io::stdin()
            .read_to_string(&mut raw)
            .map_err(|e| CrepusCliError::context(format!("read stdin: {e}")))?;
        check_template_size(raw.len()).map_err(CrepusCliError::context)?;
        let env: IrEnvelope = serde_json::from_str(&raw)
            .map_err(|e| CrepusCliError::context(format!("stdin JSON: {e}")))?;
        if let Some(value) = env.context {
            merge_json_ctx(&value, &mut ctx)?;
        }
        if let Some(base_dir) = env.base_dir {
            ctx.base_dir = Some(base_dir);
        }
        let pretty = env.pretty.unwrap_or(parsed.pretty);
        let ir = if let (Some(files), Some(entry)) = (env.files, env.entry) {
            render_from_files(&files, &entry, &ctx)
                .map_err(|e| CrepusCliError::context(e.to_string()))?
        } else if let Some(template) = env.template {
            if let Some(component) = env.component {
                render_component_file_to_ir(&template, &component, &ctx)
                    .map_err(|e| CrepusCliError::context(e.to_string()))?
            } else {
                render_template_to_ir(&template, &ctx)
                    .map_err(|e| CrepusCliError::context(e.to_string()))?
            }
        } else {
            return Err(CrepusCliError::context(
                "stdin JSON must include files+entry or template",
            ));
        };
        return stringify_ir(&ir, pretty);
    }

    if parsed.stdin {
        let mut template = String::new();
        std::io::stdin()
            .read_to_string(&mut template)
            .map_err(|e| CrepusCliError::context(format!("read stdin: {e}")))?;
        check_template_size(template.len()).map_err(CrepusCliError::context)?;
        ctx.base_dir = parsed.base_dir;
        let ir = if let Some(component) = parsed.component {
            render_component_file_to_ir(&template, &component, &ctx)
                .map_err(|e| CrepusCliError::context(e.to_string()))?
        } else {
            render_template_to_ir(&template, &ctx)
                .map_err(|e| CrepusCliError::context(e.to_string()))?
        };
        return stringify_ir(&ir, parsed.pretty);
    }

    let path = parsed.file.ok_or_else(|| {
        CrepusCliError::context("Usage: crepus native ir <file.crepus> [--component Name] [--ctx FILE] [--var k=v] [--pretty]")
    })?;
    let content = fs::read_to_string(&path).map_err(|e| CrepusCliError::io(e, path.clone()))?;
    check_template_size(content.len()).map_err(CrepusCliError::context)?;
    ctx.base_dir = path.parent().map(Path::to_path_buf);
    let ir = if let Some(component) = parsed.component {
        render_component_file_to_ir(&content, &component, &ctx)
            .map_err(|e| CrepusCliError::context(e.to_string()))?
    } else {
        render_template_to_ir(&content, &ctx).map_err(|e| CrepusCliError::context(e.to_string()))?
    };
    stringify_ir(&ir, parsed.pretty)
}

pub fn load_json_ctx(path: &Path, ctx: &mut TemplateContext) -> Result<(), CrepusCliError> {
    let raw = fs::read_to_string(path).map_err(|e| CrepusCliError::io(e, path.to_path_buf()))?;
    let value: Value =
        serde_json::from_str(&raw).map_err(|e| format!("context JSON {}: {e}", path.display()))?;
    merge_json_ctx(&value, ctx)
}

pub fn merge_json_ctx(value: &Value, ctx: &mut TemplateContext) -> Result<(), CrepusCliError> {
    let Some(obj) = value.as_object() else {
        return Err(CrepusCliError::context("context must be a JSON object"));
    };
    for (key, value) in obj {
        ctx.set(key.clone(), json_to_template_value(value)?);
    }
    Ok(())
}

pub fn json_to_template_value(value: &Value) -> Result<TemplateValue, CrepusCliError> {
    match value {
        Value::Null => Ok(TemplateValue::Null),
        Value::Bool(v) => Ok(TemplateValue::Bool(*v)),
        Value::Number(v) => {
            if let Some(n) = v.as_i64() {
                Ok(TemplateValue::Int(n))
            } else if let Some(n) = v.as_f64() {
                Ok(TemplateValue::Float(n))
            } else {
                Err(CrepusCliError::context(format!("unsupported number: {v}")))
            }
        }
        Value::String(v) => Ok(TemplateValue::Str(v.clone())),
        Value::Array(values) => {
            let mut items = Vec::new();
            for item in values {
                match item {
                    Value::Object(obj) => {
                        // Each object becomes a child context
                        let mut child = TemplateContext::new();
                        for (key, value) in obj {
                            child.set(key.clone(), json_to_template_scalar(value)?);
                        }
                        items.push(child);
                    }
                    Value::String(_) | Value::Number(_) | Value::Bool(_) | Value::Null => {
                        // Scalar values become item contexts with "value" key
                        let mut child = TemplateContext::new();
                        child.set("value", json_to_template_scalar(item)?);
                        items.push(child);
                    }
                    _ => return Err(CrepusCliError::context("unsupported array item type")),
                }
            }
            Ok(TemplateValue::List(items))
        }
        Value::Object(_) => Err(CrepusCliError::context(
            "context object values are only supported inside arrays",
        )),
    }
}

pub fn json_to_template_scalar(value: &Value) -> Result<TemplateValue, CrepusCliError> {
    match value {
        Value::Array(_) | Value::Object(_) => Err(CrepusCliError::context(
            "loop item fields must be scalar JSON values",
        )),
        _ => json_to_template_value(value),
    }
}

pub fn parse_var_value(raw: &str) -> TemplateValue {
    match raw {
        "true" => TemplateValue::Bool(true),
        "false" => TemplateValue::Bool(false),
        "null" => TemplateValue::Null,
        _ => raw
            .parse::<i64>()
            .map(TemplateValue::Int)
            .or_else(|_| raw.parse::<f64>().map(TemplateValue::Float))
            .unwrap_or_else(|_| TemplateValue::Str(raw.to_string())),
    }
}

pub fn stringify_ir(
    ir: &crepuscularity_native::ViewIr,
    pretty: bool,
) -> Result<String, CrepusCliError> {
    if pretty {
        to_json_pretty(ir).map_err(|e| CrepusCliError::context(format!("serialize IR: {e}")))
    } else {
        to_json(ir).map_err(|e| CrepusCliError::context(format!("serialize IR: {e}")))
    }
}

pub fn sync_native_fixture_inner(parsed: SyncArgs) -> Result<(), CrepusCliError> {
    if !parsed.dir.exists() {
        return Err(CrepusCliError::context(format!(
            "native scaffold dir not found: {}",
            parsed.dir.display()
        )));
    }
    let root = fs::canonicalize(&parsed.dir).unwrap_or(parsed.dir);
    let template_path = resolve_template_path(&root, &parsed.template);
    let template = fs::read_to_string(&template_path)
        .map_err(|e| CrepusCliError::io(e, template_path.clone()))?;

    let mut ctx = TemplateContext::new();
    if let Some(path) = &parsed.ctx {
        load_json_ctx(path, &mut ctx)?;
    }
    for (key, raw) in parse_kv_vars(&parsed.vars) {
        ctx.set(key, parse_var_value(&raw));
    }
    ctx.base_dir = template_path.parent().map(Path::to_path_buf);

    let component_ref = parsed.component.clone();
    let ir = if let Some(component) = &parsed.component {
        render_component_file_to_ir(&template, component, &ctx)
            .map_err(|e| CrepusCliError::context(e.to_string()))?
    } else {
        render_template_to_ir(&template, &ctx)
            .map_err(|e| CrepusCliError::context(e.to_string()))?
    };
    let mut json = stringify_ir(&ir, parsed.pretty)?;
    if !json.ends_with('\n') {
        json.push('\n');
    }

    // Cache skip: avoid rewriting unchanged fixtures (prevents git dirty
    // markers on `crepus native sync` re-runs).
    let cache = DriverCache::open(&root);
    let fp = Fingerprint::new(&template, component_ref.as_deref(), "native-ir");

    let mut written = 0;
    if !parsed.no_defaults {
        for path in default_fixture_output_paths(&root) {
            if let Some(parent) = path.parent() {
                if parent.exists() {
                    if path.is_file() && cache.is_up_to_date(&fp, &json) {
                        written += 1;
                        continue;
                    }
                    fs::write(&path, &json).map_err(|e| CrepusCliError::io(e, path.clone()))?;
                    written += 1;
                }
            }
        }
    }
    for path in explicit_fixture_output_paths(&root, &parsed.out) {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).map_err(|e| CrepusCliError::io(e, parent.to_path_buf()))?;
        }
        if path.is_file() && cache.is_up_to_date(&fp, &json) {
            written += 1;
            continue;
        }
        fs::write(&path, &json).map_err(|e| CrepusCliError::io(e, path.clone()))?;
        written += 1;
    }
    cache.record(&fp, &json);
    if written == 0 {
        return Err(CrepusCliError::context(format!(
            "no native fixture directories found under {}",
            root.display()
        )));
    }
    ui::success(&format!(
        "synced View IR fixture from {}",
        template_path.display()
    ));
    Ok(())
}

pub fn codegen_native_source_inner(parsed: CodegenArgs) -> Result<PathBuf, CrepusCliError> {
    let template_path = parsed.template.ok_or_else(|| {
        CrepusCliError::context("Usage: crepus native codegen <file.crepus> --platform swiftui|compose --out DIR --view-name NAME")
    })?;
    let platform = parsed
        .platform
        .ok_or_else(|| CrepusCliError::context("--platform swiftui|compose is required"))?;
    let out_dir = parsed
        .out
        .ok_or_else(|| CrepusCliError::context("--out DIR is required"))?;
    let view_name = parsed
        .view_name
        .ok_or_else(|| CrepusCliError::context("--view-name NAME is required"))?;

    let template = fs::read_to_string(&template_path)
        .map_err(|e| CrepusCliError::io(e, template_path.clone()))?;

    let mut ctx = TemplateContext::new();
    if let Some(path) = &parsed.ctx {
        load_json_ctx(path, &mut ctx)?;
    }
    for (key, raw) in parse_kv_vars(&parsed.vars) {
        ctx.set(key, parse_var_value(&raw));
    }
    ctx.base_dir = template_path.parent().map(Path::to_path_buf);

    let ir = if let Some(component) = &parsed.component {
        render_component_file_to_ir(&template, component, &ctx)
            .map_err(|e| CrepusCliError::context(e.to_string()))?
    } else {
        render_template_to_ir(&template, &ctx)
            .map_err(|e| CrepusCliError::context(e.to_string()))?
    };
    let mut source = generate_native_source(&ir, platform.into(), &view_name);
    if !source.ends_with('\n') {
        source.push('\n');
    }
    fs::create_dir_all(&out_dir).map_err(|e| CrepusCliError::io(e, out_dir.clone()))?;
    let ext = match platform {
        CodegenPlatform::SwiftUi => "swift",
        CodegenPlatform::Compose => "kt",
    };
    let path = out_dir.join(format!("{}.{}", view_name, ext));
    fs::write(&path, source).map_err(|e| CrepusCliError::io(e, path.clone()))?;
    if platform == CodegenPlatform::Compose && is_native_shell_android_generated_dir(&out_dir) {
        prepend_kotlin_package(&path);
    }
    ui::success(&format!("generated native source at {}", path.display()));
    Ok(path)
}

pub fn is_native_shell_android_generated_dir(path: &Path) -> bool {
    path.ends_with("android/app/src/main/java/dev/crepuscularity/nativeshell/generated")
}

pub fn resolve_template_path(root: &Path, template: &Path) -> PathBuf {
    if template.is_absolute() {
        return template.to_path_buf();
    }
    let rooted = root.join(template);
    if rooted.exists() {
        rooted
    } else {
        template.to_path_buf()
    }
}

pub fn default_fixture_output_paths(root: &Path) -> Vec<PathBuf> {
    vec![
        root.join("fixture.json"),
        root.join("ios/Sources/NativeShell/fixture.json"),
        root.join("NativeShell/Sources/NativeShell/fixture.json"),
        root.join("android/app/src/main/assets/fixture.json"),
    ]
}

pub fn explicit_fixture_output_paths(root: &Path, explicit: &[PathBuf]) -> Vec<PathBuf> {
    explicit
        .iter()
        .map(|path| {
            if path.is_absolute() {
                path.to_path_buf()
            } else {
                root.join(path)
            }
        })
        .collect()
}