Skip to content

Commit 0199e4d

Browse files
committed
Adding Minifier
1 parent 41c0611 commit 0199e4d

File tree

5 files changed

+228
-27
lines changed

5 files changed

+228
-27
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515
uses: technote-space/get-diff-action@v1
1616
- name: Generate installers
1717
run: pip install toml && python generate.py ${{ env.GIT_DIFF }}
18+
- name: Minify generated installers
19+
run: ./minify.sh ${{ env.GIT_DIFF }}
1820
- name: Archive Production Artifact
1921
uses: actions/upload-artifact@master
2022
with:

generate.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import os
44
import logging
55
import errno
6+
import constants
7+
8+
print (constants.CURL_CHECK)
69

710
methods = {
811
"curl": "$CURL",
@@ -42,14 +45,14 @@ def generate(path):
4245

4346
installer_sh.write("""#!/bin/sh
4447
45-
CURL_CMD=$(which curl) # curl tool
46-
YUM_CMD=$(which yum) # yum package manager for RHEL & CentOS
47-
DNF_CMD=$(which dnf) # dnf package manager for new RHEL & CentOS
48-
APT_GET_CMD=$(which apt-get) # apt package manager for Ubuntu & other Debian based distributions
49-
PACMAN_CMD=$(which pacman) # pacman package manager for ArchLinux
50-
APK_CMD=$(which apk) # apk package manager for Alpine
51-
GIT_CMD=$(which git) # to build from source pulling from git
52-
SUDO_CMD=$(which sudo) # check if sudo command is there
48+
CURL_CMD=$(which curl)
49+
YUM_CMD=$(which yum)
50+
DNF_CMD=$(which dnf)
51+
APT_GET_CMD=$(which apt-get)
52+
PACMAN_CMD=$(which pacman)
53+
APK_CMD=$(which apk)
54+
GIT_CMD=$(which git)
55+
SUDO_CMD=$(which sudo)
5356
5457
USER="$(id -un 2>/dev/null || true)"
5558
SUDO=''
@@ -65,10 +68,10 @@ def generate(path):
6568
fi
6669
fi
6770
68-
RESET='\033[0m' # Red
69-
RED='\033[0;31m' # Red
70-
GREEN='\033[0;32m' # Green
71-
YELLOW='\033[0;33m' # Yellow
71+
RESET='\033[0m'
72+
RED='\033[0;31m'
73+
GREEN='\033[0;32m'
74+
YELLOW='\033[0;33m'
7275
log () {
7376
echo "[`date "+%Y.%m.%d-%H:%M:%S%Z"`]$1 $2"
7477
}

installers/git/installer.sh

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/bin/sh
2-
3-
CURL_CMD=$(which curl) # curl tool
4-
YUM_CMD=$(which yum) # yum package manager for RHEL & CentOS
5-
DNF_CMD=$(which dnf) # dnf package manager for new RHEL & CentOS
6-
APT_GET_CMD=$(which apt-get) # apt package manager for Ubuntu & other Debian based distributions
7-
PACMAN_CMD=$(which pacman) # pacman package manager for ArchLinux
8-
APK_CMD=$(which apk) # apk package manager for Alpine
9-
GIT_CMD=$(which git) # to build from source pulling from git
10-
SUDO_CMD=$(which sudo) # check if sudo command is there
2+
3+
CURL_CMD=$(which curl)
4+
YUM_CMD=$(which yum)
5+
DNF_CMD=$(which dnf)
6+
APT_GET_CMD=$(which apt-get)
7+
PACMAN_CMD=$(which pacman)
8+
APK_CMD=$(which apk)
9+
GIT_CMD=$(which git)
10+
SUDO_CMD=$(which sudo)
1111

1212
USER="$(id -un 2>/dev/null || true)"
1313
SUDO=''
@@ -26,20 +26,20 @@ fi
2626
if [ ! -z $APT_GET_CMD ]; then
2727
$SUDO apt-get update
2828
$SUDO apt-get install git
29-
29+
3030
elif [ ! -z $YUM_CMD ]; then
3131
$SUDO yum install git
32-
32+
3333
elif [ ! -z $PACMAN_CMD ]; then
3434
pacman -Sy git
35-
35+
3636
elif [ ! -z $DNF_CMD ]; then
3737
$SUDO dnf install git
38-
38+
3939
elif [ ! -z $APK_CMD ]; then
4040
$SUDO apk add git
41-
41+
4242
else
4343
echo "Couldn't install package"
4444
exit 1;
45-
fi
45+
fi

minifier.sh

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#!/bin/bash
2+
# License: The MIT License (MIT)
3+
# Author Zuzzuc https://github.com/Zuzzuc/
4+
5+
# This script will Minify bash scripts.
6+
7+
# Assign variables
8+
# Default vars
9+
force=0
10+
permission="u+x"
11+
mode=RAM
12+
output=stdout
13+
debug=0
14+
self="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
15+
16+
#Functions
17+
exitw(){
18+
# Exit and print exit code.
19+
# Usage is $1, where $1 is the error code.
20+
echo "Error code: $1. Exiting"
21+
exit $1
22+
}
23+
24+
SanitizeFilePath(){
25+
# This function will remove \ and space at the end of a filepath to make it parse well into other, quoted, functions/commands
26+
# Usage $1, where $1 is a file path.
27+
echo -n "$(echo "$(echo "$1" | sed 's%\\%%g')" |sed -e 's%[[:space:]]*$%%')"
28+
}
29+
30+
readLine(){
31+
# This function will read line $1. Output to stdout
32+
# Usage is $1, where $1 is the line to read.
33+
sed "$1q;d" "$file"
34+
}
35+
36+
processData(){
37+
# This function will format any input line to be able to fit in a one liner.
38+
# Usage is $1, where $1 is the data.
39+
40+
# Remove trailing spaces.
41+
data="$(echo -e "${1}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
42+
43+
ic=0
44+
45+
# Remove comments
46+
# Temporary fix, will only remove full line comments...
47+
48+
# The following line will check if the line contains '#'
49+
if [[ "$(echo "$data" | grep -q '#';echo $?)" == "0" ]];then
50+
51+
# Remove any empty characters so comparison will be easier.
52+
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' )"
53+
if [ "${tmpString:0:1}" == "#" ];then
54+
# $data is a full line comment. We'll remove it fully.
55+
data=""
56+
ic=1
57+
fi
58+
fi
59+
60+
# We should not run this if data is a full line comment, as it will corrupt the script.
61+
if [ $ic -eq 0 ];then
62+
# Look for exceptions
63+
if [ "${data: -3}" == ";do" ] || [ "${data: -5}" == ";then" ] || [ "${data: -4}" == "else" ] || [ "${data: -4}" == "elif" ] || [ "${data: -1}" == "{" ];then
64+
# Add a space
65+
data="$(echo "$data" | sed "s%$% %")"
66+
elif [ "${data: -1}" == "}" ];then
67+
data="$(echo "$data" | sed 's%}$% };%')"
68+
else
69+
# Add ';' to end of line.
70+
data="$(echo "$data" | sed "s%$%;%")"
71+
fi
72+
fi
73+
74+
# Return $data
75+
echo -ne "$data"
76+
77+
}
78+
79+
# Handle input
80+
for i in "$@";do
81+
case $i in
82+
$self)
83+
shift
84+
;;
85+
-f=*|--file=*)
86+
file="$(SanitizeFilePath "${i#*=}")"
87+
if [ "$file" == "$self" ];then
88+
echo "You are trying to execute this script on itself."
89+
exitw 5
90+
fi
91+
shift
92+
;;
93+
-F|--force)
94+
force=1
95+
shift
96+
;;
97+
-m=*|--mode=*)
98+
mode="${i#*=}"
99+
shift
100+
;;
101+
-o=*|--output=*)
102+
if [ "${i#*=}" == "STDOUT" ] || [ "${i#*=}" == "stdout" ];then
103+
output="stdout"
104+
else
105+
output="file"
106+
outputFile="$(SanitizeFilePath "${i#*=}")"
107+
fi
108+
shift
109+
;;
110+
-p=*|--permission=*)
111+
permission="${i#*=}"
112+
shift
113+
;;
114+
--debug)
115+
debug=1
116+
shift
117+
;;
118+
*)
119+
echo "Unknown arg supplied. The failing arg is '$i'"
120+
exitw 4
121+
shift
122+
;;
123+
esac
124+
done
125+
126+
if [ ! -f "$file" ];then
127+
echo "The file you supplies, '$file', can not be found or is not a file."
128+
exitw 3
129+
fi
130+
if [ -f "$outputFile" ];then
131+
if [ "$force" != 1 ];then
132+
echo "A file already exists in output path, would you like to overwrite it? Press [y]es or [n]o"
133+
read continue
134+
if [ "$continue" != "y" ] && [ "$continue" != "Y" ];then
135+
exitw 2
136+
else
137+
echo "Continuing..."
138+
unset continue
139+
fi
140+
fi
141+
fi
142+
if [ "$force" != 1 ];then
143+
if [ "$(head -1 "$file")" != '#!/bin/bash' ] && [ "$(head -1 "$file")" != '#!/bin/sh' ] && [ "$(head -1 "$file")" != '#!/usr/bin/env bash' ];then
144+
echo "The script targeted might not be a bash script, would you still like to continue? Press [y]es or [n]o"
145+
read continue
146+
if [ "$continue" != "y" ] && [ "$continue" != "Y" ];then
147+
exitw 2
148+
else
149+
echo "Continuing..."
150+
unset continue
151+
fi
152+
fi
153+
fi
154+
155+
# Minify
156+
FirstLine="$(readLine 1)"
157+
body=""
158+
line=2
159+
linesInFile=$(wc -l < "$file")
160+
161+
while [ $(($line-1)) -le $linesInFile ];do
162+
if [ "$debug" == "1" ];then
163+
echo $line
164+
fi
165+
body+="$(processData "$(readLine $line)")"
166+
line=$((line+1))
167+
done
168+
169+
fullfile="$(echo $FirstLine;echo $body)"
170+
171+
if [ "$output" == "stdout" ];then
172+
echo -n "$fullfile"
173+
elif [ "$output" == "file" ];then
174+
echo -n "$fullfile" > "$outputFile"
175+
chmod "$permission" "$outputFile"
176+
else
177+
exitw 6
178+
fi
179+
180+
exit

minify.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
X=()
4+
5+
for word in "$@"; do
6+
X+=$(dirname "$word " | cut -d "/" -f "1 2")" "
7+
done
8+
9+
CHANGED=$(echo $X | tr ' ' '\n' | sort | uniq | xargs)
10+
11+
echo "minifying $CHANGED"
12+
#shellspec $CHANGED
13+
14+
for word in $CHANGED; do
15+
./minifier.sh -f="$word/installer.sh" > "$word/installer.min.sh"
16+
done

0 commit comments

Comments
 (0)