.package.zsh 6.0 KB

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