123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- # /////////////////////////////////////////////////////////////////////////////
- # REFCODES.ORG
- # =============================================================================
- # This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
- # under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
- # licenses:
- # =============================================================================
- # GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
- # =============================================================================
- # Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
- # =============================================================================
- # Please contact the copyright holding author(s) of the software artifacts in
- # question for licensing issues not being covered by the above listed licenses,
- # also regarding commercial licensing models or regarding the compatibility
- # with other open source licenses.
- # /////////////////////////////////////////////////////////////////////////////
- #!/bin/bash
- # ------------------------------------------------------------------------------
- # INIT:
- # ------------------------------------------------------------------------------
- CURRENT_PATH="$(pwd)"
- SCRIPT_PATH="$(dirname $0)"
- cd "${SCRIPT_PATH}"
- SCRIPT_PATH="$(pwd)"
- cd "${CURRENT_PATH}"
- SCRIPT_DIR="${SCRIPT_PATH##*/}"
- SCRIPT_NAME="$(basename $0 .sh)"
- PARENT_PATH="$(realpath $(dirname $0)/..)"
- PARENT_DIR="${PARENT_PATH##*/}"
- MODULE_NAME="$(echo -e "${SCRIPT_DIR}" | cut -d- -f3- )"
- if [ -z "${MODULE_NAME}" ]; then
- MODULE_NAME="$(echo -e "${SCRIPT_DIR}" | cut -d- -f2- )"
- if [ -z "${MODULE_NAME}" ]; then
- MODULE_NAME="${SCRIPT_DIR}"
- fi
- fi
- # ------------------------------------------------------------------------------
- # ANSI ESCAPE CODES:
- # ------------------------------------------------------------------------------
- ESC_BOLD="\E[1m"
- ESC_FAINT="\E[2m"
- ESC_FG_RED="\E[31m"
- ESC_FG_GREEN="\E[32m"
- ESC_FG_YELLOW="\E[33m"
- ESC_FG_BLUE="\E[34m"
- ESC_FG_MAGENTA="\E[35m"
- ESC_FG_CYAN="\E[36m"
- ESC_FG_WHITE="\E[37m"
- ESC_RESET="\E[0m"
- # ------------------------------------------------------------------------------
- # PRINTLN:
- # ------------------------------------------------------------------------------
- function printLn {
- char="-"
- if [[ $# == 1 ]] ; then
- char="$1"
- fi
- echo -en "${ESC_FAINT}"
- printf '%*s' "${COLUMNS:-$(tput cols)}" '' | tr ' ' ${char}
- echo -en "${ESC_RESET}"
- }
- # ------------------------------------------------------------------------------
- # QUIT:
- # ------------------------------------------------------------------------------
- function quit {
- input=""
- while ([ "$input" != "q" ] && [ "$input" != "y" ]); do
- echo -ne "> Continue? Enter [${ESC_BOLD}q${ESC_RESET}] to quit, [${ESC_BOLD}y${ESC_RESET}] to continue: ";
- read input;
- done
- # printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
- if [ "$input" == "q" ] ; then
- printLn
- echo -e "> ${ESC_BOLD}Aborting due to user input.${ESC_RESET}"
- exit
- fi
- }
- # ------------------------------------------------------------------------------
- # BANNER:
- # ------------------------------------------------------------------------------
- function printBanner {
- figlet -w 180 "/${MODULE_NAME}:>>>${SCRIPT_NAME}..." 2> /dev/null
- if [ $? -ne 0 ]; then
- banner "${SCRIPT_NAME}..." 2> /dev/null
- if [ $? -ne 0 ]; then
- echo -e "> ${SCRIPT_NAME}:" | tr a-z A-Z
- fi
- fi
- }
- printBanner
- # ------------------------------------------------------------------------------
- # HELP:
- # ------------------------------------------------------------------------------
- function printHelp {
- printLn
- echo -e "Usage: ${ESC_BOLD}${SCRIPT_NAME}${ESC_RESET}.sh [ -h | <appName> ]"
- printLn
- echo -e "Bundles the project creating a self-contained binary carrying its own JRE."
- echo -e "In case there is a <${SCRIPT_NAME}.conf> file, then the APP_NAME property is used as <appName> (if not specified other on the command line)."
- printLn
- echo -e "${ESC_BOLD}-h${ESC_RESET}: Print this help"
- printLn
- }
- if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
- printHelp
- exit 0
- fi
- # ------------------------------------------------------------------------------
- # BUNDLE:
- # ------------------------------------------------------------------------------
- function bundle {
- os="$1"
- flavor="$2"
- bundleBasePath="$3"
- bundlePath="$3/$1"
- jreArchiveURL="$4"
- bundleLauncherScript="$5"
- jarPath="$6"
- targetPath="$7"
- bundlerCommand="$8"
- bundleName="$9"
- bundleJrePath="${bundlePath}/jre"
- jreArchiveName="$(basename ${jreArchiveURL})"
- launcherScript="${bundleLauncherScript#bundle-}"
- mkdir -p "${bundlePath}"
- if [ ! -d "${bundleJrePath}" ]; then
- if [ ! -f "${bundleBasePath}/${jreArchiveName}" ]; then
- echo -e "> Downloading ${ESC_BOLD}${1^}${ESC_RESET} JRE <${ESC_BOLD}${jreArchiveName}${ESC_RESET}>..."
- wget -q "${jreArchiveURL}" -P "${bundleBasePath}"
- if [ $? -ne 0 ]; then
- echo -e "> ${ESC_BOLD}Unable to download JRE <${jreArchiveName}>!${ESC_RESET}>"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- fi
- echo -e "> Unpacking <${jreArchiveName}>..."
- if [[ $jreArchiveName = *.zip ]] ; then
- unzip -qq "${bundleBasePath}/${jreArchiveName}" -d "${bundlePath}"
- elif [[ $jreArchiveName = *.tar.gz ]] ; then
- tar -xzf "${bundleBasePath}/${jreArchiveName}" -C "${bundlePath}"
- else
- echo -e "> ${ESC_BOLD}Don't know how to extract JRE <${jreArchiveName}>!${ESC_RESET}>"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- if [ $? -ne 0 ]; then
- echo -e "> Unable to extract JRE <${jreArchiveName}>!"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- find "${bundlePath}" -maxdepth 1 -type d -name "*jre*" -exec mv {} "${bundlePath}/jre" \;
- if [ ! -d "${bundleJrePath}" ]; then
- echo -e "> ${ESC_BOLD}Unable to setup JRE <${jreArchiveName}>!${ESC_RESET}"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- fi
- if [ ! -f "${bundlePath}/${launcherScript}" ]; then
- cp "${SCRIPT_PATH}/${bundleLauncherScript}" "${bundlePath}/${launcherScript}"
- fi
- echo -e "> Copying JAR <${ESC_BOLD}${jarFile}${ESC_RESET}>..."
- cp "${jarFile}" "${bundlePath}/application.jar"
- echo -e "> Creating bundle <${ESC_BOLD}${bundleName}${ESC_RESET}> at <${ESC_BOLD}${targetPath}/${bundleName}${ESC_RESET}>..."
- if [[ "${flavor}" == "cygwin" ]]; then
- ${bundlerCommand} --arch ${os}-x64 --input_dir "$(cygpath -wa "${bundlePath}")" --exec "${launcherScript}" --output "$(cygpath -wa "${targetPath}/${bundleName}")" > /dev/null
- else
- ${bundlerCommand} --arch ${os}-x64 --input_dir "${bundlePath}" --exec "${launcherScript}" --output "${targetPath}/${bundleName}" > /dev/null
- fi
- if [ $? -ne 0 ]; then
- echo -e "> ${ESC_BOLD}Unable to create bundle <${bundleName}>!${ESC_RESET}"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- echo -e "> Bundle <${ESC_BOLD}${bundleName}${ESC_RESET}> created at <${ESC_BOLD}${targetPath}${ESC_RESET}>"
- printLn
- }
- # ------------------------------------------------------------------------------
- # MAIN:
- # ------------------------------------------------------------------------------
- cd "${SCRIPT_PATH}"
- TARGET_FOLDER="target"
- CONF_PATH="${SCRIPT_PATH}/${SCRIPT_NAME}.conf"
- if [ -f "${CONF_PATH}" ]; then
- . ${CONF_PATH}
- else
- echo -e "> ${ESC_BOLD}Cannot find configuration at <${CONF_PATH}>!${ESC_RESET}"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- # As of https://stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or-linux:
- unameOut="$(uname -s)"
- case "${unameOut}" in
- Linux*) machine="Linux"; flavor="bash"; warp="linux-x64.warp-packer";;
- CYGWIN*) machine="Windows"; flavor="cygwin"; warp="windows-x64.warp-packer.exe";;
- MINGW*) machine="Windows"; flavor="mingw"; warp="windows-x64.warp-packer.exe";;
- *) machine=""
- esac
- if [[ -z "${machine}" ]] ; then
- echo -e "> ${ESC_BOLD}Cannot determine the machine's OS, aborting!${ESC_RESET}"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- if [[ -z "${machine}" ]] ; then
- echo -e "> ${ESC_BOLD}Cannot determine the machine's OS, aborting!${ESC_RESET}"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- bundleBasePath="${SCRIPT_PATH}/.bundle"
- mkdir -p "${bundleBasePath}"
- targetPath="${SCRIPT_PATH}/${TARGET_FOLDER}"
- moduleName=$(xml2 < "${SCRIPT_PATH}/pom.xml" 2>/dev/null | grep "/project/artifactId=")
- moduleName="${moduleName#/project/artifactId=}"
- if [ -z "${moduleName}" ]; then
- moduleName=$(mvn org.apache.maven.plugins:maven-help-plugin:evaluate -Dexpression=project.artifactId -q -DforceStdout)
- fi
- binName="$1"
- if [ -z "${binName}" ]; then
- binName=${APP_NAME}
- if [ -z "${binName}" ]; then
- binName="$(echo -e "${moduleName}" | cut -d- -f3- )"
- if [ -z "${binName}" ]; then
- binName="$(echo -e "${moduleName}" | cut -d- -f2- )"
- if [ -z "${binName}" ]; then
- binName="${moduleName}"
- fi
- fi
- fi
- fi
- echo -e "> Creating self-contained <${ESC_BOLD}${binName}${ESC_RESET}> binary for <${ESC_BOLD}${machine}${ESC_RESET}> using bundler command <${ESC_BOLD}${warp}${ESC_RESET}>..."
- executable="${bundleBasePath}/${warp}"
- if [ ! -f "${executable}" ]; then
- echo -e "> Downloading bundler <${ESC_BOLD}${warp}${ESC_RESET}> from <${ESC_BOLD}${WARP_PACKER_URL}${ESC_RESET}>..."
- wget -q "${WARP_PACKER_URL}/${warp}" -P "${bundleBasePath}"
- if [ $? -ne 0 ]; then
- echo -e "> ${ESC_BOLD}Unable to download bunlder <${warp}>!${ESC_RESET}"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- fi
- chmod ug+x "${executable}"
- printLn
- jarFile=$(find "${targetPath}" -name "${moduleName}*.jar" ! -name "*tests.jar" ! -name "*sources.jar")
- if [ ! -f "${jarFile}" ]; then
- echo -e "> ${ESC_BOLD}Unable to determine fat JAR for module <${moduleName}>!${ESC_RESET}"
- cd "${CURRENT_PATH}"
- exit 1
- fi
- bundle "windows" "${flavor}" "${bundleBasePath}" "${WINDOWS_JRE_URL}" "bundle-launcher.cmd" "${jarFile}" "${targetPath}" "${executable}" "${binName}.exe"
- bundle "linux" "${flavor}" "${bundleBasePath}" "${LINUX_JRE_URL}" "bundle-launcher.sh" "${jarFile}" "${targetPath}" "${executable}" "${binName}"
- cd "${CURRENT_PATH}"
- echo -e "> Done."
|