-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsnippet.functions
97 lines (79 loc) · 3.22 KB
/
snippet.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
# This file is part of shellfire core. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT. No part of shellfire core, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire core. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT.
core_usesIn core base64/decode variable temporaryFiles
# Unfortunately, strips trailing LFs
core_dependency_requires '*' cat
core_snippet_raw_encoder()
{
# "$(<FILE)" doesn't work in all situations
core_variable_setVariable _core_snippet_embeddedData_${snippetName} "$(cat "$snippetFilePath")"
}
core_snippet_raw_decoder()
{
local core_variable_indirectValue_result
core_variable_indirectValue _core_snippet_embeddedData_${snippetName}
if core_variable_isTrue "$snippetAppend"; then
printf '%s' "$core_variable_indirectValue_result" >>"$snippetFilePath"
else
printf '%s' "$core_variable_indirectValue_result" >"$snippetFilePath"
fi
}
# Different base64 implementations wrap differently and with different switches
core_dependency_requires '*' base64 tr
core_snippet_base64_encoder()
{
_core_snippet_embeddedData_${snippetName}="$(base64 "$snippetFilePath" | tr -d '\n')"
}
core_snippet_base64_decoder()
{
core_base64_decode_string "$(core_variable_indirectValue _core_snippet_embeddedData_${snippetName})" "$snippetFilePath" "$snippetAppend" '+' '/'
}
core_usesIn core variable
core_snippet_embed()
{
local codecName="$1"
shift 1
local encoder="core_snippet_${codecName}_encoder"
local snippetName
local snippetFilePath
for snippetName in "$@"
do
if core_variable_isSet _core_snippet_embeddedData_${snippetName}; then
continue
fi
snippetFilePath="$_program_libPath"/"$core_libraryName"/"${_program_namespace}"/"$snippetName".snippet
if ! core_path_isReadableNonEmptyFilePath "$snippetFilePath"; then
core_exitError $core_commandLine_exitCode_SOFTWARE "Snippet '$snippetName' does not exist as a readable, non-empty file at '$snippetFilePath'"
fi
${encoder}
core_dependency_declares _core_snippet_embeddedData_${snippetName}
core_variable_setVariable _core_snippet_embeddedCodec_${snippetName} $codecName
core_dependency_declares _core_snippet_embeddedCodec_${snippetName}
done
}
core_snippet_retrieve()
{
local snippetName="$1"
local snippetAppend="$2"
local snippetFilePath="$3"
local core_variable_indirectValue_result
local codec
local decoder
core_variable_indirectValue _core_snippet_embeddedCodec_${snippetName}
codec="$core_variable_indirectValue_result"
decoder="core_snippet_${codec}_decoder"
${decoder}
}
core_dependency_requires '*' cat rm
core_snippet_retrieveAndSourceAsHereDoc()
{
local snippetName="$1"
local TMP_FILE
core_temporaryFiles_newFileToRemoveOnExit
local snippetFilePath="$TMP_FILE"
printf '%s\n' 'cat <<EOF' >"$snippetFilePath"
core_snippet_retrieve "$snippetName" yes "$snippetFilePath"
printf '\nEOF' >>"$snippetFilePath"
. "$snippetFilePath"
rm "$snippetFilePath" || true
}