.package.zsh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/usr/bin/env zsh
  2. builtin emulate -L zsh
  3. setopt EXTENDED_GLOB
  4. setopt PUSHD_SILENT
  5. setopt ERR_EXIT
  6. setopt ERR_RETURN
  7. setopt NO_UNSET
  8. setopt PIPE_FAIL
  9. setopt NO_AUTO_PUSHD
  10. setopt NO_PUSHD_IGNORE_DUPS
  11. setopt FUNCTION_ARGZERO
  12. ## Enable for script debugging
  13. # setopt WARN_CREATE_GLOBAL
  14. # setopt WARN_NESTED_VAR
  15. # setopt XTRACE
  16. autoload -Uz is-at-least && if ! is-at-least 5.2; then
  17. print -u2 -PR "${CI:+::error::}%F{1}${funcstack[1]##*/}:%f Running on Zsh version %B${ZSH_VERSION}%b, but Zsh %B5.2%b is the minimum supported version. Upgrade Zsh to fix this issue."
  18. exit 1
  19. fi
  20. TRAPEXIT() {
  21. local return_value=$?
  22. if (( ${+CI} )) {
  23. unset NSUnbufferedIO
  24. }
  25. return ${return_value}
  26. }
  27. TRAPZERR() {
  28. if (( ${_loglevel:-3} > 2 )) {
  29. print -u2 -PR "${CI:+::error::}%F{1} ✖︎ script execution error%f"
  30. print -PR -e "
  31. Callstack:
  32. ${(j:\n :)funcfiletrace}
  33. "
  34. }
  35. exit 2
  36. }
  37. package() {
  38. if (( ! ${+SCRIPT_HOME} )) typeset -g SCRIPT_HOME=${ZSH_ARGZERO:A:h}
  39. local host_os=${${(s:-:)ZSH_ARGZERO:t:r}[2]}
  40. local project_root=${SCRIPT_HOME:A:h:h}
  41. local buildspec_file=${project_root}/buildspec.json
  42. fpath=("${SCRIPT_HOME}/utils.zsh" ${fpath})
  43. autoload -Uz set_loglevel log_info log_group log_error log_output check_${host_os}
  44. if [[ ! -r ${buildspec_file} ]] {
  45. log_error \
  46. 'No buildspec.json found. Please create a build specification for your project.'
  47. return 2
  48. }
  49. local -i verbosity=1
  50. local -r _version='2.0.0'
  51. local -r -a _valid_targets=(
  52. macos-universal
  53. linux-x86_64
  54. )
  55. local target
  56. local config='RelWithDebInfo'
  57. local -r -a _valid_configs=(Debug RelWithDebInfo Release MinSizeRel)
  58. local -i codesign=0
  59. local -i notarize=0
  60. local -i package=0
  61. local -i skip_deps=0
  62. if [[ ${host_os} == macos ]] {
  63. local -r _usage_host="
  64. %F{yellow} Additional options for macOS builds%f
  65. -----------------------------------------------------------------------------
  66. %B-s | --codesign%b Enable codesigning (macOS only)
  67. %B-n | --notarize%b Enable notarization (macOS only)
  68. %B-p | --package%b Create package installer (macOS only)"
  69. }
  70. local -r _usage="
  71. Usage: %B${functrace[1]%:*}%b <option> [<options>]
  72. %BOptions%b:
  73. %F{yellow} Package configuration options%f
  74. -----------------------------------------------------------------------------
  75. %B-t | --target%b Specify target
  76. %B-c | --config%b Build configuration
  77. %B--skip-deps%b Skip checking for dependencies
  78. %F{yellow} Output options%f
  79. -----------------------------------------------------------------------------
  80. %B-q | --quiet%b Quiet (error output only)
  81. %B-v | --verbose%b Verbose (more detailed output)
  82. %B--debug%b Debug (very detailed and added output)
  83. %F{yellow} General options%f
  84. -----------------------------------------------------------------------------
  85. %B-h | --help%b Print this usage help
  86. %B-V | --version%b Print script version information
  87. ${_usage_host:-}"
  88. local -a args
  89. while (( # )) {
  90. case ${1} {
  91. -t|--target|-c|--config)
  92. if (( # == 1 )) || [[ ${2:0:1} == '-' ]] {
  93. log_error "Missing value for option %B${1}%b"
  94. log_output ${_usage}
  95. exit 2
  96. }
  97. ;;
  98. }
  99. case ${1} {
  100. --)
  101. shift
  102. args+=($@)
  103. break
  104. ;;
  105. -t|--target)
  106. if (( ! ${_valid_targets[(Ie)${2}]} )) {
  107. log_error "Invalid value %B${2}%b for option %B${1}%b"
  108. log_output ${_usage}
  109. exit 2
  110. }
  111. target=${2}
  112. shift 2
  113. ;;
  114. -c|--config)
  115. if (( !${_valid_configs[(Ie)${2}]} )) {
  116. log_error "Invalid value %B${2}%b for option %B${1}%b"
  117. log_output ${_usage}
  118. exit 2
  119. }
  120. config=${2}
  121. shift 2
  122. ;;
  123. -s|--codesign) typeset -g codesign=1; shift ;;
  124. -n|--notarize) typeset -g notarize=1; typeset -g codesign=1; shift ;;
  125. -p|--package) typeset -g package=1; shift ;;
  126. --skip-deps) typeset -g skip_deps=1; shift ;;
  127. -q|--quiet) (( verbosity -= 1 )) || true; shift ;;
  128. -v|--verbose) (( verbosity += 1 )); shift ;;
  129. -h|--help) log_output ${_usage}; exit 0 ;;
  130. -V|--version) print -Pr "${_version}"; exit 0 ;;
  131. --debug) verbosity=3; shift ;;
  132. *) log_error "Unknown option: %B${1}%b"; log_output ${_usage}; exit 2 ;;
  133. }
  134. }
  135. : "${target:="${host_os}-${CPUTYPE}"}"
  136. set -- ${(@)args}
  137. set_loglevel ${verbosity}
  138. if (( ! skip_deps )) {
  139. check_${host_os}
  140. }
  141. local product_name
  142. local product_version
  143. read -r product_name product_version <<< \
  144. "$(jq -r '. | {name, version} | join(" ")' ${buildspec_file})"
  145. if [[ ${host_os} == macos ]] {
  146. autoload -Uz check_packages read_codesign read_codesign_installer read_codesign_pass
  147. local output_name="${product_name}-${product_version}-${host_os}-universal"
  148. if [[ ! -d ${project_root}/release/${config}/${product_name}.plugin ]] {
  149. log_error 'No release artifact found. Run the build script or the CMake install procedure first.'
  150. return 2
  151. }
  152. local _tarflags='cJf'
  153. if (( _loglevel > 1 || ${+CI} )) _tarflags="v${_tarflags}"
  154. if (( package )) {
  155. if [[ ! -f ${project_root}/release/${config}/${product_name}.pkg ]] {
  156. log_error 'Installer Package not found. Run the build script or the CMake build and install procedures first.'
  157. return 2
  158. }
  159. log_group "Packaging ${product_name}..."
  160. pushd ${project_root}
  161. if (( codesign )) {
  162. read_codesign_installer
  163. productsign \
  164. --sign "${CODESIGN_IDENT_INSTALLER}" \
  165. ${project_root}/release/${config}/${product_name}.pkg \
  166. ${project_root}/release/${output_name}.pkg
  167. rm ${project_root}/release/${config}/${product_name}.pkg
  168. } else {
  169. mv ${project_root}/release/${config}/${product_name}.pkg \
  170. ${project_root}/release/${output_name}.pkg
  171. }
  172. if (( codesign && notarize )) {
  173. if [[ ! -f ${project_root}/release/${output_name}.pkg ]] {
  174. log_error "No package for notarization found."
  175. return 2
  176. }
  177. read_codesign_installer
  178. read_codesign_pass
  179. xcrun notarytool submit ${project_root}/release/${output_name}.pkg \
  180. --keychain-profile "OBS-Codesign-Password" --wait
  181. local -i _status=0
  182. xcrun stapler staple ${project_root}/release/${output_name}.pkg || _status=1
  183. if (( _status )) {
  184. log_error "Notarization failed. Use 'xcrun notarytool log <submission ID>' to check for errors."
  185. return 2
  186. }
  187. }
  188. popd
  189. } else {
  190. log_group "Archiving ${product_name}..."
  191. pushd ${project_root}/release/${config}
  192. XZ_OPT=-T0 tar "-${_tarflags}" ${project_root}/release/${output_name}.tar.xz ${product_name}.plugin
  193. popd
  194. }
  195. if [[ ${config} == Release ]] {
  196. log_group "Archiving ${product_name} Debug Symbols..."
  197. pushd ${project_root}/release/${config}
  198. XZ_OPT=-T0 tar "-${_tarflags}" ${project_root}/release/${output_name}-dSYMs.tar.xz ${product_name}.plugin.dSYM
  199. popd
  200. }
  201. log_group
  202. } elif [[ ${host_os} == linux ]] {
  203. local -a cmake_args=()
  204. if (( _loglevel > 1 )) cmake_args+=(--verbose)
  205. log_group "Creating source tarball for ${product_name}..."
  206. pushd ${project_root}
  207. cmake --build build_${target##*-} --config ${config} -t package_source ${cmake_args}
  208. popd
  209. if (( package )) {
  210. log_group "Packaging ${product_name}..."
  211. pushd ${project_root}
  212. cmake --build build_${target##*-} --config ${config} -t package ${cmake_args}
  213. popd
  214. } else {
  215. log_group "Archiving ${product_name}..."
  216. local output_name="${product_name}-${product_version}-${target##*-}-linux-gnu"
  217. local _tarflags='cJf'
  218. if (( _loglevel > 1 || ${+CI} )) _tarflags="v${_tarflags}"
  219. pushd ${project_root}/release/${config}
  220. XZ_OPT=-T0 tar "-${_tarflags}" ${project_root}/release/${output_name}.tar.xz (lib|share)
  221. popd
  222. }
  223. log_group
  224. }
  225. }
  226. package ${@}