Class: Hammer::Builder

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

Overview

Context object for the block DSL - what runs inside a Hammerfile, a Hammer.run do ... end block, or any other top-level fragment. All methods proxy to the target Hammer subclass.

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Builder

Returns a new instance of Builder.



6
7
8
# File 'lib/hammer/builder.rb', line 6

def initialize(klass)
  @klass = klass
end

Instance Method Details

#before(&block) ⇒ Object

Per-target / per-namespace pre-hook. Same semantics as Hammer.before at the class level - see lux-hammer.rb.



44
45
46
# File 'lib/hammer/builder.rb', line 44

def before(&block)
  @klass.before(&block)
end

#desc(text) ⇒ Object

Top-level CLI description, shown under the Usage line in hammer --help. Multi-line strings render with each line indented.



12
13
14
# File 'lib/hammer/builder.rb', line 12

def desc(text)
  @klass.app_desc(text)
end

#dotenv(flag = true) ⇒ Object

Opt out of auto .env loading in Hammer.cli. Default is on.



49
50
51
# File 'lib/hammer/builder.rb', line 49

def dotenv(flag = true)
  @klass.dotenv(flag)
end

#evaluate(source = nil, path = nil, &block) ⇒ Object

instance_eval wrapper that also publishes the current Hammer target via Thread.current. That lets *_hammer.rb files pulled in through Ruby's own require (e.g. Dir.require_all 'lib/tasks' inside a Hammerfile) call task/namespace/before at top-level scope and have them register on the right target.



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

def evaluate(source = nil, path = nil, &block)
  Hammer.with_target(@klass) do
    block ? instance_eval(&block) : instance_eval(source, path)
  end
end

#helpers(&block) ⇒ Object

Open the underlying Hammer subclass to add private instance methods callable from inside task procs. Lets recipe / Hammerfile authors share helpers without a Foo.method prefix or a separate module:

helpers do
def run(cmd) ; say cmd, :gray ; system cmd ; end
end

task :ship do
proc { run 'git push' }
end

Procs run via instance.instance_exec(opts, &handler) on a fresh subclass instance, so anything class_eval'd here is in scope.



30
31
32
# File 'lib/hammer/builder.rb', line 30

def helpers(&block)
  @klass.class_eval(&block)
end

#load(*paths, **kwargs) ⇒ Object

Same surface as Hammer.load. Resolved relative to the file that called us, so load auto: true inside a Hammerfile picks up *_hammer.rb under the Hammerfile's directory.



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

def load(*paths, **kwargs)
  anchor = Loader.caller_anchor(caller_locations(1, 1).first)
  @klass.loader.load(anchor, paths, kwargs)
end

#namespace(name, &block) ⇒ Object



38
39
40
# File 'lib/hammer/builder.rb', line 38

def namespace(name, &block)
  @klass.namespace(name, &block)
end

#task(name, &block) ⇒ Object



34
35
36
# File 'lib/hammer/builder.rb', line 34

def task(name, &block)
  @klass.task(name, &block)
end