code-cleaner 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -59,6 +59,16 @@ h2. Pre-commit hook
59
59
 
60
60
  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":http://github.com/botanicus/nake (@code-cleaner.nake@) and "Thor":http://github.com/wycats/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.
61
61
 
62
- 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.
62
+ 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.
63
63
 
64
- 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@.
64
+ Here's an example how you can integrate code-cleaner to your tasks:
65
+
66
+ <pre>
67
+ begin
68
+ load "code-cleaner.nake"
69
+ rescue LoadError
70
+ warn "If you want to contribute to nake, you have to install code-cleaner gem and then run ./tasks.rb hooks:
71
+ end
72
+ </pre>
73
+
74
+ 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.
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "code-cleaner"
6
- s.version = "0.2"
6
+ s.version = "0.3"
7
7
  s.authors = ["Jakub Šťastný aka Botanicus"]
8
8
  s.homepage = "http://github.com/botanicus/code-cleaner"
9
9
  s.summary = "Remove trailing whitespace, append missing \\n and replace tabs by two spaces"
@@ -0,0 +1,52 @@
1
+ #!/bin/sh
2
+
3
+ echo "Entering pre-commit hook ..."
4
+
5
+ # The code-cleaner script has to be in your $PATH. Uncomment
6
+ # the following line if you have code-cleaner installed locally
7
+ <% if options[:path].nil? %>#<% end %>export PATH="<%= options[:path] || "script" %>:$PATH"
8
+
9
+ # If you haven't done any commit yet,
10
+ # git diff will fail with following message:
11
+ # fatal: No HEAD commit to compare with (yet)
12
+
13
+ # TODO: how to get list of files which will be
14
+ # committed if there isn't any commit in the index yet?
15
+ # For now we just use normalize everything in current
16
+ # directory, but it isn't very good solution.
17
+ files=$(git diff --staged --name-only 2> /dev/null || echo ".")
18
+
19
+ function abort() {
20
+ printf "[\e[31mERROR\e[0m] $*\n"
21
+ exit 1
22
+ }
23
+
24
+ # NOTE: we are using printf rather than echo because printf should be
25
+ # more portable, each echo implementation has quite different behaviour
26
+ if which code-cleaner &> /dev/null; then
27
+ for file in $files; do
28
+ if test -f "$file" ; then # ignore files which was removed by git rm
29
+ if code-cleaner "$file" --try-apply-rules &> /dev/null ; then
30
+ printf "Normalizing \"\e[36m$file\e[0m\" "
31
+ code-cleaner "$file" --apply-rules &> /dev/null
32
+ # NOTE: we have to check the exit status directly,
33
+ # so we can be sure that the command just wasn't found
34
+ if [ $? -eq 10 ]; then # code-cleaner exits with 0 if some changes were made
35
+ printf "[\e[32mCLEAN\e[0m]\n"
36
+ elif [ $? -eq 1 ]; then # something goes wrong
37
+ printf "[\e[31mERROR\e[0m]\n"
38
+ code-cleaner "$file" --apply-rules
39
+ elif [ $? -eq 0 ] ; then
40
+ printf "[\e[33mDONE\e[0m]\n"
41
+ git add "$file" # so the changes will be committed immediately
42
+ else
43
+ abort "Unexpected exit status $?"
44
+ fi
45
+ else
46
+ printf "Skipping \"\e[36m$file\e[0m\"\n"
47
+ fi
48
+ fi
49
+ done
50
+ else
51
+ abort "You have to have code-cleaner installed and the code-cleaner script has to be in your \$PATH ($PATH)"
52
+ fi
@@ -1,18 +1,26 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require "erb"
3
4
  require "fileutils"
4
5
  require "nake/task"
5
6
 
7
+ # nake hooks:whitespace:install
8
+ # nake hooks:whitespace:install --force
9
+ # nake hooks:whitespace:install --path=script
6
10
  Nake::Task.new("hooks:whitespace:install") do |task|
7
11
  task.description = "Install hook for automatically removing trailing whitespace"
8
- task.define do
12
+ task.define do |options = Hash.new|
13
+ FileUtils.rm ".git/hooks/pre-commit" if options[:force]
9
14
  if File.exist?(".git/hooks/pre-commit")
10
15
  abort "You must remove .git/hooks/pre-commit first!"
11
16
  else
12
17
  begin
13
18
  puts "Installing .git/hooks/pre-commit ..."
14
- source = File.join(File.dirname(__FILE__), "..", "support", "pre-commit")
15
- FileUtils.install source, ".git/hooks/pre-commit", :mode => 0755
19
+ source = File.join(File.dirname(__FILE__), "..", "support", "pre-commit.erb")
20
+ File.open(".git/hooks/pre-commit", "w") do |file|
21
+ file.puts(ERB.new(File.read(source)).result(binding))
22
+ end
23
+ File.chmod(0755, ".git/hooks/pre-commit")
16
24
  rescue Errno::ENOENT
17
25
  abort "You have to run git init first!"
18
26
  end
