.package.zsh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. *) log_error "Unknown option: %B${1}%b"; log_output ${_usage}; exit 2 ;;
  105. }
  106. }
  107. set -- ${(@)args}
  108. set_loglevel ${_verbosity}
  109. check_${host_os}
  110. read -r product_name product_version <<< \
  111. "$(jq -r '. | {name, version} | join(" ")' ${project_root}/buildspec.json)"
  112. if [[ ${host_os} == 'macos' ]] {
  113. autoload -Uz check_packages check_xcnotary read_codesign read_codesign_installer read_codesign_pass
  114. local output_name="${product_name}-${product_version}-${host_os}-${target##*-}.pkg"
  115. if [[ ! -d ${project_root}/release/${product_name}.plugin ]] {
  116. log_error 'No release artifact found. Run the build script or the CMake install procedure first.'
  117. return 2
  118. }
  119. if [[ ! -f ${project_root}/build_${target}/installer-macos.generated.pkgproj ]] {
  120. log_error 'Packages project file not found. Run the build script or the CMake build and install procedures first.'
  121. return 2
  122. }
  123. check_packages
  124. log_info "Packaging ${product_name}..."
  125. pushd ${project_root}
  126. packagesbuild \
  127. --build-folder ${project_root}/release \
  128. ${project_root}/build_${target}/installer-macos.generated.pkgproj
  129. if (( ${+CODESIGN} )) {
  130. read_codesign_installer
  131. productsign \
  132. --sign "${CODESIGN_IDENT_INSTALLER}" \
  133. "${project_root}/release/${product_name}.pkg" \
  134. "${project_root}/release/${output_name}"
  135. rm "${project_root}/release/${product_name}.pkg"
  136. } else {
  137. mv "${project_root}/release/${product_name}.pkg" \
  138. "${project_root}/release/${output_name}"
  139. }
  140. if (( ${+CODESIGN} + ${+NOTARIZE} > 1 )) {
  141. check_xcnotary
  142. local _error=0
  143. if [[ -f "${project_root}/release/${output_name}" ]] {
  144. xcnotary precheck "${project_root}/release/${output_name}" || _error=1
  145. } else {
  146. log_error "No package for notarization found."
  147. return 2
  148. }
  149. if (( ! _error )) {
  150. read_codesign_installer
  151. read_codesign_pass
  152. xcnotary notarize "${project_root}/release/${output_name}" \
  153. --developer-account "${CODESIGN_IDENT_USER}" \
  154. --developer-password-keychain-item "OBS-Codesign-Password" \
  155. --provider "${CODESIGN_IDENT_SHORT}"
  156. }
  157. }
  158. popd
  159. } elif [[ ${host_os} == 'linux' ]] {
  160. local -a cmake_args=()
  161. if (( _loglevel > 1 )) cmake_args+=(--verbose)
  162. pushd ${project_root}
  163. cmake --build build_${target} --config ${BUILD_CONFIG:-RelWithDebInfo} -t package ${cmake_args}
  164. popd
  165. }
  166. }
  167. package "${@}"