scriptify.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. CURRENT_PATH="$(pwd)"
  22. SCRIPT_PATH="$(dirname $0)"
  23. cd "${SCRIPT_PATH}"
  24. SCRIPT_PATH="$(pwd)"
  25. cd "${CURRENT_PATH}"
  26. SCRIPT_DIR="${SCRIPT_PATH##*/}"
  27. SCRIPT_NAME="$(basename $0 .sh)"
  28. PARENT_PATH="$(realpath $(dirname $0)/..)"
  29. PARENT_DIR="${PARENT_PATH##*/}"
  30. MODULE_NAME="$(echo -e "${SCRIPT_DIR}" | cut -d- -f3- )"
  31. if [ -z "${MODULE_NAME}" ]; then
  32. MODULE_NAME="$(echo -e "${SCRIPT_DIR}" | cut -d- -f2- )"
  33. if [ -z "${MODULE_NAME}" ]; then
  34. MODULE_NAME="${SCRIPT_DIR}"
  35. fi
  36. fi
  37. # ------------------------------------------------------------------------------
  38. # ANSI ESCAPE CODES:
  39. # ------------------------------------------------------------------------------
  40. ESC_BOLD="\E[1m"
  41. ESC_FAINT="\E[2m"
  42. ESC_FG_RED="\E[31m"
  43. ESC_FG_GREEN="\E[32m"
  44. ESC_FG_YELLOW="\E[33m"
  45. ESC_FG_BLUE="\E[34m"
  46. ESC_FG_MAGENTA="\E[35m"
  47. ESC_FG_CYAN="\E[36m"
  48. ESC_FG_WHITE="\E[37m"
  49. ESC_RESET="\E[0m"
  50. # ------------------------------------------------------------------------------
  51. # PRINTLN:
  52. # ------------------------------------------------------------------------------
  53. function printLn {
  54. char="-"
  55. if [[ $# == 1 ]] ; then
  56. char="$1"
  57. fi
  58. echo -en "${ESC_FAINT}"
  59. printf '%*s' "${COLUMNS:-$(tput cols)}" '' | tr ' ' ${char}
  60. echo -en "${ESC_RESET}"
  61. }
  62. # ------------------------------------------------------------------------------
  63. # QUIT:
  64. # ------------------------------------------------------------------------------
  65. function quit {
  66. input=""
  67. while ([ "$input" != "q" ] && [ "$input" != "y" ]); do
  68. echo -ne "> Continue? Enter [${ESC_BOLD}q${ESC_RESET}] to quit, [${ESC_BOLD}y${ESC_RESET}] to continue: ";
  69. read input;
  70. done
  71. # printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
  72. if [ "$input" == "q" ] ; then
  73. printLn
  74. echo -e "> ${ESC_BOLD}Aborting due to user input.${ESC_RESET}"
  75. exit
  76. fi
  77. }
  78. # ------------------------------------------------------------------------------
  79. # BANNER:
  80. # ------------------------------------------------------------------------------
  81. function printBanner {
  82. figlet -w 180 "/${MODULE_NAME}:>>>${SCRIPT_NAME}..." 2> /dev/null
  83. if [ $? -ne 0 ]; then
  84. banner "${SCRIPT_NAME}..." 2> /dev/null
  85. if [ $? -ne 0 ]; then
  86. echo -e "> ${SCRIPT_NAME}:" | tr a-z A-Z
  87. fi
  88. fi
  89. }
  90. printBanner
  91. # ------------------------------------------------------------------------------
  92. # HELP:
  93. # ------------------------------------------------------------------------------
  94. function printHelp {
  95. printLn
  96. echo -e "Usage: ${ESC_BOLD}${SCRIPT_NAME}${ESC_RESET}.sh [ -h | <appName> ]"
  97. printLn
  98. echo -e "Scriptifies all this project in case an executable jar JAR file is found, e.g. a fat self-executable shell script is created if applicable."
  99. 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)."
  100. printLn
  101. echo -e "${ESC_BOLD}<appName>${ESC_RESET}: The name of the module to be created (optional)."
  102. echo -e " ${ESC_BOLD}-h${ESC_RESET}: Print this help"
  103. printLn
  104. }
  105. if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
  106. printHelp
  107. exit 0
  108. fi
  109. # ------------------------------------------------------------------------------
  110. # MAIN:
  111. # ------------------------------------------------------------------------------
  112. targetPath="${SCRIPT_PATH}/target"
  113. moduleName=$(xml2 < "${SCRIPT_PATH}/pom.xml" 2>/dev/null | grep "/project/artifactId=")
  114. moduleName="${moduleName#/project/artifactId=}"
  115. if [ -z "${moduleName}" ]; then
  116. moduleName=$(mvn org.apache.maven.plugins:maven-help-plugin:evaluate -Dexpression=project.artifactId -q -DforceStdout)
  117. fi
  118. if [ ! -e "${targetPath}" ] ; then
  119. echo -e "> ${ESC_BOLD}Folder <${targetPath}> does not exist, aborting!${ESC_RESET}" 1>&2;
  120. exit 1
  121. fi
  122. jarFile=$(find "${targetPath}" -name "${moduleName}*.jar" ! -name "*tests.jar" ! -name "*sources.jar")
  123. if [ ! -e "${jarFile}" ] ; then
  124. echo -e "> ${ESC_BOLD}No JAR file found for module <${moduleName}> in folder <${targetPath}>, aborting!${ESC_RESET}" 1>&2;
  125. exit 1
  126. fi
  127. if command -v "unzip" &> /dev/null ; then
  128. isExecutable=$(unzip -q -c ${jarFile} META-INF/MANIFEST.MF | grep "Main-Class:")
  129. if [ -z "${isExecutable}" ] ; then
  130. echo -e "> ${ESC_BOLD}JAR file at <${jarFile}> is not executable, aborting!${ESC_RESET}" 1>&2;
  131. exit 1
  132. fi
  133. fi
  134. CONF_PATH="${SCRIPT_PATH}/${SCRIPT_NAME}.conf"
  135. if [ -f "${CONF_PATH}" ]; then
  136. . ${CONF_PATH}
  137. fi
  138. binName="$1"
  139. if [ -z "${binName}" ]; then
  140. binName=${APP_NAME}
  141. if [ -z "${binName}" ]; then
  142. binName="$(echo -e "${moduleName}" | cut -d- -f3- )"
  143. if [ -z "${binName}" ]; then
  144. binName="$(echo -e "${moduleName}" | cut -d- -f2- )"
  145. if [ -z "${binName}" ]; then
  146. binName="${moduleName}"
  147. fi
  148. fi
  149. fi
  150. fi
  151. binName="${binName}.sh"
  152. binFile="${targetPath}/${binName}"
  153. cat "${SCRIPT_PATH}/scriptify.inc" "${jarFile}" > "${binFile}" && chmod +x "${binFile}"
  154. if (($? != 0)); then
  155. echo -e "> ${ESC_BOLD}Unable to create executable binary bash script <${binName}> at <${targetPath}>!${ESC_RESET}" 1>&2;
  156. exit 1
  157. fi
  158. echo -e "> Created executable binary bash script <${ESC_BOLD}${binName}${ESC_RESET}> at <${ESC_BOLD}${targetPath}${ESC_RESET}>"
  159. cd "${CURRENT_PATH}"