crepuscularity-cli 0.7.4

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
//! `crepus native` — Native mobile applications for iOS and Android.
//!
//! Scaffold and build native iOS (SwiftUI) and Android (Jetpack Compose) apps
//! that use **View IR** (`crepuscularity-native::render_template_to_ir`) to
//! render `.crepus` templates.
//!
//! The scaffold is the same source tree as `examples/native-shells/` — a
//! SwiftPM package under `<dir>/ios/` and a Gradle module under
//! `<dir>/android/`, sharing a common `fixture.json`. We *don't* embed the
//! Gradle wrapper jar; users run `gradle wrapper --gradle-version 8.10`
//! (or just open the project in Android Studio, which regenerates it
//! automatically) before the first `./gradlew` invocation.

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

use console::style;

use crate::ui;

pub fn run(args: &[String]) {
    match args.first().map(|s| s.as_str()) {
        Some("new") => {
            let name = args.get(1).map(|s| s.as_str()).unwrap_or_else(|| {
                ui::error("Usage: crepus native new <name>");
            });
            scaffold_native_app(name);
        }
        Some("build") => match args.get(1).map(|s| s.as_str()) {
            Some("ios") => {
                let dir = parse_dir_arg(&args[2..]);
                build_ios(&dir);
            }
            Some("android") => {
                let dir = parse_dir_arg(&args[2..]);
                let flavor = parse_flavor(&args[2..]).unwrap_or_else(|| "Debug".to_string());
                build_android(&dir, &flavor);
            }
            _ => ui::error("Usage: crepus native build ios|android [--dir <path>]"),
        },
        Some("run") => match args.get(1).map(|s| s.as_str()) {
            Some("ios") => {
                let dir = parse_dir_arg(&args[2..]);
                run_ios_help(&dir);
            }
            Some("android") => {
                let dir = parse_dir_arg(&args[2..]);
                let flavor = parse_flavor(&args[2..]).unwrap_or_else(|| "Debug".to_string());
                run_android(&dir, &flavor);
            }
            _ => ui::error("Usage: crepus native run ios|android [--dir <path>]"),
        },
        _ => print_native_usage(),
    }
}

/// Each entry is `(relative path within the scaffold root, file content)`.
///
/// Embedding via `include_str!` keeps the templates next to the published
/// crate source without an explicit `[package].include` list — `cargo
/// publish` walks the source tree by default and picks them up.
const TEMPLATE_FILES: &[(&str, &str)] = &[
    ("README.md", include_str!("../templates/native/README.md")),
    ("fixture.json", include_str!("../templates/native/fixture.json")),
    ("ios/Package.swift", include_str!("../templates/native/ios/Package.swift")),
    (
        "ios/Sources/NativeShell/ViewIrModels.swift",
        include_str!("../templates/native/ios/Sources/NativeShell/ViewIrModels.swift"),
    ),
    (
        "ios/Sources/NativeShell/ViewIrTreeView.swift",
        include_str!("../templates/native/ios/Sources/NativeShell/ViewIrTreeView.swift"),
    ),
    (
        "ios/Sources/NativeShell/fixture.json",
        include_str!("../templates/native/ios/Sources/NativeShell/fixture.json"),
    ),
    (
        "android/build.gradle.kts",
        include_str!("../templates/native/android/build.gradle.kts"),
    ),
    (
        "android/settings.gradle.kts",
        include_str!("../templates/native/android/settings.gradle.kts"),
    ),
    (
        "android/gradle.properties",
        include_str!("../templates/native/android/gradle.properties"),
    ),
    (
        "android/gradle/wrapper/gradle-wrapper.properties",
        include_str!("../templates/native/android/gradle/wrapper/gradle-wrapper.properties"),
    ),
    (
        "android/app/build.gradle.kts",
        include_str!("../templates/native/android/app/build.gradle.kts"),
    ),
    (
        "android/app/src/main/AndroidManifest.xml",
        include_str!("../templates/native/android/app/src/main/AndroidManifest.xml"),
    ),
    (
        "android/app/src/main/assets/fixture.json",
        include_str!("../templates/native/android/app/src/main/assets/fixture.json"),
    ),
    (
        "android/app/src/main/java/dev/crepuscularity/nativeshell/MainActivity.kt",
        include_str!(
            "../templates/native/android/app/src/main/java/dev/crepuscularity/nativeshell/MainActivity.kt"
        ),
    ),
    (
        "android/app/src/main/java/dev/crepuscularity/nativeshell/ViewIr.kt",
        include_str!(
            "../templates/native/android/app/src/main/java/dev/crepuscularity/nativeshell/ViewIr.kt"
        ),
    ),
    (
        "android/app/src/main/java/dev/crepuscularity/nativeshell/ViewIrTree.kt",
        include_str!(
            "../templates/native/android/app/src/main/java/dev/crepuscularity/nativeshell/ViewIrTree.kt"
        ),
    ),
    (
        "android/app/src/main/res/values/themes.xml",
        include_str!("../templates/native/android/app/src/main/res/values/themes.xml"),
    ),
];

