ruby_scribe 0.1.1 → 0.1.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.
- data/Gemfile +3 -0
- data/Gemfile.lock +34 -0
- data/README.rdoc +8 -15
- data/lib/ruby_scribe.rb +1 -1
- data/lib/ruby_scribe/emitter.rb +5 -2
- data/lib/ruby_scribe/runner.rb +12 -7
- data/lib/ruby_scribe/sexp_helpers.rb +2 -0
- data/lib/ruby_scribe/version.rb +2 -2
- metadata +67 -77
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ruby_scribe (0.1.1)
|
5
|
+
activesupport (~> 3.0.10)
|
6
|
+
i18n (~> 0.6.0)
|
7
|
+
ruby_parser (~> 2.3.1)
|
8
|
+
thor (~> 0.13)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: http://rubygems.org/
|
12
|
+
specs:
|
13
|
+
activesupport (3.0.10)
|
14
|
+
diff-lcs (1.1.3)
|
15
|
+
i18n (0.6.0)
|
16
|
+
rspec (2.6.0)
|
17
|
+
rspec-core (~> 2.6.0)
|
18
|
+
rspec-expectations (~> 2.6.0)
|
19
|
+
rspec-mocks (~> 2.6.0)
|
20
|
+
rspec-core (2.6.4)
|
21
|
+
rspec-expectations (2.6.0)
|
22
|
+
diff-lcs (~> 1.1.2)
|
23
|
+
rspec-mocks (2.6.0)
|
24
|
+
ruby_parser (2.3.1)
|
25
|
+
sexp_processor (~> 3.0)
|
26
|
+
sexp_processor (3.0.7)
|
27
|
+
thor (0.14.6)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
rspec (~> 2.0)
|
34
|
+
ruby_scribe!
|
data/README.rdoc
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
= Ruby Scribe
|
2
2
|
|
3
|
+
Generates nicely-formatted ruby source code given a ruby abstract syntax tree (from seattlerb's ruby_parser).
|
4
|
+
|
3
5
|
== Introduction
|
4
6
|
|
5
|
-
|
7
|
+
Ruby Scribe attempts to intelligently format code (from an AST) much as a real developer would, through a series of configurable option.
|
8
|
+
|
9
|
+
== Approach
|
6
10
|
|
7
|
-
|
11
|
+
To approach creating a solid pretty-printer, I used the Rails codebase as "ideal" coding style for the emitter with default parameters. As I continued to refine ruby_scribe,
|
12
|
+
I ran it (with no AST transformations) against every ruby file in the Rails codebase. The smaller the remaining git diff after each iteration, the better the emitter was.
|
13
|
+
There are even some instances I think ruby_scribe emits more consistent code than what currently exists in the Rails codebase.
|
8
14
|
|
9
15
|
== Example
|
10
16
|
|
@@ -14,13 +20,6 @@ Imagine this crappily-formatted Ruby code:
|
|
14
20
|
# My Comment
|
15
21
|
class Sample < Base;
|
16
22
|
def method; do_something_here; end
|
17
|
-
|
18
|
-
if(new_record?) then
|
19
|
-
puts "Yes"
|
20
|
-
else
|
21
|
-
puts 'No'
|
22
|
-
end
|
23
|
-
end
|
24
23
|
end
|
25
24
|
|
26
25
|
Parse that with RubyParser:
|
@@ -39,12 +38,6 @@ And out pops this, nice and clean:
|
|
39
38
|
def method
|
40
39
|
do_something_here
|
41
40
|
end
|
42
|
-
|
43
|
-
if new_record?
|
44
|
-
puts "Yes"
|
45
|
-
else
|
46
|
-
puts "No"
|
47
|
-
end
|
48
41
|
end
|
49
42
|
end
|
50
43
|
|
data/lib/ruby_scribe.rb
CHANGED
data/lib/ruby_scribe/emitter.rb
CHANGED
@@ -10,6 +10,7 @@ module RubyScribe
|
|
10
10
|
return "" unless e
|
11
11
|
return e if e.is_a?(String)
|
12
12
|
return e.to_s if e.is_a?(Symbol)
|
13
|
+
return e unless e.respond_to?(:kind)
|
13
14
|
|
14
15
|
case e.kind
|
15
16
|
when :block
|
@@ -86,7 +87,7 @@ module RubyScribe
|
|
86
87
|
return "" if e.body.first == s(:nil)
|
87
88
|
|
88
89
|
# Special case for handling rescue blocks around entire methods (excluding the indent):
|
89
|
-
return emit_method_rescue(e.body.first) if e.body.first.rescue? && e.body.size == 1
|
90
|
+
return emit_method_rescue(e.body.first) if e.body.first.try(:rescue?) && e.body.size == 1
|
90
91
|
|
91
92
|
e.body.map do |child|
|
92
93
|
emit_block_member_prefix(e.body, child) +
|
@@ -414,6 +415,8 @@ module RubyScribe
|
|
414
415
|
end
|
415
416
|
|
416
417
|
def emit_assignments_as_arguments(e)
|
418
|
+
return nil unless e && e.respond_to?(:kind) && e.body.first
|
419
|
+
|
417
420
|
if e.kind == :masgn
|
418
421
|
e.body.first.body.map {|c| emit_assignments_as_arguments(c) }.join(", ")
|
419
422
|
elsif e.kind == :lasgn
|
@@ -518,4 +521,4 @@ module RubyScribe
|
|
518
521
|
nl("## RubyScribe-UNKNOWN: #{e.kind} ##")
|
519
522
|
end
|
520
523
|
end
|
521
|
-
end
|
524
|
+
end
|
data/lib/ruby_scribe/runner.rb
CHANGED
@@ -12,12 +12,17 @@ module RubyScribe
|
|
12
12
|
desc :convert, "Takes a single file or multiple files, parses them, then replaces the original file(s) with the scribed version."
|
13
13
|
def convert(*paths)
|
14
14
|
expand_paths(paths).each do |path|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
begin
|
16
|
+
sexp = RubyParser.new.parse(File.read(path))
|
17
|
+
output = RubyScribe::Emitter.new.emit(sexp)
|
18
|
+
|
19
|
+
File.open(path, "w") do |file|
|
20
|
+
file.write(output)
|
21
|
+
file.flush
|
22
|
+
end
|
23
|
+
rescue Racc::ParseError, SyntaxError => e
|
24
|
+
$stderr.puts "! Failed to parse #{path}:"
|
25
|
+
$stderr.puts e.message
|
21
26
|
end
|
22
27
|
end
|
23
28
|
end
|
@@ -26,7 +31,7 @@ module RubyScribe
|
|
26
31
|
|
27
32
|
def expand_paths(paths = [])
|
28
33
|
paths.map do |path|
|
29
|
-
[path] + Dir[path
|
34
|
+
[path] + Dir[File.join(path,"**/*.rb")]
|
30
35
|
end.flatten.uniq.reject {|f| File.directory?(f) }
|
31
36
|
end
|
32
37
|
end
|
data/lib/ruby_scribe/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module RubyScribe
|
2
|
-
VERSION = "0.1.
|
3
|
-
end
|
2
|
+
VERSION = "0.1.2"
|
3
|
+
end
|
metadata
CHANGED
@@ -1,78 +1,79 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_scribe
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 1
|
10
|
-
version: 0.1.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Ben Hughes
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
12
|
+
date: 2011-09-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: thor
|
16
|
+
requirement: &70221043076100 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.13'
|
22
|
+
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
|
24
|
+
version_requirements: *70221043076100
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &70221043075480 !ruby/object:Gem::Requirement
|
25
28
|
none: false
|
26
|
-
requirements:
|
29
|
+
requirements:
|
27
30
|
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 2
|
32
|
-
- 0
|
33
|
-
- 4
|
34
|
-
version: 2.0.4
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.10
|
35
33
|
type: :runtime
|
36
|
-
version_requirements: *id001
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
|
-
name: thor
|
39
34
|
prerelease: false
|
40
|
-
|
35
|
+
version_requirements: *70221043075480
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: i18n
|
38
|
+
requirement: &70221043074880 !ruby/object:Gem::Requirement
|
41
39
|
none: false
|
42
|
-
requirements:
|
40
|
+
requirements:
|
43
41
|
- - ~>
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
|
46
|
-
segments:
|
47
|
-
- 0
|
48
|
-
- 13
|
49
|
-
version: "0.13"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.6.0
|
50
44
|
type: :runtime
|
51
|
-
version_requirements: *id002
|
52
|
-
- !ruby/object:Gem::Dependency
|
53
|
-
name: rspec
|
54
45
|
prerelease: false
|
55
|
-
|
46
|
+
version_requirements: *70221043074880
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: ruby_parser
|
49
|
+
requirement: &70221043074260 !ruby/object:Gem::Requirement
|
56
50
|
none: false
|
57
|
-
requirements:
|
51
|
+
requirements:
|
58
52
|
- - ~>
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.3.1
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70221043074260
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rspec
|
60
|
+
requirement: &70221043073680 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '2.0'
|
65
66
|
type: :development
|
66
|
-
|
67
|
-
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70221043073680
|
69
|
+
description: A ruby formatting tool that takes S-expression as input and intelligently
|
70
|
+
outputs formatted Ruby code.
|
68
71
|
email: ben@railsgarden.com
|
69
|
-
executables:
|
72
|
+
executables:
|
70
73
|
- rubyscribe
|
71
74
|
extensions: []
|
72
|
-
|
73
75
|
extra_rdoc_files: []
|
74
|
-
|
75
|
-
files:
|
76
|
+
files:
|
76
77
|
- lib/ruby_scribe/emitter.rb
|
77
78
|
- lib/ruby_scribe/emitter_config.rb
|
78
79
|
- lib/ruby_scribe/emitter_helpers.rb
|
@@ -90,45 +91,34 @@ files:
|
|
90
91
|
- spec/ruby_scribe/emitter_spec.rb
|
91
92
|
- spec/ruby_scribe/sexp_helpers_spec.rb
|
92
93
|
- spec/spec_helper.rb
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
93
96
|
- LICENSE
|
94
97
|
- Rakefile
|
95
98
|
- README.rdoc
|
96
99
|
- bin/rubyscribe
|
97
|
-
has_rdoc: true
|
98
100
|
homepage: http://github.com/rubiety/ruby_scribe
|
99
101
|
licenses: []
|
100
|
-
|
101
102
|
post_install_message:
|
102
103
|
rdoc_options: []
|
103
|
-
|
104
|
-
require_paths:
|
104
|
+
require_paths:
|
105
105
|
- lib
|
106
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
107
|
none: false
|
108
|
-
requirements:
|
109
|
-
- -
|
110
|
-
- !ruby/object:Gem::Version
|
111
|
-
|
112
|
-
|
113
|
-
- 0
|
114
|
-
version: "0"
|
115
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
113
|
none: false
|
117
|
-
requirements:
|
118
|
-
- -
|
119
|
-
- !ruby/object:Gem::Version
|
120
|
-
hash: 19
|
121
|
-
segments:
|
122
|
-
- 1
|
123
|
-
- 3
|
124
|
-
- 4
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
125
117
|
version: 1.3.4
|
126
118
|
requirements: []
|
127
|
-
|
128
119
|
rubyforge_project: ruby_scribe
|
129
|
-
rubygems_version: 1.
|
120
|
+
rubygems_version: 1.8.10
|
130
121
|
signing_key:
|
131
122
|
specification_version: 3
|
132
123
|
summary: Generates formatted ruby code from S-expressions (like from ruby_parser).
|
133
124
|
test_files: []
|
134
|
-
|