greentic_dev/
pack_temp.rs

1use std::fs;
2use std::path::{Path, PathBuf};
3
4use anyhow::{Context, Result, bail};
5use tempfile::TempDir;
6
7use packc::BuildArgs;
8use packc::build::{self, BuildOptions};
9use packc::runtime;
10
11/// Ensure we have a concrete .gtpack on disk. If the input is already a file, reuse it.
12/// If it is a directory, shell out to packc to build a temporary .gtpack and return its path.
13pub fn materialize_pack_path(input: &Path, verbose: bool) -> Result<(Option<TempDir>, PathBuf)> {
14    let metadata =
15        fs::metadata(input).with_context(|| format!("unable to read input {}", input.display()))?;
16    if metadata.is_file() {
17        return Ok((None, input.to_path_buf()));
18    }
19    if metadata.is_dir() {
20        let temp = TempDir::new().context("failed to create temporary directory for pack build")?;
21        let pack_path = temp.path().join("pack.gtpack");
22        build_packc_temp(input, &pack_path, verbose)?;
23        return Ok((Some(temp), pack_path));
24    }
25    bail!(
26        "input {} is neither a file nor a directory",
27        input.display()
28    );
29}
30
31fn build_packc_temp(source: &Path, gtpack_out: &Path, verbose: bool) -> Result<()> {
32    let _ = verbose;
33    // Use packc library to build a temporary gtpack.
34    let build_args = BuildArgs {
35        input: source.to_path_buf(),
36        component_out: Some(gtpack_out.with_extension("wasm")),
37        manifest: Some(gtpack_out.with_extension("cbor")),
38        sbom: Some(gtpack_out.with_extension("cdx.json")),
39        gtpack_out: Some(gtpack_out.to_path_buf()),
40        dry_run: false,
41        secrets_req: None,
42        default_secret_scope: None,
43        allow_oci_tags: false,
44    };
45    let runtime = runtime::resolve_runtime(None, None, true, None)?;
46    let opts = BuildOptions::from_args(build_args, &runtime)?;
47    build::run(&opts).context("packc build failed for temporary .gtpack")
48}