-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecompose-functions
412 lines (353 loc) · 9.73 KB
/
decompose-functions
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# decompose functions
#
# Assume $DIR is defined as the root directory of the calling script.
#!/bin/bash
# Parse parameters and run the correct command.
#
# Parameters:
# $1: decompose parameters
decompose-parse-parameters() {
case $1 in
"--init")
decompose-init-command $2;;
"--update")
decompose-update-environment;;
"--build")
decompose-build;;
"--clean")
decompose-clean;;
"--version")
decompose-version;;
"--help"|"")
decompose-print-help;;
"--completion")
decompose-completion;;
*)
_decompose-load-processes
local p; for p in "${DECOMPOSE_PROCESSES[@]}"; do
if [ "$1" == "$p" ]; then
decompose-load-elements
_decompose-process-$p "${@:2}"
exit $?
fi
done
echo "Unknown parameter '$1'"
decompose-print-help
exit 1
;;
esac
}
# Print help
decompose-print-help() {
decompose-load-elements
cat <<HELP_EOF
decompose - Development Environment Compose
Core commands:
--init <git_url>
Initialize an environment from a git URL and name
--update
Update the current environment
--build
Build project files from templates
--clean
Removed project files generated from templates
--version
Print version
--help
This message
HELP_EOF
_decompose-load-processes
if [ "${#DECOMPOSE_PROCESSES[@]}" -gt "0" ]; then
echo -e "\nEnvironment processes:\n"
local p; for p in "${DECOMPOSE_PROCESSES[@]}"; do
echo "$p"
_decompose-process-"$p"_help
done
fi
}
# Return md5 hash of parameter $1
#
# Parameters:
# $1: string to hash
# $2: hash value returned
decompose-md5-hash() {
local string_value=$1
local __hash_value=$2
local _hash=$(echo "$string_value" | md5sum)
eval "$__hash_value=${_hash% -*}"
}
# Initialize the environment (.decompose directory)
#
# Paramaters:
# $1: environment url
decompose-init-environment() {
local environment_url=$1
# Is environment parameter valid?
if [ -z "$environment_url" ]; then
decompose_echoerr "Please supply an environment url to initialize"
return 1
fi
echo "Initializing environment..."
# If .decompose already exists then we shouldn't init
local project_root; _decompose-project-root project_root
if [ -d "$project_root" ]; then
decompose_echoerr ".decompose directory already exists. Aborting init."
return 1
fi
# If .git exists then add decompose as a submodule
local git_root; _decompose-git-root git_root
if [ -d "$git_root" ]; then
_decompose-initialize-decompose-submodule "$git_root"
return $?
fi
# If neither .git or .decompose exist then initialize git and create decompose directory
git init
_decompose-initialize-decompose-submodule "./"
return $?
}
# Initialize the .decompose environment submodule
#
# $1: Git root
_decompose-initialize-decompose-submodule() {
local git_root=$(realpath $1)
# If git_root doesn't exist then abort
if [ ! -d "$git_root" ]; then
echo "Couldn't initialize .decompose because git root doesn't exist."
return 1
fi
mkdir "$git_root"/.decompose
echo -n "$(cd "$git_root" && \
git submodule --quiet add -b master "$environment_url" .decompose/environment && \
git submodule --quiet update --init --recursive .decompose/environment )"
}
# Returns the git root directory
#
# Parameters:
# $1: return value (project git location)
_decompose-git-root() {
local __return_val=$1
local git_root=$(git rev-parse --show-toplevel 2>/dev/null)
# If git root exists then return it
if [ -d "$git_root" ]; then
if [ $# -eq 0 ]; then
echo "$git_root"
else
eval "$__return_val=$git_root"
fi
return 0
fi
return 1
}
# Initialize environment command
#
# Paramaters:
# $1: environment url
decompose-init-command() {
local environment_url=$1
# Is environment parameter valid?
if [ -z "$environment_url" ]; then
echo "Please supply an environment url to initialize"
exit 1
fi
# Create environment
decompose-init-environment $environment_url
# If error occured then quit
if [ "$?" -gt "0" ]; then
echo "Error occured initializing environment"
exit 1
fi
# Copy skeleton to working directory
cp -RL .decompose/environment/skel/. ./
# Add skeleton submodules
if [ -f .decompose/environment/skel-submodules.csv ]; then
OLDIFS=$IFS; IFS=","
while read path url; do
git submodule add "$url" "$path" &>/dev/null
git submodule update --init --recursive &>/dev/null
done < .decompose/environment/skel-submodules.csv
IFS=$OLDIFS
fi
# Add skeleton symlinks
if [ -f .decompose/environment/skel-symlinks.csv ]; then
OLDIFS=$IFS; IFS=","
while read name path; do
ln -s "$path" "$name"
done < .decompose/environment/skel-symlinks.csv
IFS=$OLDIFS
fi
}
# Return 0 if .decompose exists, else return 1
decompose-directory-exists() {
local decompose_directory
_decompose-find-decompose-directory decompose_directory
# If .decompose is missing then return error code.
if [ "$?" -gt "0" ]; then
return 1
fi
return 0
}
# Load elements
decompose-load-elements() {
local decompose_directory
_decompose-find-decompose-directory decompose_directory
# Load default settings and then overrides
if [ -e $decompose_directory/environment/elements ]; then
. $decompose_directory/environment/elements
fi
if [ -e $decompose_directory/elements ]; then
. $decompose_directory/elements
fi
# Post process elements
decompose-post-process-elements
}
# Post process elements
decompose-post-process-elements() {
local p; for p in ${ELEMENTS_POST_PROCESS[@]}; do
_decompose-elements-$p
done
}
# Updates decompose environment
decompose-update-environment() {
decompose-directory-exists
if [ "$?" -gt "0" ]; then
decompose_echoerr "decompose has not been initialized"
decompose_echoerr "Try 'decompose --init <git-url>'"
return 1
fi
local project_root; _decompose-project-root project_root
# Update environment
local git_root; _decompose-git-root git_root
echo -n "$(cd "$git_root" && \
git submodule --quiet update --init --remote --recursive .decompose/environment )"
}
# Build project files from templates
decompose-build() {
echo "Building project files from templates"
# Get elements
decompose-load-elements
# Process templates
decompose-process-templates
}
# Clean project files generated from templates
decompose-clean() {
echo "Cleaning project files generated from templates"
# Get elements
decompose-load-elements
# Process templates
decompose-clean-generated-files
}
# Process .mo files
decompose-process-templates() {
# Find all .mo files
local project_root
_decompose-project-root project_root
local ignore_string=""
local i; for i in "${PROJECT_IGNORE_BUILD[@]}"; do
ignore_string="$ignore_string ! -path $i"
done
local find_results=$(find $project_root -path "$project_root/.decompose" \
-prune -o $ignore_string -name "*.mo" -print)
local templates=( $find_results )
# Process .mo files
local t; for t in ${templates[@]}; do
cat $t | . "$DIR/mo/mo" > ${t%.*}
_decompose-chmod-reference $t ${t%.*}
echo "$t -> ${t%.*}"
done
}
# chmod file using another file as reference
#
# Parameters
# $1: Reference file
# $2: File to chmod
_decompose-chmod-reference() {
case $OSTYPE in
darwin*) chmod $(stat -f "%p" "$1") $2;;
linux*) chmod --reference $1 $2;;
*) echo "Error matching OSTYPE: '$OSTYPE'"
exit 1
;;
esac
}
# Clean files generated from .mo files
decompose-clean-generated-files() {
# Find all .mo files
local project_root
_decompose-project-root project_root
local ignore_string=""
local i; for i in "${PROJECT_IGNORE_BUILD[@]}"; do
ignore_string="$ignore_string ! -path $i"
done
local find_results=$(find $project_root -path "$project_root/.decompose" \
-prune -o $ignore_string -name "*.mo" -print)
local templates=( $find_results )
# Process .mo files
local t; for t in ${templates[@]}; do
rm ${t%.*}
echo "Deleted ${t%.*}"
done
}
# Create/collect configuration where necessary
decompose-bootstrap() {
# Verify dependencies
decompose-verify-dependencies
if [ "$?" -ne "0" ]; then
echo "Please install missing dependencies"
exit 1
fi
local decompose_data_dir; decompose-global-data-dir decompose_data_dir
# Create ecosphere
if [ ! -d "$decompose_data_dir/ecosphere" ]; then
mkdir -p "$decompose_data_dir/ecosphere"
fi
}
# Return the decompose global data directory
# Will create the directory if it doesn't exist
#
# Parameters
# $1: return value (global data directory)
decompose-global-data-dir() {
local __return_value=$1
local _data_directory="$HOME/.local/share/decompose"
if [ ! -d "$_data_directory" ]; then
echo "Creating global data directory: '$_data_directory'"
mkdir -p "$_data_directory"
fi
eval "$__return_value=$_data_directory"
}
# Return 1 if the function name passed in exists
#
# Parameters
# $1: function name to check
decompose_function_exists() {
declare -f -F $1 > /dev/null
return $?
}
# Echo to stderr
decompose_echoerr() { echo "$@" 1>&2; }
# Print dependencies that are missing
decompose-verify-dependencies() {
local return_code
local checks=('bash' 'which' 'echo' 'cat' 'mkdir' 'git' 'realpath' 'cp' 'find'
'chmod' 'md5sum')
local check; for check in ${checks[@]}; do
which $check >/dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "'$check' command is missing"
return_code=1
if [ "$check" == "which" ]; then break; fi
fi
done
# Check that mo exists.
if [ ! -e "$DIR/mo/mo" ]; then
echo "'mo' is missing"
return_code=1
fi
return $return_code
}
# Print version
decompose-version() {
local version="0.18.1"
echo "decompose v$version"
}
# vim:syntax=sh tabstop=2 shiftwidth=2 expandtab