origin-xtask 0.1.0

Origin maintenance tasks as a library, so a derivative's xtask is three lines and its rules arrive with a version bump.
Documentation
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Machine-checked architecture rules.
//!
//! Documented rules rot. These are the subset of `ARCHITECTURE.md` that can be
//! verified mechanically, so a violation fails CI instead of surviving review.

use std::collections::HashMap;
use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

/// Product names that must never appear inside the platform crates (rule 2).
const PRODUCT_NAMES: &[&str] = &["contoso", "fabrikam", "northwind"];

/// Capability grants that hand the frontend far more than it needs (rule 14).
const FORBIDDEN_PERMISSIONS: &[&str] = &[
    "fs:allow-all",
    "fs:default",
    "shell:allow-execute",
    "shell:allow-spawn",
    "shell:default",
];

pub fn run(root: &Path) -> Result<(), String> {
    let root = root.to_path_buf();
    let mut failures = Vec::new();

    failures.extend(check_layer_dependencies(&root)?);
    failures.extend(check_no_product_names(&root)?);
    failures.extend(check_invoke_is_confined(&root)?);
    failures.extend(check_capabilities(&root)?);
    failures.extend(check_commands_exist(&root)?);
    failures.extend(check_manifest_matches_tauri_config(&root)?);

    if failures.is_empty() {
        println!("architecture rules: ok");
        return Ok(());
    }

    let mut report = String::from("architecture violations:\n");
    for failure in &failures {
        let _ = writeln!(report, "  - {failure}");
    }
    Err(report)
}

/// Rule 1, 5: dependencies point downwards only.
fn check_layer_dependencies(root: &Path) -> Result<Vec<String>, String> {
    let mut failures = Vec::new();
    let dependencies_by_package = workspace_dependencies(root)?;

    for (layer, forbidden) in [
        ("crates", &["tauri", "adapters", "host", "examples"][..]),
        ("adapters", &["adapters", "examples"][..]),
        ("host", &["examples"][..]),
    ] {
        for manifest in crates_in(&root.join(layer))? {
            let package = manifest
                .parent()
                .and_then(|path| path.file_name())
                .map(|name| name.to_string_lossy().into_owned())
                .unwrap_or_default();
            let dependencies = dependencies_by_package
                .get(&package)
                .cloned()
                .unwrap_or_default();

            for dependency in &dependencies {
                // `adapters` may of course depend on themselves being in that layer;
                // what is forbidden is depending on a *different* adapter.
                if layer == "adapters" && dependency == &package {
                    continue;
                }

                let violates = match layer {
                    "crates" => {
                        dependency.starts_with("tauri")
                            || is_workspace_member_of(root, dependency, forbidden)
                    }
                    _ => is_workspace_member_of(root, dependency, forbidden),
                };

                if violates {
                    failures.push(format!(
                        "{layer}/{package} depends on `{dependency}` — {layer} must not \
                         depend on {}",
                        forbidden.join(", ")
                    ));
                }
            }
        }
    }

    Ok(failures)
}

/// Rule 2: the platform never knows which product it serves.
///
/// `origin-xtask` is exempt: it is a build tool, not runtime platform code, and it has
/// to name the products in order to check for them.
fn check_no_product_names(root: &Path) -> Result<Vec<String>, String> {
    let mut failures = Vec::new();
    let tooling = root.join("crates").join("origin-xtask");

    for file in rust_sources(&root.join("crates"))? {
        if file.starts_with(&tooling) {
            continue;
        }

        let contents = read(&file)?.to_lowercase();
        for product in PRODUCT_NAMES {
            // The rule itself is quoted in ARCHITECTURE.md and in ADR text; only code
            // is checked here.
            if contents.contains(product) {
                failures.push(format!(
                    "{} mentions the product `{product}` — platform code must not know \
                     its consumers",
                    relative(root, &file)
                ));
            }
        }
    }

    Ok(failures)
}

