cargo-telar 0.2.2

Cargo subcommand for Telar apps: dev server with hot reload, and packaging into native installers.
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
//! `cargo telar package` and `cargo telar publish`: shipping workspace crates, each `[telar] library` among them together with the artifact a dependency is compiled from.
//!
//! A library compiled as a dependency is wired read-only from the Plain artifact its package carries, and nothing the consumer runs can produce a missing one or refresh a stale one. So the artifact has to be checked where it can still be fixed: here, against the exact files `cargo package` would put in the `.crate`.
//!
//! Cargo verifies each package of a multi-package call against the other packages of that same call and resolves every other dependency from the registry, so a workspace ships in one call: a plain crate depending on a library, or a library depending on a plain crate, only builds against the in-tree version of the other.

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

use telar_project::BuildFlavour;

use super::bake::bake_workspace;
use super::cli::{PackageArgs, PublishArgs};
use super::config::{find_package_dir, read_package_manifest_in};
use super::transpile::transpile_workspace;

/// A workspace package as `cargo metadata --no-deps` reports it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct WorkspacePackage {
    pub(super) name: String,
    pub(super) dir: PathBuf,
    /// False when `package.publish` is `false` or an empty registry list.
    pub(super) publishable: bool,
}

/// A workspace member an invocation ships; a `library` one declares `library = true` and has its artifact checked before cargo runs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Member {
    pub(super) name: String,
    pub(super) dir: PathBuf,
    pub(super) library: bool,
}

/// The cargo command a release pipeline ends in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Shipment {
    Package,
    Publish,
}

impl Shipment {
    fn subcommand(self) -> &'static str {
        match self {
            Shipment::Package => "package",
            Shipment::Publish => "publish",
        }
    }
}

pub(crate) fn run_package_cmd(args: PackageArgs) {
    let PackageArgs {
        package,
        workspace,
        check,
        cargo_args,
    } = args;
    ship(
        Shipment::Package,
        package.as_deref(),
        workspace,
        check,
        &cargo_args,
    );
}

pub(crate) fn run_publish_cmd(args: PublishArgs) {
    let PublishArgs {
        package,
        workspace,
        dry_run,
        cargo_args,
    } = args;
    ship(
        Shipment::Publish,
        package.as_deref(),
        workspace,
        false,
        &publish_args(dry_run, cargo_args),
    );
}

pub(super) fn publish_args(dry_run: bool, mut cargo_args: Vec<String>) -> Vec<String> {
    if dry_run && !cargo_args.iter().any(|arg| arg == "--dry-run") {
        cargo_args.insert(0, "--dry-run".to_string());
    }
    cargo_args
}

/// The packages of `metadata`, the output of `cargo metadata --no-deps`, in workspace order.
pub(super) fn workspace_packages(metadata: &str) -> Result<Vec<WorkspacePackage>, String> {
    let value: serde_json::Value = serde_json::from_str(metadata)
        .map_err(|e| format!("`cargo metadata` printed something that is not its JSON: {e}"))?;
    let packages = value["packages"]
        .as_array()
        .ok_or("`cargo metadata` printed no `packages` list")?;
    packages
        .iter()
        .map(|package| {
            let name = package["name"]
                .as_str()
                .ok_or("`cargo metadata` printed a package without a name")?;
            let dir = package["manifest_path"]
                .as_str()
                .and_then(|manifest| Path::new(manifest).parent())
                .ok_or_else(|| format!("`cargo metadata` printed no manifest path for `{name}`"))?;
            Ok(WorkspacePackage {
                name: name.to_string(),
                dir: dir.to_path_buf(),
                publishable: !package["publish"].as_array().is_some_and(Vec::is_empty),
            })
        })
        .collect()
}

