dehenzify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a5d055a0fa60b2f5d86216faf8aa646b8744bddbd78acdcd0af50d4611780a2a
4
+ data.tar.gz: 4b2915da1b53f62748e81d41da4e118d6bf5d052a2733fd21ff4aa27bdedca31
5
+ SHA512:
6
+ metadata.gz: 2559d6b5733534d6f4b9cb60a4ac03835e534bdb3eac8675b0ec43f19e0206b918f7a714ac786af2c3397a25960cfff8efa8324864113edfa2378c277c555f46
7
+ data.tar.gz: c405f6882272497965764bfc8942a83bc7e7da6be204f4ead81e2b540749d70b7147f2c30daf7bf570322932b3dbe6f80b9b63bcc2ade8c2ec596e637b3b3299
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dehenzify.gemspec
4
+ gemspec
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dehenzify (0.0.1)
5
+ activesupport
6
+ parser
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (5.2.2.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ ast (2.4.0)
17
+ coderay (1.1.2)
18
+ concurrent-ruby (1.1.5)
19
+ i18n (1.6.0)
20
+ concurrent-ruby (~> 1.0)
21
+ method_source (0.9.2)
22
+ minitest (5.11.3)
23
+ parser (2.6.2.0)
24
+ ast (~> 2.4.0)
25
+ pry (0.12.2)
26
+ coderay (~> 1.1.0)
27
+ method_source (~> 0.9.0)
28
+ rake (10.5.0)
29
+ thread_safe (0.3.6)
30
+ tzinfo (1.2.5)
31
+ thread_safe (~> 0.1)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler (~> 2.0)
38
+ dehenzify!
39
+ minitest
40
+ pry
41
+ rake (~> 10.0)
42
+
43
+ BUNDLED WITH
44
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Reinier de Lange
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # Dehenzify
2
+
3
+ Dehenzifies code!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dehenzify'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dehenzify
20
+
21
+ ## Usage example
22
+
23
+ $ bundle exec dehenzify test/fixtures/henzified_classes.rb
24
+
25
+ $ bundle exec dehenzify app/lib/stats
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/moiristo/dehenzify.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/*_test.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
@@ -0,0 +1,32 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'dehenzify/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'dehenzify'
7
+ spec.version = Dehenzify::VERSION
8
+ spec.authors = ['Reinier de Lange']
9
+ spec.email = ['rjdelange@icloud.com']
10
+
11
+ spec.summary = 'Dehenzify'
12
+ spec.description = 'Dehenzify'
13
+ spec.homepage = "https://github.com/moiristo/dehenzify"
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_runtime_dependency 'activesupport'
26
+ spec.add_runtime_dependency 'parser'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 2.0'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'minitest'
31
+ spec.add_development_dependency 'pry'
32
+ end
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ # EXAMPLE: bin/dehenzify app/lib/stats
3
+ require 'bundler/setup'
4
+ require 'dehenzify'
5
+
6
+ file_or_path = ARGV[0]
7
+
8
+ exit(1) if file_or_path.nil?
9
+
10
+ base_dir = nil
11
+
12
+ files =
13
+ if file_or_path.end_with?('.rb')
14
+ [file_or_path]
15
+ else
16
+ base_dir = Pathname.new(file_or_path)
17
+ base_dir.glob('**/*.rb')
18
+ end
19
+
20
+ files.each do |file_path|
21
+ dehenzifier = Dehenzify::Dehenzifier.new(file_path)
22
+ extracted_sources = dehenzifier.run!(base_dir: base_dir)
23
+
24
+ extracted_sources.each do |extracted_source|
25
+ if extracted_source.target_file_path != file_path.to_s || !extracted_source.empty?
26
+ extracted_source.write!
27
+ else
28
+ extracted_source.delete!
29
+ end
30
+ end
31
+ end
32
+
33
+ exit(0)
@@ -0,0 +1,13 @@
1
+ require 'active_support/core_ext/object/blank.rb'
2
+ require 'active_support/core_ext/string/inflections'
3
+
4
+ require 'fileutils'
5
+ require 'parser/current'
6
+
7
+ require 'dehenzify/version'
8
+ require 'dehenzify/extracted_source'
9
+ require 'dehenzify/extractor'
10
+ require 'dehenzify/dehenzifier'
11
+
12
+ module Dehenzify
13
+ end
@@ -0,0 +1,15 @@
1
+ module Dehenzify
2
+
3
+ class Dehenzifier
4
+ attr_accessor :file_name
5
+
6
+ def initialize(file_name)
7
+ self.file_name = file_name
8
+ end
9
+
10
+ def run!(base_dir: nil)
11
+ extractor = Dehenzify::Extractor.new
12
+ extractor.extract(file_name, base_dir: base_dir)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ module Dehenzify
2
+ class ExtractedSource < Struct.new(:target_file_path, :source, :namespace_modules)
3
+
4
+ attr_accessor :node
5
+
6
+ def namespaced_source
7
+ raw_namespaced_source = source
8
+
9
+ namespace_modules.reverse_each.with_index(1) do |module_name, index|
10
+ space_offset = ' ' * (namespace_modules.size - index)
11
+
12
+ modulized_source = ''
13
+ modulized_source << "module #{module_name}" << "\n"
14
+ modulized_source << space_offset << ' '
15
+ modulized_source << raw_namespaced_source << "\n"
16
+ modulized_source << space_offset << 'end'
17
+ raw_namespaced_source = modulized_source
18
+ end if namespace_modules
19
+
20
+ raw_namespaced_source << "\n" unless raw_namespaced_source.end_with?("\n")
21
+ raw_namespaced_source
22
+ end
23
+
24
+ def empty?
25
+ source.blank? || Dehenzify::Extractor.empty_node?(Parser::CurrentRuby.parse(source))
26
+ end
27
+
28
+ def exists?
29
+ File.exists?(target_file_path)
30
+ end
31
+
32
+ def write!
33
+ FileUtils.mkdir_p(File.dirname(target_file_path))
34
+ File.write(target_file_path, namespaced_source)
35
+ target_file_path
36
+ end
37
+
38
+ def delete!
39
+ File.delete(target_file_path) if exists?
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,70 @@
1
+ module Dehenzify
2
+ class Extractor < Parser::TreeRewriter
3
+ def self.namespace_module?(node)
4
+ last_child = node.children.last
5
+ children =
6
+ if last_child&.type == :begin
7
+ last_child.children
8
+ else
9
+ node.children[1..-1]
10
+ end
11
+
12
+ (children.compact.map(&:type) - [:class, :module]).none?
13
+ end
14
+
15
+ def self.empty_node?(node)
16
+ node.type == :module &&
17
+ namespace_module?(node) &&
18
+ node.children.select { |child| child&.type == :module }.all? { |child| empty_node?(child) }
19
+ end
20
+
21
+ def on_module(node)
22
+ @modules << node.children.first.loc.expression.source
23
+ super
24
+ @modules.pop
25
+
26
+ extract_source(node) if !Dehenzify::Extractor.namespace_module?(node) && (@source_type.nil? || @source_type == :module)
27
+ end
28
+
29
+ def on_class(node)
30
+ extract_source(node) if @source_type.nil? || @source_type == :class
31
+ # Don't descend into classes
32
+ end
33
+
34
+ def extract(file_path, code: nil, source_type: nil, base_dir: nil)
35
+ @root_dir = (base_dir || Pathname.new(File.dirname(file_path))).join('../')
36
+ @file_path = file_path.to_s
37
+ @source_type = source_type
38
+ @modules = []
39
+ @extracted_sources = []
40
+
41
+ code ||= File.read(file_path)
42
+ ast = Parser::CurrentRuby.parse(code)
43
+
44
+ buffer = Parser::Source::Buffer.new('')
45
+ buffer.source = code
46
+ new_source = rewrite(buffer, ast).gsub(/\n^\s*\n/, "\n\n")
47
+
48
+ @extracted_sources << Dehenzify::ExtractedSource.new(@file_path, new_source)
49
+ @extracted_sources
50
+ end
51
+
52
+ private
53
+
54
+ def extract_source(node)
55
+ source_models = @modules.dup
56
+
57
+ target_dir = @root_dir.join(source_models.map(&:underscore).join('/'))
58
+ file_name = node.children.first.loc.expression.source.underscore + '.rb'
59
+ target_file_path = target_dir.join(file_name).to_s
60
+
61
+ unless target_file_path == @file_path
62
+ extracted_source = Dehenzify::ExtractedSource.new(target_file_path, node.loc.expression.source, source_models)
63
+ extracted_source.node = node
64
+ @extracted_sources << extracted_source
65
+
66
+ remove(node.loc.expression)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module Dehenzify
2
+ VERSION = '0.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dehenzify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Reinier de Lange
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Dehenzify
98
+ email:
99
+ - rjdelange@icloud.com
100
+ executables:
101
+ - dehenzify
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/rake
112
+ - dehenzify.gemspec
113
+ - exe/dehenzify
114
+ - lib/dehenzify.rb
115
+ - lib/dehenzify/dehenzifier.rb
116
+ - lib/dehenzify/extracted_source.rb
117
+ - lib/dehenzify/extractor.rb
118
+ - lib/dehenzify/version.rb
119
+ homepage: https://github.com/moiristo/dehenzify
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.0.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Dehenzify
142
+ test_files: []