namespacer-rb 0.1.4 → 0.1.6

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
  SHA256:
3
- metadata.gz: 4f81c3fa425b80731d4d5284b91ae35e989d94fe32eed933a945cb66b764aec4
4
- data.tar.gz: 4e079d426c518db24555ced5fd890864deeff9236e4314d22fdeb5a88128b75a
3
+ metadata.gz: d185c337c9121c5caa1b0d5c8050189b5dc7f4bdb2711335eeb1b508a617b4ef
4
+ data.tar.gz: 34021011d450232a6222b0ba9c6de5f80c6909d487ba733d550e2ff95c02cfd3
5
5
  SHA512:
6
- metadata.gz: ce7d981e6b1e4649860c3ed40b4ce6a08e027b473de05c691513e0a3f0f7e1b54fa91b1d441d4d9fbb626ea19cc6b2386eebbcb004980cb97432f4de9f0072c7
7
- data.tar.gz: a71dae25f3007cc75f225efdfd5e8511c08e93104cc8e16d4e7a23690918903a4f74432dc47012e5aa2fd85a9b29075e96cee42342b00e413e012341b0a26144
6
+ metadata.gz: 42884144adc93a87ee7af9da506ec51670d6289ef66b2f8ffcde00ab8eeafe40d0fa2e1f58c9239ff576438dddbc6cdb6910ce0b42cab060dae500147c40a248
7
+ data.tar.gz: 48421f57144fc65224be350c60a7ba8948851624c8d90bf36d218cab05f880075659c058757c7177dc6f68e4f9b8668d720692fe769d2c4bcadb5e7b0337cc01
data/exe/namespacer CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'filemagic'
4
5
  require 'namespacer'
5
6
  require 'optparse'
6
7
  require 'pathname'
@@ -8,13 +9,17 @@ require 'tty-command'
8
9
  module Rubyists
9
10
  # Namespace for the namespacer CLI
10
11
  module Namespacer
11
- CliOptions = Struct.new(:recursive, :verbose, :in_place)
12
+ CliOptions = Struct.new(:recursive, :verbose, :in_place, :fail_rubocop_silently)
12
13
  def self.cli_options
13
14
  @cli_options ||= CliOptions.new(verbose: false, recursive: false, in_place: false)
14
15
  end
15
16
 
16
17
  def self.cmd
17
- @cmd = TTY::Command.new
18
+ @cmd ||= TTY::Command.new printer: :null
19
+ end
20
+
21
+ def self.magic
22
+ @magic ||= FileMagic.new(FileMagic::MAGIC_MIME)
18
23
  end
19
24
  end
20
25
  end
@@ -28,6 +33,10 @@ parser = OptionParser.new do |opts|
28
33
 
29
34
  opts.on('-i', '--in-place', 'Modify files in-place') { |_| options.in_place = true }
30
35
 
36
+ opts.on('-f', '--fail-rubocop-silently', 'Write files even if they are not rubocop-friendly') do |_|
37
+ options.fail_rubocop_silently = true
38
+ end
39
+
31
40
  opts.on('-V', '--version', 'Print version') do
32
41
  puts "namespacer #{Rubyists::Namespacer::VERSION}"
33
42
  exit
@@ -74,21 +83,53 @@ def new_path(path)
74
83
  dir.join("#{base}.namespaced#{ext}")
75
84
  end
76
85
 
77
- def namespace_file(namespace, path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
78
- namespaced = Rubyists::Namespacer.namespace!(path.read, namespace)
79
- write_path = Rubyists::Namespacer.cli_options.in_place ? path : new_path(path)
80
- if Rubyists::Namespacer.cli_options.verbose
81
- msg = "Namespacing #{write_path} with #{namespace}"
82
- msg << ' (in-place)' if Rubyists::Namespacer.cli_options.in_place
83
- warn msg
86
+ def rubocop_cmd(path, ruby_code) # rubocop:disable Metrics/MethodLength
87
+ result = Rubyists::Namespacer.cmd.run!(:rubocop,
88
+ '-A',
89
+ '--stdin',
90
+ path.basename.to_s,
91
+ '--stderr',
92
+ input: ruby_code,
93
+ only_output_on_error: true)
94
+ if result.failure?
95
+ warn "Rubocop failed for #{path}:\n#{result.err}"
96
+ if Rubyists::Namespacer.cli_options.fail_rubocop_silently
97
+ warn 'Continuing anyway'
98
+ else
99
+ exit 6
100
+ end
84
101
  end
85
- cmd = Rubyists::Namespacer.cmd.run(:rubocop,
86
- '-A',
87
- '--stdin',
88
- write_path.basename.to_s,
89
- '--stderr',
90
- input: namespaced,
91
- only_output_on_error: true)
102
+
103
+ result
104
+ end
105
+
106
+ def rubocop_friendly?(path)
107
+ magic = Rubyists::Namespacer.magic
108
+ mime = magic.file(path.to_s)
109
+ return true if mime.include?('text/x-ruby')
110
+
111
+ warn "File #{path} is not a Ruby file (#{mime})" if Rubyists::Namespacer.cli_options.verbose
112
+ false
113
+ end
114
+
115
+ def log_namespace(namespace, write_path)
116
+ opts = Rubyists::Namespacer.cli_options
117
+ return unless opts.verbose
118
+
119
+ msg = "Namespacing #{write_path} with #{namespace}"
120
+ msg << ' (in-place)' if opts.in_place
121
+ warn msg
122
+ end
123
+
124
+ def namespace_file(namespace, path)
125
+ return unless rubocop_friendly?(path)
126
+
127
+ opts = Rubyists::Namespacer.cli_options
128
+ namespaced = Rubyists::Namespacer.namespace!(path.read, namespace)
129
+ write_path = opts.in_place ? path : new_path(path)
130
+ log_namespace namespace, write_path
131
+ cmd = rubocop_cmd(write_path, namespaced)
132
+ warn "Writing namespaced file to #{write_path}" if opts.verbose
92
133
  write_path.write(cmd.out)
93
134
  end
94
135
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rubyists
4
4
  module Namespacer
5
- VERSION = '0.1.4'
5
+ VERSION = '0.1.6'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: namespacer-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tj (bougyman) Vanderpoel
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-filemagic
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: tty-command
29
43
  requirement: !ruby/object:Gem::Requirement