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 +4 -4
 - data/.overcommit.yml +11 -0
 - 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 +3 -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: af2aa4d4cbdf4fc10dba7f5af4baeece22091dd92f9da68abed73e574d48dff3
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 1b142abdfe26c43b19e8957e2a2415d87f0b122568f72083a2212fe78ac5fa64
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 8270dabf68e4ca29bd83012ec696d34c45197519e5c85dd2f279262a0806f70298ce492b02aaff682b93fbf86663247b8b35d2a0ed280259dabbf1d1b7c52c8c
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 1bf580fcebfcc2a4982ab89a307e76f2aa7fd44506b4f3a34fdfe0bcb1cf391dd9f4eb2720f2261f176b39fcd8fcef6f67d41bb8db94d2d9f71973d6e2f3f91a
         
     | 
    
        data/.overcommit.yml
    ADDED
    
    
    
        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.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