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