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
# File 'lib/rubysmith/builder.rb', line 30

def check
  build_path.then do |path|
    path.exist? ? logger.abort("Path exists: #{path}.") : log_debug("Checked: #{path}.")
  end
end

#deleteObject



36
37
38
39
40
# File 'lib/rubysmith/builder.rb', line 36

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

#insert_after(pattern, content) ⇒ Object



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

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



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

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



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

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

#permit(mode) ⇒ Object



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

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

#prepend(content) ⇒ Object



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

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

#rename(name) ⇒ Object



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

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

#renderObject



78
79
80
81
82
83
84
85
86
# File 'lib/rubysmith/builder.rb', line 78

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



88
89
90
91
92
# File 'lib/rubysmith/builder.rb', line 88

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



94
95
96
97
98
99
100
# File 'lib/rubysmith/builder.rb', line 94

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

#touchObject



102
103
104
105
106
# File 'lib/rubysmith/builder.rb', line 102

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