lib_sxng_static.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. STATIC_BUILD_COMMIT="[build] /static"
  4. STATIC_BUILT_PATHS=(
  5. 'searx/templates/simple/icons.html'
  6. 'searx/static/themes/simple'
  7. 'client/simple/package-lock.json'
  8. )
  9. static.help(){
  10. cat <<EOF
  11. static.build.: ${STATIC_BUILD_COMMIT}
  12. commit : build & commit /static folder
  13. drop : drop last commit if it was previously done by static.build.commit
  14. restore : git restore of the /static folder (after themes.all)
  15. EOF
  16. }
  17. is.static.build.commit() {
  18. local commit_sha="$1"
  19. local commit_message
  20. local commit_files
  21. # check commit message
  22. commit_message=$(git show -s --format=%s "${commit_sha}")
  23. if [ "${commit_message}" != "${STATIC_BUILD_COMMIT}" ]; then
  24. err_msg "expecting commit message: '${STATIC_BUILD_COMMIT}'"
  25. err_msg "commit message of ${commit_sha} is: '${commit_message}'"
  26. return 1
  27. fi
  28. # check all files of the commit belongs to $STATIC_BUILT_PATHS
  29. commit_files=$(git diff-tree --no-commit-id --name-only -r "${commit_sha}")
  30. for i in "${STATIC_BUILT_PATHS[@]}"; do
  31. # remove files of ${STATIC_BUILT_PATHS}
  32. commit_files=$(echo "${commit_files}" | grep -v "^${i}")
  33. done
  34. if [ -n "${commit_files}" ]; then
  35. err_msg "commit ${commit_sha} contains files not a part of ${STATIC_BUILD_COMMIT}"
  36. echo "${commit_files}" | prefix_stdout " "
  37. return 2
  38. fi
  39. return 0
  40. }
  41. static.build.drop() {
  42. # drop last commit if it was made by the static.build.commit command
  43. local last_commit_id
  44. local branch
  45. build_msg STATIC "drop last commit if it was previously done by static.build.commit"
  46. # get only last (option -n1) local commit not in remotes
  47. branch="$(git branch --show-current)"
  48. last_commit_id="$(git log -n1 "${branch}" --pretty=format:'%h'\
  49. --not --exclude="${branch}" --branches --remotes)"
  50. if [ -z "${last_commit_id}" ]; then
  51. err_msg "there are no local commits"
  52. return 1
  53. fi
  54. if ! is.static.build.commit "${last_commit_id}"; then
  55. return $?
  56. fi
  57. build_msg STATIC "drop last commit ${last_commit_id}"
  58. git reset --hard HEAD~1
  59. }
  60. static.build.commit() {
  61. # call the "static.build.drop" command, then "themes.all" then commit the
  62. # built files ($BUILT_PATHS).
  63. build_msg STATIC "build & commit /static files"
  64. # check for not committed files
  65. if [ -n "$(git diff --name-only)" ]; then
  66. err_msg "some files are not committed:"
  67. git diff --name-only | prefix_stdout " "
  68. return 1
  69. fi
  70. # check for staged files
  71. if [ -n "$(git diff --name-only --cached)" ]; then
  72. err_msg "some files are staged:"
  73. git diff --name-only --cached | prefix_stdout " "
  74. return 1
  75. fi
  76. # drop existing commit from previous build
  77. static.build.drop &>/dev/null
  78. ( set -e
  79. # fix & build the themes
  80. themes.fix
  81. themes.all
  82. # add build files
  83. for built_path in "${STATIC_BUILT_PATHS[@]}"; do
  84. git add -v "${built_path}"
  85. done
  86. # check if any file has been added (in case of no changes)
  87. if [ -z "$(git diff --name-only --cached)" ]; then
  88. build_msg STATIC "no changes applied / nothing to commit"
  89. return 0
  90. fi
  91. # check for modified files that are not staged
  92. if [ -n "$(git diff --name-only)" ]; then
  93. die 42 "themes.all has created files that are not in STATIC_BUILT_PATHS"
  94. fi
  95. git commit -m "${STATIC_BUILD_COMMIT}"
  96. )
  97. }
  98. static.build.restore() {
  99. build_msg STATIC "git-restore of the built files (/static)"
  100. git restore --staged "${STATIC_BUILT_PATHS[@]}"
  101. git restore --worktree "${STATIC_BUILT_PATHS[@]}"
  102. }