class Guard::Guardfile::Evaluator

TODO: rename this to a Locator or Loader or something
@see Guard::Dsl
Guard::Dsl for the actual objects generation from the Guardfile content.
This class is responsible for evaluating the Guardfile. It delegates to

def _fetch_guardfile_contents

def _fetch_guardfile_contents
  _use_inline || _use_provided || _use_default
  @evaluated = true
  return if _guardfile_contents_usable?
  UI.error "No Guard plugins found in Guardfile,"\
    " please add at least one."
end

def _from_deprecated(opts)

def _from_deprecated(opts)
  res = opts.dup
  if opts.key?(:guardfile_contents)
    res[:contents] = opts[:guardfile_contents]
  end
  res
end

def _guardfile_contents

def _guardfile_contents
  @user_config ||= Pathname("~/.guard.rb").expand_path.read
  [@contents, @user_config].compact.join("\n")
rescue Errno::ENOENT
  @contents || ""
end

def _guardfile_contents_usable?

def _guardfile_contents_usable?
  guardfile_contents && guardfile_contents =~ /guard/m
end

def _guardfile_contents_without_user_config

def _guardfile_contents_without_user_config
  @guardfile_contents || ""
end

def _instance_eval_guardfile(contents)

def _instance_eval_guardfile(contents)
  Dsl.new.evaluate(contents, @guardfile_path || "", 1)
rescue => ex
  UI.error "Invalid Guardfile, original error is:\n#{ $! }"
  raise ex
end

def _read(path)

def _read(path)
  full_path = Pathname(path).expand_path
  [full_path, full_path.read]
rescue Errno::ENOENT
  fail
rescue SystemCallError => e
  UI.error "Error reading file #{full_path}:"
  UI.error e.inspect
  UI.error e.backtrace
  abort
end

def _use_default!

def _use_default!
  @path, @contents = _read("Guardfile")
  @type = :default
rescue Errno::ENOENT
  begin
    @path, @contents = _read("~/.Guardfile")
    @type = :default
  rescue Errno::ENOENT
    fail NoGuardfileError, ERROR_NO_GUARDFILE
  end
end

def _use_inline

def _use_inline
  source_from_option = @source.nil? && options[:guardfile_contents]
  inline = @source == :inline
  return false unless (source_from_option) || inline
  @source = :inline
  @guardfile_contents = options[:guardfile_contents]
  UI.info "Using inline Guardfile."
  true
end

def _use_provided

def _use_provided
  return unless custom?
  @path, @contents = _read(@path)
  true
rescue Errno::ENOENT
  fail NoCustomGuardfile, "No Guardfile exists at #{ @path }."
end

def custom?

def custom?
  @type == :custom
end

def evaluate

Other tags:
    Example: Programmatically evaluate a Guardfile with an inline Guardfile -
    Example: Programmatically evaluate a Guardfile with a custom Guardfile -
    Example: Programmatically evaluate a Guardfile -
def evaluate
  inline? || _use_provided || _use_default!
  contents = _guardfile_contents
  fail NoPluginsError, ERROR_NO_PLUGINS unless /guard/m =~ contents
  Dsl.new.evaluate(contents, @path || "", 1)
end

def guardfile_contents

Returns:
  • (String) - the Guardfile content

Other tags:
    Example: Programmatically get the content of the current Guardfile -
def guardfile_contents
  config = File.read(_user_config_path) if File.exist?(_user_config_path)
  [_guardfile_contents_without_user_config, config].compact.join("\n")
end

def guardfile_include?(plugin_name)

Returns:
  • (Boolean) - whether the Guard plugin has been declared

Parameters:
  • plugin_name (String) -- the name of the Guard

Other tags:
    Example: Programmatically test if a Guardfile contains a specific Guard -
def guardfile_include?(plugin_name)
  reader = DslReader.new
  reader.evaluate(@contents, @path || "", 1)
  reader.plugin_names.include?(plugin_name)
end

def guardfile_source

def guardfile_source
  @source
end

def initialize(opts = {})

Options Hash: (**opts)
  • contents (String) -- a string representing the
  • guardfile (String) -- the path to a valid Guardfile
def initialize(opts = {})
  @type = nil
  @path = nil
  @user_config = nil
  opts = _from_deprecated(opts)
  if opts[:contents]
    @type = :inline
    @contents = opts[:contents]
  else
    if opts[:guardfile]
      @type = :custom
      @path = Pathname(opts[:guardfile]) # may be updated by _read
    end
  end
end

def inline?

def inline?
  @type == :inline
end