Skip to content

Commit

Permalink
Adds better rubocop handling, and an option to bypass it
Browse files Browse the repository at this point in the history
  • Loading branch information
bougyman committed Apr 11, 2024
1 parent 305d391 commit 3104006
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
namespacer-rb (0.1.4)
parser (~> 3)
ruby-filemagic (~> 0.7)
tty-command (~> 0.10)
unparser (~> 0.6)

Expand Down Expand Up @@ -55,6 +56,7 @@ GEM
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
ruby-filemagic (0.7.3)
ruby-progressbar (1.13.0)
tty-color (0.6.0)
tty-command (0.10.1)
Expand Down
48 changes: 35 additions & 13 deletions exe/namespacer
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ require 'tty-command'
module Rubyists
# Namespace for the namespacer CLI
module Namespacer
CliOptions = Struct.new(:recursive, :verbose, :in_place)
CliOptions = Struct.new(:recursive, :verbose, :in_place, :fail_rubocop_silently)
def self.cli_options
@cli_options ||= CliOptions.new(verbose: false, recursive: false, in_place: false)
end

def self.cmd
@cmd = TTY::Command.new
@cmd = TTY::Command.new printer: :null
end
end
end
Expand All @@ -28,6 +28,10 @@ parser = OptionParser.new do |opts|

opts.on('-i', '--in-place', 'Modify files in-place') { |_| options.in_place = true }

opts.on('-f', '--fail-rubocop-silently', 'Write files even if they are not rubocop-friendly') do |_|
options.fail_rubocop_silently = true
end

opts.on('-V', '--version', 'Print version') do
puts "namespacer #{Rubyists::Namespacer::VERSION}"
exit
Expand Down Expand Up @@ -74,21 +78,39 @@ def new_path(path)
dir.join("#{base}.namespaced#{ext}")
end

def namespace_file(namespace, path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def rubocop_cmd(path, ruby_code) # rubocop:disable Metrics/MethodLength
result = Rubyists::Namespacer.cmd.run!(:rubocop,
'-A',
'--stdin',
path.basename.to_s,
'--stderr',
input: ruby_code,
only_output_on_error: true)
if result.failure?
warn "Rubocop failed for #{path}:\n#{result.err}"
if Rubyists::Namespacer.cli_options.fail_rubocop_silently
warn 'Continuing anyway'
else
exit 6
end
end

result
end

def namespace_file(namespace, path) # rubocop:disable Metrics/MethodLength
opts = Rubyists::Namespacer.cli_options
namespaced = Rubyists::Namespacer.namespace!(path.read, namespace)
write_path = Rubyists::Namespacer.cli_options.in_place ? path : new_path(path)
if Rubyists::Namespacer.cli_options.verbose
write_path = opts.in_place ? path : new_path(path)
if opts.verbose
msg = "Namespacing #{write_path} with #{namespace}"
msg << ' (in-place)' if Rubyists::Namespacer.cli_options.in_place
msg << ' (in-place)' if opts.in_place
warn msg
end
cmd = Rubyists::Namespacer.cmd.run(:rubocop,
'-A',
'--stdin',
write_path.basename.to_s,
'--stderr',
input: namespaced,
only_output_on_error: true)
cmd = rubocop_cmd(write_path, namespaced)
return unless cmd&.success?

warn "Writing namespaced file to #{write_path}" if opts.verbose
write_path.write(cmd.out)
end

Expand Down
2 changes: 1 addition & 1 deletion namespacer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

# Uncomment to register a new dependency of your gem
spec.add_dependency 'parser', '~> 3'
spec.add_dependency 'ruby-filemagic', '~> 0.7'
spec.add_dependency 'tty-command', '~> 0.10'
spec.add_dependency 'unparser', '~> 0.6'
spec.metadata['rubygems_mfa_required'] = 'true'
Expand Down

0 comments on commit 3104006

Please sign in to comment.