#DELPEUT Augustin, j 'ai integrer et tester toute les fonctions demander sur le sujet #!/bin/dash #fonction help qui va etre afficher si l'option --help est presente help(){ echo "Minifies HTML and CSS files with : DIR_SOURCE path to the root directory of the website to be minified DIR_DEST path to the root directory of the minified website OPTIONS --help show help and exit -v displays the list of minified files ; and for each file, its final and initial sizes, and its reduction percentage -f if the DIR_DEST file exists, its content is removed without asking for confirmation of deletion -t TAGS_FILE the \"white space\" characters preceding and following the tags (opening or closing) listed in the ’TAGS_FILE’ are deleted" } HELP="try --help for more information to use correctly minifier.sh" #fonction qui supprime les commentaire puis les saut de ligne puis les espace repeter puis les balises dans le tags_file si l option -t est presente dans les fichier htm minification_html(){ sed -E -i -- 's///g' "$1" tr "\n" " " < $1 > $TEMP sed -E -i -- 's/[[:blank:]]+/ /g' "$TEMP" cat $TEMP > $1 if test "$T" -eq 1; then for K in $(awk '{print $0}' < $TAGS_FILE); do sed -E -i -- 's/\s*<'$K'>\s*/<'$K'>/g' "$1" sed -E -i -- 's/\s*<\/'$K'>\s*/<\/'$K'>/g' "$1" done fi } #fonction qui supprime les commentaire puis les saut de ligne puis les espace repeter dans les fichier css minification_css(){ tr "\n" " " < $1 > $TEMP sed -E -i -- 's/\/\*([^*]|(\*+([^*\/])))*\*+\///g' "$TEMP" sed -E -i -- 's/[[:blank:]]+/ /g' "$TEMP" cat $TEMP > $1 } #calcule le % pour l option -v percent(){ X=$(( ($1*100) /$2)) NX=$((100-X)) echo "$3 file: $4 $1/$2: $NX %" } #minifie de maniere recusrcive le dossier dans dir_dest minification(){ for I in echo $(ls $1); do if test -d "$1/$I"; then minification "$1/$I" elif [ "${I##*.}" = "css" ]; then if test "$V" -eq 1; then PREVL=$(stat -c "%s" -- "$1/$I") minification_css "$1/$I" ACTL=$(stat -c "%s" -- "$1/$I") percent $ACTL $PREVL "CSS" "$1/$I" else minification_css "$1/$I" fi elif [ "${I##*.}" = "html" ]; then PREVL=$(stat -c "%s" -- "$1/$I") if test "$V" -eq 1; then PREVL=$(stat -c "%s" -- "$1/$I") minification_html "$1/$I" ACTL=$(stat -c "%s" -- "$1/$I") percent $ACTL $PREVL "HTML" "$1/$I" else minification_html "$1/$I" fi fi done } #test sur l option --help if test $# = 0;then echo "Error, no argument" return 1 exit fi if test $# -eq 1 && test $1 = "--help"; then help exit fi V=0 T=0 F=0 DIR_SOURCE="" DIR_DEST="" CURR="" TAGS_FILE="" TEMP="/tmp/minitmp" touch $TEMP #test la validité des options for I in $(seq 1 $#); do NEXTF=$(eval echo "\"\$$((I+1))\"") ACTF=$(eval echo "\"\$$I\"") if test $(eval echo "\"\$$I\"") = "--help";then echo "The option --help must be the only argument" return 1 exit fi case $(eval echo "\"\$$I\"") in "-v" ) if test $V -eq 1; then echo "Error: The argument -v is present too many time, "$HELP return 1 exit fi V=$(($V+1)) if test ! "$DIR_SOURCE" = ""; then echo "Error: -v argument figure after DIR_SOURCE, "$HELP return 1 exit fi ;; "-f") if test $F -eq 1; then echo "Error: The argument -f is present too many time, "$HELP return 1 exit fi F=$(($F+1)) if test ! "$DIR_SOURCE" = ""; then echo "Error: -f argument figure after DIR_SOURCE, "$HELP return 1 exit fi ;; "-t") if test $T -eq 1; then echo "Error: The argument -t is present too many time, "$HELP return 1 exit fi T=$(($T+1)) if [ $(file $NEXTF 2>/dev/null | grep "$NEXTF: ASCII text" 2>/dev/null | wc -l) -ne 1 ]; then echo "TAG file isn't valid, "$HELP return 1 exit fi if [ "${AFPARA##*.}" = "css" ]; then echo "TAG file isn't valid, "$HELP return 1 exit fi TAGS_FILE=$NEXTF if test ! "$DIR_SOURCE" = ""; then echo "Error: -t argument figure after DIR_SOURCE, "$HELP return 1 exit fi ;; * ) if test -d $ACTF ; then if test "$DIR_SOURCE" = ""; then DIR_SOURCE=$ACTF elif test ! "$DIR_SOURCE" = "" && test "$DIR_DEST" = ""; then DIR_DEST=$ACTF elif test ! "$DIR_SOURCE" = "" && test ! "$DIR_DEST" = ""; then echo "Error: too many directory than my creator, the ultimate god of devlopper excepted, "$HELP return 1 exit fi elif test ! "$DIR_SOURCE" = "" && test "$DIR_DEST" = ""; then DIR_DEST=$ACTF elif test ! $TAGS_FILE = $ACTF; then echo "The $(eval echo "\"\$$I\"") option is not supported,$HELP" return 1 exit fi ;; esac done if test "$DIR_SOURCE" = "$DIR_DEST"; then echo "Error: DIR_SOURCE and DIR_DEST must be different "$HELP return 1 exit fi if test "$DIR_DEST" = ""; then echo "Error: Are you kidding me? no destination? really? "$HELP return 1 exit elif test "$DIR_SOURCE" = ""; then echo "Error: No source, "$HELP return 1 exit fi #gere la copie dans dir_dest et l'option -f if test -d "$DIR_DEST" && test "$F" -ne 1; then echo "Are you sure u want to delete the destination directory [y/n]" while test ! "$CURR" = "y" && test ! "$CURR" = "n"; do read CURR case $CURR in "y") rm -r -- "$DIR_DEST" cp -r -- "$DIR_SOURCE" "$DIR_DEST" ;; "n") echo "minification cancelled" return 1 exit ;; * ) echo "I want an y or a n please " ;; esac done elif test -d "$DIR_DEST" && test "$F" -eq 1; then rm -r -- "$DIR_DEST" cp -r -- "$DIR_SOURCE" "$DIR_DEST" else cp -r -- "$DIR_SOURCE" "$DIR_DEST" fi #minification finale minification $DIR_DEST rm "$TEMP" echo " minification completed" return 0