-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor-variables-in-scripts.sh
executable file
·51 lines (37 loc) · 1.46 KB
/
refactor-variables-in-scripts.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
#!/bin/bash
# Determine toolpath if not set already
relativepath="./" # Define relative path to go from this script to the root level of the tool
if [[ ! -v toolpath ]]; then scriptpath=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ); toolpath=$(realpath --canonicalize-missing $scriptpath/$relativepath); fi
# Define test folder
testfolder="test"
# Remove existing Test Folder
rm -rf "${toolpath}/${testfolder}/*"
# Create Test folder if not exist
mkdir -p "${toolpath}/${testfolder}"
# Get list of Script Files
mapfile -t scriptfiles < <( find . -iname "*.sh" | grep -v "/${testfolder}/" | grep -v "$(basename $0)" )
# Load Shell Script Helpers
source "${toolpath}/check-script.sh"
# Iterate over Files
for scriptfile in "${scriptfiles[@]}"
do
# Exclude ./ at the start of the filename
scriptfile=$(echo "${scriptfile}" | sed -E "s|\./(.+)$|\1|g")
# Get Subfolder Name if it's a Subfolder
subfolder=$(dirname "${scriptfile}")
# Creeate Subfolder if not Exist
if [[ -n "${subfolder}" ]]
then
mkdir -p "${testfolder}/${subfolder}"
fi
# Add some Separator Text
add_section "#" "1" "Processing File ${scriptfile}"
# Dry Run
cat "${scriptfile}" | sed -E 's|\$([a-zA-Z0-9]+)|\${\1\}|g' > "${testfolder}/${scriptfile}"
# Check for Errors using ShellCheck
check_script "${testfolder}/${scriptfile}"
# Perform Replacement for Real
sed -Ei 's|\$([a-zA-Z0-9]+)|\$\{\1\}|g' "${toolpath}/${scriptfile}"
# Echo
echo -e "\n\n"
done