feat: build-ci.sh script

This commit is contained in:
blek 2023-09-15 11:18:23 +10:00
parent 97d916f6b4
commit c7de0120ea
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 227 additions and 0 deletions

227
scripts/build-ci.sh Executable file
View File

@ -0,0 +1,227 @@
#!/bin/bash
# man build-ci.sh
# NAME
# build-ci.sh - An automated tool to build a pacman repository out of PKGBUILDs
#
# SYNOPSIS
# build-ci.sh
#
# DESCRIPTION
# Build the PKGBUILDs into a pacman repository
#
# The PKGBUILDs are assumed to be placed in a similar structure:
#
# ```
# |- $SRC_DIR
# |- (package name)
# |- PKGBUILD
# |- .SRCINFO
# ```
#
# $SRCDIR is $(pwd) by default, but you can set it to your own path like this:
#
# ```bash
# $ SRCDIR=/path/to/src/dir build-ci.sh
# ```
#
# Also this script accepts a number of other arguments to be set via environemt variable:
#
# **SUDO_PASS** - Sudo password to run commands as root (required)
# **MAKE_CACHE** - Directory for cached files (default: $(pwd)/alc_make_cache)
# **TARGET_DIR** - Directory to output the repository (default: $(pwd)/target)
# **SRC_DIR** - Directory to take packages from (default: $(pwd)/packages)
# **CLEAN_MAKE_DIR** - Whether to clean the cache directory (default: true)
# DEPENDENCIES
# This script has the following dependencies:
# bash, [, wc, rm, mkdir, mkarchroot
#
# The arch packages for these commands are:
# `coreutils devtools`
# ---- Vars & Configs ----- #
# Sudo password is required
SUDO_PASS=${SUDO_PASS}
# The cache directory
MAKE_CACHE=${MAKE_CACHE:-$(pwd)/alc_make_cache}
# The output directory
TARGET_DIR=${TARGET_DIR:-$(pwd)/target}
# The PKGBUILDs directory
SRC_DIR=${SRC_DIR:-$(pwd)/packages}
# Whether to clean the cache directory
CLEAN_MAKE_DIR=${CLEAN_MAKE_DIR:-true}
# Load .env if exists
if [ -f $(pwd)/.env ]; then
source .env
fi
# ----- Functions ----- #
errcho() {
echo $@ 1>&2
}
error() {
errcho -e "\x1b[1;31m[!] Error: $@\x1b[0m"
}
error_details() {
errcho -e "\x1b[1;31m[*]\x1b[0m\x1b[31m \t $@\x1b[0m"
}
info() {
echo -e "\x1b[1m[i]\x1b[0m $@"
}
info_level_1() {
info "\t$@"
}
rootexec() {
# Pre-init sudo to avoid ugly lines
echo -e "$SUDO_PASS\n" | sudo -S echo > /dev/null 2>&1
echo -e "$SUDO_PASS\n" | sudo -S $@
}
# ----- Checks ----- #
# Check for the sudo password
if [ -z "$SUDO_PASS" ]; then
error "Sudo password is not set"
error_details "This is required because some functinoality will require root access"
error_details "Please set the SUDO_PASS variable to the password"
error_details ""
error_details "Tip: to securely set your password (not visible via history), create a"
error_details ".env file in the current directory and set the password like this:"
error_details
error_details "\`\`\`"
error_details "SUDO_PASS=super_pass"
error_details "\`\`\`"
exit 1
fi
# Check that the packages directory exists
if ! [ -d $SRC_DIR ]; then
error "Sources directory does not exist"
error_details "The sources directory is set to $SRC_DIR and it doesn't exist"
error_details ""
error_details "Maybe you have misspelled the path?"
error_details "To fix the error, you need to set SRC_DIR to an existing directory"
exit 1
fi
# ----- Main code ----- #
# Set up the cache directory
if ! mkdir -p $MAKE_CACHE; then
error "Can't create cache directory at $MAKE_CACHE"
error_details "Check output above for why"
exit 1
fi
# Set up the target dir
if ! mkdir -p $TARGET_DIR; then
error "Can't create target directory at $TARGET_DIR"
error_details "Check the output above for why"
exit 1
fi
info "Cache and target directories created"
info_level_1 "Cache directory is at $MAKE_CACHE"
info_level_1 "Target directory is at $TARGET_DIR"
# Make cache dir might be un-empty because it is created
# via mkdir -p which doesn't clean it or ensure that its
# empty.
if ! [ "$(ls -A $MAKE_CACHE | wc -l)" = "0" ]; then
if $CLEAN_MAKE_DIR = "true"; then
info "Make directory at $MAKE_CACHE is not empty, cleaning it"
if ! rootexec rm -r $MAKE_CACHE; then
error "Can't clean make directory at $MAKE_CACHE"
error_details "Check the output above for why"
exit 1
fi
mkdir -p $MAKE_CACHE
else
error "Make directory at $MAKE_CACHE is not empty"
error_details "Can't clean the make directory because CLEAN_MAKE_DIR is not true"
error_details "You can either clean it manually or set MAKE_CACHE to another path"
exit 1
fi
fi
# The same thing for target_dir, but it is enforced
# that it must be cleaned manually
if ! [ "$(ls -A $TARGET_DIR | wc -l)" = "0" ]; then
error "Target directory at $TARGET_DIR is not empty"
error_details "This script won't clean it automatically, you have to do it manually."
error_details "As an alternative solution, you can set TARGET_DIR to another path."
exit 1
fi
# Let's get to setting up the target directory
cd $TARGET_DIR
mkdir -p x86_64
# This readme file is stored in the script because
# copying it from source might be tricky because of
# paths
echo "
# Alice's Collection of Software Package index
Welcome to the Alice's Collection of Software (ACS) package index!
## Where can i find more info about this?
You can check out the [official repository](https://git.blek.codes/blek/acs)
## What is this?
This is a pacman repository. To set it up in a pacman-based system,
add the following code to the \`/etc/pacman.conf\`:
\`\`\`
[acs]
Server = https://acs.blek.codes/\$arch
\`\`\`
Then run \`pacman -Sy\`.
" > README.md
# This will be it for the target directory (for now)
# Now we'll set up the cache directory, which will
# also be the chroot
cd $MAKE_CACHE
# Build a chroot
info "Building a clean chroot at $MAKE_CACHE"
info_level_1 "This might take a bit while,"
info_level_1 "as building a clean chroot means"
info_level_1 "installing all the pacman packages"
rootexec mkarchroot $MAKE_CACHE/root base-devel
# Now do the actual work: build the packages
cd $SRC_DIR
for pkgdir in $(find $SRC_DIR -maxdepth 1 -mindepth 1 -type d); do
cd $pkgdir
rootexec makechrootpkg -c -r $MAKE_CACHE
done
info "\x1b[1;32mAll done!\x1b[0m"