fn workspace_packages_in(workspace_root: &Path) -> Result<Vec<WorkspacePackage>, String> {
    let output = Command::new("cargo")
        .args([
            "metadata",
            "--format-version",
            "1",
            "--no-deps",
            "--manifest-path",
        ])
        .arg(workspace_root.join("Cargo.toml"))
        .output()
        .map_err(|e| format!("could not run `cargo metadata`: {e}"))?;
    if !output.status.success() {
        return Err(format!(
            "`cargo metadata` failed for {}:\n{}",
            workspace_root.display(),
            String::from_utf8_lossy(&output.stderr).trim_end()
        ));
    }
    workspace_packages(&String::from_utf8_lossy(&output.stdout))
}

/// Bakes and transpiles the workspace when a library is among the selected members, refuses any library that is not ready to ship, then runs `cargo package` or `cargo publish` over every selected member at once, so cargo orders them by their dependencies on each other and verifies each against the in-tree versions of the rest. With `check_only` it stops after the libraries' readiness verdict.
fn ship(
    shipment: Shipment,
    package: Option<&str>,
    workspace: bool,
    check_only: bool,
    cargo_args: &[String],
) {
    let here = find_package_dir(&[]);
    let workspace_root = telar_project::find_workspace_root(&here).unwrap_or_else(|| here.clone());
    let packages = workspace_packages_in(&workspace_root).unwrap_or_else(|message| fail(&message));
    let members = select_members(
        &here,
        &workspace_root,
        &packages,
        shipment,
        package,
        workspace,
    )
    .unwrap_or_else(|message| fail(&message));
    let verb = shipment.subcommand();
    if members.is_empty() {
        eprintln!("[cargo-telar] every workspace member forbids publishing; nothing to {verb}");
        return;
    }
    let libraries: Vec<&Member> = members.iter().filter(|member| member.library).collect();
    if !libraries.is_empty() {
        check_libraries(&workspace_root, &libraries, verb);
    } else if check_only {
        eprintln!("[cargo-telar] no selected member declares `library = true`; nothing to check");
    }
    if check_only {
        return;
    }
    run_cargo(shipment, &workspace_root, &members, cargo_args);
}

/// Bakes and transpiles the workspace, then exits non-zero after naming every one of `libraries` whose package would not carry an artifact that answers for its sources.
fn check_libraries(workspace_root: &Path, libraries: &[&Member], verb: &str) {
    if !bake_workspace() {
        fail(
            "the bake reported errors, so no library can ship an artifact that answers for its sources; fix the errors above and run this again",
        );
    }
    if !transpile_workspace() {
        fail(
            "the transpile failed, so no library can ship an artifact that answers for its sources; fix the errors above and run this again",
        );
    }
    let telar_version = telar_project::resolve_telar_version(workspace_root)
        .unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());

    let mut ready = true;
    for library in libraries {
        let problems = readiness(library, &telar_version);
        if problems.is_empty() {
            eprintln!("[cargo-telar] {}: ready to {verb}", library.name);
            continue;
        }
        ready = false;
        eprintln!(
            "[cargo-telar] error: {} is not ready to {verb}:",
            library.name
        );
        for problem in &problems {
            eprintln!("\n{}", indent(problem));
        }
        eprintln!();
    }
    if !ready {
        std::process::exit(1);
    }
}

/// The members a `cargo telar package` or `publish` invocation ships, out of the workspace's `packages`.
///
/// `--workspace` takes every member that may be published, libraries and plain crates alike, since only one cargo call holding all of them can verify each against the others. Otherwise it is the library called `package`, or else the package `here` is in; a member that is not a library is an error, and so is publishing one that forbids it.
pub(super) fn select_members(
    here: &Path,
    workspace_root: &Path,
    packages: &[WorkspacePackage],
    shipment: Shipment,
    package: Option<&str>,
    workspace: bool,
) -> Result<Vec<Member>, String> {
    if workspace {
        return packages
            .iter()
            .filter(|package| package.publishable)
            .map(|package| {
                Ok(Member {
                    name: package.name.clone(),
                    dir: package.dir.clone(),
                    library: is_library(&package.dir)?,
                })
            })
            .collect();
    }
    let name = match package {
        Some(name) => name.to_string(),
        None => {
            read_package_manifest_in(here)
                .ok_or_else(|| {
                    format!(
                        "{} is not a package: run this in a library's directory, or pass `-p <name>` or `--workspace`",
                        here.display()
                    )
                })?
                .name
        }
    };
    let selected = packages
        .iter()
        .find(|package| package.name == name)
        .ok_or_else(|| {
            format!(
                "no package named `{name}` in the workspace at {}",
                workspace_root.display()
            )
        })?;
    if !is_library(&selected.dir)? {
        return Err(format!(
            "`{name}` is not a telar library, so nothing ships a transpiled artifact for it. A package other crates depend on declares it in {}:\n    [telar]\n    library = true",
            selected
                .dir
                .join(telar_project::MANIFEST_FILENAME)
                .display()
        ));
    }
    if shipment == Shipment::Publish && !selected.publishable {
        return Err(format!(
            "`{name}` cannot be published: `package.publish` is false or an empty list in its Cargo.toml"
        ));
    }
    Ok(vec![Member {
        name,
        dir: selected.dir.clone(),
        library: true,
    }])
}

