-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline_source.sh
More file actions
109 lines (95 loc) · 3.49 KB
/
inline_source.sh
File metadata and controls
109 lines (95 loc) · 3.49 KB
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
#!/usr/bin/env bash
set -o errexit -o errtrace -o nounset -o pipefail
source 'deps/includes/src/comment/license/comment.license-mpl2.inc'
source 'deps/includes/src/comment/comment.code-style-potherca.inc'
# ==============================================================================
# INLINE SOURCE CODE
# ------------------------------------------------------------------------------
## Usage: $0 [-h] <source-path>
##
## Where:
## \t- <source-path>\tis the path to the file that should have all its sources inlined
##
## Options:
## \t-h|--help\tPrint this help dialogue and exit
##
## This script will output a file for a given path, will all `sourced` files
## inlined.
##
## This allows for a single script to be created from source(d) files for
## distribution purposes.
##
## If a script sources files that are not in the `$PATH`, this will need to be
## resolved before calling this script.
##
## The result is output to the screen. To "save" the result, redirect STDOUT to a
## file.
##
## The script leaves everything "as-is", except for any shebang lines that it
## might find. To clean things up, pipe this script through a formatter, (for
## instance the excellent shfmt)
##
## Usage example:
##
## inline_source /path/to/file.sh
##
## Usage example redirecting output to a file:
##
## inline_source ./src/file.sh > ./dist/file-name
##
## Usage example setting an include path:
##
## export PATH="${PATH}:./includes" && inline_source ./src/file-name.sh
##
## Usage example piping to a formatter:
##
## inline_source ./src/file-name.sh | shfmt -bn -ci -sr -s -i 4
##
## Full usage example, combining all of the above:
##
## export PATH="${PATH}:./includes" && inline_source ./src/file-name.sh | shfmt -bn -ci -sr -s -i 4 > ./dist/file-name
##
# ===============================================================================
source 'deps/includes/src/declare/declare.exit-codes.inc'
source 'src/function.usage.sh'
inline_source() {
source 'src/function.get_directory_path.sh'
source 'src/function.get_file_from_string.sh'
source 'src/function.get_file_path.sh'
source 'src/function.handleParameters.sh'
source 'src/function.replace_line_with_file_content.sh'
if [[ $# -lt 1 || $1 == "" ]]; then
echo -e 'One parameter expected: <source-file>\nCall with --help for usage information.' >&2
exit "${EXIT_NOT_ENOUGH_PARAMETERS}"
else
local sSourceDirectory sSourceFilePath sTrimmedLine
handleParameters "${@}"
if [[ "${g_bShowUsage}" == "true" ]]; then
usage "${0}"
exit "${EXIT_OK}"
elif [[ ! -f ${1} ]]; then
echo "Could not find <source-file> '${1}'" >&2
exit "${EXIT_COULD_NOT_FIND_FILE}"
fi
sSourceFilePath="$(get_file_path "${1}")"
sSourceDirectory=$(dirname "${sSourceFilePath}")
readonly sSourceFilePath sSourceDirectory
pushd "${sSourceDirectory}" > /dev/null || exit "${EXIT_COULD_NOT_FIND_DIRECTORY}"
while read -r; do
# @NOTE: $REPLY is set by `read`
sTrimmedLine="$(echo "${REPLY}" | xargs)"
if [[ "${sTrimmedLine:0:7}" == 'source ' && ${REPLY} != *\$* ]]; then
replace_line_with_file_content "${sTrimmedLine}"
else
echo "${REPLY}"
fi
done < "${sSourceFilePath}"
popd > /dev/null 2>&1 || true
fi
}
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f inline_source
else
inline_source "${@-}"
fi
# EOF