js_dependency 0.2.3.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a59fced735fccc952d4f31d5dc27e41637ef5ee67bc5f5b7eb8038f5458808c
4
- data.tar.gz: b4e713344b1a58d6dac1143068a20cf1e43bad0158c50c4ef28c1e8cd0b7ddc8
3
+ metadata.gz: df7ebbe8ac02b295b1ec9d1f9829e7e94868933af1c935f1f5094f4e74f1e876
4
+ data.tar.gz: 388b4b017e4b1986b86f221a0bdc19490a9a9c8ca13eaea0847d032e094d6c31
5
5
  SHA512:
6
- metadata.gz: f2432b1e03352673908567cfc7f0ec5798bae146201db548c48899b1601e6d79680a3bef33daec2b82fa00bc541ff0b099757d65ded91f9801f026dcc5d52a56
7
- data.tar.gz: 9f952a5dc433c9e687c12505dbb47136cbf8da671f5981cf008d1f002d282c3195f14976a94a911364c581adc55f36c68cb1e0d0459961328a49fa44b5e8bc4f
6
+ metadata.gz: 753bccf5874fbe9edd2784d6a999393c13bac53d1da213c861c4d6c4f93fa48d70c6c7bebacaa863aa894e3a38174f2d3a547809c8ee1d6181f793d76d949ab6
7
+ data.tar.gz: e449209ee8d7eaa9328db4b94cd28ef61352ace72e776d4297572a385d45bc8e2f5267dd05a638797f04b84268efd6cb61784ca6f4df939053d7172f7d7da8b0
data/.rubocop_todo.yml CHANGED
@@ -1,37 +1,32 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2022-07-20 23:10:21 UTC using RuboCop version 1.31.2.
3
+ # on 2022-07-24 12:55:17 UTC using RuboCop version 1.31.2.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 6
9
+ # Offense count: 4
10
10
  # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
11
11
  Metrics/AbcSize:
12
12
  Max: 37
13
13
 
14
- # Offense count: 6
14
+ # Offense count: 4
15
15
  # Configuration parameters: IgnoredMethods.
16
16
  Metrics/CyclomaticComplexity:
17
17
  Max: 15
18
18
 
19
- # Offense count: 8
19
+ # Offense count: 7
20
20
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
21
21
  Metrics/MethodLength:
22
22
  Max: 26
23
23
 
24
- # Offense count: 1
25
- # Configuration parameters: CountComments, CountAsOne.
26
- Metrics/ModuleLength:
27
- Max: 114
28
-
29
24
  # Offense count: 3
30
25
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
31
26
  Metrics/ParameterLists:
32
27
  Max: 9
33
28
 
34
- # Offense count: 6
29
+ # Offense count: 4
35
30
  # Configuration parameters: IgnoredMethods.
36
31
  Metrics/PerceivedComplexity:
37
32
  Max: 16
@@ -42,7 +37,12 @@ Style/IfUnlessModifier:
42
37
  Exclude:
43
38
  - 'lib/js_dependency/index_creator.rb'
44
39
 
45
- # Offense count: 2
40
+ # Offense count: 1
41
+ Style/MultilineBlockChain:
42
+ Exclude:
43
+ - 'lib/js_dependency/mermaid/root.rb'
44
+
45
+ # Offense count: 1
46
46
  # This cop supports safe autocorrection (--autocorrect).
47
47
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
48
48
  # URISchemes: http, https
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2022-07-24
4
+
5
+ - Stable mermaid support.
6
+ - Stable `excludes` option.
7
+ - Refactor JsDependency class method using `JsDependency::TargetPathname`.
8
+
3
9
  ## [0.2.3.1] - 2022-07-21
4
10
 
5
11
  - Revert for script tag with line brake.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- js_dependency (0.2.3.1)
4
+ js_dependency (0.3.0)
5
5
  pathname
6
6
  thor
7
7
  yaml
@@ -78,7 +78,7 @@ module JsDependency
78
78
  output_path: output_path,
79
79
  alias_paths: alias_paths,
80
80
  excludes: excludes
81
- ).sort.uniq.join("\n")
81
+ ).join("\n")
82
82
 
83
83
  puts str
84
84
  end
@@ -113,7 +113,7 @@ module JsDependency
113
113
  output_path: output_path,
114
114
  alias_paths: alias_paths,
115
115
  excludes: excludes
116
- ).sort.uniq.join("\n")
116
+ ).join("\n")
117
117
 
118
118
  puts str
119
119
  end
@@ -12,8 +12,11 @@ module JsDependency
12
12
  @alias_paths = alias_paths
13
13
  end
14
14
 
