portaki-cli 9.7.0

Portaki module CLI (portaki) — init, build, test, hosted sandbox, OCI publish and registry announcement
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
//! `portaki build` — wasm build + manifest + i18n bundle.

use std::path::PathBuf;
use std::process::Command;

use anyhow::{Context, Result};
use clap::Parser;

use crate::manifest::{
    collect_emissions, find_emissions_dir_in, generate_manifest, write_manifest,
    write_migration_bundle, write_operations_bundle,
};
use crate::oci::pack;
use crate::{ui, workspace};

#[derive(Debug, Parser)]
/// Arguments for `portaki build`.
pub struct BuildArgs {
    /// Build in release mode.
    #[arg(long)]
    pub release: bool,
    /// Skip `cargo build` (manifest-only refresh).
    #[arg(long)]
    pub manifest_only: bool,
    /// In a repository holding several modules, the one to build.
    #[arg(long, conflicts_with = "all")]
    pub module: Option<String>,
    /// Build every module of the repository.
    #[arg(long)]
    pub all: bool,

    /// `release` chains straight on to `build`: a second header would look like two commands.
    #[arg(skip)]
    pub nested: bool,
}

/// Runs `portaki build`.
pub async fn run(args: BuildArgs) -> Result<()> {
    if !args.nested {
        ui::header(
            "portaki build",
            &crate::tr!(
                "Compile to wasm32, then turn the SDK's emissions into what the host reads.",
                "Compiler en wasm32, puis transformer les émissions du SDK en ce que l'hôte lit."
            ),
        );
    }
    // `release` has already moved into the module it is publishing.
    if args.nested {
        return build_here(&args).await;
    }
    let chosen = workspace::resolve(args.module.as_deref(), Some(args.all))?;
    for member in &chosen {
        if chosen.len() > 1 {
            ui::rule(&member.id);
        }
        workspace::enter(member)?;
        build_here(&args)
            .await
            .with_context(|| format!("build {}", member.id))?;
    }
    Ok(())
}

/// `portaki build` for the module in the current directory.
async fn build_here(args: &BuildArgs) -> Result<()> {
    let started = std::time::Instant::now();

    let module_root = std::env::current_dir().context("current_dir")?;
    let out_dir = module_root.join("target/portaki");
    std::fs::create_dir_all(&out_dir)?;

    let profile = if args.release { "release" } else { "debug" };

    if args.manifest_only {
        ui::skipped("cargo build skipped (--manifest-only)");
    } else {
        if let Some(written) = ensure_build_script(&module_root)? {
            ui::wrote("build.rs", written);
        }
        let mut cmd = Command::new("cargo");
        cmd.arg("build")
            .arg("--target")
            .arg("wasm32-unknown-unknown");
        if args.release {
            cmd.arg("--release");
        }
        ui::command(
            &format!("compiling wasm32-unknown-unknown ({profile})"),
            &mut cmd,
        )
        .context("cargo build wasm32")?;
    }

    refresh_outputs(&module_root)?;

    if !args.manifest_only {
        let coords = pack::read_module_coordinates(&module_root, &out_dir)?;
        // The profile that was just compiled — not the release one, which a debug build
        // never produced.
        reject_wasm_bindgen(&pack::find_wasm_artifact_in(
            &module_root,
            &coords.id,
            profile,
        )?)?;
    }

    ui::blank();
    ui::detail(crate::tr!(
        "built in {}",
        "compilé en {}",
        ui::elapsed(started.elapsed())
    ));
    if !args.nested {
        ui::next(&[
            (
                "portaki check",
                &crate::tr!(
                    "the gate portaki release applies: tests, manifest, texts",
                    "la porte que portaki release applique : tests, manifeste, textes"
                ),
            ),
            (
                "portaki dev --watch",
                &crate::tr!(
                    "deploy to the sandbox and see what a run does",
                    "déployer en sandbox et voir ce que fait une exécution"
                ),
            ),
        ]);
        ui::blank();
    }
    Ok(())
}

