This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcleanup.sh
executable file
·79 lines (58 loc) · 3.15 KB
/
cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
set -e
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
cd "${SCRIPT_DIR}"
nsudo() { [[ ${EUID} -ne 0 ]] && echo "sudo" ; true ; }
havecmd() { command command type "${1}" >/dev/null 2>&1 || return 1 ; }
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
check_dependencies() {
if [[ "$(uname -s)" == "Linux" ]] ; then
if havecmd dnf ; then # Debian / Ubuntu etc.
if ! havecmd parallel ; then $(nsudo) dnf install -y parallel; fi
if ! havecmd dos2unix ; then $(nsudo) dnf install -y dos2unix; fi
if ! havecmd perltidy ; then $(nsudo) dnf install -y perltidy; fi
nvm use --lts
if ! havecmd luafmt ; then npm install -g lua-fmt; fi
fi
fi
if ! havecmd dos2unix || ! havecmd parallel || ! havecmd luafmt ; then
echo_fail "Aborting cleanup, missing dependencies."
exit 1
fi
if [[ ! -d "${SCRIPT_DIR}/.packager/.git" ]] ; then
git clone https://github.com/BigWigsMods/packager.git "${SCRIPT_DIR}/.packager"
fi
pushd "${SCRIPT_DIR}/.packager" >/dev/null 2>&1 \
&& { pwd && git pull --ff-only || true ; } \
&& popd >/dev/null 2>&1
}
tjfind() {
find . \( "$@" \) -and -not -path './.git/*' -and -not -path './simc*' -and -not -path './.release/*' -and -not -path './.packager/*' -print | sort
}
check_dependencies
# Remove executable flag on all files
tjfind -type f | parallel "chmod -x '{1}'"
# Make sure scripts are executable
tjfind -iname '*.sh' -or -iname '*.pl' -or -iname '*.py' | parallel "chmod +x '{1}'"
# Make sure everything has Unix line endings
tjfind -iname '*.toc' -or -iname '*.lua' -or -iname '*.sh' -or -iname '*.pl' -or -iname '*.py' -or -iname '*.simc' -or -iname '*.xml' -or -iname '*.cpp' -or -iname '*.h' -or -iname '*.hpp' -or -iname '*.inl' -or -iname '*.md' | parallel "dos2unix '{1}' >/dev/null 2>&1"
# Remove trailing whitespace
tjfind -iname '*.toc' -or -iname '*.lua' -or -iname '*.sh' -or -iname '*.pl' -or -iname '*.py' -or -iname '*.simc' -or -iname '*.cpp' -or -iname '*.h' -or -iname '*.hpp' -or -iname '*.inl' | parallel "sed -i 's/[ \t]*\$//' '{1}'"
# Reformat perl scripts
tjfind -iname '*.pl' | parallel "echo \"Formatting '{1}'\" && perltidy -pt=2 -dws -nsak='if for while' -l=200 '{1}' && cat '{1}.tdy' > '{1}' && rm '{1}.tdy'"
# Reformat lua files
tjfind -iname '*.lua' | parallel "echo \"Formatting '{1}'\" && luafmt --quotemark=single --use-tabs -w replace '{1}'"
# Reformat cpp files
tjfind -iname '*.cpp' -or -iname '*.h' -or -iname '*.hpp' -or -iname '*.inl' | parallel "echo \"Formatting '{1}'\" && clang-format -i '{1}'"
# Disable devMode
tjfind -iname '*.lua' | parallel "sed -i 's/^local devMode = true/local devMode = false/' '{1}'"
# Install this script as a pre-commit hook if it's not already present
if [[ ! -L "${SCRIPT_DIR}/.git/hooks/pre-commit" ]] ; then
(cd "${SCRIPT_DIR}/.git/hooks" && ln -sf ../../cleanup.sh pre-commit)
fi
# Drop out of "git commit" if there are any uncommitted changes after the cleanup.
if [[ ! -z "$(echo "${BASH_SOURCE[@]}" | grep "pre-commit")" ]] ; then
[[ ! -z "$(git diff)" ]] && echo -e "\e[0;37m[\e[1;31mFAIL\e[0;37m]\e[0m Aborting commit: 'git diff' says changes are still present." && exit 1
fi
exit 0