Class: Rubysmith::Builder

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

Overview

:reek:DataClump Provides common functionality necessary for all builders.

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(settings, helpers: HELPERS) ⇒ Builder

Returns a new instance of Builder.



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

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

Instance Method Details

#append(content) ⇒ Object



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

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

#checkObject



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

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

#deleteObject



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

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

#insert_after(pattern, content) ⇒ Object



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

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



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

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



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

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

#permit(mode) ⇒ Object



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

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

#prepend(content) ⇒ Object



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

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

#rename(name) ⇒ Object



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

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

#renderObject



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

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



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

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



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

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

#touchObject



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

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