namespacer-rb 0.1.7 → 0.1.9

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: 80c1319eb56d8321d3acacca45c0e724378f49c287f3447f22a709d3c8dfebec
4
- data.tar.gz: 7b0c82bb2371409a298fd90bdbb15b655ca7e113600448cdd62a4a0a19ae80f1
3
+ metadata.gz: 799712754c8eb33dc80bd4abe91dd6e238ef49ccdc3b34c439f2b65a75c75aff
4
+ data.tar.gz: 8f69c56fcc7e24e4033ea0d6a7d62f7bb82b695fa94c1c22017e40ff89c1ffc8
5
5
  SHA512:
6
- metadata.gz: 7a8679263fad4f27695766449de5f6a01c0111bbea1f818607ed3c1565d219b88da75469fe6ffc1e4fe2dad7d352b7c4fbb5e09d0e30c50e1640b7ad7a8bf81c
7
- data.tar.gz: b0e8bb9897e55062169f119fb7a684ab6549cdc3a085b1bcb61d2b88b9869a9cd2e26f9d6b44142316f36ae09990c0ef0b59e476b72b72902961f0d39e85081d
6
+ metadata.gz: 0b56ad847515db054a56c39b7c3b00d558a6a68e580cccccc4cf1d714e79004d13b46a7a83004fda9f42077ef026fe20fc39266d99bf20df6a0ae66bf57b4ed4
7
+ data.tar.gz: 3f9835a59451fd0dddfbf915b5ad2707a8b0cc616ace9ccda04a007c2080c86f3e74dc4fd6472757e06c5987eb021802b5fd0f528e1930c083a5464384337d83
data/exe/namespacer CHANGED
@@ -9,7 +9,8 @@ require 'tty-command'
9
9
  module Rubyists
10
10
  # Namespace for the namespacer CLI
11
11
  module Namespacer
12
- CliOptions = Struct.new(:recursive, :verbose, :in_place, :fail_rubocop_silently)
12
+ CLI_OPTS = %i[recursive verbose in_place fail_rubocop_silently allow_text_files dry_run].freeze
13
+ CliOptions = Struct.new(*CLI_OPTS)
13
14
  def self.cli_options
14
15
  @cli_options ||= CliOptions.new(verbose: false, recursive: false, in_place: false)
15
16
  end
@@ -33,6 +34,12 @@ parser = OptionParser.new do |opts|
33
34
 
34
35
  opts.on('-i', '--in-place', 'Modify files in-place') { |_| options.in_place = true }
35
36
 
37
+ opts.on('-d', '--dry-run', 'Dry run, do not write files') { |_| options.dry_run = true }
38
+
39
+ opts.on('-a', '--allow-text-files', 'Allow non-ruby mime types, so long as they are text') do |_|
40
+ options.allow_text_files = true
41
+ end
42
+
36
43
  opts.on('-f', '--fail-rubocop-silently', 'Write files even if they are not rubocop-friendly') do |_|
37
44
  options.fail_rubocop_silently = true
38
45
  end
@@ -80,7 +87,7 @@ def new_path(path)
80
87
  dir = path.dirname
81
88
  ext = path.extname
82
89
  base = path.basename(ext.to_s)
83
- dir.join("#{base}.namespaced#{ext}")
90
+ dir.join("#{base}_namespaced#{ext}")
84
91
  end
85
92
 
86
93
  def rubocop_cmd(path, ruby_code) # rubocop:disable Metrics/MethodLength
@@ -104,11 +111,13 @@ def rubocop_cmd(path, ruby_code) # rubocop:disable Metrics/MethodLength
104
111
  end
105
112
 
106
113
  def rubocop_friendly?(path)
114
+ opts = Rubyists::Namespacer.cli_options
107
115
  magic = Rubyists::Namespacer.magic
108
116
  mime = magic.file(path.to_s)
109
117
  return true if mime.include?('text/x-ruby')
118
+ return true if mime.split('/').first == 'text' && opts.allow_text_files
110
119
 
111
- warn "File #{path} is not a Ruby file (#{mime})" if Rubyists::Namespacer.cli_options.verbose
120
+ warn "File #{path} is not a Ruby file (#{mime}), consider -a" if opts.verbose
112
121
  false
113
122
  end
114
123
 
@@ -121,8 +130,12 @@ def log_namespace(namespace, write_path)
121
130
  warn msg
122
131
  end
123
132
 
133
+ def valid_file?(path)
134
+ !excluded_path?(path) && rubocop_friendly?(path)
135
+ end
136
+
124
137
  def namespace_file(namespace, path)
125
- return unless rubocop_friendly?(path)
138
+ return unless valid_file?(path)
126
139
 
127
140
  opts = Rubyists::Namespacer.cli_options
128
141
  namespaced = Rubyists::Namespacer.namespace!(path.read, namespace)
@@ -133,16 +146,20 @@ def namespace_file(namespace, path)
133
146
  write_path.write(cmd.out)
134
147
  end
135
148
 
