code-cleaner 0.4 → 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/code-cleaner +4 -3
- data/code-cleaner-0.4.gem +0 -0
- data/code-cleaner.gemspec +1 -1
- data/support/pre-commit.erb +13 -5
- data/tasks.rb +6 -0
- data/tasks/code-cleaner.nake +16 -0
- data/tasks/code-cleaner.thor +4 -1
- metadata +3 -2
data/bin/code-cleaner
CHANGED
@@ -127,13 +127,14 @@ rescue Errno::EAGAIN, EOFError
|
|
127
127
|
# The trick with line.length == ARGF.pos is simple, IO#pos returns position
|
128
128
|
# of last character which was readed by gets or similar method, so if
|
129
129
|
# the position is same as is the line, then it has to be the first line.
|
130
|
-
if line.length == ARGF.pos && encoding && ! line.match(
|
130
|
+
if line.length == ARGF.pos && encoding && ! line.match(/^#(.*coding|\!)/) # the \! should means shebang. TODO: if there is a shebang on the first line, check if encoding declaration is on the second line
|
131
131
|
puts "# encoding: #{encoding}"
|
132
132
|
end
|
133
133
|
# You might be wondering WTF is going on, I'm editing files, so I have to use File.open, do the modification on the original content and then save it. The point is we used -i switch which is good for in place editing
|
134
134
|
# flag that we actually changed the file
|
135
|
-
|
136
|
-
|
135
|
+
normalized_line = line.gsub(/\t/, " ").rstrip + "\n"
|
136
|
+
status = 0 if line != normalized_line
|
137
|
+
print normalized_line
|
137
138
|
end
|
138
139
|
end
|
139
140
|
end
|
Binary file
|
data/code-cleaner.gemspec
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "code-cleaner"
|
6
|
-
s.version = "0.
|
6
|
+
s.version = "0.5"
|
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"
|
data/support/pre-commit.erb
CHANGED
@@ -6,6 +6,10 @@ echo "Entering pre-commit hook ..."
|
|
6
6
|
# the following line if you have code-cleaner installed locally
|
7
7
|
<% if options[:path].nil? %>#<% end %>export PATH="<%= options[:path] || "script" %>:$PATH"
|
8
8
|
|
9
|
+
# Setup whitelisting & blacklisting
|
10
|
+
<% if options[:whitelist].nil? %>#<% end %>export WHITELIST='<%= options[:whitelist] %>'
|
11
|
+
<% if options[:blacklist].nil? %>#<% end %>export BLACKLIST='<%= options[:blacklist] %>'
|
12
|
+
|
9
13
|
# If you haven't done any commit yet,
|
10
14
|
# git diff will fail with following message:
|
11
15
|
# fatal: No HEAD commit to compare with (yet)
|
@@ -28,15 +32,19 @@ if which code-cleaner &> /dev/null; then
|
|
28
32
|
if test -f "$file" ; then # ignore files which was removed by git rm
|
29
33
|
if code-cleaner "$file" --try-apply-rules &> /dev/null ; then
|
30
34
|
printf "Normalizing \"\e[36m$file\e[0m\" "
|
31
|
-
code-cleaner "$file" --apply-rules
|
35
|
+
code-cleaner "$file" --apply-rules <%= "--encoding=#{options[:encoding]} " if options[:encoding] %>&> /dev/null
|
36
|
+
exitstatus=$?
|
32
37
|
# NOTE: we have to check the exit status directly,
|
33
38
|
# so we can be sure that the command just wasn't found
|
34
|
-
if [
|
39
|
+
if [ $exitstatus -eq 10 ]; then # code-cleaner exits with 0 if some changes were made
|
35
40
|
printf "[\e[32mCLEAN\e[0m]\n"
|
36
|
-
elif [
|
41
|
+
elif [ $exitstatus -eq 1 ]; then # something goes wrong
|
37
42
|
printf "[\e[31mERROR\e[0m]\n"
|
38
|
-
code-cleaner "$file" --apply-rules
|
39
|
-
|
43
|
+
echo "code-cleaner \"$file\" --apply-rules <%= "--encoding=#{options[:encoding]} " if options[:encoding] %>"
|
44
|
+
code-cleaner "$file" --apply-rules <%= "--encoding=#{options[:encoding]} " if options[:encoding] %>
|
45
|
+
echo "Exited with status $?"
|
46
|
+
exit 1
|
47
|
+
elif [ $exitstatus -eq 0 ] ; then
|
40
48
|
printf "[\e[33mDONE\e[0m]\n"
|
41
49
|
git add "$file" # so the changes will be committed immediately
|
42
50
|
else
|
data/tasks.rb
CHANGED
@@ -5,3 +5,9 @@ begin
|
|
5
5
|
rescue LoadError
|
6
6
|
abort "If you want to contribute to code-cleaner, you have to run ./tasks.rb hooks:whitespace:install first!"
|
7
7
|
end
|
8
|
+
|
9
|
+
Nake::Task["hooks:whitespace:install"].tap do |task|
|
10
|
+
task.config[:path] = "bin"
|
11
|
+
task.config[:encoding] = "utf-8"
|
12
|
+
task.config[:whitelist] = '(bin/[^/]+|.+\.(rb|rake|nake|thor|task))$'
|
13
|
+
end
|
data/tasks/code-cleaner.nake
CHANGED
@@ -7,7 +7,11 @@ require "nake/task"
|
|
7
7
|
# nake hooks:whitespace:install
|
8
8
|
# nake hooks:whitespace:install --force
|
9
9
|
# nake hooks:whitespace:install --path=script
|
10
|
+
# nake hooks:whitespace:install --encoding=utf-8
|
11
|
+
# nake hooks:whitespace:install --whitelist=pattern
|
12
|
+
# nake hooks:whitespace:install --blacklist=pattern
|
10
13
|
Nake::Task.new("hooks:whitespace:install") do |task|
|
14
|
+
task.config.declare(:path, :encoding, :whitelist, :blacklist)
|
11
15
|
task.description = "Install hook for automatically removing trailing whitespace"
|
12
16
|
task.define do |options = Hash.new|
|
13
17
|
# --force
|
@@ -15,6 +19,18 @@ Nake::Task.new("hooks:whitespace:install") do |task|
|
|
15
19
|
FileUtils.rm ".git/hooks/pre-commit"
|
16
20
|
end
|
17
21
|
|
22
|
+
# --path=script
|
23
|
+
options[:path] = task.config[:path] unless options[:path]
|
24
|
+
|
25
|
+
# --encoding=utf-8
|
26
|
+
options[:encoding] = task.config[:encoding] unless options[:encoding]
|
27
|
+
|
28
|
+
# --whitelist=pattern
|
29
|
+
options[:whitelist] = task.config[:whitelist] unless options[:whitelist]
|
30
|
+
|
31
|
+
# --blacklist=pattern
|
32
|
+
options[:blacklist] = task.config[:blacklist] unless options[:blacklist]
|
33
|
+
|
18
34
|
if File.exist?(".git/hooks/pre-commit")
|
19
35
|
abort "You must remove .git/hooks/pre-commit first!"
|
20
36
|
else
|
data/tasks/code-cleaner.thor
CHANGED
@@ -5,9 +5,12 @@ require "fileutils"
|
|
5
5
|
# thor hooks:whitespace:install
|
6
6
|
# thor hooks:whitespace:install --force
|
7
7
|
# thor hooks:whitespace:install --path=script
|
8
|
+
# nake hooks:whitespace:install --encoding=utf-8
|
9
|
+
# nake hooks:whitespace:install --whitelist=pattern
|
10
|
+
# nake hooks:whitespace:install --blacklist=pattern
|
8
11
|
module Hooks
|
9
12
|
class Whitespace < Thor
|
10
|
-
method_options :force => :boolean, :path => :string
|
13
|
+
method_options :force => :boolean, :path => :string, :encoding => :string, :whitelist => :string, :blacklist => :string
|
11
14
|
desc "install", "Install hook for automatically removing trailing whitespace"
|
12
15
|
def install
|
13
16
|
# --force
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code-cleaner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.5"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain:
|
11
|
-
date: 2009-12-
|
11
|
+
date: 2009-12-29 00:00:00 +01:00
|
12
12
|
default_executable: code-cleaner
|
13
13
|
dependencies: []
|
14
14
|
|
@@ -22,6 +22,7 @@ extra_rdoc_files: []
|
|
22
22
|
|
23
23
|
files:
|
24
24
|
- bin/code-cleaner
|
25
|
+
- code-cleaner-0.4.gem
|
25
26
|
- code-cleaner.gemspec
|
26
27
|
- LICENSE
|
27
28
|
- README.textile
|