-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sh
executable file
·91 lines (75 loc) · 2.58 KB
/
init.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
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# Destination directory of modifications
DEST="."
# Original project variables
originalProjectName="adapter-template"
originalAdapterName="template"
# Prepare testing
if [[ ! -z "${TEST}" ]]; then
#set -xe
DEST="tmp/inittest"
mkdir -p ${DEST}
echo "." > tmp/.gitignore
fi
function prompt() {
echo -n -e "\033[1;32m?\033[0m \033[1m$1\033[0m ($2) "
}
function replace() {
if [[ ! -z "${TEST}" ]]; then
dest=$(echo $2 | sed "s|^${DEST}/||")
mkdir -p $(dirname "${DEST}/${dest}")
if [[ "$2" == "${DEST}/${dest}" ]]; then
sed -E -e "$1" $2 > ${DEST}/${dest}.new
mv -f ${DEST}/${dest}.new ${DEST}/${dest}
else
sed -E -e "$1" $2 > ${DEST}/${dest}
fi
else
sed -E -e "$1" $2 > $2.new
mv -f $2.new $2
fi
}
function move() {
if [[ ! -z "${TEST}" ]]; then
dest=$(echo $2 | sed "s|^${DEST}/||")
mkdir -p $(dirname "${DEST}/${dest}")
cp -r "$1" ${DEST}/${dest}
else
mv $@
fi
}
function remove() {
if [[ -z "${TEST}" ]]; then
rm $@
fi
}
defaultAdapterName="template"
prompt "Adapter name" ${defaultAdapterName}
read adapterName
adapterName=$(echo "${adapterName:-${defaultAdapterName}}" | sed 's/[[:space:]]//g')
prompt "Update README" "Y/n"
read updateReadme
updateReadme=${updateReadme:-y}
prompt "Remove init script" "y/N"
read removeInit
removeInit=${removeInit:-n}
# IDE configuration
move .idea/${originalProjectName}.iml .idea/adapter-${adapterName}.iml
replace "s|.idea/${originalProjectName}.iml|.idea/adapter-${adapterName}.iml|g" .idea/modules.xml
# Run configurations
replace 's|name="'${originalProjectName}'"|name="'adapter-${adapterName}'"|' .idea/runConfigurations/Test.xml
find . -type f -name '*.go' | while read file; do replace "s|package ${originalAdapterName}|package ${adapterName}|; s|/${originalAdapterName}|/${adapterName}|g; s|Package ${originalAdapterName}|Package ${adapterName}|g; s|${originalAdapterName}adapter|${adapterName}adapter|g" "$file"; done
# Other project files
declare -a files=("CHANGELOG.md" "go.mod" ".golangci.yml")
for file in "${files[@]}"; do
if [[ -f "${file}" ]]; then
replace "s|${originalAdapterName}|${adapterName}|" ${file}
fi
done
if [[ "${removeInit}" != "n" && "${removeInit}" != "N" ]]; then
remove "$0"
fi
# Update readme
if [[ "${updateReadme}" == "y" || "${updateReadme}" == "Y" ]]; then
replace "s|${originalProjectName}|adapter-${adapterName}|g; s|/${originalAdapterName}|/${adapterName}|g; s|${originalAdapterName}adapter|${adapterName}adapter|g" README.md
fi