/// Rule 15: only `@origin/client` speaks Tauri IPC.
///
/// Scans the whole project, not a fixed list of directories: in a derivative the
/// frontend lives wherever that product put it, and a rule that silently skips it is
/// worse than no rule.
fn check_invoke_is_confined(root: &Path) -> Result<Vec<String>, String> {
    let mut failures = Vec::new();
    // The one package allowed to know the transport. In a derivative it arrives
    // through node_modules, which is never scanned.
    let allowed = root.join("frontend").join("client");

    for file in frontend_sources(root)? {
        if file.starts_with(&allowed) {
            continue;
        }

        let contents = read(&file)?;
        if contents.contains("@tauri-apps/api") {
            failures.push(format!(
                "{} imports `@tauri-apps/api` — go through `@origin/client` instead \
                 (ADR-0010)",
                relative(root, &file)
            ));
        }
    }

    Ok(failures)
}

/// Rule 14: least privilege in every capability file, wherever it lives.
fn check_capabilities(root: &Path) -> Result<Vec<String>, String> {
    let mut failures = Vec::new();

    for file in files_with_extension(root, "json")? {
        if !file
            .parent()
            .is_some_and(|parent| parent.ends_with("capabilities"))
        {
            continue;
        }

        let contents = read(&file)?;
        for permission in FORBIDDEN_PERMISSIONS {
            if contents.contains(permission) {
                failures.push(format!(
                    "{} grants `{permission}` — that is not least privilege (ADR-0007)",
                    relative(root, &file)
                ));
            }
        }
    }

    Ok(failures)
}

/// `app.toml` and `tauri.conf.json` must agree on who this product is.
///
/// They are edited in different situations — a version bump here, a window tweak there
/// — and a mismatch is invisible until a release ships under the wrong version or an
/// installer collides with another application's identifier.
fn check_manifest_matches_tauri_config(root: &Path) -> Result<Vec<String>, String> {
    let mut failures = Vec::new();

    for manifest_path in crate::find_manifests(root)? {
        let project = manifest_path.parent().unwrap_or(root);
        let config_path = project.join("src-tauri").join("tauri.conf.json");

        let config = match read(&config_path) {
            Ok(config) => config,
            Err(error) => {
                failures.push(error);
                continue;
            }
        };
        let config: serde_json::Value = match serde_json::from_str(&config) {
            Ok(config) => config,
            Err(error) => {
                failures.push(format!("{}: {error}", relative(root, &config_path)));
                continue;
            }
        };

        let manifest = match origin_manifest::Manifest::load(&manifest_path) {
            Ok(manifest) => manifest,
            Err(error) => {
                failures.push(error.to_string());
                continue;
            }
        };

        for (field, expected, actual) in [
            ("identifier", &manifest.product.id, config.get("identifier")),
            (
                "productName",
                &manifest.product.name,
                config.get("productName"),
            ),
            ("version", &manifest.product.version, config.get("version")),
        ] {
            let actual = actual
                .and_then(serde_json::Value::as_str)
                .unwrap_or_default();
            if actual != expected {
                failures.push(format!(
                    "{}: `{field}` is `{actual}`, but app.toml says `{expected}`",
                    relative(root, &config_path)
                ));
            }
        }
    }

    Ok(failures)
}

/// Rule 15, second half: every command the frontend calls must exist in Rust.
///
/// The name is a string on one side and a function on the other, so nothing else
/// catches a typo or a renamed command until it fails at runtime in front of a user.
fn check_commands_exist(root: &Path) -> Result<Vec<String>, String> {
    let mut defined = Vec::new();
    for file in rust_sources(root)? {
        let contents = read(&file)?;
        defined.extend(tauri_command_names(&contents));
    }

    let mut failures = Vec::new();
    for file in frontend_sources(root)? {
        let contents = read(&file)?;
        for name in invoked_command_names(&contents) {
            if !defined.contains(&name) {
                failures.push(format!(
                    "{} calls the command `{name}`, which no `#[tauri::command]` defines",
                    relative(root, &file)
                ));
            }
        }
    }

    Ok(failures)
}

/// Names of functions annotated with `#[tauri::command]`.
fn tauri_command_names(contents: &str) -> Vec<String> {
    let mut names = Vec::new();
    let mut annotated = false;

    for line in contents.lines() {
        let line = line.trim();

        if line.starts_with("#[tauri::command") {
            annotated = true;
            continue;
        }

        if annotated {
            if line.starts_with("#[") {
                continue;
            }
            if let Some(name) = line
                .split("fn ")
                .nth(1)
                .and_then(|rest| rest.split(['(', '<', ' ']).next())
            {
                names.push(name.to_owned());
            }
            annotated = false;
        }
    }

    names
}

