-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleanup.sh
executable file
·167 lines (153 loc) · 4.91 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
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
#!/bin/bash
SCRIPT_DIR="$(dirname "$0")";
source $SCRIPT_DIR/commons.sh;
echo "================================================================================";
echo "> CLEANUP SHARED...";
echo "--------------------------------------------------------------------------------";
BEFORE_DATE=$(date +%D-%X);
BEFORE_DATE_SEC=$(date +%s);
CURRENT_PATH=$(pwd);
CURRENT_DIRECTORY=$(basename ${CURRENT_PATH});
echo "Current directory: $CURRENT_DIRECTORY";
setGradleArgs;
if [[ -f "./gradlew" ]]; then
./gradlew clean ${GRADLE_ARGS};
checkResult $?;
rm gradlew; # only once
checkResult $?;
fi
if [[ -d "app-android" ]]; then
cd app-android || exit; # >>
if [[ -f "keys_cleanup.sh" ]]; then
echo ">> Cleaning keys...";
./keys_cleanup.sh;
echo "RESULT: $? (fail ok/expected)";
echo ">> Cleaning keys... DONE";
fi
cd ../; # <<
fi
function cleanupFile() {
if [[ "$#" -ne 2 ]]; then
echo "> cleanupFile() > Illegal number of parameters!";
exit 1;
fi
local SRC_FILE_PATH=$1;
local DEST_FILE_PATH=$2;
local FILE_NAME=$(basename ${SRC_FILE_PATH});
if [[ $FILE_NAME == ".gitignore" || $FILE_NAME == "MT.gitignore" ]]; then
return; # keep .gitignore files, even if modified
fi
if [[ -f "$DEST_FILE_PATH" ]]; then
diff -q ${SRC_FILE_PATH} ${DEST_FILE_PATH};
local RESULT=$?;
if [[ ${RESULT} -ne 0 ]]; then # FILE CHANGED
echo "> Deployed shared file '$DEST_FILE_PATH' was changed from '$SRC_FILE_PATH'!";
ls -l $DEST_FILE_PATH;
ls -l $SRC_FILE_PATH;
diff --color ${SRC_FILE_PATH} ${DEST_FILE_PATH};
exit ${RESULT};
fi
echo -n "> Cleaning-up file '$DEST_FILE_PATH'...";
rm ${DEST_FILE_PATH};
RESULT=$?;
if [[ ${RESULT} -ne 0 ]]; then
echo " ERROR !";
exit ${RESULT};
fi
echo " DONE ✓";
else
echo "> Ignoring missing file '$DEST_FILE_PATH'...";
fi
}
function cleanupDirectory() {
if [[ "$#" -ne 2 ]]; then
echo "> cleanupDirectory() > Illegal number of parameters!";
exit 1;
fi
local SRC_FILE_PATH=$1;
local DEST_FILE_PATH=$2;
local FILE_NAME=$(basename ${SRC_FILE_PATH});
if [[ -d "$DEST_FILE_PATH" ]]; then
local S_FILE_NAME;
for S_FILE_NAME in $(ls -a ${SRC_FILE_PATH}/) ; do
local S_SRC_FILE_PATH=${SRC_FILE_PATH}/$S_FILE_NAME;
if [[ $S_FILE_NAME == "." ]] || [[ $S_FILE_NAME == ".." ]]; then
continue;
fi
local S_FILE_NAME_DEST=${S_FILE_NAME#"MT"}; # MT+filename used to ignore ".gitignore"
local S_DEST_FILE_PATH="$DEST_FILE_PATH/$S_FILE_NAME_DEST";
if [[ -f "$S_SRC_FILE_PATH" ]]; then
cleanupFile ${S_SRC_FILE_PATH} ${S_DEST_FILE_PATH};
checkResult $?;
elif [[ -d "$S_SRC_FILE_PATH" ]]; then
cleanupDirectory ${S_SRC_FILE_PATH} ${S_DEST_FILE_PATH};
checkResult $?;
else #WTF
echo "--------------------------------------------------------------------------------";
echo "> File to cleanup '$S_FILENAME' ($S_SRC_FILE_PATH) is neither a directory or a file!";
ls -l ${S_FILENAME};
exit 1;
fi
done
if ! [[ "$(ls -A ${DEST_FILE_PATH})" ]]; then
echo -n "> Deleting empty directory '$DEST_FILE_PATH'...";
rm -r ${DEST_FILE_PATH};
local RESULT=$?;
if [[ ${RESULT} -ne 0 ]]; then
echo " ERROR !";
exit ${RESULT};
fi
echo "DONE ✓";
fi
else
echo "> Ignoring missing directory '$DEST_FILE_PATH'.";
fi
}
DEST_PATH=".";
SRC_DIR_PATH="commons/shared";
for FILENAME in $(ls -a $SRC_DIR_PATH/) ; do
SRC_FILE_PATH=$SRC_DIR_PATH/$FILENAME;
if [[ $FILENAME == "." ]] || [[ $FILENAME == ".." ]]; then
continue;
fi
FILENAME_DEST=${FILENAME#"MT"}; # MT+filename used to ignore ".gitignore"
DEST_FILE_PATH="$DEST_PATH/$FILENAME_DEST"
if [[ -f ${SRC_FILE_PATH} ]]; then
cleanupFile ${SRC_FILE_PATH} ${DEST_FILE_PATH};
checkResult $?;
elif [[ -d "$SRC_FILE_PATH" ]]; then
cleanupDirectory ${SRC_FILE_PATH} ${DEST_FILE_PATH};
checkResult $?;
else #WTF
echo "> File to cleanup '$FILENAME' ($SRC_FILE_PATH) is neither a directory or a file!";
ls -l ${FILENAME};
exit 1;
fi
done
SRC_DIR_PATH="commons/shared-opt-dir";
for FILENAME in $(ls -a $SRC_DIR_PATH/) ; do
SRC_FILE_PATH=$SRC_DIR_PATH/$FILENAME;
if [[ $FILENAME == "." ]] || [[ $FILENAME == ".." ]]; then
continue;
fi
FILENAME_DEST=${FILENAME#"MT"}; # MT+filename used to ignore ".gitignore"
DEST_FILE_PATH="$DEST_PATH/$FILENAME_DEST"
if [[ -f ${SRC_FILE_PATH} ]]; then
cleanupFile ${SRC_FILE_PATH} ${DEST_FILE_PATH};
checkResult $?;
elif [[ -d "$SRC_FILE_PATH" ]]; then
cleanupDirectory ${SRC_FILE_PATH} ${DEST_FILE_PATH};
checkResult $?;
else #WTF
echo "> File to cleanup '$FILENAME' ($SRC_FILE_PATH) is neither a directory or a file!";
ls -l ${FILENAME};
exit 1;
fi
done
echo "--------------------------------------------------------------------------------";
AFTER_DATE=$(date +%D-%X);
AFTER_DATE_SEC=$(date +%s);
DURATION_SEC=$(($AFTER_DATE_SEC-$BEFORE_DATE_SEC));
echo "> $DURATION_SEC secs FROM $BEFORE_DATE TO $AFTER_DATE";
echo "> CLEANUP SHARED... DONE";
echo "================================================================================";