fn is_library(dir: &Path) -> Result<bool, String> {
    telar_project::TelarManifest::load(dir)
        .map(|manifest| manifest.telar.library)
        .map_err(|e| e.to_string())
}

/// Everything that keeps `library` from being packaged as it stands, each phrased with what fixes it. Empty when the package would carry a complete artifact that answers for its sources.
///
/// The artifact is checked in a copy holding exactly the files `cargo package` lists, so what is judged is what would ship, not what happens to be on disk beside it. When the list misses a file, the copy cannot answer anything the missing file would not explain, so the package directory itself is checked instead and both problems are reported.
fn readiness(library: &Member, telar_version: &str) -> Vec<String> {
    let listed = match packaged_files(&library.dir) {
        Ok(listed) => listed,
        Err(message) => return vec![message],
    };
    let mut problems: Vec<String> = missing_from_package(library, &listed).into_iter().collect();
    if !problems.is_empty() {
        problems.extend(artifact_problems(&library.dir, telar_version));
        return problems;
    }
    let scratch = match crate::runner::scratch::scratch_dir("package") {
        Ok(scratch) => scratch,
        Err(message) => {
            problems.push(message);
            return problems;
        }
    };
    let staged = scratch.path().join(&library.name);
    match stage(&library.dir, &listed, &staged) {
        Ok(()) => problems.extend(artifact_problems(&staged, telar_version)),
        Err(e) => problems.push(format!(
            "could not copy the packaged files to {} to check them: {e}",
            staged.display()
        )),
    }
    problems
}

/// The files `cargo package` would put in the package at `dir`, `/`-separated and relative to it.
///
/// `--allow-dirty` because cargo counts a gitignored file it would package as an uncommitted change, and `.telar/` is gitignored in every telar project: the question here is what the package holds, and whether its sources are committed is asked separately before packaging.
pub(super) fn packaged_files(dir: &Path) -> Result<BTreeSet<String>, String> {
    let output = Command::new("cargo")
        .args([
            "package",
            "--list",
            "--allow-dirty",
            "--quiet",
            "--manifest-path",
        ])
        .arg(dir.join("Cargo.toml"))
        .output()
        .map_err(|e| format!("could not run `cargo package --list`: {e}"))?;
    if !output.status.success() {
        return Err(format!(
            "`cargo package --list` failed for {}:\n{}",
            dir.display(),
            String::from_utf8_lossy(&output.stderr).trim_end()
        ));
    }
    Ok(String::from_utf8_lossy(&output.stdout)
        .lines()
        .map(|line| line.trim().replace('\\', "/"))
        .filter(|line| !line.is_empty())
        .collect())
}