/// Command names passed to the client's `command("...")` helper.
fn invoked_command_names(contents: &str) -> Vec<String> {
    let mut names = Vec::new();
    let mut remaining = contents;

    while let Some(index) = remaining.find("command") {
        remaining = &remaining[index + "command".len()..];
        let Some(arguments) = remaining.find('(') else {
            break;
        };
        let before_arguments = &remaining[..arguments];
        let plain_call = before_arguments.trim().is_empty();
        let generic_call = before_arguments.trim_start().starts_with('<')
            && before_arguments.trim_end().ends_with('>');
        if !(plain_call || generic_call) {
            continue;
        }

        let rest = &remaining[arguments + 1..];
        let rest = rest.trim_start();
        let Some(quote) = rest
            .chars()
            .next()
            .filter(|quote| matches!(quote, '"' | '\''))
        else {
            continue;
        };
        let value = &rest[quote.len_utf8()..];
        let Some(close) = value.find(quote) else {
            continue;
        };
        names.push(value[..close].to_owned());
    }

    names
}

// ---------------------------------------------------------------------------
// helpers
// ---------------------------------------------------------------------------

/// Whether `name` is a crate living in one of `layers`.
fn is_workspace_member_of(root: &Path, name: &str, layers: &[&str]) -> bool {
    layers.iter().any(|layer| {
        let direct = root.join(layer).join(name).join("Cargo.toml").exists();
        // `examples/demo/src-tauri` does not follow the flat layout.
        let nested =
            root.join(layer).join("demo").join("src-tauri").exists() && name == "origin-demo";
        direct || (*layer == "examples" && nested)
    })
}

fn crates_in(directory: &Path) -> Result<Vec<PathBuf>, String> {
    if !directory.exists() {
        return Ok(Vec::new());
    }

    let mut manifests = Vec::new();
    for entry in read_dir(directory)? {
        let manifest = entry.join("Cargo.toml");
        if manifest.exists() {
            manifests.push(manifest);
        }
    }
    Ok(manifests)
}

/// Every workspace member's dependency names, keyed by package name.
///
/// Backed by `cargo metadata` rather than a manifest's top-level TOML keys: reading
/// only `[dependencies]` and `[build-dependencies]` missed `[dev-dependencies]`,
/// target-specific tables (`[target.'cfg(...)'.dependencies]`), and reported a renamed
/// dependency's local alias (`desktop = { package = "tauri" }`) instead of the crate it
/// actually names — three ways a layering violation could pass this check unnoticed.
/// `cargo metadata` resolves all of that once, for every member, in one call.
fn workspace_dependencies(root: &Path) -> Result<HashMap<String, Vec<String>>, String> {
    let output = Command::new("cargo")
        .args(["metadata", "--no-deps", "--format-version", "1"])
        .current_dir(root)
        .output()
        .map_err(|error| format!("cannot run cargo metadata: {error}"))?;

    if !output.status.success() {
        return Err(format!(
            "cargo metadata failed: {}",
            String::from_utf8_lossy(&output.stderr)
        ));
    }

    let metadata: serde_json::Value = serde_json::from_slice(&output.stdout)
        .map_err(|error| format!("cannot parse cargo metadata output: {error}"))?;

    dependencies_from_metadata(&metadata)
}

