forked from fidian/bash-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash-minifier
More file actions
executable file
·75 lines (57 loc) · 1.86 KB
/
Copy pathbash-minifier
File metadata and controls
executable file
·75 lines (57 loc) · 1.86 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
#! /usr/bin/env bash
# shellcheck disable=SC1091
. bpm
bpm::include assign
bpm::include strict
. lib/bash-tokenizer
# to profile, unset these two and run. (ps4-python)
# ./bash-minifier SCRIPT 2>&1 | grep '^\+' | ./profile.py
# PS4='+$? (${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): } $BASH_COMMAND\n -> '
# set -x
# Yet another method (debug trap) with only Bash 5
# PROFILE=true ./bash-minifier SCRIPT 2>&1 | ./profile-bash-5
if [[ -n "$PROFILE" ]]; then
set -o functrace
if [[ -n "$EPOCHREALTIME" ]]; then
trap 'echo "+$? $EPOCHREALTIME (${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): } $BASH_COMMAND" >&2' DEBUG
elif [[ "$(date +%N)" == N ]]; then
# BSD version of date
trap 'echo "+$? $(python -c '"'"'from time import time; print "%.9f" % time();'"'"') (${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): } $BASH_COMMAND" >&2' DEBUG
else
# GNU version of date
trap 'echo "+$? $(date +%s.%N) (${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): } $BASH_COMMAND" >&2' DEBUG
fi
fi
bash-minifier::help() {
cat <<'EOF'
Bash Minifier
Parses a shell script and writes a minified version to stdout.
Usage:
bash-minifier INPUT_FILE > OUTPUT_FILE
EOF
}
bash-minifier::load() {
local content=()
while IFS=$'\n' read -r line; do
content[${#content[@]}]=$line
done < <(cat -- "$2")
local "$1" && assign::array "$1" "${content[@]}"
}
bash-minifier() {
local content filename
strict::mode
filename=${1-}
if [[ ! -f "$filename" ]] && [[ "$filename" != "-" ]]; then
filename=
fi
if [[ -z "$filename" ]]; then
bash-minifier::help
exit 1
fi
content=$(cat -- "$filename"; echo x)
content=${content%x}
bash-tokenizer::tokenize content "$content"
}
if ! bpm::isSourced; then
bash-minifier ${@+"$@"}
fi