/// The problem with `library`'s `include`, when `listed` leaves out a file a build compiling it as a dependency reads, with the entries that would put each one back.
pub(super) fn missing_from_package(library: &Member, listed: &BTreeSet<String>) -> Option<String> {
    let missing: Vec<String> = telar_project::library_files(&library.dir)
        .into_iter()
        .filter(|file| !listed.contains(file))
        .collect();
    if missing.is_empty() {
        return None;
    }
    let manifest = library.dir.join("Cargo.toml");
    let entries: Vec<String> = telar_project::library_include()
        .into_iter()
        .filter(|entry| {
            missing
                .iter()
                .any(|file| telar_project::library_include_covers(entry, file))
        })
        .collect();
    let mut message = format!(
        "`cargo package` would leave out {} file(s) that a build compiling `{}` as a dependency reads:\n{}",
        missing.len(),
        library.name,
        listing(&missing)
    );
    match declared_include(&manifest) {
        Some(_) => message.push_str(&format!(
            "\nAdd to `include` under `[package]` in {}:\n{}",
            manifest.display(),
            entries
                .iter()
                .map(|entry| format!("    \"{entry}\","))
                .collect::<Vec<_>>()
                .join("\n")
        )),
        None => message.push_str(&format!(
            "\n{} declares no `include`, and without one cargo leaves every dot-directory out of a package, `.telar/` among them. Add under `[package]`, keeping any other file the package needs, such as a build script, in the list:\n{}",
            manifest.display(),
            include_block()
        )),
    }
    Some(message)
}

/// The `include` a library's `Cargo.toml` starts with, as written there.
pub(super) fn include_block() -> String {
    let entries: Vec<String> = telar_project::library_include()
        .into_iter()
        .map(|entry| format!("    \"{entry}\","))
        .collect();
    format!("include = [\n{}\n]", entries.join("\n"))
}

fn declared_include(manifest: &Path) -> Option<Vec<String>> {
    let table: toml::Table = std::fs::read_to_string(manifest).ok()?.parse().ok()?;
    let include = table.get("package")?.get("include")?.as_array()?;
    Some(
        include
            .iter()
            .filter_map(|entry| entry.as_str().map(str::to_string))
            .collect(),
    )
}

fn listing(files: &[String]) -> String {
    const SHOWN: usize = 12;
    let mut lines: Vec<String> = files
        .iter()
        .take(SHOWN)
        .map(|file| format!("    {file}"))
        .collect();
    if files.len() > SHOWN {
        lines.push(format!("    … and {} more", files.len() - SHOWN));
    }
    lines.join("\n")
}

fn stage(dir: &Path, listed: &BTreeSet<String>, staged: &Path) -> std::io::Result<()> {
    let _ = std::fs::remove_dir_all(staged);
    std::fs::create_dir_all(staged)?;
    for file in listed {
        let source = dir.join(file);
        if !source.is_file() {
            continue;
        }
        let target = staged.join(file);
        if let Some(parent) = target.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::copy(&source, &target)?;
    }
    Ok(())
}

