yaml_ref_resolver 0.4.1 → 0.4.2
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/README.md +2 -2
- data/lib/yaml_ref_resolver/ref.rb +24 -0
- data/lib/yaml_ref_resolver/version.rb +1 -1
- data/lib/yaml_ref_resolver/yaml.rb +36 -0
- data/lib/yaml_ref_resolver.rb +19 -47
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b15959cdb30b6bd1175233f3c88618cd02654ace
|
4
|
+
data.tar.gz: 11dab92dca480e42528fdcaa75018099dd70fa32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dbe62b28a440a9be12fe06546a4f34f7d7df1b091d22297ea4c9427febbba7dbc7d1b4e721bb5d4ef330e1cfba430a9b5638198cd948bcf36fa62081b3f4ae5
|
7
|
+
data.tar.gz: 4e196c70f1a6711faad5bcc72b4539fb18c3c67797a8084a8ad1307e51c15b89adbf65a0eb6766fc0443a2387f5d2f5be7c248ee2d958884bfa3ebd687ebbdd3
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[](https://badge.fury.io/rb/yaml_ref_resolver)
|
4
4
|
[](https://travis-ci.org/Joe-noh/yaml_ref_resolver)
|
5
5
|
|
6
|
-
This is a gem that resolves `$ref` references to other YAML files.
|
6
|
+
This is a gem that resolves and expands `$ref` references to other YAML files.
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -61,7 +61,7 @@ Optionally output file path with `-o` or `--output` can be given.
|
|
61
61
|
$ yaml_ref_resolver -i ./path/to/index.yaml -o ./path/to/output.yaml
|
62
62
|
```
|
63
63
|
|
64
|
-
`-w` or `--watch`
|
64
|
+
`-w` or `--watch` option watches referenced yaml files and dump whole resolved yaml when one of them changed.
|
65
65
|
|
66
66
|
```console
|
67
67
|
$ yaml_ref_resolver -i ./path/to/index.yaml -w
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class YamlRefResolver::Ref
|
2
|
+
def initialize(target, base_path)
|
3
|
+
@base_dir = File.dirname(base_path)
|
4
|
+
|
5
|
+
if target.start_with?("#")
|
6
|
+
@target_file = nil
|
7
|
+
@target_level = target.sub("#", "")
|
8
|
+
elsif !target.include?("#")
|
9
|
+
@target_file = target
|
10
|
+
@target_level = "/"
|
11
|
+
else
|
12
|
+
@target_file, @target_level = target.split("#")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def abs_path
|
17
|
+
File.expand_path(@target_file, @base_dir)
|
18
|
+
end
|
19
|
+
|
20
|
+
def target_keys
|
21
|
+
@target_level.split("/").reject {|s| s == "" }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "yaml_ref_resolver/ref"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
class YamlRefResolver::Yaml
|
5
|
+
attr_reader :content
|
6
|
+
|
7
|
+
def initialize(path:, key: '$ref')
|
8
|
+
@filepath = path
|
9
|
+
@key = key
|
10
|
+
@content = YAML.load_file(path)
|
11
|
+
end
|
12
|
+
|
13
|
+
def refs
|
14
|
+
find_refs(@content).flatten.compact.map do |ref|
|
15
|
+
YamlRefResolver::Ref.new(ref, @filepath)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def find_refs(obj)
|
22
|
+
return find_refs_hash(obj) if obj.is_a? Hash
|
23
|
+
return find_refs_array(obj) if obj.is_a? Array
|
24
|
+
return []
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_refs_hash(hash)
|
28
|
+
hash.inject([]) do |acc, (key, val)|
|
29
|
+
acc << (key == @key ? val : find_refs(val))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_refs_array(array)
|
34
|
+
array.map {|elem| find_refs(elem) }
|
35
|
+
end
|
36
|
+
end
|
data/lib/yaml_ref_resolver.rb
CHANGED
@@ -1,58 +1,39 @@
|
|
1
1
|
require "yaml_ref_resolver/version"
|
2
|
-
require "yaml"
|
2
|
+
require "yaml_ref_resolver/yaml"
|
3
3
|
|
4
4
|
class YamlRefResolver
|
5
5
|
def initialize(opts = {})
|
6
6
|
@key = opts[:key] || '$ref'
|
7
|
-
@
|
7
|
+
@map = {}
|
8
8
|
end
|
9
9
|
|
10
10
|
def resolve(path)
|
11
11
|
entry_point = File.expand_path(path)
|
12
|
-
|
12
|
+
preload(entry_point)
|
13
13
|
|
14
|
-
resolve_refs(@
|
14
|
+
resolve_refs(@map[entry_point].content, entry_point)
|
15
15
|
end
|
16
16
|
|
17
17
|
def reload(path)
|
18
|
-
@
|
19
|
-
|
18
|
+
@map.delete(path)
|
19
|
+
preload(path)
|
20
20
|
end
|
21
21
|
|
22
22
|
def files
|
23
|
-
@
|
23
|
+
@map.keys
|
24
24
|
end
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
-
def
|
29
|
-
return if @
|
28
|
+
def preload(abs_path)
|
29
|
+
return if @map.has_key?(abs_path)
|
30
30
|
|
31
|
-
@
|
32
|
-
|
33
|
-
|
34
|
-
preload_ref_yamls(File.expand_path(rel_path, File.dirname(abs_path)))
|
31
|
+
@map[abs_path] = Yaml.new(path: abs_path, key: @key)
|
32
|
+
@map[abs_path].refs.each do |ref|
|
33
|
+
preload(ref.abs_path)
|
35
34
|
end
|
36
35
|
end
|
37
36
|
|
38
|
-
def find_refs(obj)
|
39
|
-
do_find_refs(obj).flatten.compact
|
40
|
-
end
|
41
|
-
|
42
|
-
def do_find_refs(obj)
|
43
|
-
return do_find_refs_hash(obj) if obj.is_a? Hash
|
44
|
-
return do_find_refs_array(obj) if obj.is_a? Array
|
45
|
-
return []
|
46
|
-
end
|
47
|
-
|
48
|
-
def do_find_refs_hash(hash)
|
49
|
-
hash.inject([]) {|acc, (key, val)| acc << (key == @key ? val : do_find_refs(val)) }
|
50
|
-
end
|
51
|
-
|
52
|
-
def do_find_refs_array(array)
|
53
|
-
array.map {|e| find_refs(e) }
|
54
|
-
end
|
55
|
-
|
56
37
|
def resolve_refs(obj, referrer)
|
57
38
|
return resolve_hash(obj, referrer) if obj.is_a? Hash
|
58
39
|
return resolve_array(obj, referrer) if obj.is_a? Array
|
@@ -62,14 +43,15 @@ class YamlRefResolver
|
|
62
43
|
def resolve_hash(hash, referrer)
|
63
44
|
resolved = hash.map do |key, val|
|
64
45
|
if key == @key
|
65
|
-
|
66
|
-
|
67
|
-
|
46
|
+
ref = Ref.new(val, referrer)
|
47
|
+
|
48
|
+
ref_path = ref.abs_path
|
49
|
+
target_keys = ref.target_keys
|
68
50
|
|
69
|
-
if
|
70
|
-
resolve_refs(@
|
51
|
+
if target_keys.size == 0
|
52
|
+
resolve_refs(@map[ref_path].content, ref_path)
|
71
53
|
else
|
72
|
-
resolve_refs(@
|
54
|
+
resolve_refs(@map[ref_path].content.dig(*target_keys), ref_path)
|
73
55
|
end
|
74
56
|
else
|
75
57
|
Hash[key, resolve_refs(val, referrer)]
|
@@ -82,14 +64,4 @@ class YamlRefResolver
|
|
82
64
|
def resolve_array(array, referrer)
|
83
65
|
array.map {|e| resolve_refs(e, referrer) }
|
84
66
|
end
|
85
|
-
|
86
|
-
def parse_ref(ref)
|
87
|
-
splitted = ref.split('#')
|
88
|
-
|
89
|
-
if splitted.size == 1
|
90
|
-
splitted << '/'
|
91
|
-
else
|
92
|
-
splitted
|
93
|
-
end
|
94
|
-
end
|
95
67
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml_ref_resolver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe-noh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: filewatcher
|
@@ -99,7 +99,9 @@ files:
|
|
99
99
|
- exe/yaml_ref_resolver
|
100
100
|
- lib/yaml_ref_resolver.rb
|
101
101
|
- lib/yaml_ref_resolver/cli.rb
|
102
|
+
- lib/yaml_ref_resolver/ref.rb
|
102
103
|
- lib/yaml_ref_resolver/version.rb
|
104
|
+
- lib/yaml_ref_resolver/yaml.rb
|
103
105
|
- yaml_ref_resolver.gemspec
|
104
106
|
homepage: https://github.com/Joe-noh/yaml_ref_resolver
|
105
107
|
licenses:
|