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)


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

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



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)

  # 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



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

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



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

#configurationObject



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

#debugObject



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_domObject



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_optionsObject



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|
    # requires k_funky
    log.kv(titleize.parse(key.to_s), h[key])
  end

  nil
end

#director_nameObject



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

#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



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

Common child directors



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_domObject



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_actionObject



108
109
110
# File 'lib/k_director/directors/base_director.rb', line 108

def on_action
  @options.on_action
end

#on_existObject



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_actionsObject

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 (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



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_folderObject



100
101
102
# File 'lib/k_director/directors/base_director.rb', line 100

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