zoi-rs 1.9.2

Universal Package Manager & Environment Setup Tool
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
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
use crate::{pkg, utils};
use anyhow::{Result, anyhow};
use colored::*;
use mlua::{Lua, LuaSerdeExt, Table};
use std::fs::{self, File};
use std::path::Path;
use tar::Builder as TarBuilder;
use tempfile::Builder;
use walkdir::WalkDir;
use zstd::stream::write::Encoder as ZstdEncoder;

pub fn resolve_build_type(
    requested: Option<&str>,
    supported: &[String],
    pkg_name: &str,
) -> Result<String> {
    if let Some(t) = requested {
        if !supported.iter().any(|s| s == t) {
            return Err(anyhow!(
                "Build type '{}' not supported by package '{}'. Supported types: {:?}",
                t,
                pkg_name,
                supported
            ));
        }
        return Ok(t.to_string());
    }

    if supported.iter().any(|t| t == "pre-compiled") {
        Ok("pre-compiled".to_string())
    } else if supported.iter().any(|t| t == "source") {
        Ok("source".to_string())
    } else if let Some(first) = supported.first() {
        Ok(first.clone())
    } else {
        Err(anyhow!(
            "No build types supported by package '{}'.",
            pkg_name
        ))
    }
}

fn build_for_platform(
    package_file: &Path,
    build_type: Option<&str>,
    platform: &str,
    sign_key: &Option<String>,
    output_dir: Option<&Path>,
    version_override: Option<&str>,
    sub_packages: Option<&Vec<String>>,
    quiet: bool,
    install_deps: bool,
) -> Result<()> {
    let pkg_lua_dir_str = package_file
        .parent()
        .and_then(Path::to_str)
        .ok_or_else(|| anyhow!("Could not get parent directory of package file"))?;
    let pkg_for_meta = pkg::lua::parser::parse_lua_package_for_platform(
        package_file
            .to_str()
            .ok_or_else(|| anyhow!("Path contains invalid UTF-8 characters: {:?}", package_file))?,
        platform,
        version_override,
        quiet,
    )?;

    let resolved_build_type =
        resolve_build_type(build_type, &pkg_for_meta.types, &pkg_for_meta.name)?;

    if install_deps
        && let Some(deps) = &pkg_for_meta.dependencies
        && let Some(build_deps) = &deps.build
    {
        let group = match build_deps {
            pkg::types::BuildDependencies::Group(g) => Some(g),
            pkg::types::BuildDependencies::Typed(t) => t.types.get(&resolved_build_type),
        };

        if let Some(g) = group {
            if !quiet {
                println!("{} Installing build dependencies...", "::".bold().blue());
            }
            let (req_deps, _, _) = pkg::install::resolver::collect_dependencies_for_group(
                g,
                None,
                Some("build"),
                true,
                true,
            )?;

            let processed = std::sync::Mutex::new(std::collections::HashSet::new());
            let mut installed = Vec::new();
            for dep_str in req_deps {
                let dep = pkg::dependencies::parse_dependency_string(&dep_str)?;
                pkg::dependencies::install_dependency(
                    &dep,
                    &pkg_for_meta.name,
                    pkg_for_meta.scope,
                    true,
                    true,
                    &processed,
                    &mut installed,
                    None,
                )?;
            }
        }
    }

    let version = if let Some(v) = version_override {
        v.to_string()
    } else {
        pkg::resolve::get_default_version(&pkg_for_meta, None)?
    };

    let build_dir = Builder::new()
        .prefix(&format!("zoi-build-{}-{}", pkg_for_meta.name, platform))
        .tempdir()?;
    if !quiet {
        println!("Using build directory: {}", build_dir.path().display());
    }
    let staging_dir = build_dir.path().join("staging");
    fs::create_dir_all(&staging_dir)?;

    let subs_to_build = if let Some(subs) = sub_packages {
        subs.clone()
    } else if let Some(subs) = &pkg_for_meta.sub_packages {
        subs.clone()
    } else {
        vec!["".to_string()]
    };

    for sub_package in subs_to_build {
        let sub_pkg_name = if sub_package.is_empty() {
            None
        } else {
            Some(sub_package.as_str())
        };

        if !sub_package.is_empty() && !quiet {
            println!(
                "{} Building sub-package: {}",
                "::".bold().blue(),
                sub_package.cyan()
            );
        }

        let lua = Lua::new();
        pkg::lua::functions::setup_lua_environment(
            &lua,
            platform,
            Some(&version),
            package_file.to_str(),
            None,
            sub_pkg_name,
            quiet,
        )
        .map_err(|e| {
            anyhow!(
                "Failed to setup Lua build environment for '{}': {}",
                package_file.display(),
                e
            )
        })?;
        let pkg_table = lua
            .to_value(&pkg_for_meta)
            .map_err(|e| anyhow!(e.to_string()))?;
        lua.globals()
            .set("PKG", pkg_table)
            .map_err(|e| anyhow!(e.to_string()))?;
        lua.globals()
            .set(
                "BUILD_DIR",
                build_dir
                    .path()
                    .to_str()
                    .expect("build_dir should be valid UTF-8"),
            )
            .map_err(|e| anyhow!(e.to_string()))?;
        lua.globals()
            .set(
                "STAGING_DIR",
                staging_dir
                    .to_str()
                    .expect("staging_dir should be valid UTF-8"),
            )
            .map_err(|e| anyhow!(e.to_string()))?;
        lua.globals()
            .set("BUILD_TYPE", resolved_build_type.as_str())
            .map_err(|e| anyhow!(e.to_string()))?;

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

        let args = lua.create_table().map_err(|e| anyhow!(e.to_string()))?;
        if !sub_package.is_empty() {
            args.set("sub", sub_package.clone())
                .map_err(|e| anyhow!(e.to_string()))?;
        }

        if let Ok(prepare_fn) = lua.globals().get::<mlua::Function>("prepare") {
            if !quiet {
                println!("Running prepare()...");
            }
            prepare_fn.call::<()>(args.clone()).map_err(|e| {
                anyhow!(
                    "The 'prepare' function in '{}' failed for sub-package '{}':\n{}",
                    package_file.display(),
                    sub_package,
                    e
                )
            })?;
        }

        if let Ok(package_fn) = lua.globals().get::<mlua::Function>("package") {
            if !quiet {
                println!("Running package()...");
            }
            package_fn.call::<()>(args.clone()).map_err(|e| {
                anyhow!(
                    "The 'package' function in '{}' failed for sub-package '{}':\n{}",
                    package_file.display(),
                    sub_package,
                    e
                )
            })?;
        }

        if let Ok(build_ops) = lua.globals().get::<Table>("__ZoiBuildOperations") {
            for op in build_ops.sequence_values::<Table>() {
                let op = op.map_err(|e| anyhow!(e.to_string()))?;
                let op_type: String = op.get("op").map_err(|e| anyhow!(e.to_string()))?;

                let data_prefix = if sub_package.is_empty() {
                    "data".to_string()
                } else {
                    format!("data/{}", sub_package)
                };

                match op_type.as_str() {
                    "zcp" => {
                        let source: String =
                            op.get("source").map_err(|e| anyhow!(e.to_string()))?;
                        let mut destination: String =
                            op.get("destination").map_err(|e| anyhow!(e.to_string()))?;

                        let source_path = if source.contains("${pkgluadir}") {
                            Path::new(&source.replace("${pkgluadir}", pkg_lua_dir_str))
                                .to_path_buf()
                        } else {
                            build_dir.path().join(&source)
                        };

                        destination = destination
                            .replace("${pkgstore}", &format!("{}/pkgstore", data_prefix));
                        destination = destination
                            .replace("${createpkgdir}", &format!("{}/createpkgdir", data_prefix));
                        destination =
                            destination.replace("${usrroot}", &format!("{}/usrroot", data_prefix));
                        destination =
                            destination.replace("${usrhome}", &format!("{}/usrhome", data_prefix));

                        let dest_path = staging_dir.join(&destination);

                        if let Some(parent) = dest_path.parent() {
                            fs::create_dir_all(parent)?;
                        }

                        if source_path.is_dir() {
                            for entry in WalkDir::new(&source_path)
                                .into_iter()
                                .filter_map(|e| e.ok())
                            {
                                let target_path =
                                    dest_path.join(entry.path().strip_prefix(&source_path)?);
                                if entry.file_type().is_dir() {
                                    fs::create_dir_all(&target_path)?;
                                } else {
                                    if let Some(p) = target_path.parent() {
                                        fs::create_dir_all(p)?;
                                    }
                                    fs::copy(entry.path(), &target_path)?;
                                }
                            }
                        } else {
                            fs::copy(&source_path, &dest_path)?;
                        }
                        if !quiet {
                            println!("Staged '{}' to '{}'", source, destination);
                        }
                    }
                    "zln" => {
                        let mut target: String =
                            op.get("target").map_err(|e| anyhow!(e.to_string()))?;
                        let mut link: String =
                            op.get("link").map_err(|e| anyhow!(e.to_string()))?;

                        target =
                            target.replace("${pkgstore}", &format!("{}/pkgstore", data_prefix));
                        link = link.replace("${pkgstore}", &format!("{}/pkgstore", data_prefix));
                        link = link
                            .replace("${createpkgdir}", &format!("{}/createpkgdir", data_prefix));
                        link = link.replace("${usrroot}", &format!("{}/usrroot", data_prefix));
                        link = link.replace("${usrhome}", &format!("{}/usrhome", data_prefix));

                        let link_path = staging_dir.join(&link);
                        if let Some(parent) = link_path.parent() {
                            fs::create_dir_all(parent)?;
                        }

                        utils::symlink_file(Path::new(&target), &link_path)?;
                        if !quiet {
                            println!("Created symlink '{}' -> '{}'", link, target);
                        }
                    }
                    "zchmod" => {
                        let mut path: String =
                            op.get("path").map_err(|e| anyhow!(e.to_string()))?;
                        let mode: u32 = op.get("mode").map_err(|e| anyhow!(e.to_string()))?;

                        path = path.replace("${pkgstore}", &format!("{}/pkgstore", data_prefix));
                        path = path
                            .replace("${createpkgdir}", &format!("{}/createpkgdir", data_prefix));
                        path = path.replace("${usrroot}", &format!("{}/usrroot", data_prefix));
                        path = path.replace("${usrhome}", &format!("{}/usrhome", data_prefix));

                        let _full_path = staging_dir.join(&path);
                        #[cfg(unix)]
                        {
                            use std::os::unix::fs::PermissionsExt;
                            fs::set_permissions(_full_path, fs::Permissions::from_mode(mode))?;
                        }
                        if !quiet {
                            println!("Set permissions {} on '{}'", mode, path);
                        }
                    }
                    "zchown" => {
                        let mut path: String =
                            op.get("path").map_err(|e| anyhow!(e.to_string()))?;
                        let owner: String = op.get("owner").map_err(|e| anyhow!(e.to_string()))?;
                        let group: String = op.get("group").map_err(|e| anyhow!(e.to_string()))?;

                        path = path.replace("${pkgstore}", &format!("{}/pkgstore", data_prefix));
                        path = path
                            .replace("${createpkgdir}", &format!("{}/createpkgdir", data_prefix));
                        path = path.replace("${usrroot}", &format!("{}/usrroot", data_prefix));
                        path = path.replace("${usrhome}", &format!("{}/usrhome", data_prefix));

                        let full_path = staging_dir.join(&path);
                        utils::set_path_owner(&full_path, &owner, &group)?;
                        if !quiet {
                            println!("Set ownership {}:{} on '{}'", owner, group, path);
                        }
                    }
                    "zmkdir" => {
                        let mut path: String =
                            op.get("path").map_err(|e| anyhow!(e.to_string()))?;

                        path = path.replace("${pkgstore}", &format!("{}/pkgstore", data_prefix));
                        path = path
                            .replace("${createpkgdir}", &format!("{}/createpkgdir", data_prefix));
                        path = path.replace("${usrroot}", &format!("{}/usrroot", data_prefix));
                        path = path.replace("${usrhome}", &format!("{}/usrhome", data_prefix));

                        let full_path = staging_dir.join(&path);
                        fs::create_dir_all(full_path)?;
                        if !quiet {
                            println!("Created directory '{}'", path);
                        }
                    }
                    _ => {}
                }
            }
        }

        if let Ok(verify_fn) = lua.globals().get::<mlua::Function>("verify") {
            if !quiet {
                println!("Running verify()...");
            }
            let verification_passed: bool = verify_fn.call::<bool>(args.clone()).map_err(|e| {
                anyhow!(
                    "The 'verify' function in '{}' failed for sub-package '{}':\n{}",
                    package_file.display(),
                    sub_package,
                    e
                )
            })?;
            if !verification_passed {
                if !utils::ask_for_confirmation(
                    "Package verification failed. This package may be unsafe. Continue?",
                    false,
                ) {
                    return Err(anyhow!(
                        "Build aborted by user due to verification failure."
                    ));
                }
            } else if !quiet {
                println!("Package verification passed.");
            }
        }
    }

    let mut files_list = Vec::new();
    for entry in WalkDir::new(&staging_dir) {
        let entry = entry?;
        if entry.file_type().is_file()
            && let Ok(relative_path) = entry.path().strip_prefix(&staging_dir)
        {
            files_list.push(relative_path.to_string_lossy().replace('\\', "/"));
        }
    }
    files_list.sort();

    let manifest_content = files_list.join("\n  - ").to_string();
    fs::write(staging_dir.join("manifest.yaml"), manifest_content)?;

    fs::copy(
        package_file,
        staging_dir.join(
            package_file
                .file_name()
                .expect("package_file should have a name"),
        ),
    )?;

    let output_filename = format!("{}-{}-{}.pkg.tar.zst", pkg_for_meta.name, version, platform);
    let output_base = if let Some(dir) = output_dir {
        dir.to_path_buf()
    } else {
        package_file
            .parent()
            .expect("package_file should have a parent directory")
            .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);
        tar_builder.append_dir_all(".", &staging_dir)?;
        tar_builder.finish()?;
    }

    let files_manifest_path = output_path.with_extension("pkg.tar.zst.files");
    fs::write(&files_manifest_path, files_list.join("\n"))?;

    let hash_path = output_path.with_extension("pkg.tar.zst.hash");
    let output_path_str = output_path
        .to_str()
        .ok_or_else(|| anyhow!("Output path contains invalid UTF-8: {:?}", output_path))?;
    let hash = pkg::helper::get_hash(output_path_str, pkg::helper::HashType::Sha512)?;
    fs::write(
        &hash_path,
        format!(
            "{}  {}\n",
            hash,
            output_path
                .file_name()
                .expect("output_path should have a name")
                .to_str()
                .expect("output_filename should be valid UTF-8")
        ),
    )?;

    let size_path = output_path.with_extension("pkg.tar.zst.size");
    let compressed_size = fs::metadata(&output_path)?.len();
    let uncompressed_size: u64 = WalkDir::new(&staging_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .map(|e| e.metadata().map(|m| m.len()).unwrap_or(0))
        .sum();
    fs::write(
        &size_path,
        format!(
            "down: {}\ninstall: {}\n",
            compressed_size, uncompressed_size
        ),
    )?;

    if !quiet {
        println!(
            "{}",
            format!("Successfully built package: {}", output_path.display()).green()
        );
    }

    if let Some(key_id) = sign_key {
        if !quiet {
            println!("Signing package with key '{}'...", key_id.cyan());
        }
        let signature_path = output_path.with_extension("pkg.tar.zst.sig");
        if signature_path.exists() {
            fs::remove_file(&signature_path)?;
        }
        pkg::pgp::sign_detached(&output_path, &signature_path, key_id)?;
        if !quiet {
            println!(
                "{}",
                format!(
                    "Successfully created signature: {}",
                    signature_path.display()
                )
                .green()
            );
        }
    }

    Ok(())
}

