Add some tools and README

This commit is contained in:
Sergei Eremenko 2016-12-05 23:33:56 +02:00
parent 043906b0ed
commit f21d8997ac
6 changed files with 127 additions and 3 deletions

22
tools/README.md Normal file
View file

@ -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
```

21
tools/_fix_color_scheme.sh Executable file
View file

@ -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

27
tools/run_on_all.sh Executable file
View file

@ -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 <CTRL-C> 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

29
tools/run_on_dirs.sh Executable file
View file

@ -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 <CTRL-C> 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

25
tools/run_on_files.sh Executable file
View file

@ -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

View file

@ -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