-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATProjectGenerator.sh
131 lines (103 loc) · 3.15 KB
/
ATProjectGenerator.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
# Functions
colorPrint() {
text=$1
printf "%b" "\e[1;34m${text}\e[0m"
}
errorColorPrint() {
text=$1
printf "%b" "\e[1;91m${text}\e[0m"
}
replaceInFiles() {
textToFind=$1
textToPut=$2
dir=$3
for file in $(grep -Ril "$textToFind" $dir)
do
if [ -f "$file" ] && [[ "$file" != Files/Pods* ]]
then
sed -i '' "s+$textToFind+$textToPut+g" "$file"
fi
done
}
# Code segment
templateFolder="$1"
startTargetFolder="$2"
projectName="$3"
author="$4"
colorPrint "\n"
while [ -z "$templateFolder" ]
do
colorPrint "Enter path to template folder: "
read templateFolder
done
if [ -d "$templateFolder" ]
then
while [ -z "$startTargetFolder" ]
do
colorPrint "Enter path to target folder: "
read startTargetFolder
done
while [ -z "$projectName" ]
do
colorPrint "Enter project name: "
read projectName
done
while [ -z "$author" ]
do
colorPrint "Enter the author name: "
read author
done
colorPrint "\n"
# Copy template to target folder
colorPrint "Copy resources\n"
targetFolder="$startTargetFolder/$projectName"
cp -a "$templateFolder" "$targetFolder"
if [ $? -ne 0 ]
then
errorColorPrint "\n\t\tError! Failed to copy template folder\n"
else
# Rename subdirectories and files containing the project name
colorPrint "Setting project directories names\n"
for depth in {0..50}
do
for dir in $(find $targetFolder -mindepth $depth -maxdepth $depth)
do
newDirName="$(echo "${dir//ApplicationName/$projectName}")"
if [ "$dir" != "$newDirName" ] && [[ "$dir" != Files/Pods* ]]
then
mv "$dir" "$newDirName"
fi
done
done
# Replacing all occurrences of one string with another in all files in the project directory
colorPrint "Files content configuration\n"
replaceInFiles "ApplicationName" "$projectName" "$targetFolder"
projectNameLower="$(echo "$projectName" | tr '[:upper:]' '[:lower:]')"
replaceInFiles "applicationname" "$projectNameLower" "$targetFolder"
# Remove Pods folder to avoid comments replacing in pods
rm -r "$targetFolder/Pods"
for dir in $(find $targetFolder -maxdepth 50 -name 'Pods')
do
rm -rf "$dir"
done
# Replce all author comments
date=$(date '+%d.%m.%y')
replaceInFiles ".*Created by.*" "// Created by $author on $date." "$targetFolder"
# Replace date in other files
replaceInFiles "GenerationDate" "$date" "$targetFolder"
# Install pods
colorPrint "Pods installation\n"
for dir in $(find $targetFolder -maxdepth 50 -name 'Podfile')
do
rootProjectDir="$(echo "${dir//Podfile/}")"
cd $rootProjectDir
pod install
done
colorPrint "\n\t🎉\tProject was successfully generated!\t🎉\n"
open $targetFolder
fi
else
errorColorPrint "Could not find project template by dir $templateFolder"
fi
colorPrint "\n"