/// Why the Plain artifact in `package_dir` would be refused by a build compiling it as a dependency, against `telar_version`: the same handshakes the macro runs — the build index answering for every `.rsx`, the module tree matching the source tree, and the version each artifact was written for — plus the baked catalog and assets against the sources they were baked from.
pub(super) fn artifact_problems(package_dir: &Path, telar_version: &str) -> Vec<String> {
    let mut problems = Vec::new();
    let telar = match telar_project::TelarManifest::load(package_dir) {
        Ok(manifest) => manifest.telar,
        Err(e) => return vec![e.to_string()],
    };
    let prelude = telar.prelude();
    let theme = telar_transpiler::resolve_theme_type(package_dir, &telar);
    let src_dir = package_dir.join("src");
    let plain = BuildFlavour::Plain;
    let generated_dir = telar_project::generated_dir(package_dir, plain);
    let telar_dir = package_dir.join(".telar");
    let assets = telar_project::AssetContext::load(package_dir, &telar, telar_version);
    const RETRANSPILE: &str = "Run `cargo telar transpile` and package again.";

    if !telar_project::find_rsx_files(&src_dir).is_empty() {
        let index_file = format!(".telar/{}", plain.index_filename());
        match telar_project::read_build_index(package_dir, plain) {
            None => problems.push(format!(
                "there is no transpiled artifact: `{index_file}` is missing or unreadable. {RETRANSPILE}"
            )),
            Some(index) if index.format != telar_project::BUILD_ARTIFACT_FORMAT => {
                problems.push(format!(
                    "`{index_file}` is in artifact format {}, and this telar reads format {}. {RETRANSPILE}",
                    index.format,
                    telar_project::BUILD_ARTIFACT_FORMAT
                ))
            }
            Some(index) if index.telar_version != telar_version => problems.push(format!(
                "the `.rsx` were transpiled for telar {}, and this workspace builds telar {telar_version}; a dependency is refused unless the two agree. {RETRANSPILE}",
                index.telar_version
            )),
            Some(index) if !index.was_transpiled_with(prelude) => problems.push(format!(
                "the `.rsx` were transpiled with `prelude = {:?}`, and telar.toml now declares {:?}. {RETRANSPILE}",
                index.prelude,
                prelude
                    .iter()
                    .map(telar_project::PreludeEntry::path)
                    .collect::<Vec<_>>()
            )),
            Some(index) if index.theme.as_deref() != theme.as_deref() => problems.push(format!(
                "the `.rsx` were transpiled against the theme {:?}, and the package now names {theme:?}. {RETRANSPILE}",
                index.theme
            )),
            Some(index)
                if !index.answers_for(
                    &src_dir,
                    &generated_dir,
                    theme.as_deref(),
                    prelude,
                    telar_version,
                ) =>
            {
                problems.push(format!(
                    "the transpiled Rust in `.telar/{}/` no longer answers for `src/`: a `.rsx` was added, removed or edited after the transpile, or a generated file was changed. {RETRANSPILE}",
                    plain.dir_name()
                ))
            }
            Some(index) if index.uses_assets && assets.module_file().is_none() => {
                problems.push(format!(
                    "the transpiled Rust reaches into the baked assets, and `.telar/{}` is missing or unusable. Run `cargo telar bake` and package again.",
                    telar_project::ASSETS_SOURCE_FILENAME
                ))
            }
            Some(_) => {}
        }
    }

    if telar_project::invokes_placement_macro(&src_dir)
        && let Some(stale) =
            telar_project::ModuleTree::discover(&src_dir, &generated_dir, plain.site_file_name())
                .first_difference()
    {
        let shown = telar_project::relative_slashed(stale, package_dir)
            .unwrap_or_else(|| stale.display().to_string());
        problems.push(format!(
            "the module tree file `{shown}` is missing or out of date: a Rust module was added, moved or removed after the transpile. {RETRANSPILE}"
        ));
    }

    problems.extend(catalog_problems(package_dir, &telar_dir, telar_version));
    problems.extend(asset_problems(
        &telar.assets_root(package_dir),
        &telar_dir,
        telar_version,
    ));
    problems.extend(icon_record_problem(package_dir));
    problems
}

/// Why the record of the library's baked icons, which the licence notice of every application built with it is merged from, would not answer for them.
fn icon_record_problem(package_dir: &Path) -> Option<String> {
    telar_baker::read_library_icons(package_dir).err().map(|problem| {
        format!(
            "the icons it baked could not be credited in the licence notice of an application built with it: {problem}. Run `cargo telar bake` and package again."
        )
    })
}

fn catalog_problems(package_dir: &Path, telar_dir: &Path, telar_version: &str) -> Vec<String> {
    const REBAKE: &str = "Run `cargo telar bake` and package again.";
    let index = match telar_project::read_catalog_index(telar_dir) {
        Ok(None) => return Vec::new(),
        Ok(Some(index)) => index,
        Err(e) => {
            return vec![format!(
                "the baked translation catalog cannot be read: {e}. {REBAKE}"
            )];
        }
    };
    if index.format != telar_project::CATALOG_ARTIFACT_FORMAT
        || index.telar_version != telar_version
    {
        return vec![format!(
            "the translation catalog was baked for telar {} in format {}, and this workspace builds telar {telar_version}, which reads format {}. {REBAKE}",
            index.telar_version,
            index.format,
            telar_project::CATALOG_ARTIFACT_FORMAT
        )];
    }
    let catalog = telar_project::CatalogContext::load(package_dir, telar_version);
    if catalog.module_file().is_none() {
        return vec![format!(
            "the translation catalog has an index and no `.telar/{}`. {REBAKE}",
            telar_project::CATALOG_SOURCE_FILENAME
        )];
    }
    catalog.staleness().into_iter().collect()
}

