Class: Rubysmith::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysmith/builder.rb

Overview

Provides common functionality necessary for all builders.

Constant Summary collapse

HELPERS =
{inserter: Text::Inserter, renderer: Renderers::ERB, executor: Open3}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, helpers: HELPERS) ⇒ Builder

Returns a new instance of Builder.



18
19
20
21
22
# File 'lib/rubysmith/builder.rb', line 18

def initialize(configuration, helpers: HELPERS, **)
  super(**)
  @configuration = configuration
  @helpers = helpers
end

Class Method Details

.callObject



16
# File 'lib/rubysmith/builder.rb', line 16

def self.call(...) = new(...)

Instance Method Details

#append(content) ⇒ Object



24
25
26
27
28
# File 'lib/rubysmith/builder.rb', line 24

def append content
  log_debug "Appending content to: #{relative_build_path}"
  build_path.rewrite { |body| body + content }
  self
end

#checkObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/rubysmith/builder.rb', line 30

def check
  path = build_path

  if path.exist?
    log_error "Path exists: #{path}."
    kernel.abort
  else
    log_debug "Checked: #{path}."
  end
end

#deleteObject



41
42
43
44
45
# File 'lib/rubysmith/builder.rb', line 41

def delete
  log_debug "Deleting: #{relative_build_path}"
  build_path.delete
  self
end

#insert_after(pattern, content) ⇒ Object



47
48
49
50
51
# File 'lib/rubysmith/builder.rb', line 47

def insert_after pattern, content
  log_debug "Inserting content after pattern in: #{relative_build_path}"
  build_path.write inserter.new(build_path.readlines, :after).call(content, pattern).join
  self
end

#insert_before(pattern, content) ⇒ Object



53
54
55
56
57
# File 'lib/rubysmith/builder.rb', line 53

def insert_before pattern, content
  log_debug "Inserting content before pattern in: #{relative_build_path}"
  build_path.write inserter.new(build_path.readlines, :before).call(content, pattern).join
  self
end

#make_pathObject



59
60
61
62
63
# File 'lib/rubysmith/builder.rb', line 59

def make_path
  log_debug "Creating path: #{relative_build_path}"
  build_path.make_path
  self
end

#permit(mode) ⇒ Object



65
66
67
68
69
# File 'lib/rubysmith/builder.rb', line 65

def permit mode
  log_debug "Changing permissions for: #{relative_build_path}"
  build_path.chmod mode
  self
end

#prepend(content) ⇒ Object



71
72
73
74
75
# File 'lib/rubysmith/builder.rb', line 71

def prepend content
  log_debug "Prepending content to: #{relative_build_path}"
  build_path.rewrite { |body| content + body }
  self
end

#rename(name) ⇒ Object



77
78
79
80
81
# File 'lib/rubysmith/builder.rb', line 77

def rename name
  log_debug "Renaming: #{build_path.basename} to #{name}"
  build_path.rename build_path.parent.join(name)
  self
end

#renderObject



83
84
85
86
87
88
89
90
91
# File 'lib/rubysmith/builder.rb', line 83

def render
  log_debug "Rendering: #{relative_build_path}"

  pathway.start_path.read.then do |content|
    build_path.make_ancestors.write renderer.call(content)
  end

  self
end

#replace(pattern, content) ⇒ Object



93
94
95
96
97
# File 'lib/rubysmith/builder.rb', line 93

def replace pattern, content
  log_debug "Replacing content for patterns in: #{relative_build_path}"
  build_path.rewrite { |body| body.gsub pattern, content }
  self
end

#run(*command) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/rubysmith/builder.rb', line 99

def run *command
  log_debug "Running: #{command}"
  execute(*command)
  self
rescue StandardError => error
  log_error error and self
end

#touchObject



107
108
109
110
111
# File 'lib/rubysmith/builder.rb', line 107

def touch
  log_debug "Touching: #{relative_build_path}"
  build_path.deep_touch
  self
end