pub fn run(
    package_file: &Path,
    build_type: Option<&str>,
    platforms: &[String],
    sign_key: Option<String>,
    output_dir: Option<&Path>,
    version_override: Option<&str>,
    sub_packages: Option<Vec<String>>,
    quiet: bool,
    install_deps: bool,
    method: &str,
    image: Option<&str>,
) -> Result<()> {
    if method == "docker" {
        let docker_image = image.ok_or_else(|| {
            anyhow!("An image must be specified when using the 'docker' build method.")
        })?;
        return super::docker::run(
            package_file,
            build_type,
            platforms,
            sign_key,
            output_dir,
            version_override,
            sub_packages,
            docker_image,
            install_deps,
        );
    }

    if !quiet {
        println!("Building package from: {}", package_file.display());
    }

    let platforms_to_build: Vec<String> = if platforms.contains(&"current".to_string()) {
        let mut p = platforms.to_vec();
        p.retain(|x| x != "current");
        p.push(utils::get_platform()?);
        p
    } else {
        platforms.to_vec()
    };

    if platforms.contains(&"all".to_string()) {
        return Err(anyhow!(
            "Building for 'all' platforms is not supported in this flow yet. Please specify platforms explicitly."
        ));
    }

    for platform in &platforms_to_build {
        if !quiet {
            println!(
                "{} Building for platform: {}",
                "::".bold().blue(),
                platform.cyan()
            );
        }
        if let Err(e) = build_for_platform(
            package_file,
            build_type,
            platform,
            &sign_key,
            output_dir,
            version_override,
            sub_packages.as_ref(),
            quiet,
            install_deps,
        ) {
            eprintln!(
                "{}: Failed to build for platform {}: {}",
                "Error".red().bold(),
                platform.red(),
                e
            );
        }
    }

    Ok(())
}