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_clipboard(**opts) ⇒ Object
(also: #clipboard_copy, #clipboard)
Add content to the clipboard.
-
#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.
-
#json_dom ⇒ Object
-
#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
25
26
27
28
29
|
# File 'lib/k_director/directors/base_director.rb', line 25
def builder_type
return @builder_type if defined? @builder_type
@builder_type = KDirector::Builders::ActionsBuilder
end
|
.default_builder_type(type) ⇒ Object
21
22
23
|
# File 'lib/k_director/directors/base_director.rb', line 21
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
88
89
90
|
# File 'lib/k_director/directors/base_director.rb', line 88
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
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/k_director/directors/base_director.rb', line 120
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_clipboard(**opts) ⇒ Object
Also known as:
clipboard_copy, clipboard
Add content to the clipboard
Extra options will be used as data for templates, e.g
177
178
179
180
181
182
|
# File 'lib/k_director/directors/base_director.rb', line 177
def add_clipboard(**opts)
run_action(k_builder.add_clipboard_action(**opts))
self
end
|
#add_file(file, **opts) ⇒ Object
Add a file to target folder
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/k_director/directors/base_director.rb', line 144
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
222
223
224
225
226
227
|
# File 'lib/k_director/directors/base_director.rb', line 222
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
84
85
86
|
# File 'lib/k_director/directors/base_director.rb', line 84
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
229
230
231
232
233
234
|
# File 'lib/k_director/directors/base_director.rb', line 229
def debug
debug_options
debug_dom
self
end
|
#debug_dom ⇒ Object
248
249
250
251
252
253
254
|
# File 'lib/k_director/directors/base_director.rb', line 248
def debug_dom
log.section_heading 'DOM'
builder.debug
nil
end
|
#debug_options ⇒ Object
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/k_director/directors/base_director.rb', line 236
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
92
93
94
|
# File 'lib/k_director/directors/base_director.rb', line 92
def director_name
@options.director_name
end
|
#director_name=(name) ⇒ Object
96
97
98
|
# File 'lib/k_director/directors/base_director.rb', line 96
def director_name=(name)
@options.director_name = name
end
|
#dom ⇒ Object
60
61
62
63
64
|
# File 'lib/k_director/directors/base_director.rb', line 60
def dom
return builder.dom if defined?(builder.dom)
nil
end
|
#fadd(name, **opts) ⇒ Object
139
140
141
|
# File 'lib/k_director/directors/base_director.rb', line 139
def fadd(name, **opts)
add(name, **{ on_exist: :write }.merge(opts))
end
|
#github(**opts, &block) ⇒ Object
208
209
210
211
212
213
|
# File 'lib/k_director/directors/base_director.rb', line 208
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
75
76
77
78
79
80
81
82
|
# File 'lib/k_director/directors/base_director.rb', line 75
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
|
#json_dom ⇒ Object
70
71
72
|
# File 'lib/k_director/directors/base_director.rb', line 70
def json_dom
builder.to_json
end
|
#oadd(name, **opts) ⇒ Object
131
132
133
|
# File 'lib/k_director/directors/base_director.rb', line 131
def oadd(name, **opts)
add(name, **{ open: true }.merge(opts))
end
|
#on_action ⇒ Object
108
109
110
|
# File 'lib/k_director/directors/base_director.rb', line 108
def on_action
@options.on_action
end
|
#on_exist ⇒ Object
104
105
106
|
# File 'lib/k_director/directors/base_director.rb', line 104
def on_exist
@options.on_exist
end
|
#package_json(**opts, &block) ⇒ Object
215
216
217
218
219
220
|
# File 'lib/k_director/directors/base_director.rb', line 215
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
202
203
204
|
# File 'lib/k_director/directors/base_director.rb', line 202
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
187
188
189
190
191
|
# File 'lib/k_director/directors/base_director.rb', line 187
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
195
196
197
198
199
|
# File 'lib/k_director/directors/base_director.rb', line 195
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
158
159
160
161
162
163
|
# File 'lib/k_director/directors/base_director.rb', line 158
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
135
136
137
|
# File 'lib/k_director/directors/base_director.rb', line 135
def tadd(name, **opts)
add(name, **{ open_template: true }.merge(opts))
end
|
#template_base_folder ⇒ Object
100
101
102
|
# File 'lib/k_director/directors/base_director.rb', line 100
def template_base_folder
@options.template_base_folder
end
|
#typed_dom ⇒ Object
66
67
68
|
# File 'lib/k_director/directors/base_director.rb', line 66
def typed_dom
builder.build
end
|