-
Notifications
You must be signed in to change notification settings - Fork 21
/
minifier.sh
executable file
·180 lines (159 loc) · 4.2 KB
/
minifier.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
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
#!/bin/bash
# License: The MIT License (MIT)
# Author Zuzzuc https://github.com/Zuzzuc/
# This script will Minify bash scripts.
# Assign variables
# Default vars
force=0
permission="u+x"
mode=RAM
output=stdout
debug=0
self="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
#Functions
exitw(){
# Exit and print exit code.
# Usage is $1, where $1 is the error code.
echo "Error code: $1. Exiting"
exit $1
}
SanitizeFilePath(){
# This function will remove \ and space at the end of a filepath to make it parse well into other, quoted, functions/commands
# Usage $1, where $1 is a file path.
echo -n "$(echo "$(echo "$1" | sed 's%\\%%g')" |sed -e 's%[[:space:]]*$%%')"
}
readLine(){
# This function will read line $1. Output to stdout
# Usage is $1, where $1 is the line to read.
sed "$1q;d" "$file"
}
processData(){
# This function will format any input line to be able to fit in a one liner.
# Usage is $1, where $1 is the data.
# Remove trailing spaces.
data="$(echo -e "${1}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
ic=0
# Remove comments
# Temporary fix, will only remove full line comments...
# The following line will check if the line contains '#'
if [[ "$(echo "$data" | grep -q '#';echo $?)" == "0" ]];then
# Remove any empty characters so comparison will be easier.
tmpString="$(echo "$data" | sed 's%\t%%g' | sed 's% %%g' | sed 's%\v%%g' | sed 's%\r%%g' | sed 's%\r%%g' | sed 's%\n%%g' )"
if [ "${tmpString:0:1}" == "#" ];then
# $data is a full line comment. We'll remove it fully.
data=""
ic=1
fi
fi
# We should not run this if data is a full line comment, as it will corrupt the script.
if [ $ic -eq 0 ];then
# Look for exceptions
if [ "${data: -3}" == ";do" ] || [ "${data: -5}" == ";then" ] || [ "${data: -4}" == "else" ] || [ "${data: -4}" == "elif" ] || [ "${data: -1}" == "{" ];then
# Add a space
data="$(echo "$data" | sed "s%$% %")"
elif [ "${data: -1}" == "}" ];then
data="$(echo "$data" | sed 's%}$% };%')"
else
# Add ';' to end of line.
data="$(echo "$data" | sed "s%$%;%")"
fi
fi
# Return $data
echo -ne "$data"
}
# Handle input
for i in "$@";do
case $i in
$self)
shift
;;
-f=*|--file=*)
file="$(SanitizeFilePath "${i#*=}")"
if [ "$file" == "$self" ];then
echo "You are trying to execute this script on itself."
exitw 5
fi
shift
;;
-F|--force)
force=1
shift
;;
-m=*|--mode=*)
mode="${i#*=}"
shift
;;
-o=*|--output=*)
if [ "${i#*=}" == "STDOUT" ] || [ "${i#*=}" == "stdout" ];then
output="stdout"
else
output="file"
outputFile="$(SanitizeFilePath "${i#*=}")"
fi
shift
;;
-p=*|--permission=*)
permission="${i#*=}"
shift
;;
--debug)
debug=1
shift
;;
*)
echo "Unknown arg supplied. The failing arg is '$i'"
exitw 4
shift
;;
esac
done
if [ ! -f "$file" ];then
echo "The file you supplies, '$file', can not be found or is not a file."
exitw 3
fi
if [ -f "$outputFile" ];then
if [ "$force" != 1 ];then
echo "A file already exists in output path, would you like to overwrite it? Press [y]es or [n]o"
read continue
if [ "$continue" != "y" ] && [ "$continue" != "Y" ];then
exitw 2
else
echo "Continuing..."
unset continue
fi
fi
fi
if [ "$force" != 1 ];then
if [ "$(head -1 "$file")" != '#!/bin/bash' ] && [ "$(head -1 "$file")" != '#!/bin/sh' ] && [ "$(head -1 "$file")" != '#!/usr/bin/env bash' ];then
echo "The script targeted might not be a bash script, would you still like to continue? Press [y]es or [n]o"
read continue
if [ "$continue" != "y" ] && [ "$continue" != "Y" ];then
exitw 2
else
echo "Continuing..."
unset continue
fi
fi
fi
# Minify
FirstLine="$(readLine 1)"
body=""
line=2
linesInFile=$(wc -l < "$file")
while [ $(($line-1)) -le $linesInFile ];do
if [ "$debug" == "1" ];then
echo $line
fi
body+="$(processData "$(readLine $line)")"
line=$((line+1))
done
fullfile="$(echo $FirstLine;echo $body)"
if [ "$output" == "stdout" ];then
echo -n "$fullfile"
elif [ "$output" == "file" ];then
echo -n "$fullfile" > "$outputFile"
chmod "$permission" "$outputFile"
else
exitw 6
fi
exit