About
Remove trailing whitespace, append missing \n and replace tabs by two spaces.
Usage
# edit script.rb and all Ruby scripts in lib directory code-cleaner script.rb lib # normalize standard input and print output to standard output cat script.rb | code-cleaner # add encoding declaration if missing code-cleaner --encoding=utf-8
Blacklisting & Whitelisting
Of course you don’t want to remove spaces from other files like PDF documents, images etc. You don’t even want to remove spaces from all Ruby scripts, you probably want to ignore vendored files and locally installed gems. And this is where blacklisting and whitelisting comes handy.
Default behaviour is to ignore everything from vendor and gems directories and from other directories just files ending with .rb, .rake, .task and .thor are normalized.
You can change this behaviour by setting environment variables BLACKLIST
and WHITELIST
. What you pass to these variables will be treated as a regular expression. The default behaviour respond to BLACKLIST='/(vendor|gems)/'
and WHITELIST='(^bin/[^/]+|Rakefile|Thorfile|Gemfile$)|(^.+\.(rb|rake|nake|thor|task|gemspec)$)'
. Here’s an example how you can use it:
WHITELIST=''^(Rakefile|Thorfile|Gemfile|.+\.(rb|rake|nake|thor|task|gemspec))$'' BLACKLIST='/(vendor|gems)/' code-cleaner .
Note that these rules aren’t applied for explicit arguments, so if you run code-cleaner README.textile
, your readme actually will be normalized. It’s because blacklisting and whitelisting rules are applied just on directories, so if you run code-cleaner .
, your readme will stay untouched. Because of this reason code-cleaner .
and code-cleaner *
will be different.
If you want to force blacklisting resp. whitelisting even for explicitly specified files, use --apply-rules
switch. If you want just try it, use --try-apply-rules
which will tell you which files will be skipped and exit without any editing. If some files left in ARGV, the exit status will be 0, otherwise it will be 1.
In your Ruby projects, you might add all files in your bin
and script
directories into whitelist: WHITELIST='(^bin/[^/]+|Rakefile|Thorfile|Gemfile$)|(^.+\.(rb|rake|nake|thor|task|gemspec)$)'
Exit statuses
If code-cleaner does any changes, the exit status will be 0, otherwise it will be 10. It means you can use it in commands like:
if code-cleaner "$file" --apply-rules; then # code-cleaner exits with 0 if some changes were made git add "$file" # so the changes will be committed immediately fi
Or, better and more safe way is to directly check exit status of last command via $?
as is used in the default pre-commit
hook:
code-cleaner . if [ $? -eq 10 ]; then echo Any modifications were made else echo File was changed fi
Pre-commit hook
The best way how you can ensure your sources are clean is to use pre-commit hook, if your SCM provides this function. Plus, if you are using git, just use load "code-cleaner.rake"
in your Rakefile and then run rake hooks:whitespace:install
. There are also alternatives for Nake (code-cleaner.nake
) and Thor (code-cleaner.thor
). Second way is to use rake --rakefile /path/to/code-cleaner/tasks/code-cleaner.rake hooks:whitespace:install
resp. nake /path/to/code-cleaner/tasks/code-cleaner.nake hooks:whitespace:install
directly.
It might force you to remove your .git/hooks/pre-commit
if this file exist. If you haven’t done any editation in this file, it’s safe to do so and if you done some, then you will need to merge your changes manually. You can do it manually, or, if you are using Nake or Thor, just use --force
option.
Here’s an example how you can integrate code-cleaner to your tasks:
begin load "code-cleaner.nake" rescue LoadError warn "If you want to contribute to nake, you have to install code-cleaner gem and then run ./tasks.rb hooks:whitespace:install" end
Also, if you project run in bundled environment, make sure you have your bin
or script
directory in your $PATH
or change the pre-commit hook to point to the right path to the code-cleaner
. If you are using Nake or Thor, just specify --path=bin
resp. --path=script
as an option for the hooks:whitespace:install
If you are using Rake, you will have to do it manually.
—encoding=utf-8
—whitelist=pattern & —blacklist=pattern
Nake::Task["hooks:whitespace:install"].tap do |task| task.config[:path] = "bin" task.config[:encoding] = "utf-8" task.config[:whitelist] = '(^bin/[^/]+|Rakefile|Thorfile|Gemfile$)|(^.+\.(rb|rake|nake|thor|task|gemspec)$)' #task.config[:blacklist] end