namespacer-rb 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/exe/namespacer +19 -8
- data/lib/namespacer/rewriter.rb +40 -0
- data/lib/namespacer/version.rb +1 -1
- data/lib/namespacer.rb +8 -36
- metadata +2 -2
- data/sig/namespacer.rbs +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 799712754c8eb33dc80bd4abe91dd6e238ef49ccdc3b34c439f2b65a75c75aff
|
4
|
+
data.tar.gz: 8f69c56fcc7e24e4033ea0d6a7d62f7bb82b695fa94c1c22017e40ff89c1ffc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
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
|
data/lib/namespacer/version.rb
CHANGED
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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.
|
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