lib_static.sh 3.8 KB

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