depdump 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +11 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/depdump.gemspec +33 -0
- data/exe/depdump +20 -0
- data/lib/depdump.rb +20 -0
- data/lib/depdump/configurable.rb +44 -0
- data/lib/depdump/dependency_graph.rb +58 -0
- data/lib/depdump/parser.rb +33 -0
- data/lib/depdump/registry.rb +3 -0
- data/lib/depdump/registry/tree.rb +40 -0
- data/lib/depdump/registry/tree/node.rb +62 -0
- data/lib/depdump/registry/tree/relation.rb +20 -0
- data/lib/depdump/tracer.rb +73 -0
- data/lib/depdump/version.rb +3 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bf799ff81ff8e6325d2eb5951cc6c7846e7f78129474316f25d1c63d912d174f
|
4
|
+
data.tar.gz: a93f915ca7717d494338bda447aebcc34deca93cb1da87c538123956a08448bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7719ee27728869f30fe5fe2ef19d9f5e42ce47a797ecc12d3531706446b083014a0cb1294c72f467cce2695a87d75475802f5e85a8014a819b36e9f5d46b3369
|
7
|
+
data.tar.gz: 8e8a0cc79281fff35b92771046662980d5f131d83229cf6dc38c226228231f0abc0188748f276bea24ab8573639e5065bda9c0e8105a6decd066eccb41993c15
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at upinetree@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 upinetree
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Depdump
|
2
|
+
|
3
|
+
:construction: Still a work in progress, interfaces may be changed.
|
4
|
+
|
5
|
+
A dump tool of ruby class/module dependencies.
|
6
|
+
|
7
|
+
- It aims to dump all static dependencies
|
8
|
+
- Dynamic referencing by metaprogramming is not supported
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'depdump'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle install
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install depdump
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Quick start:
|
29
|
+
|
30
|
+
$ cd my/cool/ruby/project
|
31
|
+
$ depdump
|
32
|
+
|
33
|
+
Or, specify files and directories.
|
34
|
+
|
35
|
+
$ depdump lib/greate_module.rb app
|
36
|
+
|
37
|
+
Then, the dependency graph appeared as a JSON string in stdout.
|
38
|
+
|
39
|
+
{
|
40
|
+
"nodes": [
|
41
|
+
["A"],
|
42
|
+
["A", "B"]
|
43
|
+
],
|
44
|
+
"edges": [
|
45
|
+
{
|
46
|
+
"from": ["A"],
|
47
|
+
"to": ["A", "B"]
|
48
|
+
}
|
49
|
+
]
|
50
|
+
}
|
51
|
+
|
52
|
+
And some execution warnings are written in `depdump-error.log`.
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
57
|
+
|
58
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/upinetree/depdump. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
63
|
+
|
64
|
+
## License
|
65
|
+
|
66
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
67
|
+
|
68
|
+
## Code of Conduct
|
69
|
+
|
70
|
+
Everyone interacting in the Depdump project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/depdump/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/depdump.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "depdump/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "depdump"
|
7
|
+
spec.version = Depdump::VERSION
|
8
|
+
spec.authors = ["upinetree"]
|
9
|
+
spec.email = ["upinetree@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{A dump tool of ruby class/module dependencies.}
|
12
|
+
spec.description = %q{A dump tool of ruby class/module dependencies.}
|
13
|
+
spec.homepage = "https://github.com/upinetree/depdump"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/upinetree/depdump"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/upinetree/depdump/commits/master"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_runtime_dependency "parser", "~> 2.6"
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 2.1.a"
|
32
|
+
spec.add_development_dependency "rake"
|
33
|
+
end
|
data/exe/depdump
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
# Avoid mixing error output into redirect value
|
7
|
+
$stderr.reopen("depdump-error.log", "w")
|
8
|
+
|
9
|
+
require "depdump"
|
10
|
+
require "optparse"
|
11
|
+
|
12
|
+
options = ARGV.getopts("o:", "f:")
|
13
|
+
|
14
|
+
Depdump.configure do |c|
|
15
|
+
c.output = File.open(options["o"], "w") if options["o"]
|
16
|
+
c.formatter = options["f"] if options["f"]
|
17
|
+
end
|
18
|
+
|
19
|
+
files = ARGV.empty? ? ["."] : ARGV
|
20
|
+
Depdump::Cli.run(files)
|
data/lib/depdump.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "depdump/configurable"
|
2
|
+
require_relative "depdump/dependency_graph"
|
3
|
+
require_relative "depdump/parser"
|
4
|
+
require_relative "depdump/registry"
|
5
|
+
require_relative "depdump/tracer"
|
6
|
+
require_relative "depdump/version"
|
7
|
+
|
8
|
+
require "parser/current"
|
9
|
+
|
10
|
+
module Depdump
|
11
|
+
include Configurable
|
12
|
+
|
13
|
+
class Cli
|
14
|
+
def self.run(files)
|
15
|
+
parser = Depdump::Parser.new.tap { |p| p.parse(files) }
|
16
|
+
graph = parser.dependency_graph
|
17
|
+
Depdump.config.output.write(graph.format)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Depdump
|
2
|
+
module Configurable
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.config
|
6
|
+
end
|
7
|
+
|
8
|
+
def config
|
9
|
+
self.class.config
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def configure
|
14
|
+
yield config
|
15
|
+
end
|
16
|
+
|
17
|
+
def config
|
18
|
+
@config ||= Configuration.new
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Configuration
|
23
|
+
attr_accessor :output
|
24
|
+
attr_reader :formatter
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@output = $stdout
|
28
|
+
@format = Depdump::DependencyGraph::Formatter::Json.new
|
29
|
+
end
|
30
|
+
|
31
|
+
def formatter=(type)
|
32
|
+
@formatter =
|
33
|
+
case type
|
34
|
+
when "json"
|
35
|
+
Depdump::DependencyGraph::Formatter::Json.new
|
36
|
+
when "table"
|
37
|
+
Depdump::DependencyGraph::Formatter::Table.new
|
38
|
+
else
|
39
|
+
raise "Unknow format: #{type}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module Depdump
|
4
|
+
class DependencyGraph
|
5
|
+
attr_reader :nodes, :edges
|
6
|
+
|
7
|
+
def initialize(tree)
|
8
|
+
@nodes = Set.new
|
9
|
+
@edges = Set.new
|
10
|
+
build(tree)
|
11
|
+
end
|
12
|
+
|
13
|
+
def build(tree)
|
14
|
+
tree.each_node do |node|
|
15
|
+
next if node == tree.root
|
16
|
+
|
17
|
+
@nodes << node
|
18
|
+
node.relations.each do |r|
|
19
|
+
referenced_namespaces = r.resolve(tree)
|
20
|
+
if referenced_namespaces
|
21
|
+
@edges << { from: node.namespaces, to: referenced_namespaces }
|
22
|
+
else
|
23
|
+
# TODO: Show file path and line no.
|
24
|
+
warn "[skip] cannot resolve: #{node.namespaces} => #{r.reference}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def format
|
31
|
+
Depdump.config.formatter.call(nodes, edges)
|
32
|
+
end
|
33
|
+
|
34
|
+
module Formatter
|
35
|
+
class Json
|
36
|
+
def call(nodes, edges)
|
37
|
+
JSON.dump({
|
38
|
+
nodes: nodes.map(&:namespaces),
|
39
|
+
edges: edges.to_a,
|
40
|
+
})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Table
|
45
|
+
def call(_nodes, edges)
|
46
|
+
rows = [
|
47
|
+
"| From | To |",
|
48
|
+
"| --- | --- |",
|
49
|
+
]
|
50
|
+
rows = rows + edges.map do |edge|
|
51
|
+
["|", edge[:from].join("::"), "|", edge[:to].join("::"), "|"].join(" ")
|
52
|
+
end
|
53
|
+
rows.join("\n")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Depdump
|
2
|
+
class Parser
|
3
|
+
attr_reader :tracer
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@tracer = Tracer.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse(files)
|
10
|
+
expand_directory(files).each do |file|
|
11
|
+
source = File.read(file)
|
12
|
+
ast = ::Parser::CurrentRuby.parse(source)
|
13
|
+
tracer.trace_node(ast)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def dependency_graph
|
18
|
+
DependencyGraph.new(tracer.registry_tree)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def expand_directory(paths)
|
24
|
+
paths.flat_map do |path|
|
25
|
+
if File.directory?(path)
|
26
|
+
Dir.glob(File.join(path, "**", "*.rb"))
|
27
|
+
else
|
28
|
+
path
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Depdump
|
2
|
+
class Registry
|
3
|
+
class Tree
|
4
|
+
def root
|
5
|
+
@root ||= Node.new(
|
6
|
+
namespaces: [],
|
7
|
+
parent: nil,
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
def find_or_create_node(namespaces, parent)
|
12
|
+
# TODO: could be cached rather than search everytime
|
13
|
+
registered = root.detect { |node| namespaces == node.namespaces }
|
14
|
+
return registered if registered
|
15
|
+
|
16
|
+
Node.new(namespaces: namespaces, parent: parent).tap { |n|
|
17
|
+
parent.children << n
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def each_node
|
22
|
+
return unless block_given?
|
23
|
+
root.each { |node| yield node }
|
24
|
+
end
|
25
|
+
|
26
|
+
def resolve(partial_namespaces, entry_node)
|
27
|
+
current_node = entry_node
|
28
|
+
prev_node = nil
|
29
|
+
resolved_node = nil
|
30
|
+
|
31
|
+
while current_node && resolved_node.nil?
|
32
|
+
resolved_node = current_node.dig(partial_namespaces)
|
33
|
+
prev_node, current_node = current_node, current_node&.parent
|
34
|
+
end
|
35
|
+
|
36
|
+
resolved_node
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Depdump
|
2
|
+
class Registry
|
3
|
+
class Tree
|
4
|
+
class Node
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_reader :namespaces, :children, :parent, :relations
|
8
|
+
|
9
|
+
def initialize(namespaces: [], parent:)
|
10
|
+
@namespaces = namespaces
|
11
|
+
@relations = []
|
12
|
+
@children = []
|
13
|
+
@parent = parent
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_relation(reference, search_entry_node: nil)
|
17
|
+
Relation.new(node: self, reference: reference, search_entry_node: search_entry_node).tap { |r|
|
18
|
+
@relations << r
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def root?
|
23
|
+
parent.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def hash
|
27
|
+
namespaces.hash
|
28
|
+
end
|
29
|
+
|
30
|
+
def eql?(other)
|
31
|
+
namespaces == other.namespaces
|
32
|
+
end
|
33
|
+
|
34
|
+
def each(&block)
|
35
|
+
return unless block_given?
|
36
|
+
|
37
|
+
children.each do |child|
|
38
|
+
yield child
|
39
|
+
child.each(&block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def dig(partial_namespaces)
|
44
|
+
found = nil
|
45
|
+
|
46
|
+
children.each do |node|
|
47
|
+
exactly_match = node.namespaces.last(partial_namespaces.size) == partial_namespaces
|
48
|
+
found = node and break if exactly_match
|
49
|
+
|
50
|
+
route_match = node.namespaces.last == partial_namespaces.first
|
51
|
+
if route_match
|
52
|
+
found = node.dig(partial_namespaces[1..-1])
|
53
|
+
break if found
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
found
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Depdump
|
2
|
+
class Registry
|
3
|
+
class Tree
|
4
|
+
class Relation
|
5
|
+
attr_reader :node, :reference
|
6
|
+
|
7
|
+
def initialize(node:, reference:, search_entry_node: nil)
|
8
|
+
@node = node
|
9
|
+
@reference = reference
|
10
|
+
@search_entry_node = search_entry_node || node
|
11
|
+
end
|
12
|
+
|
13
|
+
def resolve(tree)
|
14
|
+
resolved_node = tree.resolve(reference, @search_entry_node)
|
15
|
+
resolved_node&.namespaces
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Depdump
|
2
|
+
class Tracer
|
3
|
+
attr_reader :classes, :relations, :registry_tree
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@registry_tree = Registry::Tree.new
|
7
|
+
@context = @registry_tree.root
|
8
|
+
end
|
9
|
+
|
10
|
+
def trace_node(node, namespaces = [])
|
11
|
+
return unless node.respond_to?(:type)
|
12
|
+
|
13
|
+
case node.type
|
14
|
+
when :class, :module
|
15
|
+
trace_class(node, namespaces)
|
16
|
+
when :const
|
17
|
+
trace_const(node, namespaces)
|
18
|
+
else
|
19
|
+
node.children.map { |n| trace_node(n, namespaces) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def trace_class(node, namespaces)
|
26
|
+
definition_node = node.children.first
|
27
|
+
|
28
|
+
# definition_node.type should be :const (otherwise syntax error occurs)
|
29
|
+
defined_namespaces = expand_const_namespaces(definition_node, namespaces)
|
30
|
+
|
31
|
+
# Assume as top level definition is the rest of the array after last nil (cbase) appeared
|
32
|
+
# e.g.) [nil, :A, nil, :B] => [:B]
|
33
|
+
if cbase_index = defined_namespaces.rindex(nil)
|
34
|
+
namespaces_size_from_top = defined_namespaces.size - (cbase_index + 1)
|
35
|
+
defined_namespaces = defined_namespaces.last(namespaces_size_from_top)
|
36
|
+
end
|
37
|
+
|
38
|
+
stack_context(defined_namespaces) do
|
39
|
+
node.children[1..-1].each { |n| trace_node(n, defined_namespaces) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def trace_const(node, namespaces)
|
44
|
+
referenced_namespaces = expand_const_namespaces(node, [])
|
45
|
+
|
46
|
+
if referenced_namespaces.first.nil?
|
47
|
+
# Top level nil is inserted when :cbase appeared
|
48
|
+
@context.create_relation(referenced_namespaces[1..-1], search_entry_node: @registry_tree.root)
|
49
|
+
else
|
50
|
+
@context.create_relation(referenced_namespaces)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def stack_context(namespaces)
|
55
|
+
prev_context = @context
|
56
|
+
@context = @registry_tree.find_or_create_node(namespaces, prev_context)
|
57
|
+
|
58
|
+
yield
|
59
|
+
|
60
|
+
@context = prev_context
|
61
|
+
end
|
62
|
+
|
63
|
+
# returns [nil] if node.type is :cbase
|
64
|
+
def expand_const_namespaces(node, namespaces)
|
65
|
+
valid_definition = node.respond_to?(:type) && [:const, :cbase].include?(node.type)
|
66
|
+
return namespaces unless valid_definition
|
67
|
+
|
68
|
+
maybe_qualifing_node = node.children.first
|
69
|
+
qualified_namespaces = expand_const_namespaces(maybe_qualifing_node, namespaces)
|
70
|
+
qualified_namespaces + [node.children.last]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: depdump
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- upinetree
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.1.a
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.1.a
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A dump tool of ruby class/module dependencies.
|
56
|
+
email:
|
57
|
+
- upinetree@gmail.com
|
58
|
+
executables:
|
59
|
+
- depdump
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- depdump.gemspec
|
72
|
+
- exe/depdump
|
73
|
+
- lib/depdump.rb
|
74
|
+
- lib/depdump/configurable.rb
|
75
|
+
- lib/depdump/dependency_graph.rb
|
76
|
+
- lib/depdump/parser.rb
|
77
|
+
- lib/depdump/registry.rb
|
78
|
+
- lib/depdump/registry/tree.rb
|
79
|
+
- lib/depdump/registry/tree/node.rb
|
80
|
+
- lib/depdump/registry/tree/relation.rb
|
81
|
+
- lib/depdump/tracer.rb
|
82
|
+
- lib/depdump/version.rb
|
83
|
+
homepage: https://github.com/upinetree/depdump
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata:
|
87
|
+
homepage_uri: https://github.com/upinetree/depdump
|
88
|
+
source_code_uri: https://github.com/upinetree/depdump
|
89
|
+
changelog_uri: https://github.com/upinetree/depdump/commits/master
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.0.3
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: A dump tool of ruby class/module dependencies.
|
109
|
+
test_files: []
|