Class: Rubysmith::Builder

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

Overview

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

Constant Summary collapse

LOGGER =
Logger.new(STDOUT, formatter: ->(_severity, _at, _program, message) { "#{message}\n" })
HELPERS =
{
  inserter: Text::Inserter,
  renderer: Renderers::ERB,
  kernel: Open3,
  logger: LOGGER
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, helpers: HELPERS) ⇒ Builder

Returns a new instance of Builder.



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

def initialize configuration, helpers: HELPERS
  @configuration = configuration
  @helpers = helpers
end

Class Method Details

.call(configuration, helpers: HELPERS) ⇒ Object



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

def self.call configuration, helpers: HELPERS
  new configuration, helpers: helpers
end

Instance Method Details

#append(content) ⇒ Object



31
32
33
34
35
# File 'lib/rubysmith/builder.rb', line 31

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

#deleteObject



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

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

#insert_after(pattern, content) ⇒ Object



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

def insert_after pattern, content
  logger.info "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



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

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

#permit(mode) ⇒ Object



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

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

#prepend(content) ⇒ Object



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

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

#rename(name) ⇒ Object



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

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

#renderObject



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

def render
  logger.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



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

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

#run(*command) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/rubysmith/builder.rb', line 89

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

#touchObject



97
98
99
100
101
# File 'lib/rubysmith/builder.rb', line 97

def touch
  logger.info "Touching: #{relative_build_path}"
  build_path.make_ancestors.touch
  self
end