Class: EtchExternalSource

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

Constant Summary collapse

@@load_path_org =

Save the original $LOAD_PATH ($:) to be restored later.

$LOAD_PATH.clone

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.



2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
# File 'lib/etch.rb', line 2006

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
  # In the olden days all local requests were XML snippits that the etch client
  # smashed into a single XML document to send over the wire.  This supports
  # scripts expecting the old interface.
  @local_requests = nil
  if local_requests
    @local_requests = "<requests>\n#{local_requests.join('')}\n</requests>"
  end
  # And this is a new interface where we just pass them as an array
  @local_requests_array = 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.



2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
# File 'lib/etch.rb', line 2032

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)
  ensure
    restore_globals
  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.



2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
# File 'lib/etch.rb', line 2055

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
  ensure
    restore_globals
  end
  @contents
end