Tamarin/lib/build.sh

136 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "${DIR}/util.sh"
DIST_DIR="${BASE_DIR}/dist"
SRC_DIR="${BASE_DIR}/src"
function get_project_opt {
filter=${1}
manifest_path=${SRC_DIR}/tamarin.json
jq -r "${filter}" ${manifest_path} | sed 's/null//g'
}
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="${SRC_DIR}/$(get_project_opt .hooks.${hook_name})"
# Test hook script existence
if [ ! -f "${hook_script}" ]; then
info "No ${hook_name} hook."
return
fi
info "Executing ${hook_name} hook..."
# Ensure the hook script is executable
chmod +x "${hook_script}"
# Execute hook in a subshell
( cd ${SRC_DIR} && DESTDIR="${build_dir}" SRCDIR="${SRC_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}"
package_name=$(get_project_opt .name)
echo "Package: ${package_name}" > "${control_file}"
package_version=$(get_project_opt .version)
echo "Version: ${package_version:-0.0.0}" >> "${control_file}"
package_section=$(get_project_opt .section)
echo "Section: ${package_section:-extra}" >> "${control_file}"
package_priority=$(get_project_opt .priority)
echo "Priority: ${package_priority:-optional}" >> "${control_file}"
package_arch=$(get_project_opt .priority)
echo "Architecture: ${package_arch:-any}" >> "${control_file}"
dependencies=$( get_project_opt ".dependencies | .[\"${DISTRIB}\"] | @sh" | sed "s/' '/, /g" | sed "s/'//g" )
debug "Package dependencies: $dependencies"
echo "Depends: ${dependencies}" >> "${control_file}"
package_maintainer=$(get_project_opt .maintainer)
echo "Maintainer: ${package_maintainer:-Unknown}" >> "${control_file}"
package_description=$(get_project_opt .description)
echo "Description: ${package_description:-No 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}"
info "Package created ! (${build_dir}/${project_name}.deb)"
if [ $? != 0 ]; then
error "The build process has not completed successfuly !"
exit 1
fi
cd /
exec_hook "postBuild" "${build_dir}"
# Copie du paquet nouvellement créé dans le répertoire "dist"
mkdir -p "${DIST_DIR}"
cp "${build_dir}/../${project_name}.deb" "${DIST_DIR}/${project_name}.deb"
}
function main {
manifest_path=${SRC_DIR}/tamarin.json
if [ ! -f "${manifest_path}" ]; then
fatal "There is no 'tamarin.json' file found in the project directory !"
fi
build_project
}
main