fn asset_problems(assets_root: &Path, telar_dir: &Path, telar_version: &str) -> Vec<String> {
    const REBAKE: &str = "Run `cargo telar bake` and package again.";
    let index = match telar_project::check_artifact(telar_dir, telar_version) {
        Ok(telar_project::ArtifactHandshake::NotBaked) => return Vec::new(),
        Ok(telar_project::ArtifactHandshake::UpToDate(index)) => index,
        Ok(telar_project::ArtifactHandshake::FormatMismatch { found, expected }) => {
            return vec![format!(
                "the baked assets are in format {found}, and this telar reads format {expected}. {REBAKE}"
            )];
        }
        Ok(telar_project::ArtifactHandshake::VersionMismatch { found, expected }) => {
            return vec![format!(
                "the assets were baked for telar {found}, and this workspace builds telar {expected}. {REBAKE}"
            )];
        }
        Err(e) => {
            return vec![format!("the baked assets cannot be read: {e}. {REBAKE}")];
        }
    };
    let mut problems = Vec::new();
    if !telar_dir
        .join(telar_project::ASSETS_SOURCE_FILENAME)
        .is_file()
    {
        problems.push(format!(
            "the baked assets have an index and no `.telar/{}`. {REBAKE}",
            telar_project::ASSETS_SOURCE_FILENAME
        ));
    }
    problems.extend(index.entries.iter().filter_map(|entry| {
        let bytes = std::fs::read(assets_root.join(&entry.path)).ok()?;
        (telar_project::content_hash(&bytes) != entry.hash).then(|| {
            format!(
                "the asset `{}` changed after it was baked, so the package would carry its old contents. {REBAKE}",
                entry.path
            )
        })
    }));
    problems
}

/// Runs `cargo package` or `cargo publish` over every one of `members` in one call, after refusing sources that are not committed the way cargo itself would.
///
/// Cargo refuses a package holding any file git does not have committed, and the gitignored `.telar/` a library has to carry is exactly that, so cargo is asked to allow a dirty tree and the check is made here instead, for plain crates and libraries alike, over every packaged source git does not ignore.
fn run_cargo(shipment: Shipment, workspace_root: &Path, members: &[Member], cargo_args: &[String]) {
    let verb = shipment.subcommand();
    if !cargo_args.iter().any(|arg| arg == "--allow-dirty") {
        let mut dirty_members = Vec::new();
        for member in members {
            let listed = packaged_files(&member.dir).unwrap_or_else(|message| fail(&message));
            let dirty = uncommitted(&member.dir, &listed).unwrap_or_else(|message| {
                fail(&format!(
                    "{message}\nThe committed-sources check could not be made; pass `-- --allow-dirty` to `cargo telar {verb}` to skip it."
                ))
            });
            if !dirty.is_empty() {
                dirty_members.push(format!(
                    "{} file(s) of `{}` have changes not yet committed to git:\n{}",
                    dirty.len(),
                    member.name,
                    listing(&dirty)
                ));
            }
        }
        if !dirty_members.is_empty() {
            fail(&format!(
                "{}\nCommit them, or {verb} anyway by passing `-- --allow-dirty` to `cargo telar {verb}`.",
                dirty_members.join("\n")
            ));
        }
    }
    let names: Vec<&str> = members.iter().map(|member| member.name.as_str()).collect();
    eprintln!("[cargo-telar] cargo {verb} -p {}", names.join(" -p "));
    let status = Command::new("cargo")
        .args(cargo_invocation(
            shipment,
            workspace_root,
            members,
            cargo_args,
        ))
        .status();
    match status {
        Ok(status) if status.success() => {}
        Ok(status) => std::process::exit(status.code().unwrap_or(1)),
        Err(e) => fail(&format!("could not run `cargo {verb}`: {e}")),
    }
}

