.package.zsh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "%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. _trap_error() {
  21. print -u2 -PR '%F{1} ✖︎ script execution error%f'
  22. print -PR -e "
  23. Callstack:
  24. ${(j:\n :)funcfiletrace}
  25. "
  26. exit 2
  27. }
  28. package() {
  29. if (( ! ${+SCRIPT_HOME} )) typeset -g SCRIPT_HOME=${ZSH_ARGZERO:A:h}
  30. local host_os=${${(s:-:)ZSH_ARGZERO:t:r}[2]}
  31. local target="${host_os}-${CPUTYPE}"
  32. local project_root=${SCRIPT_HOME:A:h:h}
  33. local buildspec_file="${project_root}/buildspec.json"
  34. trap '_trap_error' ZERR
  35. fpath=("${SCRIPT_HOME}/utils.zsh" ${fpath})
  36. autoload -Uz set_loglevel log_info log_error log_output check_${host_os}
  37. local -i _verbosity=1
  38. local -r _version='1.0.0'
  39. local -r -a _valid_targets=(
  40. macos-x86_64
  41. macos-arm64
  42. macos-universal
  43. linux-x86_64
  44. linux-aarch64
  45. )
  46. local -r -a _valid_configs=(Debug RelWithDebInfo Release MinSizeRel)
  47. local -r _usage="
  48. Usage: %B${functrace[1]%:*}%b <option> [<options>]
  49. %BOptions%b:
  50. %F{yellow} Package configuration options%f
  51. -----------------------------------------------------------------------------
  52. %B-t | --target%b Specify target - default: %B%F{green}${host_os}-${CPUTYPE}%f%b
  53. %B-c | --config%b Build configuration - default: %B%F{green}RelWithDebInfo%f%b
  54. %B-s | --codesign%b Enable codesigning (macOS only)
  55. %B-n | --notarize%b Enable notarization (macOS only)
  56. %F{yellow} Output options%f
  57. -----------------------------------------------------------------------------
  58. %B-q | --quiet%b Quiet (error output only)
  59. %B-v | --verbose%b Verbose (more detailed output)
  60. %B--debug%b Debug (very detailed and added output)
  61. %F{yellow} General options%f
  62. -----------------------------------------------------------------------------
  63. %B-h | --help%b Print this usage help
  64. %B-V | --version%b Print script version information"
  65. local -a args
  66. while (( # )) {
  67. case ${1} {
  68. -t|--target|-c|--config)
  69. if (( # == 1 )) || [[ ${2:0:1} == '-' ]] {
  70. log_error "Missing value for option %B${1}%b"
  71. log_output ${_usage}
  72. exit 2
  73. }
  74. ;;
  75. }
  76. case ${1} {
  77. --)
  78. shift
  79. args+=($@)
  80. break
  81. ;;
  82. -t|--target)
  83. if (( ! ${_valid_targets[(Ie)${2}]} )) {
  84. log_error "Invalid value %B${2}%b for option %B${1}%b"
  85. log_output ${_usage}
  86. exit 2
  87. }
  88. target=${2}
  89. shift 2
  90. ;;
  91. -c|--config)
  92. if (( ! ${_valid_configs[(Ie)${2}]} )) {
  93. log_error "Invalid value %B${2}%b for option %B${1}%b"
  94. log_output ${_usage}
  95. exit 2
  96. }
  97. BUILD_CONFIG=${2}
  98. shift 2
  99. ;;
  100. -s|--codesign) typeset -g CODESIGN=1; shift ;;
  101. -n|--notarize) typeset -g NOTARIZE=1; typeset -g CODESIGN=1; shift ;;
  102. -q|--quiet) (( _verbosity -= 1 )) || true; shift ;;
  103. -v|--verbose) (( _verbosity += 1 )); shift ;;
  104. -h|--help) log_output ${_usage}; exit 0 ;;
  105. -V|--version) print -Pr "${_version}"; exit 0 ;;
  106. --debug) _verbosity=3; shift ;;
  107. *) log_error "Unknown option: %B${1}%b"; log_output ${_usage}; exit 2 ;;
  108. }
  109. }
  110. set -- ${(@)args}
  111. set_loglevel ${_verbosity}
  112. check_${host_os}
  113. local product_name
  114. local product_version
  115. read -r product_name product_version <<< \
  116. "$(jq -r '. | {name, version} | join(" ")' ${project_root}/buildspec.json)"
  117. if [[ ${host_os} == 'macos' ]] {
  118. autoload -Uz check_packages read_codesign read_codesign_installer read_codesign_pass
  119. local output_name="${product_name}-${product_version}-${host_os}-${target##*-}.pkg"
  120. if [[ ! -d ${project_root}/release/${product_name}.plugin ]] {
  121. log_error 'No release artifact found. Run the build script or the CMake install procedure first.'
  122. return 2
  123. }
  124. if [[ ! -f ${project_root}/build_${target##*-}/installer-macos.generated.pkgproj ]] {
  125. log_error 'Packages project file not found. Run the build script or the CMake build and install procedures first.'
  126. return 2
  127. }
  128. check_packages
  129. log_info "Packaging ${product_name}..."
  130. pushd ${project_root}
  131. packagesbuild \
  132. --build-folder ${project_root}/release \
  133. ${project_root}/build_${target##*-}/installer-macos.generated.pkgproj
  134. if (( ${+CODESIGN} )) {
  135. read_codesign_installer
  136. productsign \
  137. --sign "${CODESIGN_IDENT_INSTALLER}" \
  138. "${project_root}/release/${product_name}.pkg" \
  139. "${project_root}/release/${output_name}"
  140. rm "${project_root}/release/${product_name}.pkg"
  141. } else {
  142. mv "${project_root}/release/${product_name}.pkg" \
  143. "${project_root}/release/${output_name}"
  144. }
  145. if (( ${+CODESIGN} && ${+NOTARIZE} )) {
  146. if [[ ! -f "${project_root}/release/${output_name}" ]] {
  147. log_error "No package for notarization found."
  148. return 2
  149. }
  150. read_codesign_installer
  151. read_codesign_pass
  152. xcrun notarytool submit "${project_root}/release/${output_name}" \
  153. --keychain-profile "OBS-Codesign-Password" --wait
  154. xcrun stapler staple "${project_root}/release/${output_name}"
  155. }
  156. popd
  157. } elif [[ ${host_os} == 'linux' ]] {
  158. local -a cmake_args=()
  159. if (( _loglevel > 1 )) cmake_args+=(--verbose)
  160. pushd ${project_root}
  161. cmake --build build_${target##*-} --config ${BUILD_CONFIG:-RelWithDebInfo} -t package ${cmake_args}
  162. popd
  163. }
  164. }
  165. package ${@}