/// Writes a minimal `build.rs` when the module has none: without it Cargo gives no `OUT_DIR`, the
/// macros write nothing, and the wasm32 build stops on `portaki_module!`'s error.
/// An explicit `[package] build = …` is left exactly as it is.
fn ensure_build_script(module_root: &std::path::Path) -> Result<Option<&'static str>> {
    let path = module_root.join("build.rs");
    let cargo = std::fs::read_to_string(module_root.join("Cargo.toml")).unwrap_or_default();
    let declared = cargo.lines().any(|line| {
        line.trim_start().starts_with("build ") || line.trim_start().starts_with("build=")
    });
    if path.exists() || declared {
        return Ok(None);
    }
    std::fs::write(&path, BUILD_SCRIPT).context("write build.rs")?;
    Ok(Some("build.rs"))
}

/// What `portaki build` writes: nothing to do, only to exist.
const BUILD_SCRIPT: &str =
    "// Cargo gives the Portaki macros an OUT_DIR only when the crate has a build script.\n\
fn main() {}\n";

/// Refuses a wasm that expects `wasm-bindgen`.
///
/// The Extism host provides none of those imports: the module would load and then fail at run
/// time, far from here, with a message that does not name the cause. A dependency pulled in
/// without thinking is enough to make them appear — it happened, and the check had lived ever
/// since in one repository's bash script. It belongs in the build.
/// Turns the latest SDK emissions into what the host reads: `manifest.json`, migrations,
/// operations and i18n bundles, and the publish manifest.
///
/// Shared with `portaki dev`, which compiled but never ran this: it shipped whatever manifest
/// the last `portaki build` had left in `target/portaki/`, so a new query or surface stayed
/// invisible in the sandbox until someone thought of running `build` by hand.
pub fn refresh_outputs(module_root: &std::path::Path) -> Result<()> {
    refresh_outputs_from(module_root, &module_root.join("target"))
}

/// The same, reading the emissions from a given `target/` — where a workspace build wrote them.
pub fn refresh_outputs_from(module_root: &std::path::Path, target: &std::path::Path) -> Result<()> {
    let out_dir = module_root.join("target/portaki");
    std::fs::create_dir_all(&out_dir)?;
    let catalog_path = module_root.join("portaki.module.json");

    if let Some(emissions_dir) = find_emissions_dir_in(target, module_root) {
        let emissions = collect_emissions(&emissions_dir)?;
        let i18n_dir = module_root.join("i18n");
        let supported = read_supported_locales(&i18n_dir)
            .unwrap_or_else(|| vec!["fr-FR".to_string(), "en-US".to_string()]);
        let default_locale = pick_default_locale(&supported);

        let sdk_features = pack::sdk_features(module_root)?;
        let mut manifest = generate_manifest(&emissions, &default_locale, &supported)?;
        crate::manifest::imply_storage(&mut manifest, &emissions, &sdk_features);
        let manifest_path = out_dir.join("manifest.json");
        write_manifest(&manifest, &manifest_path)?;
        crate::manifest::catalog::check_references(&emissions, &i18n_dir, &supported)?;
        let mut catalog =
            crate::manifest::catalog::catalog_defaults(&emissions, &i18n_dir, &supported);
        let permissions = crate::manifest::catalog::permissions(&emissions, &sdk_features);
        if !permissions.is_empty() {
            catalog["permissions"] = permissions.into();
        }
        std::fs::write(
            module_root.join(crate::manifest::catalog::BUILT_CATALOG),
            serde_json::to_string_pretty(&catalog)?,
        )?;
        ui::wrote("manifest", relative(&manifest_path, module_root));
        ui::detail(format!(
            "{} entities · {} locales · default {default_locale}",
            manifest.entities.len(),
            supported.len()
        ));

        let schema_version = manifest
            .entities
            .iter()
            .map(|entity| entity.schema_version)
            .max()
            .unwrap_or(1);
        if let Some(bundle_path) =
            write_migration_bundle(module_root, &out_dir, &manifest.id, schema_version)?
        {
            ui::wrote("migrations", relative(&bundle_path, module_root));
            ui::detail(format!(
                "applied on module install to schema module_{}",
                manifest.id.replace('-', "_")
            ));
        }

        let module_version =
            catalog_module_version(&catalog_path).unwrap_or(manifest.version.clone());
        if let Some(bundle_path) = write_operations_bundle(
            &out_dir,
            &manifest.id,
            &module_version,
            schema_version,
            &manifest.entities,
        )? {
            ui::wrote("operations", relative(&bundle_path, module_root));
            ui::detail("v2 — schema.tables for typed-repo upsert");
        }

        if let Some(bundle_path) = bundle_i18n(&i18n_dir, &out_dir.join("i18n.tar.gz"))? {
            ui::wrote("i18n", relative(&bundle_path, module_root));
            ui::detail(supported.join(" "));
        }
    } else if !catalog_path.exists() {
        anyhow::bail!(crate::tr!(
            "no SDK emissions — add portaki_module!(...) to lib.rs",
            "aucune émission du SDK — ajoutez portaki_module!(...) à lib.rs"
        ));
    }

    let publish_path = pack::assemble_publish_manifest(module_root, &out_dir)?;
    ui::wrote("publish", relative(&publish_path, module_root));
    ui::advice(crate::tr!(
        "written from the code — portaki_module!, #[surface], #[email] and the portaki-sdk features",
        "écrit depuis le code — portaki_module!, #[surface], #[email] et les features de portaki-sdk"
    ));
    Ok(())
}

