| 
									
										
										
										
											2021-06-26 08:46:20 +02:00
										 |  |  | #!/usr/bin/env bash
 | 
					
						
							|  |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | STATIC_BUILD_COMMIT="[build] /static" | 
					
						
							|  |  |  | STATIC_BUILT_PATHS=( | 
					
						
							| 
									
										
										
										
											2021-12-01 20:25:39 +01:00
										 |  |  |     'searx/static/themes/simple/css' | 
					
						
							|  |  |  |     'searx/static/themes/simple/js' | 
					
						
							|  |  |  |     'searx/static/themes/simple/src/generated/pygments.less' | 
					
						
							|  |  |  |     'searx/static/themes/simple/img' | 
					
						
							| 
									
										
										
										
											2022-05-07 15:11:05 +02:00
										 |  |  |     'searx/templates/simple/searxng-wordmark.min.svg' | 
					
						
							| 
									
										
										
										
											2021-12-01 20:25:39 +01:00
										 |  |  |     'searx/templates/simple/icons.html' | 
					
						
							| 
									
										
										
										
											2021-06-26 08:46:20 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 16:39:00 +02:00
										 |  |  | static.help(){ | 
					
						
							| 
									
										
										
										
											2021-06-26 08:46:20 +02:00
										 |  |  |     cat <<EOF | 
					
						
							|  |  |  | static.build.:  ${STATIC_BUILD_COMMIT} | 
					
						
							|  |  |  |   commit    : build & commit /static folder | 
					
						
							|  |  |  |   drop      : drop last commit if it was previously done by static.build.commit | 
					
						
							|  |  |  |   restore   : git restore of the /static folder (after themes.all) | 
					
						
							|  |  |  | EOF | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | is.static.build.commit() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     local commit_sha="$1" | 
					
						
							|  |  |  |     local commit_message | 
					
						
							|  |  |  |     local commit_files | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # check commit message | 
					
						
							|  |  |  |     commit_message=$(git show -s --format=%s "${commit_sha}") | 
					
						
							|  |  |  |     if [ "${commit_message}" != "${STATIC_BUILD_COMMIT}" ]; then | 
					
						
							|  |  |  |         err_msg "expecting commit message: '${STATIC_BUILD_COMMIT}'" | 
					
						
							|  |  |  |         err_msg "commit message of ${commit_sha} is: '${commit_message}'" | 
					
						
							|  |  |  |         return 1 | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # check all files of the commit belongs to $STATIC_BUILT_PATHS | 
					
						
							|  |  |  |     commit_files=$(git diff-tree --no-commit-id --name-only -r "${commit_sha}") | 
					
						
							| 
									
										
										
										
											2024-01-07 18:37:11 +01:00
										 |  |  |     for i in "${STATIC_BUILT_PATHS[@]}"; do | 
					
						
							| 
									
										
										
										
											2021-06-26 08:46:20 +02:00
										 |  |  |         # remove files of ${STATIC_BUILT_PATHS} | 
					
						
							|  |  |  |         commit_files=$(echo "${commit_files}" | grep -v "^${i}") | 
					
						
							|  |  |  |     done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if [ -n "${commit_files}" ]; then | 
					
						
							|  |  |  |         err_msg "commit ${commit_sha} contains files not a part of ${STATIC_BUILD_COMMIT}" | 
					
						
							|  |  |  |         echo "${commit_files}" | prefix_stdout "  " | 
					
						
							|  |  |  |         return 2 | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |     return 0 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static.build.drop() { | 
					
						
							|  |  |  |     # drop last commit if it was made by the static.build.commit command | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     local last_commit_id | 
					
						
							|  |  |  |     local branch | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     build_msg STATIC "drop last commit if it was previously done by static.build.commit" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # get only last (option -n1) local commit not in remotes | 
					
						
							|  |  |  |     branch="$(git branch --show-current)" | 
					
						
							|  |  |  |     last_commit_id="$(git log -n1 "${branch}" --pretty=format:'%h'\
 | 
					
						
							|  |  |  |      --not --exclude="${branch}" --branches --remotes)"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if [ -z "${last_commit_id}" ]; then | 
					
						
							|  |  |  |         err_msg "there are no local commits" | 
					
						
							|  |  |  |         return 1 | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ! is.static.build.commit "${last_commit_id}"; then | 
					
						
							|  |  |  |         return $? | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     build_msg STATIC "drop last commit ${last_commit_id}" | 
					
						
							|  |  |  |     git reset --hard HEAD~1 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static.build.commit() { | 
					
						
							|  |  |  |     # call the "static.build.drop" command, then "themes.all" then commit the | 
					
						
							|  |  |  |     # built files ($BUILT_PATHS). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     build_msg STATIC "build & commit /static files" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-11 16:01:54 +02:00
										 |  |  |     # check for not committed files | 
					
						
							| 
									
										
										
										
											2021-06-26 08:46:20 +02:00
										 |  |  |     if [ -n "$(git diff --name-only)" ]; then | 
					
						
							| 
									
										
										
										
											2023-08-11 16:01:54 +02:00
										 |  |  |         err_msg "some files are not committed:" | 
					
						
							| 
									
										
										
										
											2021-06-26 08:46:20 +02:00
										 |  |  |         git diff --name-only | prefix_stdout "  " | 
					
						
							|  |  |  |         return 1 | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # check for staged files | 
					
						
							|  |  |  |     if [ -n "$(git diff --name-only --cached)" ]; then | 
					
						
							|  |  |  |         err_msg "some files are staged:" | 
					
						
							|  |  |  |         git diff --name-only --cached | prefix_stdout "  " | 
					
						
							|  |  |  |         return 1 | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-09 11:59:31 +02:00
										 |  |  |     # drop existing commit from previous build | 
					
						
							| 
									
										
										
										
											2021-06-26 08:46:20 +02:00
										 |  |  |     static.build.drop &>/dev/null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     (   set -e | 
					
						
							|  |  |  |         # build the themes | 
					
						
							|  |  |  |         themes.all | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # add build files | 
					
						
							|  |  |  |         for built_path in "${STATIC_BUILT_PATHS[@]}"; do | 
					
						
							|  |  |  |             git add -v "${built_path}" | 
					
						
							|  |  |  |         done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 20:25:39 +01:00
										 |  |  |         # check if any file has been added (in case of no changes) | 
					
						
							|  |  |  |         if [ -z "$(git diff --name-only --cached)" ]; then | 
					
						
							|  |  |  |             build_msg STATIC "no changes applied / nothing to commit" | 
					
						
							|  |  |  |             return 0 | 
					
						
							|  |  |  |         fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 08:46:20 +02:00
										 |  |  |         # check for modified files that are not staged | 
					
						
							|  |  |  |         if [ -n "$(git diff --name-only)" ]; then | 
					
						
							|  |  |  |             die 42 "themes.all has created files that are not in STATIC_BUILT_PATHS" | 
					
						
							|  |  |  |         fi | 
					
						
							|  |  |  |         git commit -m "${STATIC_BUILD_COMMIT}" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static.build.restore() { | 
					
						
							|  |  |  |     build_msg STATIC "git-restore of the built files (/static)" | 
					
						
							|  |  |  |     git restore --staged "${STATIC_BUILT_PATHS[@]}" | 
					
						
							|  |  |  |     git restore --worktree "${STATIC_BUILT_PATHS[@]}" | 
					
						
							|  |  |  | } |