class Guard::Guardfile::Evaluator


@see Guard::Dsl
to Guard::Dsl for the actual objects generation from the Guardfile content.
This class is responsible for evaluating the Guardfile. It delegates

def _after_reevaluate_guardfile


after the Guardfile has been re-evaluated.
Starts Guard and notification and show a message
def _after_reevaluate_guardfile
  ::Guard::Notifier.turn_on if ::Guard::Notifier.enabled?
  if ::Guard.plugins.empty?
    ::Guard::Notifier.notify('No plugins found in Guardfile, please add at least one.', title: 'Guard re-evaluate', image: :failed)
  else
    msg = 'Guardfile has been re-evaluated.'
    ::Guard::UI.info(msg)
    ::Guard::Notifier.notify(msg, title: 'Guard re-evaluate')
    ::Guard.runner.run(:start)
  end
end

def _before_reevaluate_guardfile


before the Guardfile will be re-evaluated.
Stops Guard and clear internal state
def _before_reevaluate_guardfile
  ::Guard.runner.run(:stop)
  ::Guard.reset_groups
  ::Guard.reset_plugins
  ::Guard.reset_scope
  ::Guard::Notifier.clear_notifiers
end

def _fetch_guardfile_contents


the options as `:guardfile_contents`.
Gets the content to evaluate and stores it into
def _fetch_guardfile_contents
  _use_inline_guardfile || _use_provided_guardfile || _use_default_guardfile
  unless _guardfile_contents_usable?
    ::Guard::UI.error 'No Guard plugins found in Guardfile, please add at least one.'
  end
end

def _find_default_guardfile


or nil otherwise.
Returns the first default Guardfile (either local or home Guardfile)
def _find_default_guardfile
  [_local_guardfile_path, _home_guardfile_path].find { |path| File.exist?(path) }
end

def _guardfile_contents_usable?

Returns:
  • (Boolean) - if the Guardfile is usable
def _guardfile_contents_usable?
  guardfile_contents && guardfile_contents =~ /guard/m
end

def _guardfile_contents_without_user_config

Returns:
  • (String) - the Guardfile content
def _guardfile_contents_without_user_config
  options[:guardfile_contents] || ''
end

def _home_guardfile_path

Returns:
  • (String) - the path to `~/.Guardfile`
def _home_guardfile_path
  File.expand_path(File.join('~', '.Guardfile'))
end

def _instance_eval_guardfile(contents)

Parameters:
  • contents (String) -- the content to evaluate.
def _instance_eval_guardfile(contents)
  ::Guard::Dsl.new.instance_eval(contents, options[:guardfile_path], 1)
rescue => ex
  ::Guard::UI.error "Invalid Guardfile, original error is:\n#{ $! }"
  raise ex
end

def _local_guardfile_path

Returns:
  • (String) - the path to the local Guardfile
def _local_guardfile_path
  File.expand_path(File.join(Dir.pwd, 'Guardfile'))
end

def _read_guardfile(guardfile_path)

Parameters:
  • guardfile_path (String) -- the path to the Guardfile
def _read_guardfile(guardfile_path)
  options[:guardfile_path]     = guardfile_path
  options[:guardfile_contents] = File.read(guardfile_path)
rescue => ex
  ::Guard::UI.error ex.inspect
  ::Guard::UI.error("Error reading file #{ guardfile_path }")
  exit 1
end

def _use_default_guardfile


Exits Guard if no Guardfile is found.
Try to use one of the default Guardfiles (local or home Guardfile).
def _use_default_guardfile
  if guardfile_path = _find_default_guardfile
    _read_guardfile(guardfile_path)
  else
    ::Guard::UI.error 'No Guardfile found, please create one with `guard init`.'
    exit 1
  end
end

def _use_inline_guardfile


Use the provided inline Guardfile if provided.
def _use_inline_guardfile
  return false unless options[:guardfile_contents]
  ::Guard::UI.info 'Using inline Guardfile.'
  options[:guardfile_path] = 'Inline Guardfile'
end

def _use_provided_guardfile


be found.
Try to use the provided Guardfile. Exits Guard if the Guardfile cannot
def _use_provided_guardfile
  return false unless options[:guardfile]
  options[:guardfile] = File.expand_path(options[:guardfile])
  if File.exist?(options[:guardfile])
    _read_guardfile(options[:guardfile])
    ::Guard::UI.info "Using Guardfile at #{ options[:guardfile] }."
    true
  else
    ::Guard::UI.error "No Guardfile exists at #{ options[:guardfile] }."
    exit 1
  end
end

def _user_config_path

Returns:
  • (String) - the path to `~/.guard.rb`
def _user_config_path
  File.expand_path(File.join('~', '.guard.rb'))
end

def evaluate_guardfile

Other tags:
    Example: Programmatically evaluate a Guardfile with an inline Guardfile -
    Example: Programmatically evaluate a Guardfile with a custom Guardfile path -
    Example: Programmatically evaluate a Guardfile -
def evaluate_guardfile
  _fetch_guardfile_contents
  _instance_eval_guardfile(guardfile_contents)
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 plugin -
def guardfile_include?(plugin_name)
  _guardfile_contents_without_user_config.match(/^guard\s*\(?\s*['":]#{ plugin_name }['"]?/)
end

def guardfile_path

Returns:
  • (String) - the path to the Guardfile or 'Inline Guardfile' if

Other tags:
    Example: Gets the "path" of an inline Guardfile -
    Example: Gets the path of the currently evaluated Guardfile -
def guardfile_path
  options[:guardfile_path] || ''
end

def initialize(opts = {})

Options Hash: (**opts)
  • guardfile_contents (String) -- a string representing the content of a valid Guardfile
  • guardfile (String) -- the path to a valid Guardfile
def initialize(opts = {})
  @options = ::Guard::Options.new(opts.select { |k, _| [:guardfile, :guardfile_contents].include?(k.to_sym) })
end

def reevaluate_guardfile


the current Guard configuration.
Re-evaluates the `Guardfile` to update
def reevaluate_guardfile
  _before_reevaluate_guardfile
  evaluate_guardfile
  _after_reevaluate_guardfile
end