210 lines
5.3 KiB
Bash
Executable file
210 lines
5.3 KiB
Bash
Executable file
#!/bin/bash
|
|
#
|
|
# Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
#
|
|
|
|
# Takes Parameters:
|
|
# $1 = Input Directory
|
|
# $2 = Output Directory
|
|
# $3 = Package Version
|
|
|
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
## Load Functions ##
|
|
source $SCRIPT_DIR/scripts/debian_build_lib.sh
|
|
|
|
INPUT_DIR="$1"
|
|
OUTPUT_DIR="$2"
|
|
INPUT_PACKAGE_VERSION="$3"
|
|
|
|
# Special Input Directories + Paths
|
|
ABSOLUTE_PLACEMENT_DIR="${INPUT_DIR}/\$"
|
|
PACKAGE_ROOT_PLACEMENT_DIR="${INPUT_DIR}/package_root"
|
|
|
|
# Inputs
|
|
INPUT_SAMPLES_DIR="$INPUT_DIR/samples"
|
|
INPUT_DOCS_DIR="$INPUT_DIR/docs"
|
|
DOCS_JSON_PATH="$INPUT_DIR/docs.json"
|
|
CONFIG="$INPUT_DIR/debian_config.json"
|
|
|
|
## Debian Package Creation Functions ##
|
|
execute(){
|
|
# Exit if required validation fails
|
|
if ! validate_inputs; then
|
|
exit 1
|
|
fi
|
|
|
|
parse_config_and_set_env_vars
|
|
clean_or_create_build_dirs
|
|
package_all
|
|
generate_all
|
|
create_source_tarball
|
|
|
|
# Actually Build Package Files
|
|
(cd ${PACKAGE_SOURCE_DIR}; debuild -us -uc)
|
|
|
|
copy_files_to_output
|
|
}
|
|
|
|
validate_inputs(){
|
|
local ret=0
|
|
if [[ ! -d $ABSOLUTE_PLACEMENT_DIR ]]; then
|
|
echo "ERROR: $ directory does not exist"
|
|
echo $ABSOLUTE_PLACEMENT_DIR
|
|
ret=1
|
|
fi
|
|
|
|
if [[ ! -d $PACKAGE_ROOT_PLACEMENT_DIR ]]; then
|
|
echo "ERROR: package_root directory does not exist"
|
|
echo $PACKAGE_ROOT_PLACEMENT_DIR
|
|
ret=1
|
|
fi
|
|
|
|
if [[ ! -f $CONFIG ]]; then
|
|
echo "ERROR: debian_config.json file does not exist"
|
|
echo $CONFIG
|
|
ret=1
|
|
fi
|
|
|
|
return $ret
|
|
}
|
|
|
|
parse_config_and_set_env_vars(){
|
|
extract_base_cmd="python $SCRIPT_DIR/scripts/extract_json_value.py"
|
|
|
|
PACKAGE_NAME=$($extract_base_cmd $CONFIG "package_name")
|
|
|
|
# Override JSON Defined Version w/ Cmd Line ARG
|
|
if [ -z $INPUT_PACKAGE_VERSION ]; then
|
|
PACKAGE_VERSION=$($extract_base_cmd $CONFIG "release.package_version")
|
|
else
|
|
PACKAGE_VERSION=$INPUT_PACKAGE_VERSION
|
|
fi
|
|
|
|
PACKAGE_SOURCE_DIR="${OUTPUT_DIR}/${PACKAGE_NAME}-${PACKAGE_VERSION}"
|
|
INSTALL_ROOT="/usr/share/${PACKAGE_NAME}"
|
|
|
|
DEBIAN_DIR="${PACKAGE_SOURCE_DIR}/debian"
|
|
DOCS_DIR="${PACKAGE_SOURCE_DIR}/docs"
|
|
}
|
|
|
|
clean_or_create_build_dirs(){
|
|
rm -rf ${PACKAGE_SOURCE_DIR}
|
|
mkdir -p $DEBIAN_DIR
|
|
}
|
|
|
|
package_all(){
|
|
package_static_files
|
|
package_package_root_placement
|
|
package_absolute_placement
|
|
package_samples
|
|
package_docs
|
|
}
|
|
|
|
generate_all(){
|
|
generate_config_templates
|
|
generate_manpages
|
|
generate_manpage_manifest
|
|
generate_sample_manifest
|
|
write_debian_install_file
|
|
}
|
|
|
|
create_source_tarball(){
|
|
rm -f ${OUTPUT_DIR}/${PACKAGE_NAME}_${PACKAGE_VERSION}.orig.tar.gz
|
|
tar -cvzf ${OUTPUT_DIR}/${PACKAGE_NAME}_${PACKAGE_VERSION}.orig.tar.gz -C $PACKAGE_SOURCE_DIR .
|
|
}
|
|
|
|
copy_files_to_output(){
|
|
# .deb, .dsc, etc.. Already in output dir
|
|
# Copy Test files
|
|
|
|
cp $SCRIPT_DIR/test/integration_tests/test_package.bats $OUTPUT_DIR
|
|
}
|
|
|
|
## Packaging Functions ##
|
|
package_static_files(){
|
|
cp -a $SCRIPT_DIR/package_files/debian/* ${PACKAGE_SOURCE_DIR}/debian
|
|
}
|
|
|
|
package_package_root_placement(){
|
|
add_dir_to_install ${PACKAGE_ROOT_PLACEMENT_DIR} ""
|
|
}
|
|
|
|
package_absolute_placement(){
|
|
abs_in_package_dir="\$"
|
|
|
|
add_dir_to_install ${ABSOLUTE_PLACEMENT_DIR} $abs_in_package_dir
|
|
|
|
# Get List of all files in directory tree, relative to ABSOLUTE_PLACEMENT_DIR
|
|
abs_files=( $(_get_files_in_dir_tree $ABSOLUTE_PLACEMENT_DIR) )
|
|
|
|
# For each file add a a system placement
|
|
for abs_file in $abs_files
|
|
do
|
|
parent_dir=$(dirname $abs_file)
|
|
filename=$(basename $abs_file)
|
|
|
|
add_system_file_placement "$abs_in_package_dir/$abs_file" "/$parent_dir"
|
|
done
|
|
}
|
|
|
|
package_samples(){
|
|
if [[ -d "$INPUT_SAMPLES_DIR" ]]; then
|
|
cp -a $INPUT_SAMPLES_DIR/. $PACKAGE_SOURCE_DIR
|
|
fi
|
|
}
|
|
|
|
package_docs(){
|
|
if [[ -d "$INPUT_DOCS_DIR" ]]; then
|
|
mkdir -p $DOCS_DIR
|
|
cp -a $INPUT_DOCS_DIR/. $DOCS_DIR
|
|
fi
|
|
}
|
|
|
|
## Generation Functions ##
|
|
generate_config_templates(){
|
|
python ${SCRIPT_DIR}/scripts/config_template_generator.py $CONFIG $SCRIPT_DIR/templates/debian $DEBIAN_DIR $PACKAGE_VERSION
|
|
}
|
|
|
|
generate_manpages(){
|
|
if [[ -f "$DOCS_JSON_PATH" ]]; then
|
|
mkdir -p $DOCS_DIR
|
|
|
|
# Generate the manpages from json spec
|
|
python ${SCRIPT_DIR}/scripts/manpage_generator.py ${DOCS_JSON_PATH} ${DOCS_DIR}
|
|
fi
|
|
}
|
|
|
|
generate_manpage_manifest(){
|
|
# Get a list of files generated relative to $DOCS_DIR
|
|
generated_manpages=( $(_get_files_in_dir_tree $DOCS_DIR) )
|
|
|
|
# Get path relative to $PACKAGE_SOURCE_DIR to prepend to each filename
|
|
# This syntax is bash substring removal
|
|
docs_rel_path=${DOCS_DIR#${PACKAGE_SOURCE_DIR}/}
|
|
|
|
# Remove any existing manifest
|
|
rm -f ${DEBIAN_DIR}/${PACKAGE_NAME}.manpages
|
|
|
|
for manpage in $generated_manpages
|
|
do
|
|
echo "${docs_rel_path}/${manpage}" >> "${DEBIAN_DIR}/${PACKAGE_NAME}.manpages"
|
|
done
|
|
}
|
|
|
|
generate_sample_manifest(){
|
|
if [[ -d "$INPUT_SAMPLES_DIR" ]]; then
|
|
generated_manpages=( $(_get_files_in_dir_tree $INPUT_SAMPLES_DIR) )
|
|
|
|
rm sample_manifest
|
|
for sample in $samples
|
|
do
|
|
echo "$sample" >> "${DEBIAN_DIR}/${PACKAGE_NAME}.examples"
|
|
done
|
|
else
|
|
echo "Provide a 'samples' directory in INPUT_DIR to package samples"
|
|
fi
|
|
}
|
|
|
|
execute
|