Skip to content

Commit

Permalink
Adds ability to namespace non-ruby mime types, so long as they are text
Browse files Browse the repository at this point in the history
  • Loading branch information
bougyman committed Apr 11, 2024
1 parent 0df518f commit fce11c5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
namespacer-rb (0.1.6)
namespacer-rb (0.1.7)
parser (~> 3)
rubocop (~> 1.21)
ruby-filemagic (~> 0.7)
Expand Down
12 changes: 9 additions & 3 deletions exe/namespacer
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require 'tty-command'
module Rubyists
# Namespace for the namespacer CLI
module Namespacer
CliOptions = Struct.new(:recursive, :verbose, :in_place, :fail_rubocop_silently)
CliOptions = Struct.new(:recursive, :verbose, :in_place, :fail_rubocop_silently, :allow_text_files)
def self.cli_options
@cli_options ||= CliOptions.new(verbose: false, recursive: false, in_place: false)
end
Expand All @@ -33,6 +33,10 @@ parser = OptionParser.new do |opts|

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

opts.on('-a', '--allow-text-files', 'Allow non-ruby mime types, so long as they are text') do |_|
options.allow_text_files = true
end

opts.on('-f', '--fail-rubocop-silently', 'Write files even if they are not rubocop-friendly') do |_|
options.fail_rubocop_silently = true
end
Expand Down Expand Up @@ -80,7 +84,7 @@ def new_path(path)
dir = path.dirname
ext = path.extname
base = path.basename(ext.to_s)
dir.join("#{base}.namespaced#{ext}")
dir.join("#{base}_namespaced#{ext}")
end

def rubocop_cmd(path, ruby_code) # rubocop:disable Metrics/MethodLength
Expand All @@ -104,11 +108,13 @@ def rubocop_cmd(path, ruby_code) # rubocop:disable Metrics/MethodLength
end

def rubocop_friendly?(path)
opts = Rubyists::Namespacer.cli_options
magic = Rubyists::Namespacer.magic
mime = magic.file(path.to_s)
return true if mime.include?('text/x-ruby')
return true if mime.split('/').first == 'text' && opts.allow_text_files

warn "File #{path} is not a Ruby file (#{mime})" if Rubyists::Namespacer.cli_options.verbose
warn "File #{path} is not a Ruby file (#{mime})" if opts.verbose
false
end

Expand Down

0 comments on commit fce11c5

Please sign in to comment.