build 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env bash
  2. # build - build Jellyfin binaries or packages
  3. set -o errexit
  4. set -o pipefail
  5. # The list of possible package actions (except 'clean')
  6. declare -a actions=( 'build' 'package' 'sign' 'publish' )
  7. # The list of possible platforms, based on directories under 'deployment/'
  8. declare -a platforms=( $(
  9. find deployment/ -maxdepth 1 -mindepth 1 -type d -exec basename {} \; | sort
  10. ) )
  11. # The list of standard dependencies required by all build scripts; individual
  12. # action scripts may specify their own dependencies
  13. declare -a dependencies=( 'tar' 'zip' )
  14. usage() {
  15. echo -e "build - build Jellyfin binaries or packages"
  16. echo -e ""
  17. echo -e "Usage:"
  18. echo -e " $ build --list-platforms"
  19. echo -e " $ build --list-actions <platform>"
  20. echo -e " $ build [-k/--keep-artifacts] [-b/--web-branch <web_branch>] <platform> <action>"
  21. echo -e ""
  22. echo -e "The 'keep-artifacts' option preserves build artifacts, e.g. Docker images for system package builds."
  23. echo -e "The web_branch defaults to the same branch name as the current main branch or can be 'local' to not touch the submodule branching."
  24. echo -e "To build all platforms, use 'all'."
  25. echo -e "To perform all build actions, use 'all'."
  26. echo -e "Build output files are collected at '../bin/<platform>'."
  27. }
  28. # Show usage on stderr with exit 1 on argless
  29. if [[ -z $1 ]]; then
  30. usage >&2
  31. exit 1
  32. fi
  33. # Show usage if -h or --help are specified in the args
  34. if [[ $@ =~ '-h' || $@ =~ '--help' ]]; then
  35. usage
  36. exit 0
  37. fi
  38. # List all available platforms then exit
  39. if [[ $1 == '--list-platforms' ]]; then
  40. echo -e "Available platforms:"
  41. for platform in ${platforms[@]}; do
  42. echo -e " ${platform}"
  43. done
  44. exit 0
  45. fi
  46. # List all available actions for a given platform then exit
  47. if [[ $1 == '--list-actions' ]]; then
  48. platform="$2"
  49. if [[ ! " ${platforms[@]} " =~ " ${platform} " ]]; then
  50. echo "ERROR: Platform ${platform} does not exist."
  51. exit 1
  52. fi
  53. echo -e "Available actions for platform ${platform}:"
  54. for action in ${actions[@]}; do
  55. if [[ -f deployment/${platform}/${action}.sh ]]; then
  56. echo -e " ${action}"
  57. fi
  58. done
  59. exit 0
  60. fi
  61. # Parse keep-artifacts option
  62. if [[ $1 == '-k' || $1 == '--keep-artifacts' ]]; then
  63. keep_artifacts="y"
  64. shift 1
  65. else
  66. keep_artifacts="n"
  67. fi
  68. # Parse branch option
  69. if [[ $1 == '-b' || $1 == '--web-branch' ]]; then
  70. web_branch="$2"
  71. shift 2
  72. else
  73. web_branch="$( git describe --tags --exact-match || true )"
  74. if [[ -z "$web_branch" ]]; then
  75. web_branch="$( git branch 2>/dev/null | grep -v 'HEAD detached' | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' )"
  76. if [[ -z "${web_branch}" ]]; then
  77. echo "Cannot determine web branch, pass it explicitly via --web-branch option"
  78. exit 1
  79. fi
  80. fi
  81. fi
  82. # Parse platform option
  83. if [[ -n $1 ]]; then
  84. cli_platform="$1"
  85. shift
  86. else
  87. echo "ERROR: A platform must be specified. Use 'all' to specify all platforms."
  88. exit 1
  89. fi
  90. if [[ ${cli_platform} == 'all' ]]; then
  91. declare -a platform=( ${platforms[@]} )
  92. else
  93. if [[ ! " ${platforms[@]} " =~ " ${cli_platform} " ]]; then
  94. echo "ERROR: Platform ${cli_platform} is invalid. Use the '--list-platforms' option to list available platforms."
  95. exit 1
  96. else
  97. declare -a platform=( "${cli_platform}" )
  98. fi
  99. fi
  100. # Parse action option
  101. if [[ -n $1 ]]; then
  102. cli_action="$1"
  103. shift
  104. else
  105. echo "ERROR: An action must be specified. Use 'all' to specify all actions."
  106. exit 1
  107. fi
  108. if [[ ${cli_action} == 'all' ]]; then
  109. declare -a action=( ${actions[@]} )
  110. else
  111. if [[ ! " ${actions[@]} " =~ " ${cli_action} " ]]; then
  112. echo "ERROR: Action ${cli_action} is invalid. Use the '--list-actions <platform>' option to list available actions."
  113. exit 1
  114. else
  115. declare -a action=( "${cli_action}" )
  116. fi
  117. fi
  118. # Verify required utilities are installed
  119. missing_deps=()
  120. for utility in ${dependencies[@]}; do
  121. if ! which ${utility} &>/dev/null; then
  122. missing_deps+=( ${utility} )
  123. fi
  124. done
  125. # Error if we're missing anything
  126. if [[ ${#missing_deps[@]} -gt 0 ]]; then
  127. echo -e "ERROR: This script requires the following missing utilities:"
  128. for utility in ${missing_deps[@]}; do
  129. echo -e " ${utility}"
  130. done
  131. exit 1
  132. fi
  133. # Parse platform-specific dependencies
  134. for target_platform in ${platform[@]}; do
  135. # Read platform-specific dependencies
  136. if [[ -f deployment/${target_platform}/dependencies.txt ]]; then
  137. platform_dependencies="$( grep -v '^#' deployment/${target_platform}/dependencies.txt )"
  138. # Verify required utilities are installed
  139. missing_deps=()
  140. for utility in ${platform_dependencies[@]}; do
  141. if ! which ${utility} &>/dev/null; then
  142. missing_deps+=( ${utility} )
  143. fi
  144. done
  145. # Error if we're missing anything
  146. if [[ ${#missing_deps[@]} -gt 0 ]]; then
  147. echo -e "ERROR: The ${target_platform} platform requires the following utilities:"
  148. for utility in ${missing_deps[@]}; do
  149. echo -e " ${utility}"
  150. done
  151. exit 1
  152. fi
  153. fi
  154. done
  155. # Execute each platform and action in order, if said action is enabled
  156. pushd deployment/
  157. for target_platform in ${platform[@]}; do
  158. echo -e "> Processing platform ${target_platform}"
  159. date_start=$( date +%s )
  160. pushd ${target_platform}
  161. cleanup() {
  162. echo -e ">> Processing action clean"
  163. if [[ -f clean.sh && -x clean.sh ]]; then
  164. ./clean.sh ${keep_artifacts}
  165. fi
  166. }
  167. trap cleanup EXIT INT
  168. for target_action in ${action[@]}; do
  169. echo -e ">> Processing action ${target_action}"
  170. if [[ -f ${target_action}.sh && -x ${target_action}.sh ]]; then
  171. ./${target_action}.sh web_branch=${web_branch}
  172. fi
  173. done
  174. if [[ -d pkg-dist/ ]]; then
  175. echo -e ">> Collecting build artifacts"
  176. target_dir="../../../bin/${target_platform}"
  177. mkdir -p ${target_dir}
  178. mv pkg-dist/* ${target_dir}/
  179. fi
  180. cleanup
  181. date_end=$( date +%s )
  182. echo -e "> Completed platform ${target_platform} in $( expr ${date_end} - ${date_start} ) seconds."
  183. popd
  184. done
  185. popd