fn reject_wasm_bindgen(wasm: &std::path::Path) -> Result<()> {
    let bytes = std::fs::read(wasm).with_context(|| format!("read {}", wasm.display()))?;
    // Looked for in the raw bytes rather than in a decoded import table: the name appears in
    // clear in the import section, and reading the whole format just for one string would cost
    // yet another wasm parser.
    if bytes
        .windows(WBINDGEN.len())
        .any(|window| window == WBINDGEN)
    {
        anyhow::bail!(
            "{} imports wasm-bindgen — the Extism host provides none of it, so the module \
             would load and then fail at run time. A dependency pulled it in: build with \
             --target wasm32-unknown-unknown only, and check what was added recently",
            wasm.display()
        );
    }
    Ok(())
}

/// The mark `wasm-bindgen` leaves in the import section.
const WBINDGEN: &[u8] = b"__wbindgen";

/// The path as one would retype it: from the module root, not from the root of the disk.
fn relative(path: &std::path::Path, root: &std::path::Path) -> String {
    path.strip_prefix(root)
        .unwrap_or(path)
        .display()
        .to_string()
}

fn catalog_module_version(catalog_path: &std::path::Path) -> Option<String> {
    let raw = std::fs::read_to_string(catalog_path).ok()?;
    let value: serde_json::Value = serde_json::from_str(&raw).ok()?;
    value
        .get("version")
        .and_then(|version| version.as_str())
        .map(str::to_string)
}

/// The locale a module falls back to, which the manifest carries to the platform.
///
/// It used to be whichever file sorted first, which nobody chose and nothing declared. With the two
/// locales every module shipped that quietly meant `en-US`, since `e` precedes `f`. The moment a
/// module gained a third — translating it into the ten languages the picker offers — the default
/// became `ar-SA`, and every host without a matching locale would have been served Arabic.
///
/// French first because it is the reference language these modules are written in; English next,
/// for a module that ever drops French. Alphabetical order remains the last resort, so a module
/// with neither still builds.
fn pick_default_locale(supported: &[String]) -> String {
    for preferred in ["fr-FR", "en-US"] {
        if supported.iter().any(|locale| locale == preferred) {
            return preferred.to_string();
        }
    }
    supported
        .first()
        .cloned()
        .unwrap_or_else(|| "fr-FR".to_string())
}