/// The parsing half of [`workspace_dependencies`], separated so it can be tested
/// against a fixed `cargo metadata` document instead of a real cargo invocation.
fn dependencies_from_metadata(
    metadata: &serde_json::Value,
) -> Result<HashMap<String, Vec<String>>, String> {
    let packages = metadata
        .get("packages")
        .and_then(serde_json::Value::as_array)
        .ok_or_else(|| "cargo metadata: no `packages` array in its output".to_owned())?;

    let mut by_package = HashMap::new();
    for package in packages {
        let Some(name) = package.get("name").and_then(serde_json::Value::as_str) else {
            continue;
        };

        // The dependency's own name, not `rename` — a `package = "..."` alias is a
        // local name for the crate in *this* manifest's code, not a different crate.
        // No filtering on `kind` (normal/dev/build) or `target`: all of them are real
        // dependency edges this check must see.
        let dependencies = package
            .get("dependencies")
            .and_then(serde_json::Value::as_array)
            .map(|dependencies| {
                dependencies
                    .iter()
                    .filter_map(|dependency| {
                        dependency
                            .get("name")
                            .and_then(serde_json::Value::as_str)
                            .map(str::to_owned)
                    })
                    .collect()
            })
            .unwrap_or_default();

        by_package.insert(name.to_owned(), dependencies);
    }

    Ok(by_package)
}

fn rust_sources(directory: &Path) -> Result<Vec<PathBuf>, String> {
    files_with_extension(directory, "rs")
}

fn frontend_sources(directory: &Path) -> Result<Vec<PathBuf>, String> {
    let mut files = files_with_extension(directory, "ts")?;
    files.extend(files_with_extension(directory, "svelte")?);
    Ok(files)
}

fn files_with_extension(directory: &Path, extension: &str) -> Result<Vec<PathBuf>, String> {
    if !directory.exists() {
        return Ok(Vec::new());
    }

    let mut files = Vec::new();
    for entry in read_dir(directory)? {
        let name = entry
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .into_owned();
        if entry.is_dir() {
            // Never walk into build output or installed packages.
            if matches!(
                name.as_str(),
                "node_modules" | "target" | "dist" | "gen" | ".git"
            ) {
                continue;
            }
            files.extend(files_with_extension(&entry, extension)?);
        } else if entry.extension().is_some_and(|found| found == extension) {
            files.push(entry);
        }
    }
    Ok(files)
}

fn read_dir(directory: &Path) -> Result<Vec<PathBuf>, String> {
    let mut entries: Vec<PathBuf> = fs::read_dir(directory)
        .map_err(|error| format!("cannot read {}: {error}", directory.display()))?
        .filter_map(Result::ok)
        .map(|entry| entry.path())
        .collect();
    entries.sort();
    Ok(entries)
}

fn read(file: &Path) -> Result<String, String> {
    fs::read_to_string(file).map_err(|error| format!("cannot read {}: {error}", file.display()))
}

fn relative(root: &Path, file: &Path) -> String {
    file.strip_prefix(root)
        .unwrap_or(file)
        .to_string_lossy()
        .into_owned()
}

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

    #[test]
    fn command_calls_with_and_without_a_generic_are_found() {
        let source = r#"
            command<Result>("with_result");
            command('without_result');
        "#;

        assert_eq!(
            invoked_command_names(source),
            vec!["with_result", "without_result"]
        );
    }

    /// The three gaps a top-level-TOML-keys check had: a dev-dependency, a
    /// target-specific dependency, and a renamed dependency reported under its real
    /// name rather than its local alias.
    #[test]
    fn dev_target_and_renamed_dependencies_are_all_reported_by_their_real_name() {
        let metadata = serde_json::json!({
            "packages": [
                {
                    "name": "origin-example",
                    "dependencies": [
                        { "name": "origin-core", "rename": null, "kind": null, "target": null },
                        { "name": "tauri", "rename": "desktop", "kind": null, "target": null },
                        { "name": "origin-storage", "rename": null, "kind": "dev", "target": null },
                        {
                            "name": "winapi",
                            "rename": null,
                            "kind": null,
                            "target": "cfg(windows)"
                        }
                    ]
                }
            ]
        });

        let dependencies = dependencies_from_metadata(&metadata).unwrap();

        let mut names = dependencies["origin-example"].clone();
        names.sort();
        assert_eq!(
            names,
            vec!["origin-core", "origin-storage", "tauri", "winapi"]
        );
    }

    #[test]
    fn a_package_with_no_dependencies_field_is_reported_as_having_none() {
        let metadata = serde_json::json!({
            "packages": [{ "name": "origin-leaf" }]
        });

        let dependencies = dependencies_from_metadata(&metadata).unwrap();
        assert_eq!(dependencies["origin-leaf"], Vec::<String>::new());
    }
}