15
- def self.call(src, alias_paths: nil)
16
- new(src, alias_paths: alias_paths).call
15
+ def self.call(src, alias_paths: nil, excludes: nil)
16
+ index = new(src, alias_paths: alias_paths).call
17
+ index.transform_values do |value|
18
+ value.reject { |path| excludes&.any? { |exclude| path.include?(exclude) } }
19
+ end
17
20
  end
18
21
 
19
22
  def call
@@ -6,29 +6,26 @@ module JsDependency
6
6
  attr_reader :parent, :child
7
7
 
8
8
  def initialize(parent, child)
9
- @parent = parent
10
- @child = child
9
+ @parent = Pathname.new(parent)
10
+ @child = Pathname.new(child)
11
11
  end
12
12
 
13
13
  def parent_module_name(level = 0)
14
- parse(Pathname.new(@parent), level).join("/")
14
+ mermaid_str(@parent, level)
15
15
  end
16
16
 
17
17
  def child_module_name(level = 0)
18
- parse(Pathname.new(@child), level).join("/")
18
+ mermaid_str(@child, level)
19
19
  end
20
20
 
21
21
  private
22
22
 
23
- def parse(pathname, level)
24
- return [pathname.to_s] unless pathname.exist?
23
+ def mermaid_str(pathname, level = 0)
24
+ "#{parse(pathname).join("_")}[\"#{parse(pathname, level).join("/")}\"]"
25
+ end
25
26
 
26
- array = [pathname.basename]
27
- level.times do
28
- array.unshift(pathname.parent.basename.to_s)
29
- pathname = pathname.parent
30
- end
31
- array
27
+ def parse(pathname, level = -1)
28
+ pathname.each_filename.with_object([]) { |filename, array| array << filename }.reverse[0..level].reverse
32
29
  end
33
30
  end
34
31
  end
@@ -15,9 +15,20 @@ module JsDependency
15
15
  @list << NodesLink.new(parent, child)
16
16
  end
17
17
 
18
- def export(name_level: 1)
18
+ def export(name_level: 1, src_path: nil)
19
+ nodes_links = if src_path
20
+ src_pathname = Pathname.new(src_path).realpath
21
+ @list.map do |nodes_link|
22
+ NodesLink.new(nodes_link.parent.exist? ? nodes_link.parent.relative_path_from(src_pathname.to_s) : nodes_link.parent.to_s,
23
+ nodes_link.child.exist? ? nodes_link.child.relative_path_from(src_pathname.to_s) : nodes_link.child.to_s)
24
+ end
25
+ else
26
+ @list
27
+ end
19
28
  str = "flowchart #{orientation}\n"
20
- str + @list.uniq { |link| "#{link.parent}__#{link.child}" }.sort_by { |link| "#{link.parent}__#{link.child}" }.map do |link|
29
+ str + nodes_links.uniq do |link|
30
+ "#{link.parent}__#{link.child}"
31
+ end.sort_by { |link| "#{link.parent}__#{link.child}" }.map do |link|
21
32
  "#{link.parent_module_name(name_level)} --> #{link.child_module_name(name_level)}"
22
33
  end.join("\n")
23
34
  end
@@ -13,5 +13,13 @@ module JsDependency
13
13
 
14
14
  pathname
15
15
  end
16
+
17
+ def self.to_target_pathname(target_path)
18
+ if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
19
+ Pathname.new(target_path).realpath
20
+ else
21
+ Pathname.new(target_path)
22
+ end
23
+ end
16
24
  end
17
25
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ class TargetPathname
5
+ # @param [String] target_path
6
+ def initialize(target_path)
7
+ @pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
8
+ Pathname.new(target_path).realpath
9
+ else
10
+ Pathname.new(target_path)
11
+ end
12
+ end
13
+
14
+ # @param [Integer] analyze_level
15
+ # @param [Hash] index
16
+ def each_parent_path(analyze_level, index)
17
+ temp_paths = [@pathname.to_s]
18
+ analyze_level.times do
19
+ list = []
20
+ temp_paths.each do |temp_path|
21
+ temp_pathname = to_target_pathname(temp_path)
22
+
23
+ list += extract_parent_paths(temp_pathname.to_s, index).each do |parent_path|
24
+ yield parent_path, temp_pathname.to_s
25
+ end
26
+ end
27
+ temp_paths = list
28
+ end
29
+ end
30
+
31
+ # @param [Integer] analyze_level
32
+ # @param [Hash] index
33
+ def each_child_path(analyze_level, index)
34
+ temp_paths = [@pathname.to_s]
35
+ analyze_level.times do
36
+ list = []
37
+ temp_paths.each do |temp_path|
38
+ temp_pathname = to_target_pathname(temp_path)
39
+
40
+ list += extract_children_paths(temp_pathname.to_s, index).each do |child_path|
41
+ yield temp_pathname.to_s, child_path
42
+ end
43
+ end
44
+ temp_paths = list
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ # @param [String] target_path
51
+ # @param [Hash] index
52
+ # @return [Array]
53
+ def extract_parent_paths(target_path, index)
54
+ index.each_with_object([]) do |(parent, children), list|
55
+ list << parent if children.any?(target_path)
56
+ end
57
+ end
58
+
59
+ # @param [String] target_path
60
+ # @param [Hash] index
61
+ # @return [Array]
62
+ def extract_children_paths(target_path, index)
63
+ index[target_path] || []
64
+ end
65
+
66
+ def to_target_pathname(target_path)
67
+ if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
68
+ Pathname.new(target_path).realpath
69
+ else
70
+ Pathname.new(target_path)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsDependency
4
- VERSION = "0.2.3.1"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/js_dependency.rb CHANGED
@@ -4,11 +4,20 @@ require_relative "js_dependency/version"
4
4
  require_relative "js_dependency/index_creator"
