|
| 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 |
0 commit comments