lib_sxng_static.sh 3.9 KB

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