5
5
  require_relative "js_dependency/mermaid/root"
6
6
  require_relative "js_dependency/cli"
7
+ require_relative "js_dependency/target_pathname"
7
8
  require "pathname"
8
9
 
9
10
  module JsDependency
10
11
  class Error < StandardError; end
11
12
 
13
+ # @param [String] src_path
14
+ # @param [Hash, nil] alias_paths
15
+ # @param [Array, nil] excludes
16
+ # @return [Hash]
17
+ def self.export_index(src_path, alias_paths: nil, excludes: nil)
18
+ JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
19
+ end
20
+
12
21
  # @param [String] src_path
13
22
  # @param [String] target_path
14
23
  # @param [String] orientation
@@ -21,146 +30,70 @@ module JsDependency
21
30
  # @return [String]
22
31
  def self.export_mermaid(src_path, target_path, orientation: "LR", alias_paths: nil, child_analyze_level: 1, parent_analyze_level: 1, name_level: 1, output_path: nil, excludes: nil)
23
32
  output_pathname = Pathname.new(output_path) if output_path
24
- index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
33
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
25
34
 
26
- target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
27
- Pathname.new(target_path).realpath
28
- else
29
- Pathname.new(target_path)
30
- end
35
+ target_pathname = JsDependency::TargetPathname.new(target_path)
31
36
 
32
- root = JsDependency::Mermaid::Root.new(orientation)
37
+ mermaid_root = JsDependency::Mermaid::Root.new(orientation)
33
38
 
34
- parents_paths(target_pathname, parent_analyze_level, index, excludes: excludes) do |parent_path, child_path|
35
- root.add(parent_path, child_path)
39
+ target_pathname.each_parent_path(parent_analyze_level, index) do |parent_path, child_path|
40
+ mermaid_root.add(parent_path, child_path)
36
41
  end
37
42
 
38
- children_paths(target_pathname, child_analyze_level, index, excludes: excludes) do |parent_path, child_path|
39
- root.add(parent_path, child_path)
43
+ target_pathname.each_child_path(child_analyze_level, index) do |parent_path, child_path|
44
+ mermaid_root.add(parent_path, child_path)
40
45
  end
41
46
 
42
- output = root.export(name_level: name_level)
47
+ output = mermaid_root.export(name_level: name_level, src_path: src_path)
43
48
  output_pathname&.write(output)
44
49
  output
45
50
  end
46
51
 
52
+ # @param [String] src_path
53
+ # @param [String] target_path
54
+ # @param [String] orientation
55
+ # @param [Hash, nil] alias_paths
56
+ # @param [Integer] parent_analyze_level
57
+ # @param [String, nil] output_path
58
+ # @param [Array, nil] excludes
59
+ # @return [Array<Pathname>]
47
60
  def self.parents(src_path, target_path, alias_paths: nil, parent_analyze_level: 1, output_path: nil, excludes: nil)
48
61
  output_pathname = Pathname.new(output_path) if output_path
49
- index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
62
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
50
63
 
51
- target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
52
- Pathname.new(target_path).realpath
53
- else
54
- Pathname.new(target_path)
55
- end
64
+ target_pathname = JsDependency::TargetPathname.new(target_path)
56
65
  list = []
57
- parents_paths(target_pathname, parent_analyze_level, index, excludes: excludes) do |parent_path, _child_path|
66
+ target_pathname.each_parent_path(parent_analyze_level, index) do |parent_path, _child_path|
58
67
  list << parent_path
59
68
  end
60
- output = list.uniq
69
+ output = list.uniq.sort.map do |path|
70
+ Pathname.new(path).relative_path_from(Pathname.new(src_path).realpath.to_s)
71
+ end
61
72
  output_pathname&.write(output.sort.join("\n"))
62
73
  output
63
74
  end
64
75
 
