2022-06-01 20:34:36 +02:00
|
|
|
module build
|
|
|
|
|
2022-12-16 20:38:26 +01:00
|
|
|
import models { BuildConfig }
|
|
|
|
|
2022-06-01 20:34:36 +02:00
|
|
|
// escape_shell_string escapes any characters that could be interpreted
|
|
|
|
// incorrectly by a shell. The resulting value should be safe to use inside an
|
|
|
|
// echo statement.
|
|
|
|
fn escape_shell_string(s string) string {
|
|
|
|
return s.replace(r'\', r'\\').replace("'", r"'\''")
|
|
|
|
}
|
|
|
|
|
|
|
|
// echo_commands takes a list of shell commands & prepends each one with
|
|
|
|
// an echo call displaying said command.
|
|
|
|
pub fn echo_commands(cmds []string) []string {
|
|
|
|
mut out := []string{cap: 2 * cmds.len}
|
|
|
|
|
|
|
|
for cmd in cmds {
|
|
|
|
out << "echo -e '+ ${escape_shell_string(cmd)}'"
|
|
|
|
out << cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
// create_build_script generates a shell script that builds a given Target.
|
2022-12-12 22:09:57 +01:00
|
|
|
fn create_build_script(address string, config BuildConfig, build_arch string) string {
|
|
|
|
repo_url := '$address/$config.repo'
|
2022-06-01 20:34:36 +02:00
|
|
|
|
2022-06-17 14:31:34 +02:00
|
|
|
mut commands := [
|
2022-06-01 20:34:36 +02:00
|
|
|
// This will later be replaced by a proper setting for changing the
|
|
|
|
// mirrorlist
|
2022-12-12 22:09:57 +01:00
|
|
|
"echo -e '[$config.repo]\\nServer = $address/\$repo/\$arch\\nSigLevel = Optional' >> /etc/pacman.conf"
|
2022-06-01 20:34:36 +02:00
|
|
|
// We need to update the package list of the repo we just added above.
|
|
|
|
// This should however not pull in a lot of packages as long as the
|
|
|
|
// builder image is rebuilt frequently.
|
|
|
|
'pacman -Syu --needed --noconfirm',
|
|
|
|
// makepkg can't run as root
|
|
|
|
'su builder',
|
2022-06-17 14:31:34 +02:00
|
|
|
]
|
|
|
|
|
2022-12-12 22:09:57 +01:00
|
|
|
commands << match config.kind {
|
2022-06-17 14:31:34 +02:00
|
|
|
'git' {
|
2022-12-12 22:09:57 +01:00
|
|
|
if config.branch == '' {
|
2022-06-17 14:31:34 +02:00
|
|
|
[
|
2022-12-12 22:09:57 +01:00
|
|
|
"git clone --single-branch --depth 1 '$config.url' repo",
|
2022-06-17 14:31:34 +02:00
|
|
|
]
|
|
|
|
} else {
|
|
|
|
[
|
2022-12-12 22:09:57 +01:00
|
|
|
"git clone --single-branch --depth 1 --branch $config.branch '$config.url' repo",
|
2022-06-17 14:31:34 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'url' {
|
|
|
|
[
|
|
|
|
'mkdir repo',
|
2022-12-12 22:09:57 +01:00
|
|
|
"curl -o repo/PKGBUILD -L '$config.url'",
|
2022-06-17 14:31:34 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic("Invalid kind. This shouldn't be possible.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 11:21:28 +01:00
|
|
|
commands << if config.path != '' {
|
|
|
|
"cd 'repo/$config.path'"
|
|
|
|
} else {
|
|
|
|
'cd repo'
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:31:34 +02:00
|
|
|
commands << [
|
2022-06-01 20:34:36 +02:00
|
|
|
'makepkg --nobuild --syncdeps --needed --noconfirm',
|
|
|
|
'source PKGBUILD',
|
2022-12-13 19:59:18 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
if !config.force {
|
2022-06-01 20:34:36 +02:00
|
|
|
// The build container checks whether the package is already present on
|
|
|
|
// the server.
|
2022-12-13 19:59:18 +01:00
|
|
|
commands << [
|
|
|
|
'curl -s --head --fail $repo_url/$build_arch/\$pkgname-\$pkgver-\$pkgrel && exit 0',
|
|
|
|
// If the above curl command succeeds, we don't need to rebuild the
|
|
|
|
// package. However, because we're in a su shell, the exit command will
|
|
|
|
// drop us back into the root shell. Therefore, we must check whether
|
|
|
|
// we're in root so we don't proceed.
|
|
|
|
'[ "\$(id -u)" == 0 ] && exit 0',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
commands << [
|
2022-12-15 23:47:41 +01:00
|
|
|
'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" $repo_url/publish; done',
|
2022-06-17 14:31:34 +02:00
|
|
|
]
|
2022-06-01 20:34:36 +02:00
|
|
|
|
2022-06-17 14:31:34 +02:00
|
|
|
return echo_commands(commands).join('\n')
|
2022-06-01 20:34:36 +02:00
|
|
|
}
|