diff --git a/tools/README.md b/tools/README.md new file mode 100644 index 0000000000..060d83a892 --- /dev/null +++ b/tools/README.md @@ -0,0 +1,22 @@ +# Tools + +* `run_on_all.sh` — optimize and fix all SVG files in the repository +* `run_on_dirs.sh DIRECTORY...` — optimize and fix SVG files in the directory +* `run_on_files.sh FILE...` — optimize and fix the SVG files +* `_fix_color_scheme.sh FILE...` — remove a color property from `style="color:#5c616c;fill:currentColor;opacity:1"` if class `ColorScheme-*` exists +* `svgo.yml` — [SVGO](https://github.com/svg/svgo) configuraion + + +## Useful snippets + +Optimize and fix SVG files that are changed but not committed + +``` +git diff --name-only | xargs ./tools/run_on_files.sh +``` + +Optimize and fix SVG files that are committed in 043906b + +``` +git show --name-only 043906b | xargs ./tools/run_on_files.sh +``` diff --git a/tools/_fix_color_scheme.sh b/tools/_fix_color_scheme.sh new file mode 100755 index 0000000000..884adf3602 --- /dev/null +++ b/tools/_fix_color_scheme.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# +# This script deletes a color property from style attribute +# if a class ColorScheme-* exist, and deletes the style +# attribute if is empty. +# +# limitations: +# - works only with single-line elements +# +# usage: +# _fix_color_scheme.sh FILE... + +set -e + +for file in "$@"; do + [ -f "$file" ] || continue + + sed -r -i \ + -e '/class="ColorScheme-/ s/color:#([0-9a-zA-Z]{3}|[0-9a-zA-Z]{6});?//g' \ + -e 's/\s?style=""//g' "$file" +done diff --git a/tools/run_on_all.sh b/tools/run_on_all.sh new file mode 100755 index 0000000000..91649d8834 --- /dev/null +++ b/tools/run_on_all.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# +# usage: +# ./run_on_all.sh + +GIT_ROOT=$(git rev-parse --show-toplevel) + +# continue if is directory +[ -d "$GIT_ROOT" ] || exit 1 + +echo "=> Directory '$GIT_ROOT'." >&2 +echo " Press to abort (wait 5 seconds) ..." >&2 + +sleep 5 + +# get all svg files w/o symlinks +FILES=$(find "$GIT_ROOT" -type f -iname '*.svg' -print) + +for file in $FILES; do + echo "=> Workon '$file' ..." >&2 + + # optimize + svgo --config="$GIT_ROOT/tools/svgo.yml" -i "$file" + + # fix + eval "$GIT_ROOT/tools/_fix_color_scheme.sh" "$file" +done diff --git a/tools/run_on_dirs.sh b/tools/run_on_dirs.sh new file mode 100755 index 0000000000..01d7dc62df --- /dev/null +++ b/tools/run_on_dirs.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# +# usage: +# ./run_on_dirs.sh DIR... + +GIT_ROOT=$(git rev-parse --show-toplevel) + +for dir in "$@"; do + # continue if arg is directory + [ -d "$dir" ] || continue + + echo "=> Directory '$dir'." >&2 + echo " Press to abort (wait 5 seconds) ..." >&2 + + sleep 5 + + # get all svg files w/o symlinks + FILES=$(find "$dir" -type f -iname '*.svg' -print) + + for file in $FILES; do + echo "=> Workon '$file' ..." >&2 + + # optimize + svgo --config="$GIT_ROOT/tools/svgo.yml" -i "$file" + + # fix + eval "$GIT_ROOT/tools/_fix_color_scheme.sh" "$file" + done +done diff --git a/tools/run_on_files.sh b/tools/run_on_files.sh new file mode 100755 index 0000000000..612c3ff3e1 --- /dev/null +++ b/tools/run_on_files.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# +# usage: +# ./run_on_files.sh FILE... + +GIT_ROOT=$(git rev-parse --show-toplevel) + +for file in "$@"; do + # continue if exist + [ -e "$file" ] || continue + + # continue if not symlink + [ ! -L "$file" ] || continue + + # continue if extension = svg + [ "${file##*.}" == "svg" ] || continue + + echo "=> Workon '$file' ..." >&2 + + # optimize + svgo --config="$GIT_ROOT/tools/svgo.yml" -i "$file" + + # fix + eval "$GIT_ROOT/tools/_fix_color_scheme.sh" "$file" +done diff --git a/tools/3-svgo.sh b/tools/svgo.yml similarity index 94% rename from tools/3-svgo.sh rename to tools/svgo.yml index 98ab3fee55..3361dd9f02 100644 --- a/tools/3-svgo.sh +++ b/tools/svgo.yml @@ -5,16 +5,16 @@ # for d in Papirus/16x16/* Papirus/22x22/* Papirus/24x24/* \ # Papirus/32x32/* Papirus/48x48/* Papirus/64x64/* # do -# svgo --config=.svgo.yml -f "$d" +# svgo --config=svgo.yml -f "$d" # done # # - or - # -# svgo --config=.svgo.yml -f Papirus/16x16/actions +# svgo --config=svgo.yml -f Papirus/16x16/actions # # - or - # -# svgo --config=.svgo.yml -i FILE +# svgo --config=svgo.yml -i FILE full: true multipass: true