76
+ # @param [String] src_path
77
+ # @param [String] target_path
78
+ # @param [String] orientation
79
+ # @param [Hash, nil] alias_paths
80
+ # @param [Integer] child_analyze_level
81
+ # @param [String, nil] output_path
82
+ # @param [Array, nil] excludes
83
+ # @return [Array<Pathname>]
65
84
  def self.children(src_path, target_path, alias_paths: nil, child_analyze_level: 1, output_path: nil, excludes: nil)
66
85
  output_pathname = Pathname.new(output_path) if output_path
67
- index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
86
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
68
87
 
69
- target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
70
- Pathname.new(target_path).realpath
71
- else
72
- Pathname.new(target_path)
73
- end
88
+ target_pathname = JsDependency::TargetPathname.new(target_path)
74
89
  list = []
75
- children_paths(target_pathname, child_analyze_level, index, excludes: excludes) do |_parent_path, child_path|
90
+ target_pathname.each_child_path(child_analyze_level, index) do |_parent_path, child_path|
76
91
  list << child_path
77
92
  end
78
- output = list.uniq
93
+ output = list.uniq.sort.map do |path|
94
+ Pathname.new(path).relative_path_from(Pathname.new(src_path).realpath.to_s)
95
+ end
79
96
  output_pathname&.write(output.sort.join("\n"))
80
97
  output
81
98
  end
82
-
83
- # @param [String] target_path
84
- # @param [Hash] index
85
- # @return [Array]
86
- def self.extract_parent_paths(target_path, index)
87
- target_pathname = if Pathname.new(target_path).exist?
88
- Pathname.new(target_path).realpath
89
- else
90
- Pathname.new(target_path)
91
- end
92
- index.each_with_object([]) do |(parent, children), list|
93
- list << parent if children.any?(target_pathname.to_s)
94
- end
95
- end
96
-
97
- private_class_method :extract_parent_paths
98
-
99
- # @param [String] target_path
100
- # @param [Hash] index
101
- # @return [Array]
102
- def self.extract_children_paths(target_path, index)
103
- target_pathname = if Pathname.new(target_path).exist?
104
- Pathname.new(target_path).realpath
105
- else
106
- Pathname.new(target_path)
107
- end
108
- index[target_pathname.to_s] || []
109
- end
110
-
111
- private_class_method :extract_children_paths
112
-
113
- # @param [String] src
114
- # @param [nil, Hash] alias_paths
115
- def self.create_index(src_path, alias_paths: nil)
116
- JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
117
- end
118
-
119
- private_class_method :create_index
120
-
121
- def self.parents_paths(target_pathname, analyze_level, index, excludes: nil)
122
- temp_paths = [target_pathname.to_s]
123
- analyze_level.times do
124
- list = []
125
- temp_paths.each do |temp_path|
126
- temp_pathname = if Pathname.new(temp_path).relative? && Pathname.new(temp_path).exist?
127
- Pathname.new(temp_path).realpath
128
- else
129
- Pathname.new(temp_path)
130
- end
131
-
132
- list += extract_parent_paths(temp_pathname.to_s, index).each do |parent_path|
133
- next if excludes&.any? { |ignore| parent_path.to_s.include?(ignore) || temp_pathname.to_s.include?(ignore) }
134
-
135
- yield parent_path, temp_pathname.to_s
136
- end
137
- end
138
- temp_paths = list
139
- end
140
- end
141
-
142
- private_class_method :parents_paths
143
-
144
- def self.children_paths(target_pathname, analyze_level, index, excludes: nil)
145
- temp_paths = [target_pathname.to_s]
146
- analyze_level.times do
147
- list = []
148
- temp_paths.each do |temp_path|
149
- temp_pathname = if Pathname.new(temp_path).relative? && Pathname.new(temp_path).exist?
150
- Pathname.new(temp_path).realpath
151
- else
152
- Pathname.new(temp_path)
153
- end
154
-
155
- list += extract_children_paths(temp_pathname.to_s, index).each do |child_path|
156
- next if excludes&.any? { |ignore| child_path.to_s.include?(ignore) || temp_pathname.to_s.include?(ignore) }
157
-
158
- yield temp_pathname.to_s, child_path
159
- end
160
- end
161
- temp_paths = list
162
- end
163
- end
164
-
165
- private_class_method :children_paths
166
99
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_dependency
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - junara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-21 00:00:00.000000000 Z
11
+ date: 2022-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pathname
@@ -166,6 +166,7 @@ files:
166
166
  - lib/js_dependency/mermaid/root.rb
167
167
  - lib/js_dependency/pathname_utility.rb
168
168
  - lib/js_dependency/replace_path_alias.rb
169
+ - lib/js_dependency/target_pathname.rb
169
170
  - lib/js_dependency/version.rb
170
171
  - sig/js_dependency.rbs
171
172
  homepage: https://github.com/junara/js_dependency