fn read_supported_locales(i18n_dir: &PathBuf) -> Option<Vec<String>> {
    let entries = std::fs::read_dir(i18n_dir).ok()?;
    let mut locales = Vec::new();
    for entry in entries.flatten() {
        let name = entry.file_name().to_string_lossy().to_string();
        if name.ends_with(".json") {
            locales.push(name.trim_end_matches(".json").to_string());
        }
    }
    if locales.is_empty() {
        None
    } else {
        locales.sort();
        Some(locales)
    }
}

fn bundle_i18n(i18n_dir: &PathBuf, dest: &PathBuf) -> Result<Option<PathBuf>> {
    if !i18n_dir.exists() {
        return Ok(None);
    }
    let file = std::fs::File::create(dest)?;
    let encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default());
    let mut archive = tar::Builder::new(encoder);
    for entry in std::fs::read_dir(i18n_dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) == Some("json") {
            archive.append_path_with_name(&path, path.file_name().unwrap())?;
        }
    }
    archive.finish()?;
    Ok(Some(dest.clone()))
}

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

    fn locales(names: &[&str]) -> Vec<String> {
        let mut sorted: Vec<String> = names.iter().map(|name| name.to_string()).collect();
        // Comme `read_supported_locales` : trié, donc `ar-SA` arrive en tête.
        sorted.sort();
        sorted
    }

    #[test]
    fn french_wins_over_the_alphabet() {
        assert_eq!(pick_default_locale(&locales(&["fr-FR", "en-US"])), "fr-FR");
        assert_eq!(
            pick_default_locale(&locales(&["ar-SA", "de-DE", "en-US", "fr-FR", "ja-JP"])),
            "fr-FR"
        );
    }

    #[test]
    fn english_covers_a_module_without_french() {
        assert_eq!(
            pick_default_locale(&locales(&["ar-SA", "en-US", "zh-CN"])),
            "en-US"
        );
    }

    #[test]
    fn the_alphabet_remains_the_last_resort() {
        assert_eq!(pick_default_locale(&locales(&["de-DE", "zh-CN"])), "de-DE");
        assert_eq!(pick_default_locale(&[]), "fr-FR");
    }

    use super::*;

    #[test]
    fn a_missing_build_script_is_written_once_and_a_declared_one_kept() {
        let module = tempfile::tempdir().unwrap();
        std::fs::write(
            module.path().join("Cargo.toml"),
            "[package]\nname = \"m\"\n",
        )
        .unwrap();
        assert_eq!(
            ensure_build_script(module.path()).unwrap(),
            Some("build.rs")
        );
        let written = std::fs::read_to_string(module.path().join("build.rs")).unwrap();
        assert!(written.contains("fn main() {}"), "{written}");
        assert_eq!(ensure_build_script(module.path()).unwrap(), None);

        let custom = tempfile::tempdir().unwrap();
        std::fs::write(
            custom.path().join("Cargo.toml"),
            "[package]\nname = \"m\"\nbuild = \"tools/build.rs\"\n",
        )
        .unwrap();
        assert_eq!(ensure_build_script(custom.path()).unwrap(), None);
        assert!(!custom.path().join("build.rs").exists());
    }

    /// The check that used to live in a bash script: a wasm that expects `wasm-bindgen` loads
    /// and then fails at run time, far from the build, with a message that does not name the
    /// cause.
    #[test]
    fn a_wasm_importing_wasm_bindgen_is_refused() {
        let directory = tempfile::tempdir().unwrap();
        let wasm = directory.path().join("module.wasm");
        let mut bytes = b"\0asm\x01\0\0\0".to_vec();
        bytes.extend_from_slice(b"__wbindgen_placeholder__");
        std::fs::write(&wasm, bytes).unwrap();

        let refusal = reject_wasm_bindgen(&wasm).unwrap_err().to_string();

        assert!(refusal.contains("wasm-bindgen"));
        assert!(refusal.contains("run time"));
    }

    #[test]
    fn a_plain_wasm_passes() {
        let directory = tempfile::tempdir().unwrap();
        let wasm = directory.path().join("module.wasm");
        std::fs::write(&wasm, b"\0asm\x01\0\0\0kv.get").unwrap();

        assert!(reject_wasm_bindgen(&wasm).is_ok());
    }
}