dtk-dsl 1.0.0
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 +7 -0
- data/.byebug_history +70 -0
- data/.gitignore +2 -0
- data/Gemfile +2 -0
- data/README.md +2 -0
- data/dtk-dsl.gemspec +16 -0
- data/lib/dsl/directory_generator.rb +27 -0
- data/lib/dsl/directory_parser/path_info.rb +62 -0
- data/lib/dsl/directory_parser.rb +33 -0
- data/lib/dsl/dsl_version.rb +36 -0
- data/lib/dsl/error/subclasses.rb +38 -0
- data/lib/dsl/error.rb +24 -0
- data/lib/dsl/file_generator/content_input/array.rb +31 -0
- data/lib/dsl/file_generator/content_input/hash.rb +53 -0
- data/lib/dsl/file_generator/content_input/mixin.rb +68 -0
- data/lib/dsl/file_generator/content_input/string.rb +31 -0
- data/lib/dsl/file_generator/content_input/tags.rb +87 -0
- data/lib/dsl/file_generator/content_input.rb +29 -0
- data/lib/dsl/file_generator/yaml_object.rb +24 -0
- data/lib/dsl/file_generator.rb +39 -0
- data/lib/dsl/file_obj.rb +119 -0
- data/lib/dsl/file_parser/input/array.rb +27 -0
- data/lib/dsl/file_parser/input/hash.rb +40 -0
- data/lib/dsl/file_parser/input.rb +29 -0
- data/lib/dsl/file_parser/output.rb +24 -0
- data/lib/dsl/file_parser.rb +71 -0
- data/lib/dsl/file_type.rb +79 -0
- data/lib/dsl/input_output_common/array.rb +43 -0
- data/lib/dsl/input_output_common/canonical/array.rb +27 -0
- data/lib/dsl/input_output_common/canonical/hash.rb +69 -0
- data/lib/dsl/input_output_common/canonical/hash_key.rb +54 -0
- data/lib/dsl/input_output_common/canonical.rb +28 -0
- data/lib/dsl/input_output_common/hash.rb +44 -0
- data/lib/dsl/input_output_common/output_class_mixin.rb +34 -0
- data/lib/dsl/input_output_common/semantic_parse/hash.rb +33 -0
- data/lib/dsl/input_output_common/semantic_parse/mixin.rb +44 -0
- data/lib/dsl/input_output_common/semantic_parse.rb +26 -0
- data/lib/dsl/input_output_common.rb +60 -0
- data/lib/dsl/qualified_key.rb +55 -0
- data/lib/dsl/template/constant_class_mixin.rb +91 -0
- data/lib/dsl/template/generation/class_mixin.rb +40 -0
- data/lib/dsl/template/generation/mixin.rb +134 -0
- data/lib/dsl/template/generation.rb +26 -0
- data/lib/dsl/template/loader.rb +66 -0
- data/lib/dsl/template/parsing/class_mixin.rb +67 -0
- data/lib/dsl/template/parsing/mixin.rb +214 -0
- data/lib/dsl/template/parsing/parent_key.rb +82 -0
- data/lib/dsl/template/parsing/parsing_error/subclasses.rb +62 -0
- data/lib/dsl/template/parsing/parsing_error.rb +52 -0
- data/lib/dsl/template/parsing.rb +29 -0
- data/lib/dsl/template/v1/assembly.rb +82 -0
- data/lib/dsl/template/v1/attribute/semantic_parse.rb +26 -0
- data/lib/dsl/template/v1/attribute.rb +75 -0
- data/lib/dsl/template/v1/common_module.rb +38 -0
- data/lib/dsl/template/v1/common_module_summary.rb +42 -0
- data/lib/dsl/template/v1/component/semantic_parse.rb +26 -0
- data/lib/dsl/template/v1/component.rb +121 -0
- data/lib/dsl/template/v1/dependency.rb +63 -0
- data/lib/dsl/template/v1/module_ref.rb +62 -0
- data/lib/dsl/template/v1/node/semantic_parse.rb +62 -0
- data/lib/dsl/template/v1/node.rb +72 -0
- data/lib/dsl/template/v1/node_attribute/semantic_parse.rb +26 -0
- data/lib/dsl/template/v1/node_attribute.rb +25 -0
- data/lib/dsl/template/v1/service_instance.rb +50 -0
- data/lib/dsl/template/v1/service_module_summary.rb +37 -0
- data/lib/dsl/template/v1/workflow/semantic_parse.rb +26 -0
- data/lib/dsl/template/v1/workflow.rb +65 -0
- data/lib/dsl/template/v1.rb +43 -0
- data/lib/dsl/template.rb +110 -0
- data/lib/dsl/version.rb +6 -0
- data/lib/dsl/yaml_helper.rb +56 -0
- data/lib/dtk_dsl.rb +36 -0
- metadata +114 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class InputOutputCommon
|
20
|
+
class Hash < ::Hash
|
21
|
+
def initialize(parent_class, hash = nil)
|
22
|
+
super()
|
23
|
+
replace(reify(parent_class, hash)) if hash
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def reify(parent_class, obj)
|
29
|
+
if obj.kind_of?(self.class)
|
30
|
+
obj
|
31
|
+
elsif obj.kind_of?(::Hash)
|
32
|
+
obj.inject(self.class.new(parent_class)) { |h, (k, v)| h.merge(k => reify(parent_class, v)) }
|
33
|
+
elsif obj.kind_of?(::Array)
|
34
|
+
parent_class::Array.new(parent_class, obj)
|
35
|
+
else
|
36
|
+
obj
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class InputOutputCommon
|
20
|
+
module OutputClassMixin
|
21
|
+
# opts can have keys
|
22
|
+
# :output_type
|
23
|
+
# :input
|
24
|
+
# In both cases an empty object is created using :output_type or type of :input to determine its type
|
25
|
+
def create(opts = {})
|
26
|
+
unless opts[:output_type] or opts[:input]
|
27
|
+
raise Error, "opts must have one of the keys :output_type or :input"
|
28
|
+
end
|
29
|
+
obj_type = opts[:output_type] || obj_type(opts[:input])
|
30
|
+
create_aux(obj_type)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class InputOutputCommon
|
20
|
+
module SemanticParse
|
21
|
+
class Hash < Canonical::Hash
|
22
|
+
include Mixin
|
23
|
+
# opts can have keys
|
24
|
+
# :qualified_key
|
25
|
+
def initialize(parent_class, opts = {})
|
26
|
+
super(parent_class)
|
27
|
+
initialize_semantic_parse(opts)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class InputOutputCommon
|
20
|
+
module SemanticParse
|
21
|
+
module Mixin
|
22
|
+
# opts can have keys
|
23
|
+
# :qualified_key
|
24
|
+
def initialize_semantic_parse(opts = {})
|
25
|
+
@qualified_key = opts[:qualified_key]
|
26
|
+
end
|
27
|
+
private :initialize_semantic_parse
|
28
|
+
|
29
|
+
def qualified_key
|
30
|
+
@qualified_key || fail(Error, "Unexepcetd that @qualified_key is nil")
|
31
|
+
end
|
32
|
+
|
33
|
+
def name
|
34
|
+
qualified_key.relative_distinguished_name
|
35
|
+
end
|
36
|
+
|
37
|
+
def qualified_name
|
38
|
+
qualified_key.print_form
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class InputOutputCommon
|
20
|
+
module SemanticParse
|
21
|
+
require_relative('semantic_parse/mixin')
|
22
|
+
# mixin needs to go before hash
|
23
|
+
require_relative('semantic_parse/hash')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
# Base classes for getting input from file to map to canonical form and to store canonical form that
|
20
|
+
# is used for generation
|
21
|
+
class InputOutputCommon
|
22
|
+
require_relative('input_output_common/output_class_mixin')
|
23
|
+
require_relative('input_output_common/hash')
|
24
|
+
require_relative('input_output_common/array')
|
25
|
+
# hash and array must go before canonical
|
26
|
+
require_relative('input_output_common/canonical')
|
27
|
+
# semantic_parse needs to go before canonical
|
28
|
+
require_relative('input_output_common/semantic_parse')
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def self.obj_type(obj)
|
33
|
+
if obj.kind_of?(::Hash)
|
34
|
+
:hash
|
35
|
+
elsif obj.kind_of?(::Array)
|
36
|
+
:array
|
37
|
+
elsif obj.kind_of?(::String)
|
38
|
+
:string
|
39
|
+
else
|
40
|
+
raise Error, "Unexpected type '#{obj.class}'"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.create_aux(type, obj = nil)
|
45
|
+
case type
|
46
|
+
when :hash
|
47
|
+
# if obj.class not ::Hash then reified already
|
48
|
+
(obj.nil? or obj.class == ::Hash) ? self::Hash.new(self, obj) : obj
|
49
|
+
when :array
|
50
|
+
# if obj.class not ::Array then reified already
|
51
|
+
(obj.nil? or obj.class == ::Array) ? self::Array.new(self, obj) : obj
|
52
|
+
when :string
|
53
|
+
# no reification for string
|
54
|
+
::String.new(obj || '')
|
55
|
+
else
|
56
|
+
raise Error, "Unexpected type '#{type}'"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK
|
19
|
+
module DSL
|
20
|
+
class QualifiedKey
|
21
|
+
Element = Struct.new(:type, :key)
|
22
|
+
def initialize(elements_to_copy = [])
|
23
|
+
@key_elements = copy(elements_to_copy)
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_with_new_element?(type, key)
|
27
|
+
new_key_elements = key.nil? ? @key_elements : @key_elements + [Element.new(type, key)]
|
28
|
+
self.class.new(new_key_elements)
|
29
|
+
end
|
30
|
+
|
31
|
+
QUALIFIED_KEY_DELIM = '/'
|
32
|
+
def print_form
|
33
|
+
@key_elements.inject('') do |s, el|
|
34
|
+
s.empty? ? el.key : s + QUALIFIED_KEY_DELIM + el.key
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def relative_distinguished_name
|
39
|
+
unless last = @key_elements.last
|
40
|
+
fail Error, "Unexpected that @key_elements is empty"
|
41
|
+
end
|
42
|
+
last.key
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def copy(key_elements)
|
48
|
+
ret = []
|
49
|
+
key_elements.each { |el| ret << Element.new(el.type, el.key) }
|
50
|
+
ret
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class Template
|
20
|
+
# assumption is that where this included could have a Variations module
|
21
|
+
module ClassMixin
|
22
|
+
module Constant
|
23
|
+
def matches?(object, constant)
|
24
|
+
unless object.nil?
|
25
|
+
variations = variations(constant)
|
26
|
+
if object.is_a?(Hash)
|
27
|
+
hash_value_of_matching_key?(object, variations)
|
28
|
+
elsif object.is_a?(String) || object.is_a?(Symbol)
|
29
|
+
variations.include?(object.to_s)
|
30
|
+
else
|
31
|
+
fail Error.new("Unexpected object class (#{object.class})")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def matching_key_and_value?(hash, constant)
|
37
|
+
variations = variations(constant)
|
38
|
+
if matching_key = hash_key_if_match?(hash, variations)
|
39
|
+
{ matching_key => hash[matching_key] }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def all_string_variations(*constants)
|
44
|
+
constants.flat_map { |constant| variations(constant, string_only: true) }.uniq
|
45
|
+
end
|
46
|
+
|
47
|
+
def its_legal_values(constant)
|
48
|
+
single_or_set = variations(constant, string_only: true)
|
49
|
+
if single_or_set.is_a?(Array)
|
50
|
+
"its legal values are: #{single_or_set.join(',')}"
|
51
|
+
else
|
52
|
+
"its legal value is: #{single_or_set}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def canonical_value(constant)
|
57
|
+
# self. is important beacuse want to evalute wrt to class that calls this
|
58
|
+
begin
|
59
|
+
self.const_get(constant.to_s)
|
60
|
+
rescue
|
61
|
+
raise Error, "Illegal Input parsing constant '#{constant}'"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def variations(constant, opts = {})
|
68
|
+
# use of self:: and self. are important because want to evalute wrt to module that pulls this in
|
69
|
+
variations = self::Variations.const_get(constant.to_s)
|
70
|
+
string_variations = variations.map(&:to_s)
|
71
|
+
opts[:string_only] ? string_variations : string_variations + variations.map(&:to_sym)
|
72
|
+
rescue
|
73
|
+
# if Variations not defined
|
74
|
+
term = canonical_value(constant)
|
75
|
+
opts[:string_only] ? [term.to_s] : [term.to_s, term.to_sym]
|
76
|
+
end
|
77
|
+
|
78
|
+
def hash_key_if_match?(hash, variations)
|
79
|
+
variations.find { |key| hash.key?(key) }
|
80
|
+
end
|
81
|
+
|
82
|
+
def hash_value_of_matching_key?(hash, variations)
|
83
|
+
if matching_key = hash_key_if_match?(hash, variations)
|
84
|
+
hash[matching_key]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class Template
|
20
|
+
module Generation
|
21
|
+
module ClassMixin
|
22
|
+
# Main template-specific generate call; Concrete classes overwrite this
|
23
|
+
def generate_elements(_content_elements, _parent)
|
24
|
+
raise Error::NoMethodForConcreteClass.new(self)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def generate_element(content, parent)
|
30
|
+
create_for_generation(content, :filter => parent.filter).generate_yaml_object
|
31
|
+
end
|
32
|
+
|
33
|
+
def generate_element?(content, parent)
|
34
|
+
create_for_generation(content, :filter => parent.filter).generate_yaml_object?
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class Template
|
20
|
+
module Generation
|
21
|
+
module Mixin
|
22
|
+
# Main template-specific generate call; Concrete classes overwrite this
|
23
|
+
def generate!
|
24
|
+
raise Error::NoMethodForConcreteClass.new(self.class)
|
25
|
+
end
|
26
|
+
|
27
|
+
# This is overwritten if template can conditionally generate elements
|
28
|
+
def generate?
|
29
|
+
generate!
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :filter
|
33
|
+
|
34
|
+
# opts can have keys
|
35
|
+
# :content (required)
|
36
|
+
# :filter
|
37
|
+
def generation_initialize(opts = {})
|
38
|
+
unless content = opts[:content]
|
39
|
+
raise Error, "Unexpected that opts[:content] is nil"
|
40
|
+
end
|
41
|
+
@content = content
|
42
|
+
@filter = opts[:filter]
|
43
|
+
@yaml_object = empty_yaml_object(content)
|
44
|
+
end
|
45
|
+
private :generation_initialize
|
46
|
+
|
47
|
+
# generate_yaml_object? can be ovewritten
|
48
|
+
def generate_yaml_object?
|
49
|
+
generate?
|
50
|
+
is_empty?(@yaml_object) ? nil : @yaml_object
|
51
|
+
end
|
52
|
+
|
53
|
+
def generate_yaml_object
|
54
|
+
generate!
|
55
|
+
@yaml_object
|
56
|
+
end
|
57
|
+
|
58
|
+
def generate_yaml_text
|
59
|
+
self.generate!
|
60
|
+
YamlHelper.generate(@yaml_object)
|
61
|
+
end
|
62
|
+
|
63
|
+
# The methods yaml_object_type can be set on concrete class; it wil be set if input and output types are different
|
64
|
+
def yaml_object_type
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def generate_child(parse_template_type, content)
|
71
|
+
if content.nil?
|
72
|
+
nil
|
73
|
+
else
|
74
|
+
template_class(parse_template_type).create_for_generation(content, :filter => @filter).generate_yaml_object
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def generate_child_elements(parse_template_type, content_elements)
|
79
|
+
unless content_elements.nil?
|
80
|
+
template_class(parse_template_type).generate_elements(content_elements, self)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def empty_yaml_object(content_input)
|
85
|
+
if self.yaml_object_type
|
86
|
+
FileGenerator::YamlObject.create(:output_type => yaml_object_type)
|
87
|
+
else
|
88
|
+
FileGenerator::YamlObject.create(:input => content_input)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def set_generation_hash(hash, constant, val)
|
93
|
+
unless val.nil? or is_empty?(val)
|
94
|
+
hash[canonical_key(constant)] = val
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def is_empty?(obj)
|
99
|
+
obj.respond_to?(:empty?) and obj.empty?
|
100
|
+
end
|
101
|
+
|
102
|
+
def skip_for_generation?
|
103
|
+
@content.skip_for_generation?
|
104
|
+
end
|
105
|
+
|
106
|
+
def generation_set(constant, val)
|
107
|
+
set_generation_hash(@yaml_object, constant, val)
|
108
|
+
end
|
109
|
+
|
110
|
+
def generation_set_scalar(scalar)
|
111
|
+
@yaml_object = scalar
|
112
|
+
end
|
113
|
+
|
114
|
+
def generation_merge(hash)
|
115
|
+
@yaml_object.merge!(hash)
|
116
|
+
end
|
117
|
+
|
118
|
+
def generation_add(array_element)
|
119
|
+
@yaml_object << array_element
|
120
|
+
end
|
121
|
+
|
122
|
+
def generation_val(key)
|
123
|
+
@content.val(key)
|
124
|
+
end
|
125
|
+
|
126
|
+
def generation_req(key)
|
127
|
+
@content.req(key)
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module DTK::DSL
|
19
|
+
class Template
|
20
|
+
module Generation
|
21
|
+
require_relative('generation/mixin')
|
22
|
+
require_relative('generation/class_mixin')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2010-2016 dtk contributors
|
3
|
+
#
|
4
|
+
# This file is part of the dtk project.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
module DTK::DSL
|
20
|
+
class Template
|
21
|
+
class Loader
|
22
|
+
TEMPLATE_VERSIONS = [1]
|
23
|
+
|
24
|
+
# opts can have keys
|
25
|
+
# :dsl_version
|
26
|
+
# :template_version
|
27
|
+
def self.template_class(template_type, opts = {})
|
28
|
+
template_version = opts[:template_version] || template_version(opts[:dsl_version])
|
29
|
+
load_version(template_version) unless version_loaded?(template_version)
|
30
|
+
template_class_aux(template_type, template_version)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.version_loaded?(template_version)
|
36
|
+
@loaded_versions and @loaded_versions.include?(template_version)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.template_version(_dsl_version)
|
40
|
+
# TODO: when have multiple versions thn want a mapping between
|
41
|
+
# dsl version and template version, which could also be per template type
|
42
|
+
# (i.e., same dsl version can map to different template versions depending on template_type)
|
43
|
+
raise Error, "Unsupported when have multiple template versions" unless TEMPLATE_VERSIONS.size == 1
|
44
|
+
TEMPLATE_VERSIONS.first
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.load_version(template_version)
|
48
|
+
require_relative("v#{template_version}")
|
49
|
+
(@loaded_versions ||= []) << template_version
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.template_class_aux(template_type, template_version)
|
53
|
+
base_class = Template.const_get("V#{template_version}")
|
54
|
+
begin
|
55
|
+
base_class.const_get(::DTK::Common::Aux.snake_to_camel_case(template_type.to_s))
|
56
|
+
rescue
|
57
|
+
raise Error, "Invalid template_type '#{template_type}'"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|