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