-
Notifications
You must be signed in to change notification settings - Fork 19
/
include-phraseanet-sources.sh
319 lines (262 loc) · 7.65 KB
/
include-phraseanet-sources.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash
# Files to copy from Phraseanet github repository
GITHUB_FILE_LIST=(
"/lib/conf.d/configuration.yml" \
)
# Destination where to copy source files
DESTINATION_DIRECTORY="./common/github_source"
# Path to reference version file
VERSION_INC_FILE_PATH="./__version__.inc"
#######################################
# Display title in terminal
# Arguments:
# title (string)
# Returns:
# console displaying title
#######################################
display_title()
{
local title
local lenTitle
local hyphen
title=" $1 "
lenTitle=${#title}
hyphen=$(printf "%-${lenTitle}s" "─")
echo "┌─${hyphen// /─}─┐"
echo "│$title│"
echo "└─${hyphen// /─}─┘"
}
#######################################
# Colorize output
# Arguments:
# $1 (string)
#######################################
colorize()
{
echo -e "\e[33m$1\e[0m"
}
#######################################
# Return Github repository tag list
# Arguments:
# variable name will contain result
# repo name (e.g. 'alchemy-fr/Phraseanet')
# Usage:
# getGithubTagsList tagList 'alchemy-fr/Phraseanet'
# printf '%s\n' "${tagList[@]}"
#######################################
getGithubTagsList()
{
# Namespaced variable name to avoid collision
local getGithubTagsList_returnValue=$1
local getGithubTagsList_repoName=$2
local getGithubTagsList_tagList
getGithubTagsList_tagList=$(wget -q https://api.github.com/repos/$getGithubTagsList_repoName/tags -O - | grep '"name": "' | sed 's/\s*"name": "//g' | sed 's/",//g')
local IFS=$'\n'
mapfile -t "$getGithubTagsList_returnValue" < <( \
echo "${getGithubTagsList_tagList[*]}" )
}
#######################################
# Return semver result comparison :
# - 0 : semver1 = semver2
# - 1 : semver1 > semver2
# - 2 : semver1 < semver2
# Arguments:
# semver1 (string)
# semver2 (string)
#######################################
semver_comp ()
{
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
#######################################
# Return last patched version
# Arguments:
# targetVersion (string) (e.g. "4.1")
# versionList (array)
# Return example : 4.1.3
#######################################
getLastPatchFromVersion()
{
local targetVersion=$1;
shift;
declare -A tagMap # Create an associative array
local listEntries
listEntries=("$@")
for i in "${listEntries[@]}";
do
semver=$(echo "$i" | egrep -o '[0-9]+\.[0-9]+\.[0-9][0-9]*')
key=$(echo "$i" | egrep -o '[0-9]+\.[0-9]')
if [[ ${tagMap[$key]+_} ]];
then
semver_comp ${semver} "${tagMap[$key]}"
if [[ $? == "1" ]]; then
tagMap[$key]=${semver}
fi
else
tagMap[$key]=$semver
fi
done
for versionFounded in "${!tagMap[@]}";
do
if [[ "$versionFounded" == "$targetVersion" ]]; then
echo "${tagMap[$versionFounded]}"
fi
done
}
#######################################
# Display all last patch for each version X.X
# Example :
# 4.0 -> 4.0.12
# 4.1 -> 4.1.3
# 3.1 -> 3.1.10
# 3.8 -> 3.8.8
#######################################
showAllLastPatch()
{
declare -A tagMap
local listEntries
listEntries=("$@")
for i in "${listEntries[@]}";
do
semver=$(echo "$i" | egrep -o '[0-9]+\.[0-9]+\.[0-9][0-9]*')
key=$(echo "$i" | egrep -o '[0-9]+\.[0-9]')
if [[ ${tagMap[$key]+_} ]];
then
semver_comp ${semver} "${tagMap[$key]}"
if [[ $? == "1" ]]; then
tagMap[$key]=${semver}
fi
else
tagMap[$key]=$semver
fi
done
echo 'All last minor version :'
for K in "${!tagMap[@]}";
do
echo "$K -> "${tagMap[$K]};
done
}
#######################################
# Return current version from
# $VERSION_INC_FILE_PATH file
#######################################
get_current_version()
{
local currentVersion
if [[ -e $VERSION_INC_FILE_PATH ]]; then
currentVersion=$(sed -n -e '/'"CURRENT_VERSION"'/ s/.*\= *//p' "$VERSION_INC_FILE_PATH")
fi
echo "$currentVersion"
}
#######################################
# Download list files to dedicated directory
# Arguments:
# lastVersionPatched (string) (e.g. "4.1.3")
# destinationDirectory (string)
# githubFileList (array)
#######################################
copySourceFiles()
{
local lastVersionPatched=$1;
shift;
local destinationDirectory=$1;
shift;
local githubFileList=$1;
githubFileList=("$@")
local githubPath
local localDir
local localPathFile
local tmpFile
if [[ ! -d "$destinationDirectory" ]]; then
mkdir -p "$destinationDirectory"
fi
if [[ ! -d "$destinationDirectory" ]]; then
echo "error Can't create directory $destinationDirectory"
exit
fi
for i in "${githubFileList[@]}";
do
echo
githubPath="https://raw.githubusercontent.com/alchemy-fr/Phraseanet/$lastVersionPatched$i"
tmpFile=$(mktemp)
echo -e "download source... ("$(colorize "$githubPath")")"
wget -o "logfile" "$githubPath" -O "$tmpFile"
if [[ $? -ne 0 ]]
then
# wget fail, display log
cat "logfile"
else
localDir="$destinationDirectory"$(dirname "${i}")
localPathFile="$destinationDirectory$i"
echo "to: "$(colorize "$localPathFile")
if [[ ! -d "$localDir" ]]; then
mkdir -p "$localDir"
fi
mv "$tmpFile" "$localPathFile"
fi
rm "logfile"
done
}
#######################################
# Entrypoint
#######################################
start()
{
local currentVersionOrBranchName
local lastVersionPatchNumber
local tagList
local branchName
echo
display_title "Include Phraseanet Sources | Start"
currentVersionOrBranchName=$(get_current_version)
# Switch "Version" or "Branch" mode by regex pattern
if [[ "$currentVersionOrBranchName" =~ ^[0-9]{1,}\.[0-9]{1,}$ ]]; then
# Version mode (e.g: "4.2")
echo -e "Target Phraseanet version : "$(colorize "$currentVersionOrBranchName")
getGithubTagsList tagList 'alchemy-fr/Phraseanet'
#showAllLastPatch "${tagList[@]}"
lastVersionPatchNumber=$(getLastPatchFromVersion "$currentVersionOrBranchName" "${tagList[@]}")
if [[ -z "$lastVersionPatchNumber" ]]; then
echo "No last version patch number found... Exit."
else
echo "Last version patch number : "$(colorize "$lastVersionPatchNumber")
copySourceFiles "$lastVersionPatchNumber" "$DESTINATION_DIRECTORY" "${GITHUB_FILE_LIST[@]}"
fi
else
# Branch mode (e.g: "master")
echo -e "Target Phraseanet branch : "$(colorize "$currentVersionOrBranchName")
copySourceFiles "$currentVersionOrBranchName" "$DESTINATION_DIRECTORY" "${GITHUB_FILE_LIST[@]}"
fi
display_title "Include Phraseanet Sources | End"
echo
}
start
# https://raw.githubusercontent.com/alchemy-fr/Phraseanet/master/config/configuration.sample.yml
# https://raw.githubusercontent.com/alchemy-fr/Phraseanet/4.1.3/lib/conf.d/configuration.yml