| 1 | module main |
| 2 | |
| 3 | import crypto.sha256 |
| 4 | import os |
| 5 | |
| 6 | // a local checkout or git repository is reviewed, built as the invoking |
| 7 | // user, then installed as one transaction, including split packages. |
| 8 | struct ConfigBuild { |
| 9 | pub mut: |
| 10 | show_help bool @[long: help; short: h] |
| 11 | noconfirm bool @[long: noconfirm] |
| 12 | no_pkgbuild_confirm bool @[long: nopkgbuildconfirm] |
| 13 | query []string @[tail] |
| 14 | } |
| 15 | |
| 16 | // pull_dir copies a local repo into its build dir as the building user: a |
| 17 | // root-owned copy would stop makepkg from touching anything |
| 18 | fn pull_dir(user string, src string, dst string) ! { |
| 19 | run_as_user(user, ['mkdir', '-p', os.dir(dst)])! |
| 20 | run_as_user(user, ['rm', '-rf', dst])! |
| 21 | run_as_user(user, ['cp', '-R', src, dst])! |
| 22 | } |
| 23 | |
| 24 | // repo_name is the name a repo goes by: the last part of its path or url, |
| 25 | // without a .git suffix. an scp-style git address (user@host:path) counts |
| 26 | // from after the colon. empty when the target names nothing, like / or . |
| 27 | fn repo_name(source string) string { |
| 28 | name := os.base(source.trim_right('/')).all_after_last(':').trim_string_right('.git') |
| 29 | return if name in ['', '.', '..', '/'] { '' } else { name } |
| 30 | } |
| 31 | |
| 32 | // is_scp_remote reports scp-style git remotes (user@host:path): a colon that |
| 33 | // comes before the first slash, where the colon of a host:port transfer url |
| 34 | // or of a plain path with a colon in it would not. git itself accepts both |
| 35 | // spellings of that, so the clone does not need the target spelled further |
| 36 | fn is_scp_remote(target string) bool { |
| 37 | colon := target.index(':') or { return false } |
| 38 | // no slash: the colon splits the host from the path |
| 39 | slash := target.index('/') or { return true } |
| 40 | return colon < slash |
| 41 | } |
| 42 | |
| 43 | // is_git_url reports whether git clone can fetch the target: a transfer url, |
| 44 | // a name ending in .git, or an scp-style remote |
| 45 | fn is_git_url(target string) bool { |
| 46 | return target.ends_with('.git') || target.contains('://') || is_scp_remote(target) |
| 47 | } |
| 48 | |
| 49 | // repo_work_dir is where a b target is pulled and built, and the name the |
| 50 | // repo goes by. the dir is the name and a hash of the source, so two repos |
| 51 | // of one name (a fork, a mirror, two checkouts) and a pkgbuild package of |
| 52 | // that name never share it, and the same repo always lands in the same dir. |
| 53 | // a directory is keyed on its real path, so ./x and /abs/x are one repo |
| 54 | fn repo_work_dir(user string, target string) !(string, string) { |
| 55 | source := if os.is_dir(target) { os.real_path(target) } else { target.trim_right('/') } |
| 56 | name := repo_name(source) |
| 57 | if name == '' { |
| 58 | return error('b: "${target}" names no repo to build') |
| 59 | } |
| 60 | return name, pkgbuild_build_dir(user, '${name}-${sha256.hexhash(source)[..8]}')! |
| 61 | } |
| 62 | |
| 63 | fn build_recipe_name(dir string) !string { |
| 64 | for name in ['PKGBUILD.vile', 'PKGBUILD.scm', 'PKGBUILD.vsh', 'PKGBUILD'] { |
| 65 | if os.is_file(os.join_path(dir, name)) { |
| 66 | return name |
| 67 | } |
| 68 | } |
| 69 | return error('${dir} has no PKGBUILD, PKGBUILD.vile, PKGBUILD.scm or PKGBUILD.vsh') |
| 70 | } |
| 71 | |
| 72 | fn recipe_review(user string, base string, dir string, source_name string) !ReviewEvent { |
| 73 | if source_name == 'PKGBUILD' { return read_review(base, dir) } |
| 74 | source := output_as_user(user, ['cat', '--', os.join_path(dir, source_name)])! |
| 75 | return ReviewEvent{ package: base, files: [ReviewFile{ name: source_name, text: source }] } |
| 76 | } |
| 77 | |
| 78 | fn build_archives(user string, work_dir string, source_name string) ![]string { |
| 79 | if source_name == 'PKGBUILD' { |
| 80 | run_makepkg(user, work_dir)! |
| 81 | return built_packages(user, work_dir) |
| 82 | } |
| 83 | mut archives := []string{} |
| 84 | recipe := os.join_path(work_dir, source_name) |
| 85 | |
| 86 | manifest := os.join_path(work_dir, '.vpac-packages') |
| 87 | mut args := ['env', 'VPAC_MAKEPKG_ROOT=' + root_path, 'VPAC_MAKEPKG_DB=' + db_path] |
| 88 | if os.getenv(env_v) != '' { args << 'VPAC_V=' + os.getenv(env_v) } |
| 89 | args << [os.executable(), '--native-build', recipe, manifest] |
| 90 | if '--sign' in pkgbuild_makepkg.fields() { args << '--sign' } |
| 91 | run_as_user(user, args)! |
| 92 | paths := output_as_user(user, ['cat', '--', manifest])! |
| 93 | for path in paths.trim_right('\n').split_into_lines() { |
| 94 | if !path.ends_with('.pkg.tar.zst') || !os.real_path(path).starts_with(os.real_path(work_dir) + '/') { |
| 95 | return error('invalid native package output: ${path}') |
| 96 | } |
| 97 | archives << path |
| 98 | } |
| 99 | return archives |
| 100 | } |
| 101 | |
| 102 | // build_repo_target pulls one target, reviews it, builds and installs it. |
| 103 | // a target is an existing directory (a checkout, taken as the repo stands) |
| 104 | // or a git url to clone; bare names belong to the pkgbuild repos, which the |
| 105 | // --pkgbuild installs already cover, so the error points there |
| 106 | fn build_repo_target(config ConfigBuild, target string, mut ui TransUI) ! { |
| 107 | user := invoker_user()! |
| 108 | base, work_dir := repo_work_dir(user, target)! |
| 109 | // with --nopkgbuildconfirm nothing is asked, and the review is still |
| 110 | // shown: the record of what was built stays in the output |
| 111 | ask := !config.no_pkgbuild_confirm |
| 112 | ui.pause() |
| 113 | run_as_user(user, ['mkdir', '-p', os.dir(work_dir)])! |
| 114 | defer { ui.sync() } |
| 115 | if os.is_dir(target) { |
| 116 | pull_dir(user, target, work_dir)! |
| 117 | } else if is_git_url(target) { |
| 118 | // git is the one tool a clone cannot happen without: an absent one is |
| 119 | // said plainly, below user's name, before anything pulls |
| 120 | output_as_user(user, ['git', '--version']) or { |
| 121 | return error('git is needed to clone ${target} as user ${user}, but it was not found (or failed): ${err.msg()}') |
| 122 | } |
| 123 | run_as_user(user, ['rm', '-rf', work_dir])! |
| 124 | run_as_user(user, ['git', 'clone', '--depth', '${pkgbuild_depth}', target, work_dir])! |
| 125 | } else { |
| 126 | return error('b: "${target}" is neither a directory nor a git url; pkgbuild repos are covered by vpac i --pkgbuild ${target}') |
| 127 | } |
| 128 | source_name := build_recipe_name(work_dir)! |
| 129 | review := recipe_review(user, base, work_dir, source_name)! |
| 130 | show_review(review, if source_name == 'PKGBUILD' { 'review ${base}' } else { '' }) |
| 131 | if ask && !ui.noyes('build and install ${base}?') { |
| 132 | return |
| 133 | } |
| 134 | build_phase(base, 'start') |
| 135 | archives := build_archives(user, work_dir, source_name)! |
| 136 | build_phase(base, 'done') |
| 137 | if archives.len == 0 { |
| 138 | return error('b: no package archives produced for ${base}') |
| 139 | } |
| 140 | install(ConfigInstall{ |
| 141 | query: archives |
| 142 | noconfirm: config.noconfirm |
| 143 | })! |
| 144 | } |
| 145 | |
| 146 | // build_repo is the mode's entry: every query is one repo to build |
| 147 | fn build_repo(config ConfigBuild) ! { |
| 148 | need_query(config.query, 'repo (a directory or a git url)')! |
| 149 | // --noconfirm would take the build question's preset, which is no, and |
| 150 | // build nothing with a clean exit: the refusal says so first, as the |
| 151 | // pkgbuild installs' does |
| 152 | if config.noconfirm && !config.no_pkgbuild_confirm { |
| 153 | return error('a build is asked about: drop --noconfirm, or pass --nopkgbuildconfirm, to build ${config.query.join(', ')}') |
| 154 | } |
| 155 | mut ui := new_trans_ui(noconfirm: config.noconfirm) |
| 156 | defer { ui.stop() } |
| 157 | for target in config.query { |
| 158 | build_repo_target(config, target, mut ui)! |
| 159 | } |
| 160 | } |
| 161 | |