-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_sources.sh
120 lines (90 loc) · 4.61 KB
/
update_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
#!/usr/bin/env bash
set -o errexit -o errtrace -o nounset -o pipefail
: "${GIT:=git}"
update_sources() {
local -r sScriptPath="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
# shellcheck source=./src/function.get_local_version.sh
source "${sScriptPath}/src/function.get_local_version.sh"
# shellcheck source=./src/function.get_project_name_from_github_url.sh
source "${sScriptPath}/src/function.get_project_name_from_github_url.sh"
# shellcheck source=./src/function.get_version_from_remote_git.sh
source "${sScriptPath}/src/function.get_version_from_remote_git.sh"
update_source() {
local iLength sConfig sFilePath sPath sProject sRemoteVersion sSource sSourceFile sSourcePath sTargetFile sTargetFolder sTargetPath
readonly sProject="${1?Five parameters required: <project> <target-path> <source-path> <path> <config>}"
readonly sTargetPath="${2?Five parameters required: <project> <target-path> <source-path> <path> <config>}"
readonly sSourcePath="${3?Five parameters required: <project> <target-path> <source-path> <path> <config>}"
readonly sPath="${4?Five parameters required: <project> <target-path> <source-path> <path> <config>}"
readonly sConfig="${5?Five parameters required: <project> <target-path> <source-path> <path> <config>}"
echo -e "\n =====> Updating ${sProject} (${sPath})"
if [[ ! -d "${sSourcePath}/${sPath}" ]];then
echo ' -----> Creating git clone'
git clone "git://github.com/${sProject}.git" "${sSourcePath}/${sPath}"
else
echo ' -----> Git clone exists, pull latest changes'
git --git-dir="${sSourcePath}/${sPath}/.git" --work-tree="${sSourcePath}/${sPath}" pull
fi
echo ' -----> Looking up config'
sSource="$(echo "${sConfig}" | grep "\b${sPath}\b" | tr -d ' ')" || true
if [[ "${sSource}" == '' ]];then
echo -e "\tNo config available. Skipping"
else
readonly iLength=$(( "${#sSourcePath} + ${#sSource}" + 1))
echo ' -----> Copying "*.puml" files'
if [ "$(find "${sSourcePath}/${sSource}" -name '*.puml')" ];then
find "${sSourcePath}/${sSource}" -name '*.puml' -print0 |
while read -r -d $'\0' sFilePath; do
sSourceFile="${sSourcePath}/${sSource}${sFilePath:${iLength}}"
sTargetFile="${sTargetPath}/${sPath}/${sFilePath:${iLength}}"
sTargetFolder="$(dirname "${sTargetFile}")"
if [[ ! -d "${sTargetFolder}" ]];then
mkdir -p "${sTargetFolder}"
fi
cp "${sSourceFile}" "${sTargetFile}"
done
else
echo -e "\tNo '*.puml' files found. Skipping"
# @TODO: Check if there is an SVG (or PNG?) source dir and generate '*.puml' files from there
fi
echo " -----> Writing INFO file"
sRemoteVersion="$(get_version_from_remote_git "${sProject}")"
if [[ "${sRemoteVersion}" == '' ]];then
sRemoteVersion="0.0.1"
fi
if echo "${sRemoteVersion}" | grep -E '^[0-9]+\.[0-9]+$';then
sRemoteVersion="${sRemoteVersion}.0"
fi
cat > "${sTargetPath}/${sPath}/INFO" <<TXT
VERSION=${sRemoteVersion}
SOURCE=https://github.com/${sProject}
TXT
fi
}
run() {
local sConfigFile sContent sFolder sPath sProject sSourcesFile sSourcePath sTargetPath
readonly sTargetPath="${1?Three parameters required: <target-path> <source-path> <config-file>}"
readonly sSourcePath="${2?Three parameters required: <target-path> <source-path> <config-file>}"
readonly sConfigFile="${3?Three parameters required: <target-path> <source-path> <config-file>}"
for sSourcesFile in "${sTargetPath}/"*'/INFO';do
sPath="$(dirname "${sSourcesFile}")"
sFolder="$(basename "${sPath}")"
sContent="$(cat "${sSourcesFile}")"
sProject="$(get_project_name_from_github_url "${sContent}")"
update_source \
"${sProject}" \
"$(realpath "${sTargetPath}")" \
"$(realpath "${sSourcePath}")" \
"${sFolder}" \
"$(cat "${sConfigFile}")"
done;
# Cleanup
rm -rf "${sTargetPath}/C4/samples/" "${sTargetPath}/C4/percy/" "${sTargetPath}/tupadr3/examples/"
}
run "${@}"
}
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
export -f update_sources
else
update_sources "${@}"
exit $?
fi