module Guard::Helpers::Starter

Public Instance Methods

act_on(directory, file) click to toggle source

The action to be performed on a file (e.g., compilation)

# File lib/guard/helpers/starter.rb, line 58
def act_on(directory, file)
  raise NotImplementedError.new('act_on not implemented')
end
detect_nested_directories(watchers, files, options) click to toggle source

Copyright © 2010-2012 Michael Kessler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Detects the output directory for each CoffeeScript file. Builds the product of all watchers and assigns to each directory the files to which it belongs to.

@param [Array<Guard::Watcher>] watchers the Guard watchers in the block @param [Array<String>] files the CoffeeScript files @param [Hash] options the options for the execution @option options [String] :output the output directory @option options [Boolean] :shallow do not create nested directories

# File lib/guard/helpers/starter.rb, line 32
def detect_nested_directories(watchers, files, options)
  return { options[:output] => files } if options[:shallow]

  directories = { }

  watchers.product(files).each do |watcher, file|
    if matches = file.match(watcher.pattern)
      target = matches[1] ? File.join(options[:output], File.dirname(matches[1])).gsub(/\/\.$/, '') : options[:output] || File.dirname(file)
      if directories[target]
        directories[target] << file
      else
        directories[target] = [file]
      end
    end
  end

  directories
end
error(message, path) click to toggle source

Informs the user of an error

# File lib/guard/helpers/starter.rb, line 63
def error(message, path)
  message = "#{self.class.name} ERROR: #{path} with error '#{message}'"
  ::Guard::Helpers::Formatter.error message
  ::Guard::Helpers::Formatter.notify message, :image => :failed
end
notify(paths, action="COMPILED") click to toggle source

Informs the user that an action has succeeded.

# File lib/guard/helpers/starter.rb, line 70
def notify(paths, action="COMPILED")
  message = if paths.length <= 8
              "#{self.class.name} #{action}: #{paths.join(', ')}" 
            else 
              "#{self.class.name} #{action}: #{paths[0..7].join(', ')}, and others." 
            end
  ::Guard::Helpers::Formatter.success message
  ::Guard::Helpers::Formatter.notify message, :image => :success
end
run_all() click to toggle source

Call run_on_change for all files which match this guard.

# File lib/guard/helpers/starter.rb, line 88
def run_all
  run_on_changes(Watcher.match_files(self, Dir.glob('**{,/*/**}/*').uniq.compact))
end
run_on_changes(paths) click to toggle source

Run ‘act_on` on each file that is watched

# File lib/guard/helpers/starter.rb, line 116
def run_on_changes(paths)
  if options[:all_on_change]
    paths = Watcher.match_files(self, Dir.glob('**{,/*/**}/*').uniq.compact)
  end
  paths = paths.select {|path| not options[:exclude] =~ path and File.file? path}

  directories = detect_nested_directories(watchers, paths, options)
  written = []

  directories.each do |directory, files|
    files.each do |file|
      begin
        act_on(directory, file)
        written << file
      rescue Exception => e
        error(e.message, file)
        throw :task_has_failed
      end
    end
  end
  if written.length > 0
    notify(written)
  end
end
run_on_removals(paths) click to toggle source

The default action when a path is removed is to remove the matching compiled file

# File lib/guard/helpers/starter.rb, line 94
def run_on_removals(paths)
  paths = paths.select {|path| not options[:exclude] =~ path and File.file? path}

  directories = detect_nested_directories(watchers, paths, options)
  removed = []

  directories.each do |directory, files|
    files.each do |file|
      begin
        File.delete file
        removed << file
      rescue Error::ENOENT
        # Ignore
      end
    end
  end
  if removed.length > 0
    notify(removed, "REMOVED")
  end
end
start() click to toggle source
Defaults

Calls run_all if the :all_on_start option is present.

# File lib/guard/helpers/starter.rb, line 83
def start
  run_all if options[:all_on_start]
end
target_filename(directory, file) click to toggle source

The filename of the target file

# File lib/guard/helpers/starter.rb, line 53
def target_filename(directory, file)
  File.join(directory, File.basename(file))
end