build 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' )"
  74. fi
  75. # Parse platform option
  76. if [[ -n $1 ]]; then
  77. cli_platform="$1"
  78. shift
  79. else
  80. echo "ERROR: A platform must be specified. Use 'all' to specify all platforms."
  81. exit 1
  82. fi
  83. if [[ ${cli_platform} == 'all' ]]; then
  84. declare -a platform=( ${platforms[@]} )
  85. else
  86. if [[ ! " ${platforms[@]} " =~ " ${cli_platform} " ]]; then
  87. echo "ERROR: Platform ${cli_platform} is invalid. Use the '--list-platforms' option to list available platforms."
  88. exit 1
  89. else
  90. declare -a platform=( "${cli_platform}" )
  91. fi
  92. fi
  93. # Parse action option
  94. if [[ -n $1 ]]; then
  95. cli_action="$1"
  96. shift
  97. else
  98. echo "ERROR: An action must be specified. Use 'all' to specify all actions."
  99. exit 1
  100. fi
  101. if [[ ${cli_action} == 'all' ]]; then
  102. declare -a action=( ${actions[@]} )
  103. else
  104. if [[ ! " ${actions[@]} " =~ " ${cli_action} " ]]; then
  105. echo "ERROR: Action ${cli_action} is invalid. Use the '--list-actions <platform>' option to list available actions."
  106. exit 1
  107. else
  108. declare -a action=( "${cli_action}" )
  109. fi
  110. fi
  111. # Verify required utilities are installed
  112. missing_deps=()
  113. for utility in ${dependencies[@]}; do
  114. if ! which ${utility} &>/dev/null; then
  115. missing_deps+=( ${utility} )
  116. fi
  117. done
  118. # Error if we're missing anything
  119. if [[ ${#missing_deps[@]} -gt 0 ]]; then
  120. echo -e "ERROR: This script requires the following missing utilities:"
  121. for utility in ${missing_deps[@]}; do
  122. echo -e " ${utility}"
  123. done
  124. exit 1
  125. fi
  126. # Parse platform-specific dependencies
  127. for target_platform in ${platform[@]}; do
  128. # Read platform-specific dependencies
  129. if [[ -f deployment/${target_platform}/dependencies.txt ]]; then
  130. platform_dependencies="$( grep -v '^#' deployment/${target_platform}/dependencies.txt )"
  131. # Verify required utilities are installed
  132. missing_deps=()
  133. for utility in ${platform_dependencies[@]}; do
  134. if ! which ${utility} &>/dev/null; then
  135. missing_deps+=( ${utility} )
  136. fi
  137. done
  138. # Error if we're missing anything
  139. if [[ ${#missing_deps[@]} -gt 0 ]]; then
  140. echo -e "ERROR: The ${target_platform} platform requires the following utilities:"
  141. for utility in ${missing_deps[@]}; do
  142. echo -e " ${utility}"
  143. done
  144. exit 1
  145. fi
  146. fi
  147. done
  148. # Execute each platform and action in order, if said action is enabled
  149. pushd deployment/
  150. for target_platform in ${platform[@]}; do
  151. echo -e "> Processing platform ${target_platform}"
  152. date_start=$( date +%s )
  153. pushd ${target_platform}
  154. cleanup() {
  155. echo -e ">> Processing action clean"
  156. if [[ -f clean.sh && -x clean.sh ]]; then
  157. ./clean.sh ${keep_artifacts}
  158. fi
  159. }
  160. trap cleanup EXIT INT
  161. for target_action in ${action[@]}; do
  162. echo -e ">> Processing action ${target_action}"
  163. if [[ -f ${target_action}.sh && -x ${target_action}.sh ]]; then
  164. ./${target_action}.sh web_branch=${web_branch}
  165. fi
  166. done
  167. if [[ -d pkg-dist/ ]]; then
  168. echo -e ">> Collecting build artifacts"
  169. target_dir="../../../bin/${target_platform}"
  170. mkdir -p ${target_dir}
  171. mv pkg-dist/* ${target_dir}/
  172. fi
  173. cleanup
  174. date_end=$( date +%s )
  175. echo -e "> Completed platform ${target_platform} in $( expr ${date_end} - ${date_start} ) seconds."
  176. popd
  177. done
  178. popd