ruumba 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 720efab8198b60cdffe7d365d875120915545701
4
- data.tar.gz: d110e7fff04bce288f9d1fa3888515d73f41355d
3
+ metadata.gz: 1780f255bb4860a2e5ec396d04a9f2f4e1f78292
4
+ data.tar.gz: 67cd8d13e193589e2457cd430781e13dea848b23
5
5
  SHA512:
6
- metadata.gz: 07e940ab956ebaaf848c1e87b729963b2356d8cdd5d153fd5cb3c84da061b3e95a102a8cb12030a9be5b7f8420a81d692eea97a05e141cad3c4d9a4cdd6619eb
7
- data.tar.gz: 81bdfa3737493fa3ece728be44f6f7895794132ea5f27a0b008e041134b58f711b4994c31c95592d38d0fd08e0cae78abbb481c86f2d7cf719d8007fab1184de
6
+ metadata.gz: 8ae430ea86b94aa6c4aedcdcbcd9fd1c4d49f81920240ff6bf4e391b70ea678585cb26166b821eda0067140e8acc808bccee33bfa396066dd10fd2a6a818d574
7
+ data.tar.gz: 33dbdaef53dfb16676740b451cd176df4c330218ebb2d4ce1dfa9550580c8180ce2ba277d7a57cbb4da0bd440165c35f38aabf0680ed787ec7aea9d6d96c1f0f
data/bin/ruumba CHANGED
@@ -22,15 +22,17 @@ opts_parser = OptionParser.new do |opts|
22
22
  end
23
23
 
24
24
  opts.on('-c', '--config [CONFIG]', 'Use this config for rubocop') do |c|
25
- options[:arguments] << "--config #{File.expand_path(c)}"
25
+ options[:arguments] << '--config'
26
+ options[:arguments] << File.expand_path(c)
26
27
  end
27
28
 
28
29
  opts.on('-D', '--display-cop-names', 'Display cop names') do ||
29
- options[:arguments] << "--display-cop-names"
30
+ options[:arguments] << '--display-cop-names'
30
31
  end
31
32
 
32
33
  opts.on('-F', '--fail-level [LEVEL]', 'Rubocop fail level') do |l|
33
- options[:arguments] << "--fail-level #{l}"
34
+ options[:arguments] << '--fail-level'
35
+ options[:arguments] << l
34
36
  end
35
37
 
36
38
  opts.on('-e', '--disable-ruby-ext', 'Disable auto-append of .rb extension') do
@@ -38,26 +40,31 @@ opts_parser = OptionParser.new do |opts|
38
40
  end
39
41
 
40
42
  opts.on('--only [COP1,COP2,...]', 'Run only the given cop(s).') do |cops|
41
- options[:arguments] << "--only #{cops}"
43
+ options[:arguments] << '--only'
44
+ options[:arguments] << cops
42
45
  end
43
46
 
44
47
  opts.on('-r', '--require [FILE]', 'Require Ruby file.') do |file|
45
- options[:arguments] << "--require #{file}"
48
+ options[:arguments] << '--require'
49
+ options[:arguments] << file
46
50
  end
47
51
 
48
52
  opts.on('-o', '--out [FILE]', 'Write output to a file instead of STDOUT.') do |file|
49
- options[:arguments] << "--out #{file}"
53
+ options[:arguments] << '--out'
54
+ options[:arguments] << file
50
55
  end
51
56
 
52
57
  opts.on('-f', '--format [FORMAT]', 'Choose an output formatter.') do |format|
53
- options[:arguments] << "--format #{format}"
58
+ options[:arguments] << '--format'
59
+ options[:arguments] << format
54
60
  end
55
61
  end
56
62
 
57
- begin opts_parser.parse!
58
- rescue OptionParser::InvalidOption => e
63
+ begin
64
+ opts_parser.parse!
65
+ rescue OptionParser::InvalidOption => e
59
66
  abort "An error occurred: #{e}. Run ruumba -h for help."
60
67
  end
61
68
 
62
69
  analyzer = Ruumba::Analyzer.new(options)
63
- analyzer.run
70
+ exit(analyzer.run)
@@ -3,6 +3,8 @@
3
3
  require 'securerandom'
4
4
  require 'pathname'
5
5
  require 'tmpdir'
6
+ require 'open3'
7
+ require 'English'
6
8
 
7
9
  # Ruumba: RuboCop's sidekick.
8
10
  module Ruumba
@@ -32,13 +34,58 @@ module Ruumba
32
34
  # @param [String] filename The filename.
33
35
  # @return [String] The extracted Ruby code.
34
36
  def extract(filename)
