diff options
author | Paul Phillips <paulp@improving.org> | 2011-08-03 20:53:20 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-08-03 20:53:20 +0000 |
commit | 5aa8100a48b8b6c74d3bffed7430eaeb4ee98753 (patch) | |
tree | d64ebc7cc35b1201e75f41065c2adfeba93ced76 /tools/flag-usages.sh | |
parent | a1a870a72c74ec9cc5b9f449ba39292c78467e3b (diff) | |
download | scala-5aa8100a48b8b6c74d3bffed7430eaeb4ee98753.tar.gz scala-5aa8100a48b8b6c74d3bffed7430eaeb4ee98753.tar.bz2 scala-5aa8100a48b8b6c74d3bffed7430eaeb4ee98753.zip |
Added a command line script to ~/tools to count...
Added a command line script to ~/tools to count flag usages. I'm
checking this in less out of the enormous demand for flag counting
scripts than because I wanted to lower the barrier to people writing
reusable bash scripts.
*** DO YOU WANT TO WRITE NICE BASH SCRIPTS? ***
*** Look at tools/flag-usages.sh ***
*** It's easy to understand and full of helpful comments! ***
I'm not making any claims here about having massive bash expertise,
but I know a lot of people resist learning any of it (I was once like
you) so I wanted to lower the barrier a little. Because as a mechanism
for the composition and modification of the world of existing tools,
nothing comes close to the shell. And I know many of us write way too
many one-offs which we delete in shame and horror shortly after their
immediate purpose is served.
No review. (I should say r-e-v-i-e-w by everyone but I'm sure it would
give me a nice pile of crucible errors.)
Diffstat (limited to 'tools/flag-usages.sh')
-rwxr-xr-x | tools/flag-usages.sh | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tools/flag-usages.sh b/tools/flag-usages.sh new file mode 100755 index 0000000000..82696bd1da --- /dev/null +++ b/tools/flag-usages.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# +# find-flag-usages +# Paul Phillips <paulp@typesafe.com> +# +# Looks through the scala source tree for direct references to flag names. + +set -e # Good idea in almost all scripts: causes script to exit on any error. + +# Would be better not to hardcode this. +flags='\b(ABSOVERRIDE|ABSTRACT|ACCESSOR|BRIDGE|BYNAMEPARAM|CAPTURED|CASE|CASEACCESSOR|CONTRAVARIANT|COVARIANT|DEFAULTINIT|DEFAULTPARAM|DEFERRED|EXISTENTIAL|EXPANDEDNAME|FINAL|IMPLCLASS|IMPLICIT|INCONSTRUCTOR|INTERFACE|JAVA|LABEL|LAZY|LIFTED|LOCAL|LOCKED|METHOD|MIXEDIN|MODULE|MODULEVAR|MUTABLE|OVERLOADED|OVERRIDE|PACKAGE|PARAM|PARAMACCESSOR|PRESUPER|PRIVATE|PROTECTED|SEALED|SPECIALIZED|STABLE|STATIC|SUPERACCESSOR|SYNTHETIC|TRAIT|TRIEDCOOKING|VARARGS|VBRIDGE)\b' + +# $() runs a command in a subshell. This is calculating the root of the +# repository by looking relative to the location of the script. +rootdir=$(cd $(dirname $0) ; pwd)/.. + +# A bash function. Can be used like a command. +usage () { + # A here string. Allows for blocks of text without lots of quoting. + # Variable interpolation still takes place, e.g. $(basename $0). + cat <<EOM +Usage: $(basename $0) [-achs] + -a show all flag usages + -c count flag usages per file + -h show this message + -s count total flag usages +EOM +} + +# Checking for no arguments or missing requirements. +if [[ $# -eq 0 ]]; then + usage + exit 1 +elif [[ ! $(which ack) ]]; then # no ack + echo "Error: cannot find required program ack." + exit 1 +fi + +# Using pushd/popd for directory changes is a way to make moving +# the current directory around somewhat more composable. +pushd "$rootdir" >/dev/null + +# The leading : in :achs suppresses some errors. Each letter is a valid +# option. If an option takes an argument, a colon follows it, e.g. +# it would be :ach:s if -h took an argument. +while getopts :achs opt; do + case $opt in + a) ack "$flags" src ;; + c) ack --files-with-matches -c "$flags" src ;; + h) usage ;; + s) ack --no-filename -o "$flags" src | sort | uniq -c | sort -gr ;; + :) echo "Option -$OPTARG requires an argument." >&2 ;; # this case is called for a missing option argument + *) echo "Unrecognized argument $OPTARG" ;; # this is the catch-all implying an unknown option + esac +done + +# This removes all the options from $@, as getopts doesn't touch it. +# After this, "$@" contains the non-option arguments. +shift $((OPTIND-1)) + +# In this program we don't expect any. +if [[ $# -ne 0 ]]; then + echo "This program does not take arguments." +fi + +popd >/dev/null +exit 0 |