fn scaffold_native_app(name: &str) {
    let root = PathBuf::from(name);
    if root.exists() {
        ui::error(&format!(
            "destination '{}' already exists; pick a fresh name or remove it first",
            root.display()
        ));
    }

    for (rel, content) in TEMPLATE_FILES {
        let target = root.join(rel);
        if let Some(parent) = target.parent() {
            fs::create_dir_all(parent).unwrap_or_else(|e| {
                ui::error(&format!("failed to create '{}': {e}", parent.display()));
            });
        }
        fs::write(&target, content).unwrap_or_else(|e| {
            ui::error(&format!("failed to write '{}': {e}", target.display()));
        });
    }

    let gitignore = "# Build outputs and IDE caches kept out of source control.\n\
                     ios/.build/\n\
                     android/.gradle/\n\
                     android/build/\n\
                     android/app/build/\n\
                     android/local.properties\n\
                     .idea/\n\
                     *.iml\n";
    fs::write(root.join(".gitignore"), gitignore).unwrap_or_else(|e| {
        ui::error(&format!("failed to write .gitignore: {e}"));
    });

    ui::success(&format!(
        "scaffolded native app '{}' at '{}'",
        name,
        root.display()
    ));
    eprintln!();
    eprintln!("{}", style("Next steps").dim());
    eprintln!(
        "  iOS:     cd {dir}/ios && swift build              # or open Package.swift in Xcode",
        dir = name
    );
    eprintln!(
        "  Android: cd {dir}/android && gradle wrapper --gradle-version 8.10 && \\\n           ./gradlew :app:assembleDebug",
        dir = name
    );
    eprintln!(
        "  Build via crepus: crepus native build ios --dir {dir}",
        dir = name
    );
    eprintln!(
        "                    crepus native build android --dir {dir}",
        dir = name
    );
}

fn parse_dir_arg(args: &[String]) -> PathBuf {
    for window in args.windows(2) {
        if window[0] == "--dir" {
            return PathBuf::from(&window[1]);
        }
    }
    for arg in args {
        if let Some(rest) = arg.strip_prefix("--dir=") {
            return PathBuf::from(rest);
        }
    }
    PathBuf::from(".")
}

fn parse_flavor(args: &[String]) -> Option<String> {
    for window in args.windows(2) {
        if window[0] == "--flavor" {
            return Some(window[1].clone());
        }
    }
    for arg in args {
        if let Some(rest) = arg.strip_prefix("--flavor=") {
            return Some(rest.to_string());
        }
    }
    None
}

fn build_ios(dir: &Path) {
    let ios_dir = dir.join("ios");
    if !ios_dir.join("Package.swift").exists() {
        ui::error(&format!(
            "no Package.swift at '{}'. Pass --dir <path-to-scaffold-root> if the project lives elsewhere.",
            ios_dir.display()
        ));
    }
    let mut cmd = Command::new("swift");
    cmd.arg("build").current_dir(&ios_dir);
    delegate(cmd, "swift build");
}

fn build_android(dir: &Path, flavor: &str) {
    let android_dir = dir.join("android");
    let gradlew = android_dir.join("gradlew");
    if !android_dir.join("settings.gradle.kts").exists() {
        ui::error(&format!(
            "no settings.gradle.kts at '{}'. Pass --dir <path-to-scaffold-root> if the project lives elsewhere.",
            android_dir.display()
        ));
    }

    let task = format!(":app:assemble{}", capitalize_ascii(flavor));
    let mut cmd = if gradlew.exists() {
        let mut c = Command::new(&gradlew);
        c.current_dir(&android_dir);
        c.arg(&task);
        c
    } else {
        // Fall back to system `gradle`. Print a hint either way so users know
        // why we're not invoking ./gradlew.
        eprintln!(
            "{} no ./gradlew at {}; using system `gradle` (run `gradle wrapper --gradle-version 8.10` to generate the wrapper)",
            style("note:").yellow(),
            gradlew.display()
        );
        let mut c = Command::new("gradle");
        c.current_dir(&android_dir);
        c.arg(&task);
        c
    };
    cmd.arg("--quiet"); // don't drown the user in gradle log spam
    delegate(cmd, "gradle build");
}

fn run_ios_help(dir: &Path) {
    eprintln!(
        "{}",
        style("crepus native run ios — open in Xcode").cyan().bold()
    );
    eprintln!();
    eprintln!("  open {dir}/ios/Package.swift", dir = dir.display());
    eprintln!();
    eprintln!(
        "{} SwiftPM does not run apps directly; opening Package.swift in Xcode lets you pick a simulator and Run.",
        style("note:").yellow()
    );
    eprintln!(
        "{} for a fresh iOS app target with a generated `.xcodeproj`, see `crepus ios new`.",
        style("hint:").dim()
    );
}

