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(settings, helpers: HELPERS) ⇒ Builder

Returns a new instance of Builder.



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

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

Class Method Details

.callObject



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

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

Instance Method Details

#append(content) ⇒ Object



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

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

#checkObject



29
30
31
32
33
# File 'lib/rubysmith/builder.rb', line 29

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

#deleteObject



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

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

#insert_after(pattern, content) ⇒ Object



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

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



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

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



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

def make_path
  log_info "Making: #{relative_build_path}"
  build_path.make_path
  self
end

#permit(mode) ⇒ Object



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

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

#prepend(content) ⇒ Object



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

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

#rename(name) ⇒ Object



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

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

#renderObject



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

def render
  log_info "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



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

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



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

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

#touchObject



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

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