lib_static.sh 3.7 KB

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