jexefy.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # /////////////////////////////////////////////////////////////////////////////
  2. # REFCODES.ORG
  3. # =============================================================================
  4. # This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
  5. # under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
  6. # licenses:
  7. # =============================================================================
  8. # GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
  9. # =============================================================================
  10. # Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
  11. # =============================================================================
  12. # Please contact the copyright holding author(s) of the software artifacts in
  13. # question for licensing issues not being covered by the above listed licenses,
  14. # also regarding commercial licensing models or regarding the compatibility
  15. # with other open source licenses.
  16. # /////////////////////////////////////////////////////////////////////////////
  17. #!/bin/bash
  18. # ------------------------------------------------------------------------------
  19. # INIT:
  20. # ------------------------------------------------------------------------------
  21. TARGET_DIR="target"
  22. CURRENT_PATH="$(pwd)"
  23. SCRIPT_PATH="$(dirname $0)"
  24. cd "${SCRIPT_PATH}"
  25. SCRIPT_PATH="$(pwd)"
  26. cd "${CURRENT_PATH}"
  27. SCRIPT_DIR="${SCRIPT_PATH##*/}"
  28. SCRIPT_NAME="$(basename $0 .sh)"
  29. PARENT_PATH="$(realpath $(dirname $0)/..)"
  30. PARENT_DIR="${PARENT_PATH##*/}"
  31. MODULE_NAME="$(echo -e "${SCRIPT_DIR}" | cut -d- -f3- )"
  32. if [ -z "${MODULE_NAME}" ]; then
  33. MODULE_NAME="$(echo -e "${SCRIPT_DIR}" | cut -d- -f2- )"
  34. if [ -z "${MODULE_NAME}" ]; then
  35. MODULE_NAME="${SCRIPT_DIR}"
  36. fi
  37. fi
  38. if [ -z ${COLUMNS} ] ; then
  39. export COLUMNS=$(tput cols)
  40. fi
  41. # ------------------------------------------------------------------------------
  42. # ANSI ESCAPE CODES:
  43. # ------------------------------------------------------------------------------
  44. ESC_BOLD="\E[1m"
  45. ESC_FAINT="\E[2m"
  46. ESC_FG_RED="\E[31m"
  47. ESC_FG_GREEN="\E[32m"
  48. ESC_FG_YELLOW="\E[33m"
  49. ESC_FG_BLUE="\E[34m"
  50. ESC_FG_MAGENTA="\E[35m"
  51. ESC_FG_CYAN="\E[36m"
  52. ESC_FG_WHITE="\E[37m"
  53. ESC_RESET="\E[0m"
  54. # ------------------------------------------------------------------------------
  55. # PRINTLN:
  56. # ------------------------------------------------------------------------------
  57. function printLn {
  58. char="-"
  59. if [[ $# == 1 ]] ; then
  60. char="$1"
  61. fi
  62. echo -en "${ESC_FAINT}"
  63. for (( i=0; i< ${COLUMNS}; i++ )) ; do
  64. echo -en "${char}"
  65. done
  66. echo -e "${ESC_RESET}"
  67. }
  68. # ------------------------------------------------------------------------------
  69. # QUIT:
  70. # ------------------------------------------------------------------------------
  71. function quit {
  72. input=""
  73. while ([ "$input" != "q" ] && [ "$input" != "y" ]); do
  74. echo -ne "> Continue? Enter [${ESC_BOLD}q${ESC_RESET}] to quit, [${ESC_BOLD}y${ESC_RESET}] to continue: ";
  75. read input;
  76. done
  77. # printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
  78. if [ "$input" == "q" ] ; then
  79. printLn
  80. echo -e "> ${ESC_BOLD}Aborting due to user input.${ESC_RESET}"
  81. cd "${CURRENT_PATH}"
  82. exit 1
  83. fi
  84. }
  85. # ------------------------------------------------------------------------------
  86. # BANNER:
  87. # ------------------------------------------------------------------------------
  88. function printBanner {
  89. figlet -w 180 "/${MODULE_NAME}:>>>${SCRIPT_NAME}..." 2> /dev/null
  90. if [ $? -ne 0 ]; then
  91. banner "${SCRIPT_NAME}..." 2> /dev/null
  92. if [ $? -ne 0 ]; then
  93. echo -e "> ${SCRIPT_NAME}:" | tr a-z A-Z
  94. fi
  95. fi
  96. }
  97. printBanner
  98. # ------------------------------------------------------------------------------
  99. # HELP:
  100. # ------------------------------------------------------------------------------
  101. function printHelp {
  102. printLn
  103. echo -e "Usage: ${ESC_BOLD}${SCRIPT_NAME}${ESC_RESET}.sh [ -h ] | [ <appName> [ <appVersion> ] ]"
  104. printLn
  105. echo -ne "jEXEfy all this project in case an executable jar JAR file is found, e.g. a fat self-executable executable is created if applicable. "
  106. echo -ne "In case there is a <${ESC_BOLD}${SCRIPT_NAME}.conf${ESC_RESET}> file, then the ${ESC_BOLD}APP_NAME${ESC_RESET} property is used as ${ESC_BOLD}<appName>${ESC_RESET} and the ${ESC_BOLD}APP_VERSION${ESC_RESET} property is used as the ${ESC_BOLD}<appVersion>${ESC_RESET} (if not specified other on the command line). "
  107. echo -e "An empty version declaration or a version specified as ${ESC_BOLD}\"-1\"${ESC_RESET} denotes that no version is to be used in the resulting filename!"
  108. printLn
  109. echo -e " ${ESC_BOLD}<appName>${ESC_RESET}: The name of the file to be created (optional)."
  110. echo -e "${ESC_BOLD}<appVersion>${ESC_RESET}: The version of the file with <appName> to be created (optional)."
  111. echo -e " ${ESC_BOLD}-h${ESC_RESET}: Print this help"
  112. printLn
  113. }
  114. if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
  115. printHelp
  116. exit 0
  117. fi
  118. # ------------------------------------------------------------------------------
  119. # JEXEFY:
  120. # ------------------------------------------------------------------------------
  121. function jexefy {
  122. local jarFile=$1
  123. local launcher=$2
  124. local targetPath=$3
  125. local binName=$4
  126. local arch=${launcher/jexefy-launcher-/}
  127. if [[ $arch = *.exe ]] ; then
  128. arch="${arch/\.exe/}"
  129. arch="Windows-${arch}"
  130. fi
  131. if [[ $arch = *.elf ]] ; then
  132. arch="${arch/\.elf/}"
  133. arch="Linux-${arch}"
  134. fi
  135. binFile="${targetPath}/${binName}"
  136. cat "${SCRIPT_PATH}/${launcher}" "${jarFile}" > "${binFile}" && chmod +x ${binFile}
  137. if (($? != 0)); then
  138. echo -e "> ${ESC_BOLD}Unable to create executable <${binName}> at <${targetPath}>!${ESC_RESET}" 1>&2;
  139. exit 1
  140. fi
  141. echo -e "> Created executable <${ESC_BOLD}${binName}${ESC_RESET}> for <${ESC_BOLD}${arch}${ESC_RESET}> at <${ESC_BOLD}${targetPath}${ESC_RESET}>!"
  142. }
  143. # ------------------------------------------------------------------------------
  144. # LAUNCHER NAME:
  145. # ------------------------------------------------------------------------------
  146. function toLauncherBaseName {
  147. local moduleName=$1
  148. local moduleVersion=$2
  149. local suffix=$3
  150. local arch=$4
  151. local argBinName=$5
  152. local confBinName=$6
  153. local argVersion=$7
  154. local confVersion=$8
  155. local binVersion="${argVersion}"
  156. if [ -z "${binVersion}" ]; then
  157. binVersion="${confVersion}"
  158. if [ -z "${binVersion}" ]; then
  159. binVersion="${moduleVersion}"
  160. fi
  161. fi
  162. local binName="${argBinName}"
  163. if [ ! -z "${binName}" ]; then
  164. if [ ! -z "${argVersion}" ] && [ "${argVersion}" != "-1" ] ; then
  165. binName="${binName}-${argVersion}"
  166. fi
  167. else
  168. binName=${confBinName}
  169. if [ ! -z "${binName}" ]; then
  170. if [[ "${binVersion}" == "-1" ]] ; then
  171. binName="${binName}"
  172. else
  173. binName="${binName}-${binVersion}"
  174. fi
  175. else
  176. binName="$(echo -e "${moduleName}" | cut -d- -f3- )"
  177. if [ -z "${binName}" ]; then
  178. binName="$(echo -e "${moduleName}" | cut -d- -f2- )"
  179. if [ -z "${binName}" ]; then
  180. binName="${moduleName}"
  181. fi
  182. fi
  183. if [[ "${binVersion}" == "-1" ]] ; then
  184. binName="${binName}-launcher-${arch}"
  185. else
  186. binName="${binName}-launcher-${arch}-${binVersion}"
  187. fi
  188. fi
  189. fi
  190. echo "${binName}.${suffix}"
  191. }
  192. # ------------------------------------------------------------------------------
  193. # MAIN:
  194. # ------------------------------------------------------------------------------
  195. CONF_PATH="${SCRIPT_PATH}/${SCRIPT_NAME}.conf"
  196. if [ -f "${CONF_PATH}" ]; then
  197. . ${CONF_PATH}
  198. fi
  199. moduleName=$(xml2 < "${SCRIPT_PATH}/pom.xml" 2>/dev/null | grep "/project/artifactId=")
  200. moduleName="${moduleName#/project/artifactId=}"
  201. if [ -z "${moduleName}" ]; then
  202. moduleName=$(mvn org.apache.maven.plugins:maven-help-plugin:evaluate -Dexpression=project.artifactId -q -DforceStdout)
  203. fi
  204. moduleVersion="$(xml2 <"${SCRIPT_PATH}/pom.xml" | grep "/project/version=")"
  205. moduleVersion="${moduleVersion#/project/version=}"
  206. moduleVersion="${moduleVersion/-SNAPSHOT}"
  207. moduleVersion="${moduleVersion/-RELEASE}"
  208. if [ -z "${moduleVersion}" ]; then
  209. moduleVersion=$(mvn org.apache.maven.plugins:maven-help-plugin:evaluate -Dexpression=project.version -q -DforceStdout)
  210. if [ -z "${moduleVersion}" ]; then
  211. moduleVersion="X.Y.Z"
  212. else
  213. moduleVersion="${moduleVersion#/project/version=}"
  214. moduleVersion="${moduleVersion/-SNAPSHOT}"
  215. moduleVersion="${moduleVersion/-RELEASE}"
  216. fi
  217. fi
  218. targetPath="${SCRIPT_PATH}/${TARGET_DIR}"
  219. if [ ! -e "${targetPath}" ] ; then
  220. echo -e "> ${ESC_BOLD}Folder <${targetPath}> does not exist, aborting!${ESC_RESET}" 1>&2;
  221. exit 1
  222. fi
  223. jarFile=$(find "${targetPath}" -name "${moduleName}*.jar" ! -name "*tests.jar" ! -name "*sources.jar")
  224. if [ ! -e "${jarFile}" ] ; then
  225. echo -e "> ${ESC_BOLD}No JAR file found for module <${moduleName}> in folder <${targetPath}>, aborting!${ESC_RESET}" 1>&2;
  226. exit 1
  227. fi
  228. if command -v "unzip" &> /dev/null ; then
  229. isExecutable=$(unzip -q -c ${jarFile} META-INF/MANIFEST.MF | grep "Main-Class:")
  230. if [ -z "${isExecutable}" ] ; then
  231. echo -e "> ${ESC_BOLD}JAR file at <${jarFile}> is not executable, aborting!${ESC_RESET}" 1>&2;
  232. exit 1
  233. fi
  234. fi
  235. argAppName="$1"
  236. argAppVersion="$2"
  237. if [ -v APP_VERSION ] && [ -z "${APP_VERSION}" ] ; then
  238. APP_VERSION=-1
  239. fi
  240. jexefy "${jarFile}" "jexefy-launcher-x86_64.elf" "${targetPath}" "$(toLauncherBaseName "${moduleName}" "${moduleVersion}" "elf" "x86_64" "${argAppName}" "${APP_NAME}" "${argAppVersion}" "${APP_VERSION}")"
  241. jexefy "${jarFile}" "jexefy-launcher-x86_64.exe" "${targetPath}" "$(toLauncherBaseName "${moduleName}" "${moduleVersion}" "exe" "x86_64" "${argAppName}" "${APP_NAME}" "${argAppVersion}" "${APP_VERSION}")"
  242. cd "${CURRENT_PATH}"