module main import crypto.sha256 import os // a local checkout or git repository is reviewed, built as the invoking // user, then installed as one transaction, including split packages. struct ConfigBuild { pub mut: show_help bool @[long: help; short: h] noconfirm bool @[long: noconfirm] no_pkgbuild_confirm bool @[long: nopkgbuildconfirm] query []string @[tail] } // pull_dir copies a local repo into its build dir as the building user: a // root-owned copy would stop makepkg from touching anything fn pull_dir(user string, src string, dst string) ! { run_as_user(user, ['mkdir', '-p', os.dir(dst)])! run_as_user(user, ['rm', '-rf', dst])! run_as_user(user, ['cp', '-R', src, dst])! } // repo_name is the name a repo goes by: the last part of its path or url, // without a .git suffix. an scp-style git address (user@host:path) counts // from after the colon. empty when the target names nothing, like / or . fn repo_name(source string) string { name := os.base(source.trim_right('/')).all_after_last(':').trim_string_right('.git') return if name in ['', '.', '..', '/'] { '' } else { name } } // is_scp_remote reports scp-style git remotes (user@host:path): a colon that // comes before the first slash, where the colon of a host:port transfer url // or of a plain path with a colon in it would not. git itself accepts both // spellings of that, so the clone does not need the target spelled further fn is_scp_remote(target string) bool { colon := target.index(':') or { return false } // no slash: the colon splits the host from the path slash := target.index('/') or { return true } return colon < slash } // is_git_url reports whether git clone can fetch the target: a transfer url, // a name ending in .git, or an scp-style remote fn is_git_url(target string) bool { return target.ends_with('.git') || target.contains('://') || is_scp_remote(target) } // repo_work_dir is where a b target is pulled and built, and the name the // repo goes by. the dir is the name and a hash of the source, so two repos // of one name (a fork, a mirror, two checkouts) and a pkgbuild package of // that name never share it, and the same repo always lands in the same dir. // a directory is keyed on its real path, so ./x and /abs/x are one repo fn repo_work_dir(user string, target string) !(string, string) { source := if os.is_dir(target) { os.real_path(target) } else { target.trim_right('/') } name := repo_name(source) if name == '' { return error('b: "${target}" names no repo to build') } return name, pkgbuild_build_dir(user, '${name}-${sha256.hexhash(source)[..8]}')! } fn build_recipe_name(dir string) !string { for name in ['PKGBUILD.vile', 'PKGBUILD.scm', 'PKGBUILD.vsh', 'PKGBUILD'] { if os.is_file(os.join_path(dir, name)) { return name } } return error('${dir} has no PKGBUILD, PKGBUILD.vile, PKGBUILD.scm or PKGBUILD.vsh') } fn recipe_review(user string, base string, dir string, source_name string) !ReviewEvent { if source_name == 'PKGBUILD' { return read_review(base, dir) } source := output_as_user(user, ['cat', '--', os.join_path(dir, source_name)])! return ReviewEvent{ package: base, files: [ReviewFile{ name: source_name, text: source }] } } fn build_archives(user string, work_dir string, source_name string) ![]string { if source_name == 'PKGBUILD' { run_makepkg(user, work_dir)! return built_packages(user, work_dir) } mut archives := []string{} recipe := os.join_path(work_dir, source_name) manifest := os.join_path(work_dir, '.vpac-packages') mut args := ['env', 'VPAC_MAKEPKG_ROOT=' + root_path, 'VPAC_MAKEPKG_DB=' + db_path] if os.getenv(env_v) != '' { args << 'VPAC_V=' + os.getenv(env_v) } args << [os.executable(), '--native-build', recipe, manifest] if '--sign' in pkgbuild_makepkg.fields() { args << '--sign' } run_as_user(user, args)! paths := output_as_user(user, ['cat', '--', manifest])! for path in paths.trim_right('\n').split_into_lines() { if !path.ends_with('.pkg.tar.zst') || !os.real_path(path).starts_with(os.real_path(work_dir) + '/') { return error('invalid native package output: ${path}') } archives << path } return archives } // build_repo_target pulls one target, reviews it, builds and installs it. // a target is an existing directory (a checkout, taken as the repo stands) // or a git url to clone; bare names belong to the pkgbuild repos, which the // --pkgbuild installs already cover, so the error points there fn build_repo_target(config ConfigBuild, target string, mut ui TransUI) ! { user := invoker_user()! base, work_dir := repo_work_dir(user, target)! // with --nopkgbuildconfirm nothing is asked, and the review is still // shown: the record of what was built stays in the output ask := !config.no_pkgbuild_confirm ui.pause() run_as_user(user, ['mkdir', '-p', os.dir(work_dir)])! defer { ui.sync() } if os.is_dir(target) { pull_dir(user, target, work_dir)! } else if is_git_url(target) { // git is the one tool a clone cannot happen without: an absent one is // said plainly, below user's name, before anything pulls output_as_user(user, ['git', '--version']) or { return error('git is needed to clone ${target} as user ${user}, but it was not found (or failed): ${err.msg()}') } run_as_user(user, ['rm', '-rf', work_dir])! run_as_user(user, ['git', 'clone', '--depth', '${pkgbuild_depth}', target, work_dir])! } else { return error('b: "${target}" is neither a directory nor a git url; pkgbuild repos are covered by vpac i --pkgbuild ${target}') } source_name := build_recipe_name(work_dir)! review := recipe_review(user, base, work_dir, source_name)! show_review(review, if source_name == 'PKGBUILD' { 'review ${base}' } else { '' }) if ask && !ui.noyes('build and install ${base}?') { return } build_phase(base, 'start') archives := build_archives(user, work_dir, source_name)! build_phase(base, 'done') if archives.len == 0 { return error('b: no package archives produced for ${base}') } install(ConfigInstall{ query: archives noconfirm: config.noconfirm })! } // build_repo is the mode's entry: every query is one repo to build fn build_repo(config ConfigBuild) ! { need_query(config.query, 'repo (a directory or a git url)')! // --noconfirm would take the build question's preset, which is no, and // build nothing with a clean exit: the refusal says so first, as the // pkgbuild installs' does if config.noconfirm && !config.no_pkgbuild_confirm { return error('a build is asked about: drop --noconfirm, or pass --nopkgbuildconfirm, to build ${config.query.join(', ')}') } mut ui := new_trans_ui(noconfirm: config.noconfirm) defer { ui.stop() } for target in config.query { build_repo_target(config, target, mut ui)! } }