fn run_android(dir: &Path, flavor: &str) {
    let android_dir = dir.join("android");
    let gradlew = android_dir.join("gradlew");
    let task = format!(":app:install{}", capitalize_ascii(flavor));

    let mut cmd = if gradlew.exists() {
        let mut c = Command::new(&gradlew);
        c.current_dir(&android_dir);
        c.arg(&task);
        c
    } else {
        let mut c = Command::new("gradle");
        c.current_dir(&android_dir);
        c.arg(&task);
        c
    };
    cmd.arg("--quiet");
    delegate(cmd, "gradle install");

    eprintln!(
        "\n{} APK installed; launch with:\n  adb shell am start -n dev.crepuscularity.nativeshell/.MainActivity",
        style("note:").dim()
    );
}

fn delegate(mut cmd: Command, label: &str) {
    match cmd.status() {
        Ok(status) if status.success() => {}
        Ok(status) => std::process::exit(status.code().unwrap_or(1)),
        Err(e) => ui::error(&format!(
            "failed to invoke `{label}`: {e}. Is the toolchain installed and on PATH?"
        )),
    }
}

fn capitalize_ascii(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars();
    if let Some(c) = chars.next() {
        out.extend(c.to_uppercase());
    }
    out.extend(chars);
    out
}

fn print_native_usage() {
    eprintln!(
        "{}",
        style("crepus native — Native mobile applications (iOS + Android)")
            .cyan()
            .bold()
    );
    eprintln!();
    eprintln!("{}", style("COMMANDS").dim());
    eprintln!(
        "  {}  {}",
        style("new <name>                  ").green(),
        style("scaffold an iOS (SwiftPM) + Android (Gradle) project").dim()
    );
    eprintln!(
        "  {}  {}",
        style("build ios [--dir <path>]    ").green(),
        style("swift build inside <dir>/ios").dim()
    );
    eprintln!(
        "  {}  {}",
        style("build android [--dir P] [--flavor F]").green(),
        style("./gradlew :app:assemble<Flavor> inside <dir>/android").dim()
    );
    eprintln!(
        "  {}  {}",
        style("run ios [--dir <path>]      ").green(),
        style("print Xcode-open instructions for the SwiftPM package").dim()
    );
    eprintln!(
        "  {}  {}",
        style("run android [--dir P]       ").green(),
        style("./gradlew :app:install<Flavor> + adb launch hint").dim()
    );
    eprintln!();
    eprintln!("{}", style("EXAMPLES").dim());
    eprintln!("  crepus native new my-mobile-app");
    eprintln!("  crepus native build ios --dir my-mobile-app");
    eprintln!("  crepus native build android --dir my-mobile-app --flavor Debug");
    eprintln!("  crepus native run android --dir my-mobile-app");
    eprintln!();
    eprintln!(
        "{} Android needs the Gradle wrapper. After scaffolding, run \
         `cd <name>/android && gradle wrapper --gradle-version 8.10` once \
         (or open the project in Android Studio, which regenerates it).",
        style("note:").dim()
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn capitalize_ascii_basic() {
        assert_eq!(capitalize_ascii("debug"), "Debug");
        assert_eq!(capitalize_ascii("Release"), "Release");
        assert_eq!(capitalize_ascii(""), "");
        assert_eq!(capitalize_ascii("a"), "A");
    }

    #[test]
    fn parse_dir_arg_handles_both_styles() {
        let v = vec!["--dir".to_string(), "/tmp/x".to_string()];
        assert_eq!(parse_dir_arg(&v), PathBuf::from("/tmp/x"));
        let v = vec!["--dir=/tmp/y".to_string()];
        assert_eq!(parse_dir_arg(&v), PathBuf::from("/tmp/y"));
        let v: Vec<String> = vec![];
        assert_eq!(parse_dir_arg(&v), PathBuf::from("."));
    }

    #[test]
    fn parse_flavor_handles_both_styles() {
        let v = vec!["--flavor".to_string(), "Release".to_string()];
        assert_eq!(parse_flavor(&v), Some("Release".to_string()));
        let v = vec!["--flavor=Debug".to_string()];
        assert_eq!(parse_flavor(&v), Some("Debug".to_string()));
        let v: Vec<String> = vec![];
        assert_eq!(parse_flavor(&v), None);
    }

    #[test]
    fn template_files_present() {
        // Smoke-test: every embedded file is non-empty so we know `include_str!`
        // is wired correctly to existing template files.
        for (rel, content) in TEMPLATE_FILES {
            assert!(!content.is_empty(), "empty template content at {rel}");
        }
    }

    #[test]
    fn template_files_have_unique_paths() {
        use std::collections::BTreeSet;
        let mut seen = BTreeSet::new();
        for (rel, _) in TEMPLATE_FILES {
            assert!(seen.insert(*rel), "duplicate template entry: {rel}");
        }
    }
}