Skip to content

Commit

Permalink
scalafmt bootstraping (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasseGuillaume authored and densh committed Nov 25, 2016
1 parent 8220daa commit 8ab0a15
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 125 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ _build/
*.ppm
.idea
_build/
bin/.coursier
bin/.scalafmt
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ before_script:
fi;
script:
- java -version
- bin/scalafmt --version 0.4.7 --test
- bin/scalafmt --test
- sbt 'cleanCache' 'cleanLocal' 'nscplugin/publishLocal' 'nativelib/publishLocal' 'publishLocal' 'sandbox/run' 'demoNative/run' 'tests/run' 'tools/test' 'benchmarks/run' 'scripted' 'publishSnapshot'

132 changes: 9 additions & 123 deletions bin/scalafmt
Original file line number Diff line number Diff line change
@@ -1,131 +1,17 @@
#!/usr/bin/env bash

check_binary(){
command -v "$0" >/dev/null 2>&1 || { echo >&2 "Missing dependency $0, exiting."; exit 1; }
}
# set -x

# I'm not sure if this would have portability issues...
check_version(){
if [[ $1 = $2 ]]; then
echo $1;
else
echo -e "$1\n$2" | sort -nr | head -1
fi
}
HERE="`dirname $0`"

CURL=$(which curl)
TAR=$(which tar)
JAVA=$(which java)
GREP=$(which grep)

check_binary "$CURL"
check_binary "$TAR"
check_binary "$JAVA"
check_binary "$GREP"

check_latest () {
echo "Checking latest version from github..."
latest_version=$($CURL -u dummy4dummy:dummy2dummy -s https://api.github.com/repos/olafurpg/scalafmt/releases/latest | $GREP -Eo '"tag_name":(.*)' | $GREP -Eo 'v[0-9\.]+')
if [[ $latest_version = "" ]]; then
echo "The github call failed, either because of an internet connection issue or rate limits..."
exit 1
fi
}

current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
version_file="$HOME/.scalafmt-bin/version"
upgrade=false

if [[ -e $version_file ]]; then
latest_version=$(cat $version_file)
scalafmt_version=$latest_version
fi

print_usage (){
echo -e "scalafmt runner verson: 0.1.1"
echo -e "Usage: ./scalafmt [RUNNER OPTS] [SCALAFMT OPTS]"
echo -e "--version [VERSION] \t Version to invoke for scalafmt (fmt: 0.4.2, not v0.4.2)"
echo -e "--dir [DIR] \t\t Directory to store scalafmt jars, default is $HOME/.scalafmt-bin/"
echo -e "--upgrade \t\t Check GitHub for the latest version of scalafmt and use that"
echo -e "--h \t\t\t Display this message, this must be the first arguement for this to be invoked"
echo
echo -e "This script only checks the first three args for it's options, anything beyond that is ignored"
echo -e "and will probably cause scalafmt to produce an error since these are not options in scalafmt's runtime"
exit 0;
}

# We check all 3 now because it's possible to pass upgrade in and then
# pass a custom directory in. You can't pass all the args in, you have to choose
# specifying a version or upgrading to the latest from github.
if [[ "$1" = "--h" ]]; then
print_usage
fi
for arg in "$1" "$2" "$3"; do
case "$arg" in
--version)
if [[ $upgrade = true || $3 = "--upgrade" ]]; then
echo "You can't specify a custom version with --upgrade"
exit 1
fi
if [[ $2 =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
scalafmt_version="v$2"
echo "Using version: $scalafmt_version"
shift 2
else
echo "You must pass a valid version with --v"
exit 1
fi
;;
--dir)
echo "Using dir: $2"
default_dir="$2"
shift 2
;;
--upgrade)
if [[ $2 = "--version" ]]; then
echo "You can't specify a custom version with --upgrade"
exit 1
fi
upgrade=true
check_latest
scalafmt_version=$latest_version
shift
;;
*)
;;
esac
done

if [[ ! -e $version_file && $upgrade = false && -z $scalafmt_version ]]; then
echo "No version file, checking for latest..."
check_latest
echo "Setting latest version to: $latest_version"
scalafmt_version=$latest_version
fi

if [[ ! "$default_dir" ]]; then
default_dir="$HOME/.scalafmt-bin/releases/$scalafmt_version"
fi

if [[ ! -d "$default_dir" ]]; then
echo "$default_dir does not exist, creating it..."
mkdir -p "$default_dir"
if [ ! -f $HERE/.coursier ]; then
curl -L -o $HERE/.coursier https://git.io/vgvpD
chmod +x $HERE/.coursier
fi

echo $(check_version "$scalafmt_version" "$latest_version") > $version_file

scala_version="2.11"
scalafmt_filename="scalafmt-$scalafmt_version.jar"
scalafmt_repo="https://github.com/olafurpg/scalafmt/releases/download/$scalafmt_version/scalafmt.tar.gz"

set -e

if [[ ! -e "$default_dir/scalafmt-$scalafmt_version.jar" ]]; then
echo "No scalafmt jar for version $scalafmt_version, downloading it..."
echo "From: $scalafmt_repo"
$CURL --progress -L "$scalafmt_repo" -o "$default_dir/scalafmt.tar.gz"
$TAR -xzf "$default_dir/scalafmt.tar.gz" -O "cli/target/scala-$scala_version/scalafmt.jar" > "$default_dir/$scalafmt_filename"
rm "$default_dir/scalafmt.tar.gz"
if [ ! -f $HERE/.scalafmt ]; then
$HERE/.coursier bootstrap com.geirsson:scalafmt-cli_2.11:0.4.7 --main org.scalafmt.cli.Cli -o $HERE/.scalafmt
chmod +x $HERE/.scalafmt
fi

$JAVA -jar "$default_dir/$scalafmt_filename" "$@"
$HERE/.scalafmt "$@"
2 changes: 1 addition & 1 deletion docs/contrib/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Compatible license source code:
Coding style
------------

Scala Native is formatted via `scalafmt`_. Make sure that all of your
Scala Native is formatted via `bin/scalafmt`_. Make sure that all of your
contributions are properly formatted before suggesting any changes.

General workflow
Expand Down

0 comments on commit 8ab0a15

Please sign in to comment.