35
- File.read(filename).scan(ERB_REGEX).map(&:last)
36
- .reject { |line| line[0] == '#' }
37
- .map(&:strip).join("\n")
37
+ file_text, matches = parse_file(filename)
38
+
39
+ extracted_ruby = ''
40
+
41
+ last_match = [0, 0]
42
+ matches.each do |start_index, end_index|
43
+ handled_region_before(start_index, last_match.last, file_text, extracted_ruby)
44
+
45
+ extracted_ruby << extract_match(file_text, start_index, end_index)
46
+
47
+ last_match = [start_index, end_index]
48
+ end
49
+
50
+ extracted_ruby << file_text[last_match.last..-1].gsub(/./, ' ')
38
51
  end
39
52
 
40
53
  private
41
54
 
55
+ def parse_file(filename)
56
+ # http://edgeguides.rubyonrails.org/active_support_core_extensions.html#output-safety
57
+ # replace '<%==' with '<%= raw' to avoid generating invalid ruby code
58
+ file_text = File.read(filename).gsub(/<%==/, '<%= raw')
59
+
60
+ matching_regions = file_text.enum_for(:scan, ERB_REGEX)
61
+ .map { Regexp.last_match.offset(1) }
62
+
63
+ [file_text, matching_regions]
64
+ end
65
+
66
+ def handled_region_before(match_start, prev_end_index,
67
+ file_text, extracted_ruby)
68
+ return unless match_start > prev_end_index
69
+
70
+ region_before = file_text[prev_end_index..match_start - 1]
71
+
72
+ extracted_ruby << region_before.gsub(/./, ' ')
73
+
74
+ # if the last match was on the same line, we need to use a semicolon to
75
+ # separate statements
76
+ extracted_ruby[prev_end_index] = ';' if needs_stmt_delimiter?(prev_end_index, region_before)
77
+ end
78
+
79
+ def needs_stmt_delimiter?(last_match, region_before)
80
+ last_match.positive? && region_before.index("\n").nil?
81
+ end
82
+
83
+ def extract_match(file_text, start_index, end_index)
84
+ file_text[start_index...end_index].tap do |region|
85
+ region.gsub!(/./, ' ') if region[0] == '#'
86
+ end
87
+ end
88
+
42
89
  def create_temp_dir
43
90
  if @options[:tmp_folder]
44
91
  Pathname.new(File.expand_path(@options[:tmp_folder]))
@@ -62,12 +109,42 @@ module Ruumba
62
109
  end
63
110
 
64
111
  def run_rubocop(target, tmp)
65
- args = (@options[:arguments] || []).join(' ')
112
+ args = ['rubocop'] + (@options[:arguments] || []) + [target.to_s]
66
113
  todo = tmp + '.rubocop_todo.yml'
67
114
 
68
- system("cd #{tmp} && rubocop #{args} #{target}").tap do
69
- FileUtils.cp(todo, ENV['PWD']) if File.exist?(todo)
70
- FileUtils.rm_rf(tmp) unless @options[:tmp_folder]
115
+ pwd = ENV['PWD']
116
+
117
+ replacements = []
118
+
119
+ replacements << [/^#{Regexp.quote(tmp.to_s)}/, pwd]
120
+
121
+ unless @options[:disable_rb_extension]
122
+ replacements << [/\.erb\.rb/, '.erb']
123
+ end
124
+
125
+ result = Dir.chdir(tmp) do
126
+ stdout, stderr, status = Open3.capture3(*args)
127
+
128
+ munge_output(stdout, stderr, replacements)
129
+
130
+ status.exitstatus
131
+ end
132
+
133
+ FileUtils.cp(todo, pwd) if File.exist?(todo)
134
+ FileUtils.rm_rf(tmp) unless @options[:tmp_folder]
135
+
136
+ result
137
+ end
138
+
139
+ def munge_output(stdout, stderr, replacements)
140
+ [[STDOUT, stdout], [STDERR, stderr]].each do |output_stream, output|
141
+ next if output.nil? || output.empty?
142
+
143
+ replacements.each do |pattern, replacement|
144
+ output.gsub!(pattern, replacement)
145
+ end
146
+
147
+ output_stream.puts(output)
71
148
  end
72
149
  end
73
150
  end
@@ -19,7 +19,7 @@ module Ruumba
19
19
 
20
20
  desc 'Run RuboCop on ERB files'
21
21
  task(name, *args) do |_, task_args|
22
- block.call(*[self, task_args].slice(0, block.arity)) if block
22
+ yield(*[self, task_args].slice(0, block.arity)) if block
23
23
  run
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruumba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Weinstein
@@ -65,4 +65,3 @@ specification_version: 4
65
65
  summary: Allows users to lint Ruby code in ERB templates the same way they lint source
66
66
  code (using RuboCop).
67
67
  test_files: []
68
- has_rdoc: