remove unneeded functions, clean up code, and reduce number of lines

This commit is contained in:
hippoz 2021-07-03 19:56:28 +03:00
parent 1908130cca
commit 26fb73344f

293
aps
View file

@ -1,233 +1,138 @@
#!/bin/sh #!/bin/sh
temp_location="/tmp"
TEMP_LOCATION="/tmp" install_root="/tmp/alroot"
INSTALL_ROOT="/tmp/alroot" installed_pkg_database="${install_root}/var/aps/installed"
INSTALLED_PKG_DATABASE="${INSTALL_ROOT}/var/aps/installed" locpkg_database="${install_root}/var/aps/repos"
LOCPKG_DATABASE="${INSTALL_ROOT}/var/aps/repos" lock="${install_root}/var/aps/lock"
LOCK="${INSTALL_ROOT}/var/aps/lock"
# Repo config
official="https://git.hippoz.xyz/alnux/repo" official="https://git.hippoz.xyz/alnux/repo"
[ ! -d "${installed_pkg_database}" ] && mkdir -p "${installed_pkg_database}"
set_default_package_config_values() { die() {
PKG_CONFIG_DEPLOY=true echo "${1}"
PKG_CONFIG_VER="0.1" rm "${lock}"
PKG_CONFIG_NAME="alnuxpackage"
PKG_CONFIG_MAKEDEPENDS=""
PKG_CONFIG_DEPENDS=""
}
unset_config_values() {
unset PKG_CONFIG_DEPLOY
unset PKG_CONFIG_VER
unset PKG_CONFIG_NAME
unset PKG_CONFIG_MAKEDEPENDS
unset PKG_CONFIG_DEPENDS
}
load_installed_pkg_config() {
echo ":: Looking up package ${1} in local installed packages..."
set_default_package_config_values
if [ -d "${INSTALLED_PKG_DATABASE}/${1}" ]; then
echo ":: Package ${1} was found, loading config..."
PKG_NAME=${1}
PKG_PACKAGE_PATH="${INSTALLED_PKG_DATABASE}/${1}"
run_package_script "package"
else
echo "[E] Package ${1} was not found in the local installed package database, exiting..."
exit 1
fi
}
extract_package() {
echo "[*] Copying package ${1} into temporary location ${TEMP_LOCATION}..."
#tar -xf "${1}" -C "${TEMP_LOCATION}"
cp -prv "${1}" "${TEMP_LOCATION}"
PKG_NAME="${1%/}"
PKG_NAME="${PKG_NAME##*/}"
PKG_NAME=$(echo "${PKG_NAME}" | cut -f 1 -d '.')
cd "${INSTALL_ROOT}"
PKG_PACKAGE_PATH="${TEMP_LOCATION}/${PKG_NAME}"
set_default_package_config_values
}
add_package_to_database() {
if [ ! -d "${INSTALLED_PKG_DATABASE}" ]; then
echo "[*] Package database folder not found, creating..."
mkdir -pv "${INSTALLED_PKG_DATABASE}"
fi
if [ -x "${PKG_PACKAGE_PATH}/package" ]; then
echo ":: Adding target ${PKG_NAME} to installed package database..."
if [ ! -d "${INSTALLED_PKG_DATABASE}/${PKG_NAME}" ]; then
echo "[*] Package folder for ${PKG_NAME} not found, creating..."
mkdir "${INSTALLED_PKG_DATABASE}/${PKG_NAME}"
fi
cp -pv "${PKG_PACKAGE_PATH}/package" "${INSTALLED_PKG_DATABASE}/${PKG_NAME}/package"
if [ -e "${TEMP_LOCATION}/${PKG_NAME}-payloadfiles" ]; then
cp -pv "${TEMP_LOCATION}/${PKG_NAME}-payloadfiles" "${INSTALLED_PKG_DATABASE}/${PKG_NAME}/payloadfiles"
fi
else
echo "[E] Target ${PKG_NAME} does not have a package script and will not be added to the installed package database"
exit 2 exit 2
}
run_package_script() { # --- $2 is the package path, and $1 is the script to run
{
if [ -x "${2}/${1}" ]; then
echo ":: Running ${1}"
cd "${2}" || die "Could not enter package working directory. Exiting..."
"${2}/${1}" "${install_root}" "${2}"
fi fi
} }
run_package_script() {
if [ -x "${PKG_PACKAGE_PATH}/${1}" ]; then
echo ":: Running ${1} script for target ${PKG_NAME}..."
. "${PKG_PACKAGE_PATH}/${1}" ${INSTALL_ROOT} ${PKG_PACKAGE_PATH}
fi
} }
deploy_package_payload() {
if [ "${PKG_CONFIG_DEPLOY}" = true ]; then
echo ":: Deploying target ${PKG_NAME}..."
cp -rpv "${PKG_PACKAGE_PATH}/payload"/* "${INSTALL_ROOT}" > "${TEMP_LOCATION}/${PKG_NAME}-payloaddeploylog"
sed 's/^.*-> //' "${TEMP_LOCATION}/${PKG_NAME}-payloaddeploylog" | tr -d \'\" > "${TEMP_LOCATION}/${PKG_NAME}-payloadfiles"
rm "${TEMP_LOCATION}/${PKG_NAME}-payloaddeploylog"
fi
}
install_extracted_package() {
run_package_script "package"
run_package_script "build"
run_package_script "predeploy"
deploy_package_payload
run_package_script "postdeploy"
echo "[*] Install complete for target ${PKG_NAME}"
}
extract_cleanup() {
echo "[*] Cleaning up after target ${PKG_NAME}..."
rm -r "${PKG_PACKAGE_PATH}"
rm "${TEMP_LOCATION}/${PKG_NAME}-payloadfiles"
unset PKG_PACKAGE_PATH
unset PKG_NAME
unset_config_values
cd "${INSTALL_ROOT}"
}
install_local_package() { install_local_package() {
if [ ! -e "${1}" ]; then [ ! -e "${1}" ] && die "[E] File ${1} does not exist. Exiting..."
echo "[E] File ${1} does not exist, exiting..." # Extract the package
exit 1 echo "[*] Copying package ${1} into temporary location ${temp_location}..."
cp -prv "${1}" "${temp_location}"
pkg_name="${1%/}"
pkg_name="${pkg_name##*/}"
pkg_name=$(echo "${pkg_name}" | cut -f 1 -d '.')
pkg_path="${temp_location}/${pkg_name}"
cd "${pkg_path}" || die "Could not enter package path."
# Set default values for package
pkg_config_deploy=true
pkg_config_ver="0"
pkg_config_makedepends=""
pkg_config_depends=""
# Install package
. "${pkg_path}/package"
run_package_script "build" "${pkg_path}"
run_package_script "predeploy" "${pkg_path}"
# Deploy package
if [ "${pkg_config_deploy}" = true ]; then
echo ":: Deploying target ${pkg_name}..."
cp -rpv "${pkg_path}/payload"/* "${install_root}" > "${temp_location}/${pkg_name}-payloaddeploylog"
sed 's/^.*-> //' "${temp_location}/${pkg_name}-payloaddeploylog" | tr -d \'\" > "${temp_location}/${pkg_name}-payloadfiles"
rm "${temp_location}/${pkg_name}-payloaddeploylog"
fi fi
run_package_script "postdeploy" "${pkg_path}"
extract_package "${1}" echo "[*] Install complete for target ${pkg_name}"
install_extracted_package # Add package to database
add_package_to_database if [ -x "${pkg_path}/package" ]; then
extract_cleanup echo ":: Adding target ${pkg_name} to installed package database..."
[ ! -d "${installed_pkg_database}/${pkg_name}" ] && mkdir "${installed_pkg_database}/${pkg_name}"
cp -pv "${pkg_path}/package" "${installed_pkg_database}/${pkg_name}/package"
[ -e "${temp_location}/${pkg_name}-payloadfiles" ] && cp -pv "${temp_location}/${pkg_name}-payloadfiles" "${installed_pkg_database}/${pkg_name}/payloadfiles"
else
die "[E] Target ${pkg_name} does not have a package script (it's an invalid package!). Exiting..."
fi
# Cleanup
echo "[*] Cleaning up after target ${pkg_name}..."
rm -r "${pkg_path}"
rm "${temp_location}/${pkg_name}-payloadfiles"
cd "${install_root}" || die "Could not return to main directory. Exiting..."
unset pkg_path
unset pkg_name
unset pkg_config_deploy
unset pkg_config_ver
unset pkg_config_makedepends
unset pkg_config_depends
} }
remove_local_package() { remove_local_package() {
load_installed_pkg_config "${1}" pkg_path="${installed_pkg_database}/${1}"
PAYLOADFILES_PATH="${PKG_PACKAGE_PATH}/payloadfiles" [ ! -d "${pkg_path}" ] && die "Package could not be found in local installed package database. Exiting..."
payloadfiles_path="${pkg_path}/payloadfiles"
if [ ! -e "${PAYLOADFILES_PATH}" ]; then [ ! -e "${payloadfiles_path}" ] && die "[E] Package ${1} does not have a payloadfiles file. Exiting..."
echo "[E] Package ${1} does not have a payloadfiles file. Exiting..."
exit 0
fi
echo ":: Removing target ${1}..." echo ":: Removing target ${1}..."
rm -rv "$(cat ${payloadfiles_path})"
rm -rv $(<${PAYLOADFILES_PATH}) rm -rv "${pkg_path}"
rm -rv ${PKG_PACKAGE_PATH} echo "[*] Removal complete for target ${pkg_name}"
echo "[*] Removal complete for target ${PKG_NAME}"
} }
sync_local_repo_database() { sync_local_repo_database() {
echo ":: Syncing local database for repo ${1}..." echo ":: Syncing local database for repo ${1}..."
if [ ! -d "${LOCPKG_DATABASE}/${1}" ]; then if [ ! -d "${locpkg_database}/${1}" ]; then
echo ":: Local database for repo ${1} does not exist, cloning..." echo ":: Local database for repo ${1} does not exist, cloning..."
git clone "${2}" "${LOCPKG_DATABASE}/${1}" git clone "${2}" "${locpkg_database}/${1}"
else else
echo ":: Local database for repo ${1} exists, updating..." echo ":: Local database for repo ${1} exists, updating..."
cd "${LOCPKG_DATABASE}/${1}" cd "${locpkg_database}/${1}" || die "Could not enter local database. Exiting..."
git pull git pull
fi fi
} }
install_package_from_repo() { # NOTE(hippoz): This can get a tad confusing... ${1} is the repo and ${2} is the package install_package_from_repo() { # NOTE(hippoz): This can get a tad confusing... ${1} is the repo and ${2} is the package
echo ":: Installing package ${2} from ${1} repo" echo ":: Installing package ${2} from ${1} repo"
if [ ! -d "${LOCPKG_DATABASE}/${1}/${2}" ]; then [ ! -d "${locpkg_database}/${1}/${2}" ] && die "[E] Target not found."
echo "[E] Package ${2} was not found in ${1}, exiting..." install_local_package "${locpkg_database}/${1}/${2}"
exit 1
else
install_local_package "${LOCPKG_DATABASE}/${1}/${2}"
fi
} }
upgrade() { upgrade() {
echo ":: Upgrading installed packages..." echo ":: Upgrading installed packages..."
unset_config_values unset_config_values
cd "${INSTALLED_PKG_DATABASE}" cd "${installed_pkg_database}" || die "Could not enter installed package database. Exiting..."
for pkg in */; do for pkg in */; do
INSTALLED_PKG_PATH="${INSTALLED_PKG_DATABASE}/${pkg}" installed_pkg_path="${installed_pkg_database}/${pkg}"
. ${INSTALLED_PKG_PATH}/package . "${installed_pkg_path}/package"
PKG_NAME="${INSTALLED_PKG_PATH%/}" pkg_name="${installed_pkg_path%/}"
PKG_NAME="${PKG_NAME##*/}" pkg_name="${pkg_name##*/}"
already_installed_version=${pkg_config_ver}
ALREADY_INSTALLED_VERSION=${PKG_CONFIG_VER} [ ! -d "${locpkg_database}/${1}/${pkg_name}" ] && die "[E] Package ${pkg_name} was not found in ${1}, exiting..."
. "${locpkg_database}/${1}/${pkg_name}/package"
if [ ! -d "${LOCPKG_DATABASE}/${1}/${PKG_NAME}" ]; then if [ ! "${already_installed_version}" = "${pkg_config_ver}" ]; then
echo "[E] Package ${PKG_NAME} was not found in ${1}, exiting..." echo ":: Package ${pkg_name} is out of date, updating..."
exit 1 install_package_from_repo "${1}" "${pkg_name}"
else
. "${LOCPKG_DATABASE}/${1}/${PKG_NAME}/package"
fi fi
unset pkg_config_deploy
if [ ! "${ALREADY_INSTALLED_VERSION}" = "${PKG_CONFIG_VER}" ]; then unset pkg_config_ver
echo ":: Package ${PKG_NAME} is out of date, updating..." unset pkg_config_makedepends
install_package_from_repo "${1}" "${PKG_NAME}" unset pkg_config_depends
fi
unset_config_values
done done
} }
main() { main() {
if [ -f "${LOCK}" ]; then if [ -f "${lock}" ]; then
echo "[E] A lock file already exists." echo "[E] A lock file already exists. If you're sure no package manager is running, remove the file ${lock}."
echo " If you're sure no package manager is running,"
echo " remove the file ${LOCK}"
exit 1 exit 1
fi fi
touch "${LOCK}" touch "${lock}"
case ${1} in case ${1} in
install) install) install_local_package "${2}" ;;
install_local_package "${2}" remove) remove_local_package "${2}" ;;
;;
remove)
remove_local_package "${2}"
;;
sync) sync)
sync_local_repo_database "official" ${official} sync_local_repo_database "official" ${official}
if [ -z "${2}" ]; then [ -z "${2}" ] && die "[*] No targets specified."
echo "[*] No targets specified." install_package_from_repo "official" "${2}" ;;
else upgrade) upgrade "official" ;;
install_package_from_repo "official" "${2}" *) die "[E] Invalid option ${1}, exiting..." ;;
fi
;;
upgrade)
upgrade "official"
;;
*)
echo "[E] Invalid option ${1}, exiting..."
rm "${LOCK}"
exit 0
;;
esac esac
rm "${lock}"
rm "${LOCK}"
} }
main "${1}" "${2}" main "${1}" "${2}"