Skip to content

Instantly share code, notes, and snippets.

@wheresjames
Created December 2, 2020 15:44
Show Gist options
  • Save wheresjames/cee492ea0870aae78069115669dd9dcf to your computer and use it in GitHub Desktop.
Save wheresjames/cee492ea0870aae78069115669dd9dcf to your computer and use it in GitHub Desktop.
#!/bin/bash
# Compress web site
#--------------------------------------------------------------------------------------------------
USAGE="Usage: ./compress-web.sh <source-dir> <destination-dir>"
SCRIPT_NAME=$(basename $0)
SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
WORKING_PATH=$(pwd)
SRCDIR=$1
DSTDIR=$2
TOOLPATH="${SCRIPT_PATH}/.tools"
#--------------------------------------------------------------------------------------------------
# Functions
showWarning()
{
if [[ 0 -lt ${#@} ]]; then
echo -e "[\e[1;33mWARNING\e[1;0m] \e[1;33m${@}\e[1;0m"
fi
}
showInfo()
{
if [[ 0 -lt ${#@} ]]; then
echo -e "[\e[1;36mINFO\e[1;0m] \e[1;36m${@}\e[1;0m"
fi
}
exitWithError()
{
if [[ 0 -lt ${#@} ]]; then
echo
echo "--------------------------------------------------------------------"
echo -e "[\e[1;31mERROR\e[1;0m] \e[1;31m${@}\e[1;0m"
echo "--------------------------------------------------------------------"
echo
fi
exit -1
}
exitOnError()
{
if [[ 0 -eq $? ]]; then return 0; fi
exitWithError $@
}
warnOnError()
{
if [[ 0 -eq $? ]]; then return 0; fi
showWarning $@
return -1
}
isCmd()
{
# - separator
FOUND=$(echo "\-$COMMANDLIST-" | grep -o "\-${1}-")
if [[ ! -z $FOUND ]]; then return 0; fi
# , separator
FOUND=$(echo ",$COMMANDLIST," | grep -o ",${1},")
if [[ ! -z $FOUND ]]; then return 0; fi
return -1
}
findInStr()
{
FIND_LIST=$1
FIND_EXISTS=$(echo $FIND_LIST | grep -o $2)
if [[ ! -z $FIND_EXISTS ]]; then return 0; fi
return -1
}
findIn()
{
findInStr "$($1 2>&1)" $2
return $?
}
waitWhile()
{
if ! findIn "$1" "$2"; then return 0; fi
echo "$3 "
while findIn "$1" "$2"; do
printf .
sleep 3
done
echo
echo "Done"
return -1
}
isCommand()
{
if ! findIn "which $1" "$1"; then return -1; fi
return 0
}
aptInstall()
{
if ! findIn "apt list --installed 2>/dev/null" $1; then
apt-get -y install $1
exitOnError "Unable to install $1"
fi
}
isAptRepo()
{
if ! findIn "egrep -v '^#|^ *$' /etc/apt/sources.list /etc/apt/sources.list.d/*" "$1"; then return -1; fi
return 0
}
addAptRepo()
{
if ! isAptRepo "$1"; then
apt-add-repository $1
if ! warnOnError "Unable to add repo $1"; then
apt-get -yq update
fi
fi
}
doIf()
{
findInStr "$($1 2>&1)" $2
if [[ 0 -ne $? ]]; then return 0; fi
if [[ ! -z $4 ]]; then $4 | $3; else $3; fi
}
doIfNot()
{
findInStr "$($1 2>&1)" $2
if [[ 0 -eq $? ]]; then return 0; fi
if [[ ! -z $4 ]]; then $4 | $3; else $3; fi
}
isOnline()
{
wget -q --tries=1 --timeout=8 --spider http://google.com
return $?
}
# Download specified file
# @param $1 - File name
# @param $2 - URL Link
# @returns Path to tool
downloadTool()
{
# Tool path
if [ ! -d "$TOOLPATH" ]; then
mkdir -p "$TOOLPATH"
exitOnError "Failed to create path : $TOOLPATH"
fi
# Final tool path
TOOLDNL="${TOOLPATH}/${1}"
# Download the tool if we don't have it
if [ ! -f $TOOLDNL ]; then
echo "Downloading $1..."
echo
curl -L $2 -o $TOOLDNL
exitOnError "CURL failed to download $1"
if [ ! -f $TOOLDNL ]; then
exitWithError "Failed to download $1"
fi
fi
}
# @param $1 - Tool file name
# @param $2 - grep pattern to find exec in zip
# @param $3 - URL to zip file
downloadToolCompressed()
{
TOOLEXEC="${TOOLPATH}/$1"
# Already exists?
if [ -f "${TOOLEXEC}" ]; then return 0; fi
TOOLEXT="${3##*.}"
# Remove existing
TMPZIP="${TOOLPATH}/tmp.${TOOLEXT}"
if [ -f "$TMPZIP" ]; then
rm "$TMPZIP"
fi
# Download the archive
downloadTool "tmp.${TOOLEXT}" $3
if [ ! -f "$TMPZIP" ]; then
exitWithError "Failed to download tool $1"
fi
# Lose old path
TMPZIPPATH="${TOOLPATH}/tmp-${TOOLEXT}"
if [ -d "$TMPZIPPATH" ]; then
rm -Rf "$TMPZIPPATH"
fi
# Create path to extract files
mkdir -p "$TMPZIPPATH"
exitOnError "Failed to create path : $TMPZIPPATH"
case ${TOOLEXT,,} in
"zip")
unzip "$TMPZIP" -d "$TMPZIPPATH"
;;
*)
exitWithError "Unknown archive type : ${TOOLEXT,,}"
;;
esac
# Find the file
TOOLFIND=$(find $TMPZIPPATH | grep -E $2 | head -1)
if [ -z $TOOLFIND ] || [ ! -f $TOOLFIND ]; then
exitWithError "Failed to find in archive : $2 -> $1"
fi
# Copy the exe we found
mv "$TOOLFIND" "$TOOLEXEC"
# Cleanup
rm "$TMPZIP"
rm -Rf "$TMPZIPPATH"
}
# Download compression tools
initCompress()
{
JAVA=$(which java)
if [ -z "$JAVA" ] || [ ! -f "$JAVA" ]; then
exitOnError "Java not installed"
fi
# Closure compiler (old)
# downloadToolCompressed "cc.jar" "closure-compiler" "https://dl.google.com/closure-compiler/compiler-latest.zip"
# CCEXEC=$TOOLEXEC
# Closure compiler
downloadTool "cc.jar" "https://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v20201102/closure-compiler-v20201102.jar"
CCEXEC=$TOOLDNL
# HTML Minifier
MINEXEC=$(which html-minifier)
if [ -z "$MINEXEC" ] || [ ! -f "$MINEXEC" ]; then
npm install html-minifier -g
exitOnError "Failed to install html-minifier"
fi
MINEXEC=$(which html-minifier)
if [ -z "$MINEXEC" ] || [ ! -f "$MINEXEC" ]; then
exitOnError "Failed to install html-minifier"
fi
downloadTool "yui.jar" "https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar"
YUIEXEC=$TOOLDNL
if [ -z "$TOOLDNL" ] || [ ! -f "$TOOLDNL" ]; then
exitOnError "Failed to install html-minifier"
fi
echo
echo "--------------------------------------------------------------------"
echo "- JAVA : $JAVA"
echo "- CLOSURE COMPILER : $CCEXEC"
echo "- HTML MINIFIER : $MINEXEC"
echo "- CSS MINIFIER : $YUIEXEC"
echo "--------------------------------------------------------------------"
echo
}
# Compresses a web site
# @param $1 - Input directory
# @param $2 - Output directory
compressWeb()
{
showInfo "- Compressing : $1"
if [[ $1 =~ ".." ]]; then
exitOnError "Invalid source directory : $1"
fi
if [ ! -d "$1" ]; then
exitOnError "Directory doesn't exist : $1"
fi
if [[ $2 =~ ".." ]]; then
exitOnError "Invalid destination directory : $2"
fi
if [ ! -d "$2" ]; then
mkdir -p $2
exitOnError "Failed to create directory : $2"
fi
FILES=$1/*
for SRC in $FILES
do
FNAME=`basename $SRC`
TGT=$2/$FNAME
if [ -d $SRC ]; then
compressWeb $SRC $TGT
# Is the directory empty?
elif [[ $SRC =~ "*" ]]; then
echo "Empty directory : $SRC -> $TGT"
# Is it already minimized?
elif [[ $SRC =~ ".min." ]]; then
echo "Copy Minimized File : $SRC -> $TGT"
cp "$SRC" "$TGT"
exitOnError "(d) Failed to copy $SRC -> $TGT"
# Process this file
else
EXT="${FNAME##*.}"
echo "$EXT : $SRC -> $TGT"
case ${EXT,,} in
"js")
$JAVA -jar $CCEXEC --warning_level quiet --js_output_file "$TGT" --js "$SRC"
if [[ 0 -ne $? ]]; then
echo "!!! Failed to build $SRC"
cp "$SRC" "$TGT"
exitOnError "(js) Failed to copy $SRC -> $TGT"
fi
;;
"css")
$JAVA -jar $YUIEXEC -o "$TGT" "$SRC"
if [[ 0 -ne $? ]]; then
echo "!!! Failed to build $SRC"
cp "$SRC" "$TGT"
exitOnError "(css) Failed to copy $SRC -> $TGT"
fi
;;
"htm" | "html")
$MINEXEC --collapse-whitespace -o "$TGT" "$SRC"
if [[ 0 -ne $? ]]; then
echo "!!! Failed to build $SRC"
cp "$SRC" "$TGT"
exitOnError "(html) Failed to copy $SRC -> $TGT"
fi
;;
*)
cp "$SRC" "$TGT"
exitOnError "(*) Failed to copy $SRC -> $TGT"
;;
esac
fi
done
return 1
}
#--------------------------------------------------------------------------------------------------
# Los Geht's!
echo
echo "----------------------------------------------------------"
echo "- Source : $SRCDIR"
echo "- Destination : $DSTDIR"
echo "----------------------------------------------------------"
echo
#--------------------------------------------------------------------------------------------------
showInfo "Compressing: $SRCDIR -> $DSTDIR"
if [ ! -d $SRCDIR ]; then
exitWithError "Source directory not found : $SRCDIR"
fi
# Initialize
initCompress
# Compress the web folder
compressWeb "$SRCDIR" "$DSTDIR"
if [ ! -d "$DSTDIR" ]; then
exitWithError "Failed to compress $SRCDIR -> $DSTDIR"
fi
#--------------------------------------------------------------------------------------------------
showInfo "Success!"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment