tailor 0.0.2

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.
Files changed (37) hide show
  1. data/History.txt +19 -0
  2. data/Manifest.txt +36 -0
  3. data/PostInstall.txt +2 -0
  4. data/README.rdoc +62 -0
  5. data/Rakefile +78 -0
  6. data/bin/tailor +18 -0
  7. data/features/case_checking.feature +38 -0
  8. data/features/development.feature +13 -0
  9. data/features/spacing.feature +40 -0
  10. data/features/step_definitions/case_checking_steps.rb +42 -0
  11. data/features/step_definitions/common_steps.rb +175 -0
  12. data/features/step_definitions/spacing_steps.rb +93 -0
  13. data/features/support/1_file_with_camel_case_class/camel_case_class.rb +5 -0
  14. data/features/support/1_file_with_camel_case_method/camel_case_method.rb +3 -0
  15. data/features/support/1_file_with_hard_tabs/hard_tab.rb +3 -0
  16. data/features/support/1_file_with_long_lines/long_lines.rb +5 -0
  17. data/features/support/1_file_with_snake_case_class/snake_case_class.rb +5 -0
  18. data/features/support/1_file_with_snake_case_method/snake_case_method.rb +3 -0
  19. data/features/support/1_file_with_trailing_whitespace/trailing_whitespace.rb +5 -0
  20. data/features/support/1_good_simple_file/my_project.rb +7 -0
  21. data/features/support/common.rb +29 -0
  22. data/features/support/env.rb +15 -0
  23. data/features/support/matchers.rb +11 -0
  24. data/features/support/world.rb +53 -0
  25. data/lib/tailor.rb +143 -0
  26. data/lib/tailor/file_line.rb +228 -0
  27. data/lib/tailor/indentation_checker.rb +27 -0
  28. data/ruby-style-checker.rb +136 -0
  29. data/script/console +10 -0
  30. data/script/destroy +14 -0
  31. data/script/generate +14 -0
  32. data/spec/file_line_spec.rb +136 -0
  33. data/spec/spec.opts +1 -0
  34. data/spec/spec_helper.rb +10 -0
  35. data/spec/tailor_spec.rb +27 -0
  36. data/tasks/rspec.rake +21 -0
  37. metadata +218 -0
@@ -0,0 +1,27 @@
1
+ module Tailor
2
+ module IndentationChecker
3
+ def validate_indentation file
4
+ results = Array.new
5
+
6
+ source = File.open(file, 'r')
7
+
8
+ # Start the line number at 1, not 0
9
+ line_number = 1
10
+
11
+ source.each_line do |line_of_code|
12
+ line = FileLine.new(line_of_code)
13
+
14
+ # Make sure the line isn't hard-tabbed
15
+ if line.hard_tabbed?
16
+ results << "Line #{line_number} is hard-tabbed."
17
+ end
18
+
19
+ # Check for indentation
20
+ #spaces = line.indented_spaces
21
+ #current_depth_level = spaces / 2
22
+
23
+ line_number =+ 1
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,136 @@
1
+ $tabSize = 2
2
+ $tabStr = " "
3
+
4
+ # indent regexp tests
5
+
6
+ $indentExp = [
7
+ /^module\b/,
8
+ /(=\s*|^)if\b/,
9
+ /(=\s*|^)until\b/,
10
+ /(=\s*|^)for\b/,
11
+ /(=\s*|^)unless\b/,
12
+ /(=\s*|^)while\b/,
13
+ /(=\s*|^)begin\b/,
14
+ /(=\s*|^)case\b/,
15
+ /\bthen\b/,
16
+ /^class\b/,
17
+ /^rescue\b/,
18
+ /^def\b/,
19
+ /\bdo\b/,
20
+ /^else\b/,
21
+ /^elsif\b/,
22
+ /^ensure\b/,
23
+ /\bwhen\b/,
24
+ /\{[^\}]*$/,
25
+ /\[[^\]]*$/
26
+ ]
27
+
28
+ # outdent regexp tests
29
+
30
+ $outdentExp = [
31
+ /^rescue\b/,
32
+ /^ensure\b/,
33
+ /^elsif\b/,
34
+ /^end\b/,
35
+ /^else\b/,
36
+ /\bwhen\b/,
37
+ /^[^\{]*\}/,
38
+ /^[^\[]*\]/
39
+ ]
40
+
41
+ def makeTab(tab)
42
+ return (tab < 0)?"":$tabStr * $tabSize * tab
43
+ end
44
+
45
+ def addLine(line,tab)
46
+ line.strip!
47
+ line = makeTab(tab)+line if line.length > 0
48
+ return line + "\n"
49
+ end
50
+
51
+ def format_ruby
52
+ commentBlock = false
53
+ multiLineArray = Array.new
54
+ multiLineStr = ""
55
+ tab = 0
56
+ ############
57
+ #filename entered on command line as argument gets passed in Ruby's
58
+ #special argument array ARGV. The ARGV[0] zeroth element has the filename
59
+ ############
60
+ filename = ARGV[0]
61
+ source = File.new(filename,'r').read
62
+ dest = ""
63
+ source.split("\n").each do |line|
64
+ # combine continuing lines
65
+ if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
66
+ puts "hereeeeer"
67
+ #multiLineArray.push line
68
+ #multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
69
+ next
70
+ end
71
+
72
+ # add final line
73
+ if(multiLineStr.length > 0)
74
+ puts "HERLKJHLJKEHR"
75
+ #multiLineArray.push line
76
+ #multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
77
+ end
78
+
79
+ tline = ((multiLineStr.length > 0)?multiLineStr:line).strip
80
+ if(tline =~ /^=begin/)
81
+ commentBlock = true
82
+ end
83
+ if(commentBlock)
84
+ # add the line unchanged
85
+ dest += line + "\n"
86
+ else
87
+ commentLine = (tline =~ /^#/)
88
+ if(!commentLine)
89
+ # throw out sequences that will
90
+ # only sow confusion
91
+ tline.gsub!(/\/.*?\//,"")
92
+ tline.gsub!(/%r\{.*?\}/,"")
93
+ tline.gsub!(/%r(.).*?\1/,"")
94
+ tline.gsub!(/\\\"/,"'")
95
+ tline.gsub!(/".*?"/,"\"\"")
96
+ tline.gsub!(/'.*?'/,"''")
97
+ tline.gsub!(/#\{.*?\}/,"")
98
+ $outdentExp.each do |re|
99
+ if(tline =~ re)
100
+ tab -= 1
101
+ break
102
+ end
103
+ end
104
+ end
105
+ if (multiLineArray.length > 0)
106
+ multiLineArray.each do |ml|
107
+ dest += addLine(ml,tab)
108
+ end
109
+ multiLineArray.clear
110
+ multiLineStr = ""
111
+ else
112
+ dest += addLine(line,tab)
113
+ end
114
+ if(!commentLine)
115
+ $indentExp.each do |re|
116
+ if(tline =~ re && !(tline =~ /\s+end\s*$/))
117
+ tab += 1
118
+ break
119
+ end
120
+ end
121
+ end
122
+ end
123
+ if(tline =~ /^=end/)
124
+ commentBlock = false
125
+ end
126
+ end
127
+ #STDOUT.write(dest)
128
+ #File.open(filename, 'w') {|fw| fw.write(dest)}
129
+ puts "File #{ARGV[0]} has been formatted."
130
+ # uncomment this to complain about mismatched blocks
131
+ if(tab != 0)
132
+ STDERR.puts "Indentation error: #{tab}"
133
+ end
134
+ end
135
+
136
+ format_ruby
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/tailor'}"
9
+ puts "Loading tailor gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,136 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'tailor/file_line'
3
+
4
+ include Tailor
5
+
6
+ describe Tailor::FileLine do
7
+ context "should return the number of leading spaces in a line" do
8
+ it "when the line is not indented" do
9
+ line = FileLine.new "def do_something"
10
+ line.indented_spaces.should == 0
11
+ end
12
+
13
+ it "when the line is indented 1 space" do
14
+ line = FileLine.new " def do_something"
15
+ line.indented_spaces.should == 1
16
+ end
17
+
18
+ it "when the line is indented 1 space and a hard tab" do
19
+ line = FileLine.new " \tdef do_something"
20
+ line.indented_spaces.should == 1
21
+ end
22
+ end
23
+
24
+ context "should check hard tabs" do
25
+ it "when the line is indented 1 hard tab" do
26
+ line = FileLine.new "\tdef do_something"
27
+ line.hard_tabbed?.should be_true
28
+ end
29
+
30
+ it "when the line is indented with a space and 1 hard tab" do
31
+ line = FileLine.new " \tdef do_something"
32
+ line.hard_tabbed?.should be_true
33
+ end
34
+
35
+ it "when the line is indented with a space" do
36
+ line = FileLine.new " def do_something"
37
+ line.hard_tabbed?.should be_false
38
+ end
39
+
40
+ it "when the line is not indented" do
41
+ line = FileLine.new "def do_something"
42
+ line.hard_tabbed?.should be_false
43
+ end
44
+ end
45
+
46
+ context "should check for camel case when" do
47
+ it "is a method and the method name is camel case" do
48
+ line = FileLine.new "def doSomething"
49
+ line.camel_case_method?.should be_true
50
+ end
51
+
52
+ it "is a method and the method name is snake case" do
53
+ line = FileLine.new "def do_something"
54
+ line.camel_case_method?.should be_false
55
+ end
56
+
57
+ it "is a class and the class name is camel case" do
58
+ line = FileLine.new "class AClass"
59
+ line.camel_case_class?.should be_true
60
+ end
61
+
62
+ it "is a class and the class name is snake case" do
63
+ line = FileLine.new "class A_Class"
64
+ line.camel_case_class?.should be_false
65
+ end
66
+ end
67
+
68
+ it "should detect the number of trailing whitespace(s)" do
69
+ line = FileLine.new " puts 'This is a line.' \n"
70
+ line.trailing_whitespace_count.should == 2
71
+ end
72
+
73
+ # TODO: These methods should probably all be called by
74
+ # line.check_comma_spacingor something. As it stands, these tests are
75
+ # going to start to get confusing, plus having one entry point for
76
+ # checking commas probably makes the most sense.
77
+ context "spacing after a comma" do
78
+ it "should detect no space after a comma" do
79
+ line = FileLine.new " def do_something this,that"
80
+ line.no_space_after_comma?.should be_true
81
+ end
82
+
83
+ it "should skip code that has 1 space after a comma" do
84
+ line = FileLine.new " def do_something this, that"
85
+ line.no_space_after_comma?.should be_false
86
+ end
87
+
88
+ it "should detect 2 spaces after a comma" do
89
+ line = FileLine.new " def do_something this, that"
90
+ line.two_or_more_spaces_after_comma?.should be_true
91
+ end
92
+
93
+ it "should skip code that has 1 space after a comma" do
94
+ line = FileLine.new " def do_something this, that"
95
+ line.two_or_more_spaces_after_comma?.should be_false
96
+ end
97
+ end
98
+
99
+ context "comments" do
100
+ it "should detect a regular full line comment" do
101
+ line = FileLine.new " # This is a comment."
102
+ line.line_comment?.should be_true
103
+ end
104
+
105
+ it "should skip code that's not a full line comment" do
106
+ line = FileLine.new " puts 'this is some code.'"
107
+ line.line_comment?.should be_false
108
+ end
109
+ end
110
+
111
+ context "line length" do
112
+ it "should detect greater than 80 characters" do
113
+ string_81_chars = '#' * 81
114
+ line = FileLine.new string_81_chars
115
+ line.too_long?.should be_true
116
+ end
117
+
118
+ it "should detect greater than 80 spaces" do
119
+ string_81_spaces = ' ' * 81
120
+ line = FileLine.new string_81_spaces
121
+ line.too_long?.should be_true
122
+ end
123
+
124
+ it "should be OK with 80 chars" do
125
+ string_80_chars = '#' * 80
126
+ line = FileLine.new string_80_chars
127
+ line.too_long?.should be_false
128
+ end
129
+
130
+ it "should be OK with 80 spaces" do
131
+ string_80_spaces = ' ' * 80
132
+ line = FileLine.new string_80_spaces
133
+ line.too_long?.should be_false
134
+ end
135
+ end
136
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'tailor'
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Kernel do
4
+ def self.get_requires
5
+ Dir.chdir '../lib'
6
+ filenames = Dir.glob 'Tailor/*.rb'
7
+ requires = filenames.each do |fn|
8
+ fn.chomp!(File.extname(fn))
9
+ end
10
+ return requires
11
+ end
12
+
13
+ # Try to require each of the files in Tailor
14
+ get_requires.each do |r|
15
+ it "should require #{r}" do
16
+ # A require returns true if it was required, false if it had already been
17
+ # required, and nil if it couldn't require.
18
+ Kernel.require(r.to_s).should_not be_nil
19
+ end
20
+ end
21
+ end
22
+
23
+ describe Tailor do
24
+ it "should have a VERSION constant" do
25
+ Tailor.const_defined?('VERSION').should == true
26
+ end
27
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,218 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tailor
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Steve Loveless
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-23 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyforge
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 4
31
+ version: 2.0.4
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: hoe-yard
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 1
44
+ - 2
45
+ version: 0.1.2
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ name: yard
62
+ prerelease: false
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ - 5
70
+ - 3
71
+ version: 0.5.3
72
+ type: :development
73
+ version_requirements: *id004
74
+ - !ruby/object:Gem::Dependency
75
+ name: hoe-yard
76
+ prerelease: false
77
+ requirement: &id005 !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ - 1
84
+ - 2
85
+ version: 0.1.2
86
+ type: :development
87
+ version_requirements: *id005
88
+ - !ruby/object:Gem::Dependency
89
+ name: cucumber
90
+ prerelease: false
91
+ requirement: &id006 !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 0
97
+ - 6
98
+ - 3
99
+ version: 0.6.3
100
+ type: :development
101
+ version_requirements: *id006
102
+ - !ruby/object:Gem::Dependency
103
+ name: hoe
104
+ prerelease: false
105
+ requirement: &id007 !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ segments:
110
+ - 2
111
+ - 6
112
+ - 0
113
+ version: 2.6.0
114
+ type: :development
115
+ version_requirements: *id007
116
+ description: |+
117
+ * http://github.com/turboladen/tailor
118
+
119
+ email:
120
+ - steve.loveless@gmail.com
121
+ executables:
122
+ - tailor
123
+ extensions: []
124
+
125
+ extra_rdoc_files:
126
+ - History.txt
127
+ - Manifest.txt
128
+ - PostInstall.txt
129
+ files:
130
+ - History.txt
131
+ - Manifest.txt
132
+ - PostInstall.txt
133
+ - README.rdoc
134
+ - Rakefile
135
+ - bin/tailor
136
+ - features/case_checking.feature
137
+ - features/development.feature
138
+ - features/spacing.feature
139
+ - features/step_definitions/case_checking_steps.rb
140
+ - features/step_definitions/common_steps.rb
141
+ - features/step_definitions/spacing_steps.rb
142
+ - features/support/1_file_with_camel_case_class/camel_case_class.rb
143
+ - features/support/1_file_with_camel_case_method/camel_case_method.rb
144
+ - features/support/1_file_with_hard_tabs/hard_tab.rb
145
+ - features/support/1_file_with_long_lines/long_lines.rb
146
+ - features/support/1_file_with_snake_case_class/snake_case_class.rb
147
+ - features/support/1_file_with_snake_case_method/snake_case_method.rb
148
+ - features/support/1_file_with_trailing_whitespace/trailing_whitespace.rb
149
+ - features/support/1_good_simple_file/my_project.rb
150
+ - features/support/common.rb
151
+ - features/support/env.rb
152
+ - features/support/matchers.rb
153
+ - features/support/world.rb
154
+ - lib/tailor.rb
155
+ - lib/tailor/file_line.rb
156
+ - lib/tailor/indentation_checker.rb
157
+ - ruby-style-checker.rb
158
+ - script/console
159
+ - script/destroy
160
+ - script/generate
161
+ - spec/file_line_spec.rb
162
+ - spec/spec.opts
163
+ - spec/spec_helper.rb
164
+ - spec/tailor_spec.rb
165
+ - tasks/rspec.rake
166
+ has_rdoc: yard
167
+ homepage: http://github.com/turboladen/tailor
168
+ licenses: []
169
+
170
+ post_install_message:
171
+ - |
172
+
173
+
174
+ - |
175
+ For more information on tailor, see http://github.com/turboladen/tailor
176
+
177
+ rdoc_options:
178
+ - --main
179
+ - README.rdoc
180
+ - --output-dir
181
+ - doc
182
+ - --private
183
+ - --protected
184
+ - --verbose
185
+ - --files
186
+ - - Manifest.txt
187
+ - History.txt
188
+ - --title
189
+ - tailor Documentation (0.0.2)
190
+ - --markup
191
+ - :rdoc
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ segments:
199
+ - 0
200
+ version: "0"
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ segments:
206
+ - 0
207
+ version: "0"
208
+ requirements: []
209
+
210
+ rubyforge_project: tailor
211
+ rubygems_version: 1.3.6
212
+ signing_key:
213
+ specification_version: 3
214
+ summary: Utility for checking style of Ruby files.
215
+ test_files:
216
+ - spec/file_line_spec.rb
217
+ - spec/spec_helper.rb
218
+ - spec/tailor_spec.rb