Skip to content

Instantly share code, notes, and snippets.

@JamieHouston
Created December 5, 2016 21:52
Show Gist options
  • Save JamieHouston/5b9129dd92b503aaa287961837856bc3 to your computer and use it in GitHub Desktop.
Save JamieHouston/5b9129dd92b503aaa287961837856bc3 to your computer and use it in GitHub Desktop.
Make all spaces and line endings the same
#! /usr/bin/env bash
# This script formats sources in Djinni with standard whitespace conventions.
# Specifically, tabs are converted to spaces (2 for Scala, 4 for other languages),
# and trailing whitespace is removed.
# This script is idempotent, so can be safely run on existing code without making changes.
set -eu
# Locate the script file. Cross symlinks if necessary.
loc="$0"
while [ -h "$loc" ]; do
ls=`ls -ld "$loc"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
loc="$link" # Absolute link
else
loc="`dirname "$loc"`/$link" # Relative link
fi
done
djinni_dir=$(cd "`dirname "$loc"`" && cd .. && pwd)
# Format source types with 4-space indentation
cd "$djinni_dir" && find . '(' -name '*.[chm]' -or -name '*.[ch]pp' -or -name '*.mm' -or -name '*.java' -or -name '*.py' ')' -and -not -path './deps/*' -exec sh -c 'expand -t 4 "{}" | sed "s/ *$//" | tr -d "\r" > _tmp_ && mv _tmp_ "{}"' \;
# Format source types with 2-space indentatino
cd "$djinni_dir" && find . '(' -name '*.scala' ')' -and -not -path './deps/*' -exec sh -c 'expand -t 2 "{}" | sed "s/ *$//" | tr -d "\r" > _tmp_ && mv _tmp_ "{}"' \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment