zoi-package 1.25.1

Advanced Package Manager & Environment Orchestrator
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
//! Logic for creating Zoi Source Archives (`.zsa`).
//!
//! This module handles the "bundling" process, which packages a `.pkg.lua`
//! file together with its local assets and fetched upstream sources into
//! a single, self-contained archive. This is useful for offline builds
//! and for distributing source code along with build instructions.

use std::collections::HashSet;
use std::fs::{self, File};
use std::path::{Path, PathBuf};

use anyhow::{Result, anyhow};
use colored::Colorize;
use ignore::gitignore::GitignoreBuilder;
use mlua::{Lua, LuaSerdeExt, Table, Value};
use tar::Builder as TarBuilder;
use tempfile::Builder;
use walkdir::WalkDir;
use zstd::stream::write::Encoder as ZstdEncoder;

/// Bundles a package and its dependencies into a `.zsa` archive.
///
/// # Errors
///
/// Returns an error if:
/// - Parsing the `.pkg.lua` file fails.
/// - Upstream sources cannot be fetched.
/// - The archive cannot be created or signed.
pub fn run(
    package_file: &Path,
    output_dir: Option<&Path>,
    sign: Option<String>,
    version_override: Option<&str>,
    build_type: Option<&str>
) -> Result<()> {
    let pkg_dir = package_file
        .parent()
        .ok_or_else(|| anyhow!("Could not get parent directory"))?;

    // Load .zoiignore if it exists
    let mut ignore_builder = GitignoreBuilder::new(pkg_dir);
    let zoiignore_path = pkg_dir.join(".zoiignore");
    if zoiignore_path.exists()
        && let Some(err) = ignore_builder.add(&zoiignore_path)
    {
        eprintln!("{}: Error parsing .zoiignore: {}", "Warning".yellow(), err);
    }
    let ignore = ignore_builder.build()?;

    let is_ignored = |rel_path: &Path, is_dir: bool| -> bool {
        ignore.matched(rel_path, is_dir).is_ignore()
    };

    println!(
        "{} Bundling package: {}",
        "::".bold().blue(),
        package_file.display()
    );

    let lua = Lua::new();
    let platform = zoi_core::utils::get_platform()?;

    // Initialize global tables for tracking
    let refs_table = lua.create_table().map_err(|e| anyhow!(e.to_string()))?;
    lua.globals()
        .set("__ZoiReferencedFiles", refs_table)
        .map_err(|e| anyhow!(e.to_string()))?;

    // Setup a mocked environment for metadata and asset discovery
    // We run it twice: once to find local assets, and once to actually run
    // prepare() if needed.

    let bundle_type = build_type.unwrap_or("source");

    // Phase 1: Metadata & Local Asset Discovery
    zoi_lua::functions::setup_lua_environment(
        &lua,
        &platform,
        version_override,
        package_file.to_str(),
        None,
        Some("/tmp/mock-build"),
        Some("/tmp/mock-staging"),
        None,
        None,
        Some(bundle_type),
        true // quiet
    )
    .map_err(|e| anyhow!(e.to_string()))?;

    lua.globals()
        .set("BUILD_TYPE", bundle_type)
        .map_err(|e| anyhow!(e.to_string()))?;

    // Mock UTILS.EXTRACT to record local references but avoid downloads (in
    // this phase)
    if let Ok(utils) = lua.globals().get::<Table>("UTILS") {
        let mock_extract = lua
            .create_function(|_, (_source, _out_dir): (String, String)| Ok(()))
            .map_err(|e| anyhow!(e.to_string()))?;
        utils
            .set("EXTRACT", mock_extract)
            .map_err(|e| anyhow!(e.to_string()))?;
    }

    // Mock cmd to avoid shell execution in this phase
    let mock_cmd = lua
        .create_function(|_, _command: String| {
            Ok((String::new(), String::new(), 0))
        })
        .map_err(|e| anyhow!(e.to_string()))?;
    lua.globals()
        .set("cmd", mock_cmd)
        .map_err(|e| anyhow!(e.to_string()))?;

    // Load and execute the package file
    let lua_code = fs::read_to_string(package_file)?;
    lua.load(&lua_code).exec().map_err(|e| {
        anyhow!(
            "Failed to execute Lua package file '{}' for bundling:\n{}",
            package_file.display(),
            e
        )
    })?;

    let args = lua.create_table().map_err(|e| anyhow!(e.to_string()))?;

    // Call lifecycle functions to find ${pkgluadir} references
    if let Ok(pkg_fn) = lua.globals().get::<mlua::Function>("package") {
        let _ = pkg_fn.call::<()>(args.clone());
    }

    let mut files_to_include = HashSet::new();

    // Always include the package file itself
    let pkg_filename = package_file
        .file_name()
        .ok_or_else(|| anyhow!("Invalid package file"))?;
    files_to_include.insert(pkg_filename.to_string_lossy().to_string());

    // Collect from __ZoiReferencedFiles (IMPORT/INCLUDE)
    if let Ok(refs) = lua.globals().get::<Table>("__ZoiReferencedFiles") {
        for val in refs.sequence_values::<String>() {
            files_to_include.insert(val.map_err(|e| anyhow!(e.to_string()))?);
        }
    }

    // Collect from __ZoiBuildOperations (zcp/zln with ${pkgluadir})
    if let Ok(ops) = lua.globals().get::<Table>("__ZoiBuildOperations") {
        for op in ops.sequence_values::<Table>() {
            let op = op.map_err(|e| anyhow!(e.to_string()))?;

            // Check 'source' (used by zcp)
            if let Ok(source) = op.get::<String>("source")
                && let Some(rel) = source.strip_prefix("${pkgluadir}/")
            {
                files_to_include.insert(rel.to_string());
            }

            // Check 'target' (used by zln)
            if let Ok(target) = op.get::<String>("target")
                && let Some(rel) = target.strip_prefix("${pkgluadir}/")
            {
                files_to_include.insert(rel.to_string());
            }
        }
    }

    // Phase 2: Fetching Upstream Sources (Running prepare)
    println!("{} Fetching upstream sources...", "::".bold().blue());
    let fetch_dir = Builder::new().prefix("zoi-bundle-fetch-").tempdir()?;

    // Setup a real environment for prepare()
    let lua_fetch = Lua::new();

    // Initialize package metadata tables for the fetch state
    let pkg_meta_table_f = lua_fetch
        .create_table()
        .map_err(|e| anyhow!(e.to_string()))?;
    let pkg_deps_table_f = lua_fetch
        .create_table()
        .map_err(|e| anyhow!(e.to_string()))?;
    let pkg_updates_table_f = lua_fetch
        .create_table()
        .map_err(|e| anyhow!(e.to_string()))?;
    let pkg_hooks_table_f = lua_fetch
        .create_table()
        .map_err(|e| anyhow!(e.to_string()))?;
    let pkg_service_table_f = lua_fetch
        .create_table()
        .map_err(|e| anyhow!(e.to_string()))?;
    lua_fetch
        .globals()
        .set("__ZoiPackageMeta", pkg_meta_table_f)
        .map_err(|e| anyhow!(e.to_string()))?;
    lua_fetch
        .globals()
        .set("__ZoiPackageDeps", pkg_deps_table_f)
        .map_err(|e| anyhow!(e.to_string()))?;
    lua_fetch
        .globals()
        .set("__ZoiPackageUpdates", pkg_updates_table_f)
        .map_err(|e| anyhow!(e.to_string()))?;
    lua_fetch
        .globals()
        .set("__ZoiPackageHooks", pkg_hooks_table_f)
        .map_err(|e| anyhow!(e.to_string()))?;
    lua_fetch
        .globals()
        .set("__ZoiPackageService", pkg_service_table_f)
        .map_err(|e| anyhow!(e.to_string()))?;

    let pkg_global_table_f = lua_fetch
        .create_table()
        .map_err(|e| anyhow!(e.to_string()))?;
    lua_fetch
        .globals()
        .set("PKG", pkg_global_table_f)
        .map_err(|e| anyhow!(e.to_string()))?;

    zoi_lua::functions::setup_lua_environment(
        &lua_fetch,
        &platform,
        version_override,
        package_file.to_str(),
        None,
        Some(fetch_dir.path().to_str().unwrap_or("")),
        Some("/tmp/mock-staging"),
        None,
        None,
        Some(bundle_type),
        true // quiet
    )
    .map_err(|e| anyhow!(e.to_string()))?;

    lua_fetch
        .globals()
        .set("BUILD_TYPE", bundle_type)
        .map_err(|e| anyhow!(e.to_string()))?;

    lua_fetch
        .globals()
        .set(
            "BUILD_DIR",
            fetch_dir
                .path()
                .to_str()
                .ok_or_else(|| anyhow!("Invalid fetch path"))?
        )
        .map_err(|e| anyhow!(e.to_string()))?;

    // We use the real cmd implementation for fetching
    zoi_lua::api::system::add_cmd_util(&lua_fetch, true)
        .map_err(|e| anyhow!(e.to_string()))?;

    // Reload script in the fetch environment
    lua_fetch.load(&lua_code).exec().map_err(|e| {
        anyhow!(
            "Failed to execute Lua package file '{}' during fetch:\n{}",
            package_file.display(),
            e
        )
    })?;

    if let Ok(prep_fn) = lua_fetch.globals().get::<mlua::Function>("prepare") {
        println!("  Running prepare()...");
        let args_fetch = lua_fetch
            .create_table()
            .map_err(|e| anyhow!(e.to_string()))?;
        prep_fn.call::<()>(args_fetch).map_err(|e| {
            anyhow!(
                "The 'prepare' function in '{}' failed during bundling:\n{}",
                package_file.display(),
                e
            )
        })?;
    }

    // Determine output path
    let pkg_dir = package_file
        .parent()
        .ok_or_else(|| anyhow!("Could not get parent directory"))?;

    let final_pkg_meta: Table = lua
        .globals()
        .get("__ZoiPackageMeta")
        .map_err(|e| anyhow!(e.to_string()))?;
    let pkg_meta: zoi_core::types::Package = lua
        .from_value(Value::Table(final_pkg_meta))
        .map_err(|e| anyhow!(e.to_string()))?;

    let version = version_override
        .map(ToString::to_string)
        .or(pkg_meta.version)
        .unwrap_or_else(|| "unknown".to_string());
    let output_filename = format!("{}-{}.zsa", pkg_meta.name, version);
    let output_base =
        output_dir.map_or_else(|| pkg_dir.to_path_buf(), Path::to_path_buf);
    let output_path = output_base.join(output_filename);

    let file = File::create(&output_path)?;
    let encoder = ZstdEncoder::new(file, 0)?.auto_finish();
    let mut tar_builder = TarBuilder::new(encoder);

    // Include local files
    let mut sorted_files: Vec<_> = files_to_include.into_iter().collect();
    sorted_files.sort();

    for rel_path_str in sorted_files {
        let rel_path = Path::new(&rel_path_str);
        let abs_path = pkg_dir.join(rel_path);
        let is_dir = abs_path.is_dir();

        if is_ignored(rel_path, is_dir) {
            println!("  Ignored: {rel_path_str}");
            continue;
        }

        if abs_path.exists() {
            if is_dir {
                // Manually walk local directories to respect ignores
                // recursively
                let mut it = WalkDir::new(&abs_path).into_iter();
                loop {
                    let entry = match it.next() {
                        None => break,
                        Some(Err(e)) => return Err(e.into()),
                        Some(Ok(e)) => e
                    };
                    let entry_rel = entry.path().strip_prefix(pkg_dir)?;
                    let entry_is_dir = entry.file_type().is_dir();
                    if is_ignored(entry_rel, entry_is_dir) {
                        if entry_is_dir {
                            it.skip_current_dir();
                        }
                        continue;
                    }

                    if entry.file_type().is_file() {
                        tar_builder
                            .append_path_with_name(entry.path(), entry_rel)?;
                        println!("  Included local: {}", entry_rel.display());
                    }
                }
            } else {
                tar_builder.append_path_with_name(&abs_path, &rel_path_str)?;
                println!("  Included local: {rel_path_str}");
            }
        }
    }

    // Include fetched files from BUILD_DIR
    let mut it = WalkDir::new(fetch_dir.path()).into_iter();
    loop {
        let entry = match it.next() {
            None => break,
            Some(Err(e)) => return Err(e.into()),
            Some(Ok(e)) => e
        };

        if entry.depth() == 0 {
            continue;
        }

        let rel_path = entry.path().strip_prefix(fetch_dir.path())?;
        let rel_path_str = rel_path.to_string_lossy();
        let is_dir = entry.file_type().is_dir();

        if is_ignored(rel_path, is_dir) {
            if is_dir {
                it.skip_current_dir();
            }
            println!("  Ignored fetch: {rel_path_str}");
            continue;
        }

        if entry.file_type().is_dir() {
            // We'll add directories as we encounter their files or empty dirs
            continue;
        }

        tar_builder.append_path_with_name(entry.path(), rel_path)?;
        println!("  Included fetch: {rel_path_str}");
    }

    // Mark as a full bundle so build knows to skip prepare
    let mut header = tar::Header::new_gnu();
    header.set_path(".zoi-prepared")?;
    header.set_size(0);
    header.set_cksum();
    tar_builder.append(&header, &[][..])?;

    tar_builder.finish()?;
    println!(
        "{} Successfully created bundle: {}",
        "::".bold().green(),
        output_path.display()
    );

    if let Some(key_id) = sign {
        println!(
            "{} Signing bundle with key '{}'...",
            "::".bold().blue(),
            key_id.cyan()
        );
        let signature_path =
            PathBuf::from(format!("{}.sig", output_path.display()));
        if signature_path.exists() {
            fs::remove_file(&signature_path)?;
        }
        zoi_core::pgp::sign_detached(&output_path, &signature_path, &key_id)?;
        println!(
            "{} Successfully created signature: {}",
            "::".bold().green(),
            signature_path.display()
        );
    }

    Ok(())
}