Class: KDirector::Directors::BaseDirector
- Inherits:
-
Object
- Object
- KDirector::Directors::BaseDirector
show all
- Includes:
- KLog::Logging
- Defined in:
- lib/k_director/directors/base_director.rb
Overview
Base Director is paired with the ActionsBuilder and provides a base on which to build code generation directors.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#active? ⇒ Boolean
-
#add(output_file, **opts) ⇒ Object
Add a single file into the code base.
-
#add_file(file, **opts) ⇒ Object
Add a file to target folder.
-
#blueprint(**opts, &block) ⇒ Object
-
#configuration ⇒ Object
-
#data(name = nil, **opts) ⇒ Object
-
#debug ⇒ Object
-
#debug_dom ⇒ Object
-
#debug_options ⇒ Object
-
#director_name ⇒ Object
-
#director_name=(name) ⇒ Object
-
#dom ⇒ Object
-
#fadd(name, **opts) ⇒ Object
-
#github(**opts, &block) ⇒ Object
-
#inherited_opts(**opts) ⇒ Object
Used by child directors to inherit options from parent.
-
#initialize(k_builder, builder, **opts) ⇒ BaseDirector
constructor
A new instance of BaseDirector.
-
#oadd(name, **opts) ⇒ Object
-
#on_action ⇒ Object
-
#on_exist ⇒ Object
-
#package_json(**opts, &block) ⇒ Object
-
#play_actions ⇒ Object
play any un-played actions.
-
#run_command(command) ⇒ Object
Run a command using shell, this is useful with command line tools.
-
#run_script(script) ⇒ Object
Run a command using Open3.capture2, can be used in place of run_command but is also useful with multiline scripts.
-
#set_current_folder_action(folder_key) ⇒ Object
(also: #cd)
Set current target folder rubocop:disable Naming/AccessorMethodName.
-
#settings(**opts) ⇒ Object
-
#tadd(name, **opts) ⇒ Object
-
#template_base_folder ⇒ Object
-
#typed_dom ⇒ Object
Constructor Details
#initialize(k_builder, builder, **opts) ⇒ BaseDirector
Returns a new instance of BaseDirector.
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/k_director/directors/base_director.rb', line 36
def initialize(k_builder, builder, **opts)
@k_builder = k_builder
@builder = builder
@options = OpenStruct.new(**opts)
@options.director_name ||= default_director_name
@options.template_base_folder ||= default_template_base_folder
@options.on_exist ||= :skip
@options.on_action ||= :queue
@options.active = true unless defined?(@options.active)
end
|
Instance Attribute Details
#builder ⇒ Object
Returns the value of attribute builder.
32
33
34
|
# File 'lib/k_director/directors/base_director.rb', line 32
def builder
@builder
end
|
#k_builder ⇒ Object
Returns the value of attribute k_builder.
33
34
35
|
# File 'lib/k_director/directors/base_director.rb', line 33
def k_builder
@k_builder
end
|
#options ⇒ Object
Returns the value of attribute options.
34
35
36
|
# File 'lib/k_director/directors/base_director.rb', line 34
def options
@options
end
|
Class Method Details
.builder_type ⇒ Object
21
22
23
24
25
|
# File 'lib/k_director/directors/base_director.rb', line 21
def builder_type
return @builder_type if defined? @builder_type
@builder_type = KDirector::Builders::ActionsBuilder
end
|
.default_builder_type(type) ⇒ Object
27
28
29
|
# File 'lib/k_director/directors/base_director.rb', line 27
def default_builder_type(type)
@builder_type = type
end
|
.init(k_builder, builder = nil, **opts) ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/k_director/directors/base_director.rb', line 11
def init(k_builder, builder = nil, **opts)
if builder.nil?
builder = builder_type.new
else
builder.reset
end
new(k_builder, builder, **opts)
end
|
Instance Method Details
#active? ⇒ Boolean
82
83
84
|
# File 'lib/k_director/directors/base_director.rb', line 82
def active?
@options.active == true
end
|
#add(output_file, **opts) ⇒ Object
Add a single file into the code base
This is a wrapper around add_file that will add the file to the codebase using template path rules
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/k_director/directors/base_director.rb', line 114
def add(output_file, **opts)
template_file = opts[:template_file] || output_file
template_parts = [template_base_folder, opts[:template_subfolder], template_file].reject(&:blank?)
template_path = File.join(*template_parts)
opts[:template_file] = template_path
add_file(output_file, **opts)
end
|
#add_file(file, **opts) ⇒ Object
Add a file to target folder
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/k_director/directors/base_director.rb', line 138
def add_file(file, **opts)
opts = {
on_exist: on_exist
}.merge(opts)
opts[:dom] = dom.except(:actions) if dom
handle_action(k_builder.add_file_action(file, **opts))
self
end
|
#blueprint(**opts, &block) ⇒ Object
197
198
199
200
201
202
|
# File 'lib/k_director/directors/base_director.rb', line 197
def blueprint(**opts, &block)
blueprint = KDirector::Dsls::Children::Blueprint.new(self, **opts)
blueprint.instance_eval(&block) if blueprint.active? && block_given?
self
end
|
#configuration ⇒ Object
78
79
80
|
# File 'lib/k_director/directors/base_director.rb', line 78
def configuration
k_builder.configuration
end
|
#data(name = nil, **opts) ⇒ Object
48
49
50
51
52
|
# File 'lib/k_director/directors/base_director.rb', line 48
def data(name = nil, **opts)
KDirector::Directors::Data.new(self, name, **opts)
self
end
|
#debug ⇒ Object
204
205
206
207
|
# File 'lib/k_director/directors/base_director.rb', line 204
def debug
debug_options
debug_dom
end
|
#debug_dom ⇒ Object
221
222
223
224
225
226
227
|
# File 'lib/k_director/directors/base_director.rb', line 221
def debug_dom
log.section_heading 'DOM'
builder.debug
nil
end
|
#debug_options ⇒ Object
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/k_director/directors/base_director.rb', line 209
def debug_options
log.section_heading director_name
h = options.to_h.sort.to_h
h.each_key do |key|
log.kv(titleize.parse(key.to_s), h[key])
end
nil
end
|
#director_name ⇒ Object
86
87
88
|
# File 'lib/k_director/directors/base_director.rb', line 86
def director_name
@options.director_name
end
|
#director_name=(name) ⇒ Object
90
91
92
|
# File 'lib/k_director/directors/base_director.rb', line 90
def director_name=(name)
@options.director_name = name
end
|
#dom ⇒ Object
60
61
62
|
# File 'lib/k_director/directors/base_director.rb', line 60
def dom
builder.dom
end
|
#fadd(name, **opts) ⇒ Object
133
134
135
|
# File 'lib/k_director/directors/base_director.rb', line 133
def fadd(name, **opts)
add(name, **{ on_exist: :write }.merge(opts))
end
|
#github(**opts, &block) ⇒ Object
183
184
185
186
187
188
|
# File 'lib/k_director/directors/base_director.rb', line 183
def github(**opts, &block)
github = KDirector::Dsls::Children::Github.new(self, **opts)
github.instance_eval(&block) if github.active? && block_given?
self
end
|
#inherited_opts(**opts) ⇒ Object
Used by child directors to inherit options from parent
69
70
71
72
73
74
75
76
|
# File 'lib/k_director/directors/base_director.rb', line 69
def inherited_opts(**opts)
{
on_exist: @options.on_exist,
on_action: @options.on_action,
template_base_folder: @options.template_base_folder,
active: @options.active
}.merge(opts)
end
|
#oadd(name, **opts) ⇒ Object
125
126
127
|
# File 'lib/k_director/directors/base_director.rb', line 125
def oadd(name, **opts)
add(name, **{ open: true }.merge(opts))
end
|
#on_action ⇒ Object
102
103
104
|
# File 'lib/k_director/directors/base_director.rb', line 102
def on_action
@options.on_action
end
|
#on_exist ⇒ Object
98
99
100
|
# File 'lib/k_director/directors/base_director.rb', line 98
def on_exist
@options.on_exist
end
|
#package_json(**opts, &block) ⇒ Object
190
191
192
193
194
195
|
# File 'lib/k_director/directors/base_director.rb', line 190
def package_json(**opts, &block)
package_json = KDirector::Dsls::Children::PackageJson.new(self, **opts)
package_json.instance_eval(&block) if package_json.active? && block_given?
self
end
|
#play_actions ⇒ Object
play any un-played actions
177
178
179
|
# File 'lib/k_director/directors/base_director.rb', line 177
def play_actions
k_builder.play_actions(builder.actions)
end
|
#run_command(command) ⇒ Object
Run a command using shell, this is useful with command line tools
162
163
164
165
166
|
# File 'lib/k_director/directors/base_director.rb', line 162
def run_command(command)
handle_action(k_builder.run_command_action(command))
self
end
|
#run_script(script) ⇒ Object
Run a command using Open3.capture2, can be used in place of run_command but is also useful with multiline scripts
170
171
172
173
174
|
# File 'lib/k_director/directors/base_director.rb', line 170
def run_script(script)
handle_action(k_builder.run_script_action(script))
self
end
|
#set_current_folder_action(folder_key) ⇒ Object
Also known as:
cd
Set current target folder rubocop:disable Naming/AccessorMethodName
152
153
154
155
156
157
|
# File 'lib/k_director/directors/base_director.rb', line 152
def set_current_folder_action(folder_key)
run_action(k_builder.set_current_folder_action(folder_key))
self
end
|
#settings(**opts) ⇒ Object
54
55
56
57
58
|
# File 'lib/k_director/directors/base_director.rb', line 54
def settings(**opts)
KDirector::Directors::Data.new(self, :settings, **opts)
self
end
|
#tadd(name, **opts) ⇒ Object
129
130
131
|
# File 'lib/k_director/directors/base_director.rb', line 129
def tadd(name, **opts)
add(name, **{ open_template: true }.merge(opts))
end
|
#template_base_folder ⇒ Object
94
95
96
|
# File 'lib/k_director/directors/base_director.rb', line 94
def template_base_folder
@options.template_base_folder
end
|
#typed_dom ⇒ Object
64
65
66
|
# File 'lib/k_director/directors/base_director.rb', line 64
def typed_dom
builder.build
end
|