149
+ def excluded_path?(path)
150
+ return true if path.directory?
151
+ return true if path.basename.to_s.start_with?('.')
152
+ return true if path.basename.to_s.end_with?('_namespaced')
153
+ return true if path.basename.to_s.match?(/_namespaced\.[^\.]+$/)
154
+
155
+ false
156
+ end
157
+
136
158
  paths.each do |path|
137
159
  case path
138
160
  when ->(p) { p.directory? }
139
161
  if options.recursive
140
162
  path.find do |f|
141
- next if f.directory?
142
- next if f.basename.to_s.start_with?('.')
143
- next if f.basename.to_s.end_with?('.namespaced')
144
- next if f.basename.to_s.end_with?('.namespaced.rb')
145
-
146
163
  namespace_file(namespace, f)
147
164
  end
148
165
  else
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubyists
4
+ module Namespacer
5
+ # Do the AST rewriting with the Rewriter class
6
+ class Rewriter < Parser::TreeRewriter
7
+ attr_accessor(:namespaces)
8
+
9
+ def initialize(namespaces)
10
+ super()
11
+ @namespaces = namespaces
12
+ end
13
+
14
+ def on_module(node)
15
+ _on_module(node)
16
+ end
17
+
18
+ def on_class(node)
19
+ _on_module(node)
20
+ end
21
+
22
+ private
23
+
24
+ def wrap(ast)
25
+ # Recursively wrap the AST in the namespace modules
26
+ namespaces.split('::').reverse.inject(ast) do |current_ast, ns|
27
+ # Create a module node with the current namespace part and the current AST
28
+ Parser::AST::Node.new(:module, [Parser::AST::Node.new(:const, [nil, ns.to_sym]), current_ast])
29
+ end
30
+ end
31
+
32
+ def _on_module(node)
33
+ return unless node.location.column.zero?
34
+
35
+ (ast, comments) = Unparser.parse_with_comments(node.location.expression.source)
36
+ replace(node.location.expression, Unparser.unparse(wrap(ast), comments))
37
+ end
38
+ end
39
+ end
40
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rubyists
4
4
  module Namespacer
5
- VERSION = '0.1.7'
5
+ VERSION = '0.1.9'
6
6
  end
7
7
  end
data/lib/namespacer.rb CHANGED
@@ -7,43 +7,16 @@ require 'pry'
7
7
 
8
8
  # Top-level namespace to keep Rubyists namespaces in isolation
9
9
  module Rubyists
10
- # Wrap some namespace around top-level AST nodes of 'module' or 'class' type
10
+ # Namespace for the namespacer tool
11
11
  module Namespacer
12
- # Do the rewriting with the rewriter class
13
- class Rewriter < Parser::TreeRewriter
14
- attr_accessor(:namespaces)
15
-
16
- def initialize(namespaces)
17
- super()
18
- @namespaces = namespaces
19
- end
20
-
21
- def on_module(node)
22
- _on_module(node)
23
- end
24
-
25
- def on_class(node)
26
- _on_module(node)
27
- end
28
-
29
- private
30
-
31
- def wrap(ast)
32
- # Recursively wrap the AST in the namespace modules
33
- namespaces.split('::').reverse.inject(ast) do |current_ast, ns|
34
- # Create a module node with the current namespace part and the current AST
35
- Parser::AST::Node.new(:module, [Parser::AST::Node.new(:const, [nil, ns.to_sym]), current_ast])
36
- end
37
- end
38
-
39
- def _on_module(node)
40
- return unless node.location.column.zero?
41
-
42
- (ast, comments) = Unparser.parse_with_comments(node.location.expression.source)
43
- replace(node.location.expression, Unparser.unparse(wrap(ast), comments))
44
- end
45
- end
46
-
12
+ require_relative 'namespacer/rewriter'
13
+
14
+ # Wrap some namespace(s) around top-level AST nodes of 'module' or 'class' type
15
+ #
16
+ # @param string_or_io [String, IO] The source code to namespace
17
+ # @param namespaces [String] The namespace(s) to wrap around the top-level AST nodes
18
+ #
19
+ # @return [String] The source code with the namespace(s) wrapped around the top-level AST nodes
47
20
  def self.namespace!(string_or_io, namespaces)
48
21
  buffer = Parser::Source::Buffer.new("(#{namespaces})")
49
22
  buffer.source = string_or_io.is_a?(IO) ? string_or_io.read : string_or_io
@@ -56,5 +29,5 @@ end
56
29
 
57
30
  if $PROGRAM_NAME == __FILE__
58
31
  warn 'Wrapping myself'
59
- puts Rubyists::Namespacer.namespace!(File.read(__FILE__), 'Rubyists::Namespacer')
32
+ puts Rubyists::Namespacer.namespace!(File.read(__FILE__), 'Wrapped::Smoke::Test')
60
33
  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.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tj (bougyman) Vanderpoel
@@ -95,8 +95,8 @@ files:
95
95
  - Readme.adoc
96
96
  - exe/namespacer
97
97
  - lib/namespacer.rb
98
+ - lib/namespacer/rewriter.rb
98
99
  - lib/namespacer/version.rb
99
- - sig/namespacer.rbs
100
100
  homepage: https://github.com/rubyists/namespacer
101
101
  licenses: []
102
102
  metadata:
data/sig/namespacer.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Namespacer
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end