artifactize 0.5.2

Declare Artifacts and the evals that review them (tests, LLM reviews, human sign-offs), and reuse every verdict while its fingerprints are unchanged.
Documentation
use std::{
    collections::BTreeMap,
    env,
    ffi::OsString,
    io,
    path::{Path, PathBuf},
};

use super::Error;
use crate::{platform, workspace::canonical_target};

pub(super) fn prepare(
    workspace: &Path,
    run_dir: &Path,
) -> Result<(PathBuf, PathBuf, BTreeMap<OsString, OsString>), Error> {
    let workspace = platform::canonicalize(workspace)?;
    if !workspace.is_dir() {
        return Err(io::Error::other("runtime workspace must be a directory").into());
    }
    let output_root = canonical_target(run_dir)?;
    outside_workspace(&workspace, &output_root)?;
    platform::create_private_dir_all(&output_root)?;
    let output_root = platform::canonicalize(&output_root)?;
    outside_workspace(&workspace, &output_root)?;

    let directory = platform::private_tempdir_in("runtime-", &output_root)?;
    let root = directory.path();
    for name in ["output", "tmp", "home", "cache"] {
        platform::create_private_dir(&root.join(name))?;
    }
    let mut environment = BTreeMap::from([
        ("PATH".into(), env::var_os("PATH").unwrap_or_default()),
        (
            "LANG".into(),
            env::var_os("LANG").unwrap_or_else(|| "en_US.UTF-8".into()),
        ),
        ("ARTIFACTIZE_WORKSPACE_DIR".into(), workspace.clone().into()),
    ]);
    for (key, name) in [
        ("ARTIFACTIZE_OUTPUT_DIR", "output"),
        ("ARTIFACTIZE_TMP_DIR", "tmp"),
        ("TMPDIR", "tmp"),
        ("TMP", "tmp"),
        ("TEMP", "tmp"),
        ("HOME", "home"),
        ("XDG_CACHE_HOME", "cache"),
    ] {
        environment.insert(key.into(), root.join(name).into());
    }
    // Windows programs look for their home and caches here, and many cannot start without
    // the system variables.
    #[cfg(windows)]
    {
        for (key, name) in [
            ("USERPROFILE", "home"),
            ("APPDATA", "home"),
            ("LOCALAPPDATA", "cache"),
        ] {
            environment.insert(key.into(), root.join(name).into());
        }
        for key in ["SystemRoot", "ComSpec", "PATHEXT"] {
            if let Some(value) = env::var_os(key) {
                environment.insert(key.into(), value);
            }
        }
    }
    Ok((workspace, directory.keep(), environment))
}

fn outside_workspace(workspace: &Path, output: &Path) -> Result<(), Error> {
    crate::workspace::outside_workspace(workspace, output).map_err(|_| Error::OutputInsideWorkspace)
}