-
Notifications
You must be signed in to change notification settings - Fork 1
/
LibSafe.sh
executable file
·203 lines (165 loc) · 5.59 KB
/
LibSafe.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
# LibSafe 0.1.0
# Links
# * Array from `find` > https://stackoverflow.com/questions/23356779/how-can-i-store-find-command-result-as-arrays-in-bash
## Return
FOLDER=$1
RETURN=0
PROTOCOL_REGEX="/(?<=\@protocol).*?([a-zA-Z0-9_-][a-zA-Z0-9_-]*)(?=\s*:)/g"
INTERFACE_REGEX="/(?<=\@interface).*?([a-zA-Z0-9_-][a-zA-Z0-9_-]*)(?=\s*:)/g"
LIBSAFE_HEADER_FILE=""
if [ -z "$FOLDER" ]; then
echo "No argument supplied"
echo "Usage: $ ./LibSafe.sh folder-to-make-lib-safe"
exit -1
fi
function randomString()
{
RETURN=`cat /dev/random | LC_CTYPE=C tr -dc "[:alpha:]" | head -c 16`
}
function forEach() # $1 array, $2 function to apply an element of the array to $3-$6 extra args to pass to $2
{
ARR=("${!1}")
FUNC=$2
RESULT=()
## now loop through the array
for i in "${ARR[@]}"
do
## Call function $2 with current array element as argument.
$FUNC "${i}" $3 $4 $5 $6; RESULT+=("${RETURN[@]}")
done
RETURN=("${RESULT[@]}");
}
function findLibSafeHeader()
{
## Tried using $RETURN variable here but if was not working outside the scope of the method?!
LIBSAFE_HEADER_FILE="$(find ${FOLDER} -type f -name 'LibSafe-Header.h')"
}
function getAllHeaderFiles()
{
ARRAY=()
while IFS= read -r -d $'\0'; do
ARRAY+=("$REPLY")
done < <(find ${FOLDER} -type f -name $'*.h' -print0)
RETURN=("${ARRAY[@]}")
}
function getAllImplementationFiles()
{
ARRAY=()
while IFS= read -r -d $'\0'; do
ARRAY+=("$REPLY")
done < <(find ${FOLDER} -type f -name $'*.m' -print0)
RETURN=("${ARRAY[@]}")
}
function getAllObjectiveCFiles()
{
getAllHeaderFiles; HEADERS=("${RETURN[@]}")
getAllImplementationFiles; IMPLEMENTATIONS=("${RETURN[@]}")
RETURN=("${HEADERS[@]}" "${IMPLEMENTATIONS[@]}") # Combine the arrays
}
function getProtocolsInFile() # $1 file
{
#RESULTS=("CASSampleProtocol" "CASSampleProtocol2")
RESULTS=(`cat "${1}" | perl -wnE'say for /(?<=\@protocol).*?([a-zA-Z0-9_-][a-zA-Z0-9_-]*)(?=\s*<)/g'`)
# | grep -oP "$PROTOCOL_REGEX"`)
echo "Found the following protocols: ${RESULTS[@]}"
RETURN=("${RESULTS[@]}")
}
function getInterfacesInFile() # $1 file
{
RESULTS=(`cat "${1}" | perl -wnE'say for /(?<=\@interface).*?([a-zA-Z0-9_-][a-zA-Z0-9_-]*)(?=\s*:)/g'`)
echo "Found the following interfaces: ${RESULTS[@]}"
RETURN=("${RESULTS[@]}")
}
function scopeProtocolsAndInterfacesInFile() # $1 file, $2 scope
{
echo -e "\n\nProcessing file: '$1'"
echo -e "\nRemoving any existing LibSafe defines from file...\n"
sed -i "" '/\/\* LibSafe definition START \*\//,/\/\* LibSafe definition END \*\//d' "${1}"
# Add LibSafe define wrapper
#sed -i -e '1i\/* LibSafe definition START */\n/* LibSafe */' $1
## Get all protocol names `...@protocol _ANY_ :...` in $1 (file)
## Avoid rescoping protocol extensions `@protocol _ANY_ ()`
getProtocolsInFile "${1}"; PROTOCOLS=("${RETURN[@]}")
getInterfacesInFile "${1}"; INTERFACES=("${RETURN[@]}")
PROTOCOLS_AND_INTERFACES=("${PROTOCOLS[@]}" "${INTERFACES[@]}")
DEFINES=()
TOP_INSERT="/* LibSafe definition START */
/**
* LibSafe adds defines to your classes with random names so that your code
* can be imported multiple times without symbol conflicts.
*
* Thanks to LibSafe and the generated defines below you can use this class
* in your closed source library even if someone else has included the same
* exact class.
*
* Read more at: https://github.com/libsafe
*/
"
echo ""
## Append the $2 (scope) to the end of the protocol name
for i in "${PROTOCOLS_AND_INTERFACES[@]}"
do
## Append the
DEFINE_STRING="#define ${i} ${i}${2}"
DEFINES+=($DEFINE_STRING)
## Add a #define for each protocol `#define _ORIGINAL_NAME_ _SCOPED_NAME_` to top of $1 (file)
echo -e "Add define string: \n'${DEFINE_STRING}'"
## Insert the define string at the top of the file
# sed -i -e "2i\
# ${DEFINE_STRING}
# " $1
TOP_INSERT+="${DEFINE_STRING}\n"
done
TOP_INSERT+="\n/* LibSafe definition END */\n"
## Write to file if we have defines
if ! [ ${#DEFINES[@]} -eq 0 ]; then
echo -e "${TOP_INSERT}$(cat "${1}")" > "${1}"
fi
RETURN=("${DEFINES[@]}");
}
function replaceLibSafeHeader()
{
## Do not continue if no header file as $1
if [ -z "$LIBSAFE_HEADER_FILE" ]; then
return 0
fi
## We have the header, replace the contents with generated content
echo ""
echo "Found LibSafe-Header.h"
echo "Replacing contents in LibSafe Header... (${LIBSAFE_HEADER_FILE})"
HEADER_CONTENT="/* LibSafe Auto Generated Header */
/**
* LibSafe adds defines to your classes with random names so that your code
* can be imported multiple times without symbol conflicts.
*
* Thanks to LibSafe and the generated defines below you can use this class
* in your closed source library even if someone else has included the same
* exact class.
*
* Read more at: https://github.com/libsafe
*/
/**
* The unique random string used to scope your classes.
*
* You can use this define to scope your folder/file names when you write
* to disk to make sure you are not conflicting with other implementations
* of the same (your) code.
*/
#define LIBSAFE_RANDOM @\"${RANDOM_STRING}\"
"
echo -e "${HEADER_CONTENT}" > "${LIBSAFE_HEADER_FILE}"
}
randomString; RANDOM_STRING="${RETURN}"
echo "Generated random scope string to use: ${RANDOM_STRING}"
getAllObjectiveCFiles; FILES=("${RETURN[@]}")
echo "Number of files to scope: ${#FILES[@]}"
# Print all files on separate lines
printf '%s\n' "${FILES[@]}"
## Scope the name of all protocols and interfaces in all files
forEach FILES[@] scopeProtocolsAndInterfacesInFile "_libsafe_${RANDOM_STRING}"; RESULT=("${RETURN[@]}")
## Get LibSafe Header
findLibSafeHeader
## Write to the LibSafe Header
replaceLibSafeHeader
echo -e "\n\nDone!"