Skip to content

Instantly share code, notes, and snippets.

@homuler
Last active January 17, 2020 09:57
Show Gist options
  • Save homuler/0a42e01c423270431277d7f0aa7a9277 to your computer and use it in GitHub Desktop.
Save homuler/0a42e01c423270431277d7f0aa7a9277 to your computer and use it in GitHub Desktop.
Shell script to migrate from FontAwesome v4 to v5
#!/bin/bash
# fontawesome-4-to-5.csv
# https://gist.github.com/homuler/e4c1e381cdab254d78b253ec9bc84870
#
# Usage:
# ./fontawesome-4-to-5.sh COMMAND TARGET_DIR [--csv-path=] [...GREP_OPTIONS]
DIR=$(dirname "$PWD/$0")
COMMAND=$1
TARGET_DIR=$2
CSV_PATH='fontawesome-4-to-5.csv'
GREP_OPTIONS=''
print_info () {
echo -e "\033[0;32m[INFO] $1\033[0m"
}
print_warning () {
echo -e "\033[0;33m[Warning] $1\033[0m"
}
print_error () {
echo -e "\033[0;31m[ERROR] $1\033[0m"
}
show_manual() {
print_info "./fontawesome-4-to-5.sh COMMAND TARGET_DIR [--csv-path=] [...GREP_OPTIONS]"
}
if [[ -z "${COMMAND}" ]]; then
print_error "Command is not specified"
show_manual
exit 1
fi
while true; do
case "$3" in
--csv-path=*)
CSV_PATH="${3#*=}"
shift
;;
"")
shift
break
;;
*)
GREP_OPTIONS="${GREP_OPTIONS} $3"
shift
;;
esac
done
grep_fa() {
local header
local row
while read line; do
local result
local count
if [[ -z "$header" ]]; then
readarray -d , -t header <<< "$line"
continue
fi
readarray -d , -t row <<< $line
result=$(grep -E "(fa-${row[0]}$)|(fa-${row[0]}[^-])" --color=always -r ${TARGET_DIR} ${GREP_OPTIONS})
if [[ ! -z "${result}" ]]; then
count=$(echo "${result}" | wc -l)
print_warning "Some files may include old icon names (${row[0]}): COUNT=${count}"
print_info "Migration Guide: fa fa-${row[0]} -> ${row[2]} fa-${row[1]}"
printf "${result}\n\n"
fi
result=$(grep "${row[3]::-1}" --color=always -r ${TARGET_DIR} ${GREP_OPTIONS})
if [[ ! -z "${result}" ]]; then
count=$(echo "${result}" | wc -l)
print_warning "Some files include a Unicode value that may have changed (${row[3]::-1}): COUNT=${count}"
printf "${result}\n\n"
fi
done < $CSV_PATH
}
sed_fa() {
local header
local row
while read line; do
local result
local count
if [[ -z "$header" ]]; then
readarray -d , -t header <<< "$line"
continue
fi
readarray -d , -t row <<< $line
find ${TARGET_DIR} -type f -print0 ${COMMAN_OPTIONS} | xargs -0 sed -i -E "s/(fa fa-${row[0]}$)|(fa fa-${row[0]}([^-]))/${row[2]} fa-${row[1]}\3/g"
done < $CSV_PATH
}
case "${COMMAND}" in
grep)
grep_fa
exit 0
;;
sed)
sed_fa
exit 0
;;
*)
print_error "${COMMAND}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment