build 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 web_branch defaults to the same branch name as the current main branch."
  23. echo -e "To build all platforms, use 'all'."
  24. echo -e "To perform all build actions, use 'all'."
  25. echo -e "Build output files are collected at '../jellyfin-build/<platform>'."
  26. }
  27. # Show usage on stderr with exit 1 on argless
  28. if [[ -z $1 ]]; then
  29. usage >&2
  30. exit 1
  31. fi
  32. # Show usage if -h or --help are specified in the args
  33. if [[ $@ =~ '-h' || $@ =~ '--help' ]]; then
  34. usage
  35. exit 0
  36. fi
  37. # List all available platforms then exit
  38. if [[ $1 == '--list-platforms' ]]; then
  39. echo -e "Available platforms:"
  40. for platform in ${platforms[@]}; do
  41. echo -e " ${platform}"
  42. done
  43. exit 0
  44. fi
  45. # List all available actions for a given platform then exit
  46. if [[ $1 == '--list-actions' ]]; then
  47. platform="$2"
  48. if [[ ! " ${platforms[@]} " =~ " ${platform} " ]]; then
  49. echo "ERROR: Platform ${platform} does not exist."
  50. exit 1
  51. fi
  52. echo -e "Available actions for platform ${platform}:"
  53. for action in ${actions[@]}; do
  54. if [[ -f deployment/${platform}/${action}.sh ]]; then
  55. echo -e " ${action}"
  56. fi
  57. done
  58. exit 0
  59. fi
  60. # Parse keep-artifacts option
  61. if [[ $1 == '-k' || $1 == '--keep-artifacts' ]]; then
  62. keep_artifacts="y"
  63. shift 1
  64. else
  65. keep_artifacts="n"
  66. fi
  67. # Parse branch option
  68. if [[ $1 == '-b' || $1 == '--web-branch' ]]; then
  69. web_branch="$2"
  70. shift 2
  71. else
  72. web_branch="$( git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' )"
  73. fi
  74. # Parse platform option
  75. if [[ -n $1 ]]; then
  76. cli_platform="$1"
  77. shift
  78. else
  79. echo "ERROR: A platform must be specified. Use 'all' to specify all platforms."
  80. exit 1
  81. fi
  82. if [[ ${cli_platform} == 'all' ]]; then
  83. declare -a platform=( ${platforms[@]} )
  84. else
  85. if [[ ! " ${platforms[@]} " =~ " ${cli_platform} " ]]; then
  86. echo "ERROR: Platform ${cli_platform} is invalid. Use the '--list-platforms' option to list available platforms."
  87. exit 1
  88. else
  89. declare -a platform=( "${cli_platform}" )
  90. fi
  91. fi
  92. # Parse action option
  93. if [[ -n $1 ]]; then
  94. cli_action="$1"
  95. shift
  96. else
  97. echo "ERROR: An action must be specified. Use 'all' to specify all actions."
  98. exit 1
  99. fi
  100. if [[ ${cli_action} == 'all' ]]; then
  101. declare -a action=( ${actions[@]} )
  102. else
  103. if [[ ! " ${actions[@]} " =~ " ${cli_action} " ]]; then
  104. echo "ERROR: Action ${cli_action} is invalid. Use the '--list-actions <platform>' option to list available actions."
  105. exit 1
  106. else
  107. declare -a action=( "${cli_action}" )
  108. fi
  109. fi
  110. # Verify required utilities are installed
  111. missing_deps=()
  112. for utility in ${dependencies[@]}; do
  113. if ! which ${utility} &>/dev/null; then
  114. missing_deps+=( ${utility} )
  115. fi
  116. done
  117. # Error if we're missing anything
  118. if [[ ${#missing_deps[@]} -gt 0 ]]; then
  119. echo -e "ERROR: This script requires the following missing utilities:"
  120. for utility in ${missing_deps[@]}; do
  121. echo -e " ${utility}"
  122. done
  123. exit 1
  124. fi
  125. # Parse platform-specific dependencies
  126. for target_platform in ${platform[@]}; do
  127. # Read platform-specific dependencies
  128. if [[ -f deployment/${target_platform}/dependencies.txt ]]; then
  129. platform_dependencies="$( grep -v '^#' deployment/${target_platform}/dependencies.txt )"
  130. # Verify required utilities are installed
  131. missing_deps=()
  132. for utility in ${platform_dependencies[@]}; do
  133. if ! which ${utility} &>/dev/null; then
  134. missing_deps+=( ${utility} )
  135. fi
  136. done
  137. # Error if we're missing anything
  138. if [[ ${#missing_deps[@]} -gt 0 ]]; then
  139. echo -e "ERROR: The ${target_platform} platform requires the following utilities:"
  140. for utility in ${missing_deps[@]}; do
  141. echo -e " ${utility}"
  142. done
  143. exit 1
  144. fi
  145. fi
  146. done
  147. # Initialize submodules
  148. git submodule update --init --recursive
  149. # configure branch
  150. pushd MediaBrowser.WebDashboard/jellyfin-web
  151. if ! git diff-index --quiet HEAD --; then
  152. popd
  153. echo
  154. echo "ERROR: Your 'jellyfin-web' submodule working directory is not clean!"
  155. echo "This script will overwrite your unstaged and unpushed changes."
  156. echo "Please do development on 'jellyfin-web' outside of the submodule."
  157. exit 1
  158. fi
  159. git fetch --all
  160. # If this is an official branch name, fetch it from origin
  161. official_branches_regex="^master$|^dev$|^release-.*$|^hotfix-.*$"
  162. if [[ ${web_branch} =~ ${official_branches_regex} ]]; then
  163. git checkout origin/${web_branch} || {
  164. echo "ERROR: 'jellyfin-web' branch 'origin/${web_branch}' is invalid."
  165. exit 1
  166. }
  167. # Otherwise, just check out the local branch (for testing, etc.)
  168. else
  169. git checkout ${web_branch} || {
  170. echo "ERROR: 'jellyfin-web' branch '${web_branch}' is invalid."
  171. exit 1
  172. }
  173. fi
  174. popd
  175. # Execute each platform and action in order, if said action is enabled
  176. pushd deployment/
  177. for target_platform in ${platform[@]}; do
  178. echo -e "> Processing platform ${target_platform}"
  179. date_start=$( date +%s )
  180. pushd ${target_platform}
  181. cleanup() {
  182. echo -e ">> Processing action clean"
  183. if [[ -f clean.sh && -x clean.sh ]]; then
  184. ./clean.sh ${keep_artifacts}
  185. fi
  186. }
  187. trap cleanup EXIT INT
  188. for target_action in ${action[@]}; do
  189. echo -e ">> Processing action ${target_action}"
  190. if [[ -f ${target_action}.sh && -x ${target_action}.sh ]]; then
  191. ./${target_action}.sh
  192. fi
  193. done
  194. if [[ -d pkg-dist/ ]]; then
  195. echo -e ">> Collecting build artifacts"
  196. target_dir="../../../jellyfin-build/${target_platform}"
  197. mkdir -p ${target_dir}
  198. mv pkg-dist/* ${target_dir}/
  199. fi
  200. cleanup
  201. date_end=$( date +%s )
  202. echo -e "> Completed platform ${target_platform} in $( expr ${date_end} - ${date_start} ) seconds."
  203. popd
  204. done
  205. popd