2015-12-02 19:52:19 +00:00
|
|
|
#!/usr/bin/env bash
|
2015-10-15 23:16:58 +00:00
|
|
|
#
|
2015-11-16 19:21:57 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2015-12-02 19:52:19 +00:00
|
|
|
set -e
|
|
|
|
|
2015-10-15 23:16:58 +00:00
|
|
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
|
|
|
## Load Functions ##
|
|
|
|
source $SCRIPT_DIR/scripts/debian_build_lib.sh
|
|
|
|
|
|
|
|
## Debian Package Creation Functions ##
|
|
|
|
execute(){
|
2016-01-12 23:27:02 +00:00
|
|
|
if ! parse_args_and_set_env_vars "$@"; then
|
2016-01-07 00:46:25 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2015-10-15 23:16:58 +00:00
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2016-01-07 00:46:25 +00:00
|
|
|
parse_args_and_set_env_vars(){
|
|
|
|
OPTIND=1 # Reset in case getopts has been used previously in the shell.
|
|
|
|
|
|
|
|
while getopts ":n:v:i:o:h" opt; do
|
|
|
|
case $opt in
|
|
|
|
n)
|
2016-01-12 23:27:02 +00:00
|
|
|
export PACKAGE_NAME="$OPTARG"
|
2016-01-07 00:46:25 +00:00
|
|
|
;;
|
|
|
|
v)
|
2016-01-12 23:27:02 +00:00
|
|
|
export PACKAGE_VERSION="$OPTARG"
|
2016-01-07 00:46:25 +00:00
|
|
|
;;
|
|
|
|
i)
|
2016-01-12 23:27:02 +00:00
|
|
|
export INPUT_DIR="$OPTARG"
|
2016-01-07 00:46:25 +00:00
|
|
|
;;
|
|
|
|
o)
|
2016-01-12 23:27:02 +00:00
|
|
|
export OUTPUT_DIR="$OPTARG"
|
2016-01-07 00:46:25 +00:00
|
|
|
;;
|
|
|
|
h)
|
|
|
|
print_help
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
echo "Option -$OPTARG requires an argument." >&2
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2016-01-12 23:27:02 +00:00
|
|
|
|
|
|
|
# Special Input Directories + Paths
|
|
|
|
ABSOLUTE_PLACEMENT_DIR="${INPUT_DIR}/\$"
|
|
|
|
PACKAGE_ROOT_PLACEMENT_DIR="${INPUT_DIR}/package_root"
|
|
|
|
CONFIG="$INPUT_DIR/debian_config.json"
|
2016-01-07 00:46:25 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
print_help(){
|
|
|
|
echo "Usage: package_tool [-i <INPUT_DIR>] [-o <OUTPUT_DIRECTORY>]
|
|
|
|
[-n <PACKAGE_NAME>] [-v <PACKAGE_VERSION>] [-h]
|
|
|
|
|
|
|
|
REQUIRED:
|
|
|
|
-i <INPUT_DIR>: Input directory conforming to package_tool conventions and debian_config.json
|
|
|
|
-o <OUTPUT_DIR>: Output directory for debian package and other artifacts
|
|
|
|
|
|
|
|
OPTIONAL:
|
|
|
|
-n <PACKAGE_NAME>: name of created package, will override value in debian_config.json
|
|
|
|
-v <PACKAGE_VERSION>: version of created package, will override value in debian_config.json
|
|
|
|
-h: Show this message
|
|
|
|
|
|
|
|
NOTES:
|
|
|
|
See Readme for more information on package_tool conventions and debian_config.json format
|
|
|
|
https://github.com/dotnet/cli/tree/master/packaging/debian/package_tool
|
|
|
|
"
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:16:58 +00:00
|
|
|
validate_inputs(){
|
|
|
|
local ret=0
|
2016-01-12 23:27:02 +00:00
|
|
|
if [[ -z "$INPUT_DIR" ]]; then
|
|
|
|
echo "ERROR: -i <INPUT_DIRECTORY> Not Specified"
|
2015-10-15 23:16:58 +00:00
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
|
2016-01-12 23:27:02 +00:00
|
|
|
if [[ -z "$OUTPUT_DIR" ]]; then
|
|
|
|
echo "ERROR: -o <OUTPUT_DIRECTORY> Not Specified."
|
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -d "$PACKAGE_ROOT_PLACEMENT_DIR" ]]; then
|
2015-10-15 23:16:58 +00:00
|
|
|
echo "ERROR: package_root directory does not exist"
|
|
|
|
echo $PACKAGE_ROOT_PLACEMENT_DIR
|
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
|
2016-01-12 23:27:02 +00:00
|
|
|
if [[ ! -f "$CONFIG" ]]; then
|
2015-10-15 23:16:58 +00:00
|
|
|
echo "ERROR: debian_config.json file does not exist"
|
|
|
|
echo $CONFIG
|
|
|
|
ret=1
|
|
|
|
fi
|
2016-01-12 23:27:02 +00:00
|
|
|
|
2015-10-15 23:16:58 +00:00
|
|
|
return $ret
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_config_and_set_env_vars(){
|
|
|
|
extract_base_cmd="python $SCRIPT_DIR/scripts/extract_json_value.py"
|
2015-11-03 22:13:25 +00:00
|
|
|
|
2016-01-07 00:46:25 +00:00
|
|
|
# Arguments Take Precedence over Config
|
|
|
|
[ -z "$PACKAGE_VERSION" ] && PACKAGE_VERSION="$($extract_base_cmd $CONFIG "release.package_version")"
|
|
|
|
[ -z "$PACKAGE_NAME" ] && PACKAGE_NAME="$($extract_base_cmd $CONFIG "package_name")"
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
INPUT_SAMPLES_DIR="$INPUT_DIR/samples"
|
|
|
|
INPUT_DOCS_DIR="$INPUT_DIR/docs"
|
|
|
|
DOCS_JSON_PATH="$INPUT_DIR/docs.json"
|
2015-10-15 23:16:58 +00:00
|
|
|
|
|
|
|
PACKAGE_SOURCE_DIR="${OUTPUT_DIR}/${PACKAGE_NAME}-${PACKAGE_VERSION}"
|
2016-03-10 08:12:25 +00:00
|
|
|
|
|
|
|
if ! INSTALL_ROOT="$($extract_base_cmd $CONFIG "install_root")"; then
|
|
|
|
INSTALL_ROOT="/usr/share/$PACKAGE_NAME"
|
|
|
|
fi
|
2015-10-15 23:16:58 +00:00
|
|
|
|
|
|
|
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
|
2015-10-15 23:57:43 +00:00
|
|
|
package_docs
|
2016-04-02 02:07:16 +00:00
|
|
|
package_debian
|
2015-10-15 23:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(){
|
2016-01-12 23:27:02 +00:00
|
|
|
if [[ -d "$ABSOLUTE_PLACEMENT_DIR" ]]; then
|
|
|
|
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
|
|
|
|
fi
|
2015-10-15 23:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-02 02:07:16 +00:00
|
|
|
package_debian(){
|
|
|
|
if [[ -d "$ABSOLUTE_PLACEMENT_DIR" ]]; then
|
|
|
|
cp "$ABSOLUTE_PLACEMENT_DIR/postinst" $DEBIAN_DIR/postinst
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:16:58 +00:00
|
|
|
## Generation Functions ##
|
|
|
|
generate_config_templates(){
|
2016-03-22 18:14:29 +00:00
|
|
|
python ${SCRIPT_DIR}/scripts/config_template_generator.py $CONFIG $SCRIPT_DIR/templates/debian $DEBIAN_DIR $PACKAGE_NAME $PACKAGE_VERSION
|
2015-10-15 23:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-10-15 23:57:43 +00:00
|
|
|
docs_rel_path=${DOCS_DIR#${PACKAGE_SOURCE_DIR}/}
|
2015-10-15 23:16:58 +00:00
|
|
|
|
|
|
|
# Remove any existing manifest
|
|
|
|
rm -f ${DEBIAN_DIR}/${PACKAGE_NAME}.manpages
|
|
|
|
|
2016-01-06 21:19:23 +00:00
|
|
|
for manpage in ${generated_manpages[@]}
|
2015-10-15 23:16:58 +00:00
|
|
|
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) )
|
|
|
|
|
2015-12-02 22:41:05 +00:00
|
|
|
rm -f sample_manifest
|
2016-01-06 21:19:23 +00:00
|
|
|
for sample in ${samples[@]}
|
2015-10-15 23:16:58 +00:00
|
|
|
do
|
|
|
|
echo "$sample" >> "${DEBIAN_DIR}/${PACKAGE_NAME}.examples"
|
|
|
|
done
|
|
|
|
else
|
|
|
|
echo "Provide a 'samples' directory in INPUT_DIR to package samples"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:27:02 +00:00
|
|
|
execute "$@"
|