Class: KDirector::Directors::BaseDirector

Inherits:
Object
  • Object
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

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       # %i[skip write compare]
  @options.on_action            ||= :queue      # %i[queue execute]
  @options.active = true unless defined?(@options.active)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



32
33
34
# File 'lib/k_director/directors/base_director.rb', line 32

def builder
  @builder
end

#k_builderObject (readonly)

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

#optionsObject (readonly)

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_typeObject



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

Returns:

  • (Boolean)


90
91
92
# File 'lib/k_director/directors/base_director.rb', line 90

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

Parameters:

  • output_filename (String)

    The output file name, this can be a relative path

  • **opts (Hash)

    The options

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :template_filename (String)

    Template filename can be set or it will default to the same value as the output file name

  • :template_subfolder (String)

    Template subfolder



122
123
124
125
126
127
128
129
130
131
# File 'lib/k_director/directors/base_director.rb', line 122

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)

  # maybe template_file should be renamed to template_path in k_builder
  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

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :content (String)

    Supply the content that you want to write to the file

  • :template (String)

    Supply the template that you want to write to the file, template will be processed (‘nobody’) From address

  • :content_file (String)

    File with content, file location is based on where the program is running

  • :template_file (String)

    File with handlebars templated content that will be transformed, file location is based on the configured template_path

  • :to (String)

    Recipient email

  • :body (String)

    The email’s body



179
180
181
182
183
184
# File 'lib/k_director/directors/base_director.rb', line 179

def add_clipboard(**opts)
  # RUN (not handle), current folder effects subsequent actions and so it needs to be executed straight away.
  run_action(k_builder.add_clipboard_action(**opts))

  self
end

#add_file(file, **opts) ⇒ Object

Add a file to target folder



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/k_director/directors/base_director.rb', line 146

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



224
225
226
227
228
229
# File 'lib/k_director/directors/base_director.rb', line 224

def blueprint(**opts, &block)
  blueprint = KDirector::Dsls::Children::Blueprint.new(self, **opts)
  blueprint.instance_eval(&block) if blueprint.active? && block_given?

  self
end

#configurationObject



86
87
88
# File 'lib/k_director/directors/base_director.rb', line 86

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

#debugObject



231
232
233
234
235
236
# File 'lib/k_director/directors/base_director.rb', line 231

def debug
  debug_options
  debug_dom

  self
end

#debug_domObject



250
251
252
253
254
255
256
# File 'lib/k_director/directors/base_director.rb', line 250

def debug_dom
  log.section_heading 'DOM'

  builder.debug

  nil
end

#debug_optionsObject



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/k_director/directors/base_director.rb', line 238

def debug_options
  log.section_heading director_name

  h = options.to_h.sort.to_h
  h.each_key do |key|
    # requires k_funky
    log.kv(titleize.parse(key.to_s), h[key])
  end

  nil
end

#director_nameObject



94
95
96
# File 'lib/k_director/directors/base_director.rb', line 94

def director_name
  @options.director_name
end

#director_name=(name) ⇒ Object



98
99
100
# File 'lib/k_director/directors/base_director.rb', line 98

def director_name=(name)
  @options.director_name = name
end

#domObject



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



141
142
143
# File 'lib/k_director/directors/base_director.rb', line 141

def fadd(name, **opts)
  add(name, **{ on_exist: :write    }.merge(opts))
end

#github(**opts, &block) ⇒ Object

Common child directors



210
211
212
213
214
215
# File 'lib/k_director/directors/base_director.rb', line 210

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



77
78
79
80
81
82
83
84
# File 'lib/k_director/directors/base_director.rb', line 77

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_domObject



70
71
72
73
74
# File 'lib/k_director/directors/base_director.rb', line 70

def json_dom
  return '{}' if dom.nil?

  JSON.pretty_generate(dom)
end

#oadd(name, **opts) ⇒ Object



133
134
135
# File 'lib/k_director/directors/base_director.rb', line 133

def oadd(name, **opts)
  add(name, **{ open: true          }.merge(opts))
end

#on_actionObject



110
111
112
# File 'lib/k_director/directors/base_director.rb', line 110

def on_action
  @options.on_action
end

#on_existObject



106
107
108
# File 'lib/k_director/directors/base_director.rb', line 106

def on_exist
  @options.on_exist
end

#package_json(**opts, &block) ⇒ Object



217
218
219
220
221
222
# File 'lib/k_director/directors/base_director.rb', line 217

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_actionsObject

play any un-played actions



204
205
206
# File 'lib/k_director/directors/base_director.rb', line 204

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



189
190
191
192
193
# File 'lib/k_director/directors/base_director.rb', line 189

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



197
198
199
200
201
# File 'lib/k_director/directors/base_director.rb', line 197

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



160
161
162
163
164
165
# File 'lib/k_director/directors/base_director.rb', line 160

def set_current_folder_action(folder_key)
  # RUN (not handle), current folder effects subsequent actions and so it needs to be executed straight away.
  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



137
138
139
# File 'lib/k_director/directors/base_director.rb', line 137

def tadd(name, **opts)
  add(name, **{ open_template: true }.merge(opts))
end

#template_base_folderObject



102
103
104
# File 'lib/k_director/directors/base_director.rb', line 102

def template_base_folder
  @options.template_base_folder
end

#typed_domObject



66
67
68
# File 'lib/k_director/directors/base_director.rb', line 66

def typed_dom
  builder.build
end