Class: EtchExternalSource

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

Instance Method Summary collapse

Constructor Details

#initialize(file, original_file, facts, groups, local_requests, sourcebase, commandsbase, sitelibbase, dlogger) ⇒ EtchExternalSource

Returns a new instance of EtchExternalSource.



1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
# File 'lib/etch.rb', line 1479

def initialize(file, original_file, facts, groups, local_requests, sourcebase, commandsbase, sitelibbase, dlogger)
  # The external source is going to be processed within the same Ruby
  # instance as etch.  We want to make it clear what variables we are
  # intentionally exposing to external sources, essentially this
  # defines the "API" for those external sources.
  @file = file
  @original_file = original_file
  @facts = facts
  @groups = groups
  @local_requests = local_requests
  @sourcebase = sourcebase
  @commandsbase = commandsbase
  @sitelibbase = sitelibbase
  @dlogger = dlogger
end

Instance Method Details

#process_template(template) ⇒ Object

This method processes an ERB template (as specified via a <template> entry in a config.xml file) and returns the results.



1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
# File 'lib/etch.rb', line 1497

def process_template(template)
  @dlogger.debug "Processing template #{template} for file #{@file}"
  # The '-' arg allows folks to use <% -%> or <%- -%> to instruct ERB to
  # not insert a newline for that line, which helps avoid a bunch of blank
  # lines in the processed file where there was code in the template.
  erb = ERB.new(IO.read(template), nil, '-')
  # The binding arg ties the template's namespace to this point in the
  # code, thus ensuring that all of the variables above (@file, etc.)
  # are visible to the template code.
  begin
    erb.result(binding)
  rescue Exception => e
    # Help the user figure out where the exception occurred, otherwise they
    # just get told it happened here, which isn't very helpful.
    raise Etch.wrap_exception(e, "Exception while processing template #{template} for file #{@file}:\n" + e.message)
  end
end

#run_script(script) ⇒ Object

This method runs a etch script (as specified via a <script> entry in a config.xml file) and returns any output that the script puts in the @contents variable.



1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
# File 'lib/etch.rb', line 1518

def run_script(script)
  @dlogger.debug "Processing script #{script} for file #{@file}"
  @contents = ''
  begin
    run_script_stage2(script)
  rescue Exception => e
    if e.kind_of?(SystemExit)
      # The user might call exit within a script.  We want the scripts
      # to act as much like a real script as possible, so ignore those.
    else
      # Help the user figure out where the exception occurred, otherwise they
      # just get told it happened here in eval, which isn't very helpful.
      raise Etch.wrap_exception(e, "Exception while processing script #{script} for file #{@file}:\n" + e.message)
    end
  end
  @contents
end

#run_script_stage2(script) ⇒ Object

The user might call return within a script. We want the scripts to act as much like a real script as possible. Wrapping the eval in an extra method allows us to handle a return within the script seamlessly. If the user calls return it triggers a return from this method. Otherwise this method returns naturally. Either works for us.



1540
1541
1542
# File 'lib/etch.rb', line 1540

def run_script_stage2(script)
  eval(IO.read(script))
end