Tamarin/build.sh

128 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
DIST_DIR="${BASE_DIR}/dist"
PROJECT_DIR="${BASE_DIR}/project"
function info {
echo "[INFO] ${1}"
}
function debug {
echo "[DEBUG] ${1}"
}
function error {
echo "[ERROR] ${1}"
}
function get_project_opt {
filter=${1}
manifest_path=${PROJECT_DIR}/bricklayer.json
cat ${manifest_path} | jq -r ${filter}
}
function get_build_dir {
project_name="${1}"
temp_dir=$(mktemp -d)
build_dir="${temp_dir}/${project_name}"
mkdir -p "${build_dir}"
echo ${build_dir}
}
function exec_hook {
hook_name=${1}
build_dir=${2}
hook_script="${PROJECT_DIR}/$(get_project_opt .hooks.${hook_name})"
# Test hook script existence
if [ ! -f "${hook_script}" ]; then
info "No ${hook_name} hook."
exit
fi
info "Executing ${hook_name} hook..."
# Ensure the hook script is executable
chmod +x "${hook_script}"
# Execute hook in a subshell
( BUILD_DIR="${build_dir}" PROJECT_DIR="${PROJECT_DIR}" exec "${hook_script}" )
# If the script did not execute properly, we stop here
if [ $? != 0 ]; then
error "The '${hook_name}' hook script did not finished properly !"
exit 1
fi
}
function create_control_file {
build_dir="${1}"
mkdir -p "${build_dir}/DEBIAN"
control_file="${build_dir}/DEBIAN/control"
touch "${control_file}"
echo "Package: $(get_project_opt .name)" > "${control_file}"
echo "Version: $(get_project_opt .version)" >> "${control_file}"
echo "Section: $(get_project_opt .section)" >> "${control_file}"
echo "Priority: $(get_project_opt .priority)" >> "${control_file}"
echo "Architecture: $(get_project_opt .arch)" >> "${control_file}"
echo "Depends: " >> "${control_file}"
echo "Maintainer: $(get_project_opt .maintainer)" >> "${control_file}"
echo "Description: $(get_project_opt .description)" >> "${control_file}"
}
function build_project {
project_name="$(get_project_opt '.name')"
info "Building project '${project_name}'..."
build_dir="$(get_build_dir "${project_name}")"
debug "Build dir: ${build_dir}"
create_control_file "${build_dir}"
exec_hook "preBuild" "${build_dir}"
cd "${build_dir}/.."
dpkg-deb --build "${project_name}"
if [ $? != 0 ]; then
error "The build process has not completed successfuly !"
exit 1
fi
cd -
exec_hook "postBuild" "${build_dir}"
mkdir -p "${DIST_DIR}"
cp "${build_dir}/../${project_name}.deb" "${DIST_DIR}/${project_name}.deb"
info "Done"
}
function main {
manifest_path=${PROJECT_DIR}/bricklayer.json
if [ ! -f "${manifest_path}" ]; then
error "There is no 'bricklayer.json' file found in the project directory !"
exit 1
fi
build_project
}
main