Class: Yoker::CLI::Base

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/yoker/cli/base.rb

Direct Known Subclasses

Init, Update

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



14
15
16
# File 'lib/yoker/cli/base.rb', line 14

def self.source_root
  Yoker.template_path
end

Instance Method Details

#append_to_file(path, content) ⇒ Object



54
55
56
57
# File 'lib/yoker/cli/base.rb', line 54

def append_to_file(path, content)
  File.open(path, "a") { |f| f.write(content) }
  info "Updated #{path}"
end

#create_file(path, content, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yoker/cli/base.rb', line 42

def create_file(path, content, options = {})
  return if File.exist?(path) && !options[:force] && !prompt.yes?("File #{path} exists. Overwrite?")

  # Create directory if it doesn't exist
  dir = File.dirname(path)
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)

  # Write the file
  File.write(path, content)
  success "Created #{path}"
end

#template(source, destination, context = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yoker/cli/base.rb', line 18

def template(source, destination, context = {})
  # Read the template file
  template_path = File.join(self.class.source_root, source)

  unless File.exist?(template_path)
    error "Template not found: #{template_path}"
    return
  end

  # Create the binding with the context
  template_content = File.read(template_path)

  # Use ERB to render the template
  require "erb"
  require "ostruct"

  # Convert hash to OpenStruct for easier access in templates
  binding_context = context.is_a?(Hash) ? OpenStruct.new(context) : context
  rendered = ERB.new(template_content, trim_mode: "-").result(binding_context.instance_eval { binding })

  # Write the file
  create_file(destination, rendered, force: true)
end