namespacer-rb 0.1.8 → 0.1.10

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: d9007d905c40b79426ef2e135e520bf0a87f035a2ae2413c06398d0e04d91f73
4
- data.tar.gz: a43de5cd8ca7bde19071109dc171ee7b534647ff2d5c0729e15ad1b5410874f1
3
+ metadata.gz: af2aa4d4cbdf4fc10dba7f5af4baeece22091dd92f9da68abed73e574d48dff3
4
+ data.tar.gz: 1b142abdfe26c43b19e8957e2a2415d87f0b122568f72083a2212fe78ac5fa64
5
5
  SHA512:
6
- metadata.gz: bafd3deefcd88b7c476aa295fc7d6134e7d0e0ca0589077a24fda966b401fd3a5f6e3832832092e24723f1ea63940113b28479e6322efdd9dcb94a00df79bc35
7
- data.tar.gz: b47c888e929e11a25c6990a9578cd04c57d9da8250268a4787243013178b297f30b73bd0bb33faef05300df4e4eb249304f6b342a8984fe34809a34ce1b9e09c
6
+ metadata.gz: 8270dabf68e4ca29bd83012ec696d34c45197519e5c85dd2f279262a0806f70298ce492b02aaff682b93fbf86663247b8b35d2a0ed280259dabbf1d1b7c52c8c
7
+ data.tar.gz: 1bf580fcebfcc2a4982ab89a307e76f2aa7fd44506b4f3a34fdfe0bcb1cf391dd9f4eb2720f2261f176b39fcd8fcef6f67d41bb8db94d2d9f71973d6e2f3f91a
data/.overcommit.yml ADDED
@@ -0,0 +1,11 @@
1
+ ---
2
+ PreCommit:
3
+ RuboCop:
4
+ enabled: true
5
+ BundleAudit:
6
+ enabled: true
7
+ BundleCheck:
8
+ enabled: true
9
+ BundleOutdated:
10
+ enabled: true
11
+
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, :allow_text_files)
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,8 @@ 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
+
36
39
  opts.on('-a', '--allow-text-files', 'Allow non-ruby mime types, so long as they are text') do |_|
37
40
  options.allow_text_files = true
38
41
  end
@@ -114,7 +117,7 @@ def rubocop_friendly?(path)
114
117
  return true if mime.include?('text/x-ruby')
115
118
  return true if mime.split('/').first == 'text' && opts.allow_text_files
116
119
 
117
- warn "File #{path} is not a Ruby file (#{mime})" if opts.verbose
120
+ warn "File #{path} is not a Ruby file (#{mime}), consider -a" if opts.verbose
118
121
  false
119
122
  end
120
123
 
@@ -127,8 +130,12 @@ def log_namespace(namespace, write_path)
127
130
  warn msg
128
131
  end
129
132
 
133
+ def valid_file?(path)
134
+ !excluded_path?(path) && rubocop_friendly?(path)
135
+ end
136
+
130
137
  def namespace_file(namespace, path)
131
- return unless rubocop_friendly?(path)
138
+ return unless valid_file?(path)
132
139
 
133
140
  opts = Rubyists::Namespacer.cli_options
134
141
  namespaced = Rubyists::Namespacer.namespace!(path.read, namespace)
@@ -139,16 +146,20 @@ def namespace_file(namespace, path)
139
146
  write_path.write(cmd.out)
140
147
  end
141
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
+
142
158
  paths.each do |path|
143
159
  case path
144
160
  when ->(p) { p.directory? }
145
161
  if options.recursive
146
162
  path.find do |f|
147
- next if f.directory?
148
- next if f.basename.to_s.start_with?('.')
149
- next if f.basename.to_s.end_with?('.namespaced')
150
- next if f.basename.to_s.end_with?('.namespaced.rb')
151
-
152
163
  namespace_file(namespace, f)
153
164
  end
154
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.8'
5
+ VERSION = '0.1.10'
6
6
  end
7
7
  end
data/lib/namespacer.rb CHANGED
@@ -9,42 +9,14 @@ require 'pry'
9
9
  module Rubyists
10
10
  # Namespace for the namespacer tool
11
11
  module Namespacer
12
- # Do the AST 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
-
47
- # Wrap some namespace around top-level AST nodes of 'module' or 'class' type
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
48
20
  def self.namespace!(string_or_io, namespaces)
49
21
  buffer = Parser::Source::Buffer.new("(#{namespaces})")
50
22
  buffer.source = string_or_io.is_a?(IO) ? string_or_io.read : string_or_io
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.8
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tj (bougyman) Vanderpoel
@@ -89,14 +89,15 @@ executables:
89
89
  extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
+ - ".overcommit.yml"
92
93
  - ".rubocop.yml"
93
94
  - CHANGELOG.md
94
95
  - Rakefile
95
96
  - Readme.adoc
96
97
  - exe/namespacer
97
98
  - lib/namespacer.rb
99
+ - lib/namespacer/rewriter.rb
98
100
  - lib/namespacer/version.rb
99
- - sig/namespacer.rbs
100
101
  homepage: https://github.com/rubyists/namespacer
101
102
  licenses: []
102
103
  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