@@ -1,5 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
+ # The task for Rake doesn't support any options. Sorry, your task utility sucks, get something better.
4
+ # The reason why I can't add --path option is that Rake has very nasty bug http://gist.github.com/263325
5
+ # Yeah, I know I can use environment varibles, but why I should bother when you can just install nake which is so much better!
3
6
  desc "Install hook for automatically removing trailing whitespace"
4
7
  task "hooks:whitespace:install" do
5
8
  if File.exist?(".git/hooks/pre-commit")
@@ -7,8 +10,11 @@ task "hooks:whitespace:install" do
7
10
  else
8
11
  begin
9
12
  puts "Installing .git/hooks/pre-commit ..."
10
- source = File.join(File.dirname(__FILE__), "..", "support", "pre-commit")
11
- install source, ".git/hooks/pre-commit", :mode => 0755
13
+ source = File.join(File.dirname(__FILE__), "..", "support", "pre-commit.erb")
14
+ File.open(".git/hooks/pre-commit", "w") do |file|
15
+ file.puts(ERB.new(File.read(source)).result)
16
+ end
17
+ File.chmod(0755, ".git/hooks/pre-commit")
12
18
  rescue Errno::ENOENT
13
19
  abort "You have to run git init first!"
14
20
  end
@@ -2,16 +2,24 @@
2
2
 
3
3
  require "fileutils"
4
4
 
5
+ # thor hooks:whitespace:install
6
+ # thor hooks:whitespace:install --force
7
+ # thor hooks:whitespace:install --path=script
5
8
  module Hooks
6
9
  class Whitespace < Thor
10
+ method_options :force => :boolean, :path => :string
7
11
  desc "install", "Install hook for automatically removing trailing whitespace"
8
12
  def install
13
+ FileUtils.rm ".git/hooks/pre-commit" if options[:force]
9
14
  if File.exist?(".git/hooks/pre-commit")
10
15
  abort "You must remove .git/hooks/pre-commit first!"
11
16
  else
12
17
  puts "Installing .git/hooks/pre-commit ..."
13
- source = File.join(File.dirname(__FILE__), "..", "support", "pre-commit")
14
- FileUtils.install source, ".git/hooks/pre-commit", :mode => 0755
18
+ source = File.join(File.dirname(__FILE__), "..", "support", "pre-commit.erb")
19
+ File.open(".git/hooks/pre-commit", "w") do |file|
20
+ file.puts(ERB.new(File.read(source)).result(binding))
21
+ end
22
+ File.chmod(0755, ".git/hooks/pre-commit")
15
23
  end
16
24
  rescue Errno::ENOENT
17
25
  abort "You have to run git init first!"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
@@ -22,11 +22,10 @@ extra_rdoc_files: []
22
22
 
23
23
  files:
24
24
  - bin/code-cleaner
25
- - code-cleaner-0.1.gem
26
25
  - code-cleaner.gemspec
27
26
  - LICENSE
28
27
  - README.textile
29
- - support/pre-commit
28
+ - support/pre-commit.erb
30
29
  - tasks/code-cleaner.nake
31
30
  - tasks/code-cleaner.rake
32
31
  - tasks/code-cleaner.thor
Binary file
@@ -1,46 +0,0 @@
1
- #!/bin/sh
2
-
3
- echo "Entering pre-commit hook ..."
4
-
5
- # If you haven't done any commit yet,
6
- # git diff will fail with following message:
7
- # fatal: No HEAD commit to compare with (yet)
8
-
9
- # TODO: how to get list of files which will be
10
- # committed if there isn't any commit in the index yet?
11
- # For now we just use normalize everything in current
12
- # directory, but it isn't very good solution.
13
- files=$(git diff --staged --name-only 2> /dev/null || echo ".")
14
-
15
- function abort() {
16
- printf "[\e[31mERROR\e[0m] $*\n"
17
- exit 1
18
- }
19
-
20
- # NOTE: we are using printf rather than echo because printf should be
21
- # more portable, each echo implementation has quite different behaviour
22
- if which code-cleaner &> /dev/null; then
23
- for file in $files; do
24
- if code-cleaner "$file" --try-apply-rules &> /dev/null ; then
25
- printf "Normalizing \"\e[36m$file\e[0m\" "
26
- code-cleaner "$file" --apply-rules &> /dev/null
27
- # NOTE: we have to check the exit status directly,
28
- # so we can be sure that the command just wasn't found
29
- if [ $? -eq 10 ]; then # code-cleaner exits with 0 if some changes were made
30
- printf "[\e[32mCLEAN\e[0m]\n"
31
- elif [ $? -eq 1 ]; then # something goes wrong
32
- printf "[\e[31mERROR\e[0m]\n"
33
- code-cleaner "$file" --apply-rules
34
- elif [ $? -eq 0 ] ; then
35
- printf "[\e[33mDONE\e[0m]\n"
36
- git add "$file" # so the changes will be committed immediately
37
- else
38
- abort "Unexpected exit status $?"
39
- fi
40
- else
41
- printf "Skipping \"\e[36m$file\e[0m\"\n"
42
- fi
43
- done
44
- else
45
- abort "You have to install code-cleaner first!"
46
- fi