/// The arguments of the cargo call that ships `members`: always `--allow-dirty`, since the committed-sources check has already been made, a `-p` for each member, then the caller's own arguments.
pub(super) fn cargo_invocation(
    shipment: Shipment,
    workspace_root: &Path,
    members: &[Member],
    cargo_args: &[String],
) -> Vec<String> {
    let mut args = vec![
        shipment.subcommand().to_string(),
        "--allow-dirty".to_string(),
        "--manifest-path".to_string(),
        workspace_root.join("Cargo.toml").display().to_string(),
    ];
    for member in members {
        args.push("-p".to_string());
        args.push(member.name.clone());
    }
    args.extend(
        cargo_args
            .iter()
            .filter(|arg| *arg != "--allow-dirty")
            .cloned(),
    );
    args
}

/// The packaged sources of the package at `dir` that git reports as modified or untracked. Empty outside a git repository, where cargo has nothing to compare against either; any other git failure is an error, since cargo is told `--allow-dirty` and this is the only check left.
fn uncommitted(dir: &Path, listed: &BTreeSet<String>) -> Result<Vec<String>, String> {
    uncommitted_with("git", dir, listed)
}

fn uncommitted_with(
    program: impl AsRef<std::ffi::OsStr>,
    dir: &Path,
    listed: &BTreeSet<String>,
) -> Result<Vec<String>, String> {
    let git = |args: &[&str]| -> Result<Option<String>, String> {
        let output = Command::new(program.as_ref())
            .env("LC_ALL", "C")
            .arg("-C")
            .arg(dir)
            .args(args)
            .output()
            .map_err(|e| format!("could not run `git` to check {}: {e}", dir.display()))?;
        if output.status.success() {
            return Ok(Some(String::from_utf8_lossy(&output.stdout).into_owned()));
        }
        let stderr = String::from_utf8_lossy(&output.stderr);
        if stderr.contains("not a git repository") {
            return Ok(None);
        }
        Err(format!(
            "`git {}` failed in {}: {}",
            args.join(" "),
            dir.display(),
            stderr.trim_end()
        ))
    };
    let Some(prefix) = git(&["rev-parse", "--show-prefix"])? else {
        return Ok(Vec::new());
    };
    let Some(status) = git(&[
        "status",
        "--porcelain",
        "-z",
        "--untracked-files=all",
        "--",
        ".",
    ])?
    else {
        return Ok(Vec::new());
    };
    Ok(uncommitted_sources(&status, prefix.trim(), listed))
}

/// The packaged sources `status` (`git status --porcelain -z` run in the package, whose path from the repository root is `prefix`) reports as changed. The artifact under `.telar/` is regenerated by every transpile and checked for freshness separately, so a library that commits it is not held to the committed copy matching the fresh one.
pub(super) fn uncommitted_sources(
    status: &str,
    prefix: &str,
    listed: &BTreeSet<String>,
) -> Vec<String> {
    changed_paths(status)
        .into_iter()
        .filter_map(|path| path.strip_prefix(prefix))
        .filter(|path| !path.starts_with(".telar/"))
        .filter(|path| listed.contains(*path))
        .map(str::to_string)
        .collect()
}

/// The paths of `git status --porcelain -z`, relative to the repository root. A rename or copy is followed by the path it came from, which is not a change of its own.
fn changed_paths(status: &str) -> Vec<&str> {
    let mut paths = Vec::new();
    let mut entries = status.split('\0').filter(|entry| !entry.is_empty());
    while let Some(entry) = entries.next() {
        let Some(path) = entry.get(3..) else {
            continue;
        };
        paths.push(path);
        if entry.starts_with(['R', 'C']) {
            entries.next();
        }
    }
    paths
}

fn indent(text: &str) -> String {
    text.lines()
        .map(|line| format!("  {line}"))
        .collect::<Vec<_>>()
        .join("\n")
}

fn fail(message: &str) -> ! {
    eprintln!("[cargo-telar] error: {message}");
    std::process::exit(1);
}

#[cfg(test)]
#[path = "library_test.rs"]
mod tests;