lib_static.sh 3.7 KB

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