-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxcode-sync.sh
183 lines (154 loc) · 4.4 KB
/
xcode-sync.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
version="0.5.0"
#default config
# change to your own (ex: gitub, bitcket....etc)
path_source_url="[email protected]:5SMNOONMS5/XcodeSyncTools.git"
src1=${HOME}/Library/Developer/Xcode/Templates
src1_local=Templates
src2=${HOME}/Library/Developer/Xcode/UserData/CodeSnippets
src2_local=CodeSnippets
# I will make colorful console output in the future
declare -a colors
colors[2]=33 # yellow text
colors[4]=32 # green text
colors[8]=34 # blue text
colors[16]=36 # cyan text
colors[32]=35 # purple text
colors[64]="33m\033[7" # yellow background
colors[128]="32m\033[7" # green background
colors[256]="34m\033[7" # blue background
colors[512]="36m\033[7" # cyan background
colors[1024]="35m\033[7" # purple background
colors[2048]="31m\033[7" # red background (won with default target)
# check path
check() {
# Thanks to https://askubuntu.com/questions/648577/copying-files-from-directories-having-spaces-in-its-name
echo $(basename $1)
if [ ! -d "$1" ]; then
echo "$(basename $1) path not found, create a new one"
else
echo "$(basename $1) already exist, duplicate a $(basename $1)_old folder"
if [ -d "$1_old" ]; then
echo "has $1_old remove old version first"
rm -rf "$1_old"
fi
mv "$1" "$1_old"
fi
mkdir "$1"
}
# Usage info
usage() {
cat <<EOF
Usage:
sh xcode-sync.sh [options]
Options:
-v, --version Output version.
-s, --sync Sync code snippet and start fsevents-tools.
-w, --watch Observer the folder change.
-c, --change Change remote git URL to your own repo.
-t, --test Test script, don't use this.
-h, --help This message.
-- End of options
EOF
}
# close Xcode project
close_xcode () {
declare isXocdeOpen="$(ps aux | grep Xcode.app | wc -l | awk '{ print $1 }')"
if [ "$isXocdeOpen" == "1" ]; then
# xcode not open, return this function
return
fi
# if xcode open, quit it first
echo "In order to renew snippets, it will quit xcode first, please press [y/n] to proceed:"
read response
if [ "$response" == "y" ]; then
# Thanks to http://osxdaily.com//09/05/gracefully-quit-application-command-line/
osascript -e 'quit app "Xcode"'
else
echo "Goodbye 88"
exit 1
fi
}
# Embed fsevents-tools as submodule
updateSubmodule () {
declare isGitRepo="$(git rev-parse --is-inside-work-tree)"
# Thanks to https://stackoverflow.com/questions/2180270/check-if-current-directory-is-a-git-repository
if [ ! "$isGitRepo" ]; then
echo "Currently working directory is not git repositories"
exit 1
fi
echo "Submodule update"
git submodule update --init
cd fsevents-tools
sh autogen.sh
cd ..
}
# open Xcode project
open_xcode () {
open -a Xcode
}
# Sync Code-snippets
syncCodeSnippets () {
echo "Sync xcode snippets"
check "$src2"
cp -r "$src2_local"/* "$src2"
}
# Sync file-template
syncFileTemplates () {
echo "Sync xcode custom file"
check "$src1"
cp -r "$src1_local"/* "$src1"
}
changeGitRemoteURL () {
echo "Change remote git repo to "$path_source_url", remember to customize your .gitignore"
git remote set-url origin $path_source_url
}
# Watch the folder change
watchFolder () {
# Thanks to https://askubuntu.com/questions/476041/how-do-i-make-rsync-delete-files-that-have-been-deleted-from-the-source-folder
declare local_source1="$(pwd)"/"$src1_local"
declare rsync1=("rsync --exclude=.DS_Store -vrulptg "$src1/" "$local_source1" --delete")
declare local_source2="$(pwd)"/"$src2_local"
declare rsync2=("rsync --exclude=.DS_Store -vrulptg "$src2/" "$local_source2" --delete")
cd fsevents-tools
./notifyloop ${src1}/ ${src2}/ "${rsync1}" "${rsync2}"
}
testFunc () {
echo "testFunc"
}
# parse options
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
case $1 in
-v | --version )
echo $version
exit 1
;;
-s | --sync )
updateSubmodule
close_xcode
syncCodeSnippets
syncFileTemplates
open_xcode
watchFolder
exit 1
;;
-w | --watch )
watchFolder
exit 1
;;
-c | --chagne )
changeGitRemoteURL
exit 1
;;
-t | --test )
# check "$src2"
exit 1
;;
-h | --help | * )
usage
exit 1
;;
esac
shift
done
if [[ "$1" == "--" ]]; then shift; fi