Module: Guard::Helpers::Starter

Defined in:
lib/guard/helpers/starter.rb

Instance Method Summary collapse

Instance Method Details

#act_on(directory, file) ⇒ Object

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

Raises:

  • (NotImplementedError)


58
59
60
# 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) ⇒ Object

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.

Parameters:

  • watchers (Array<Guard::Watcher>)

    the Guard watchers in the block

  • files (Array<String>)

    the CoffeeScript files

  • options (Hash)

    the options for the execution

Options Hash (options):

  • :output (String)

    the output directory

  • :shallow (Boolean)

    do not create nested directories



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object

Informs the user of an error



63
64
65
66
67
# 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") ⇒ Object

Informs the user that an action has succeeded.



70
71
72
73
74
75
76
77
78
# 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_allObject

Call #run_on_change for all files which match this guard.



88
89
90
# 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) ⇒ Object

Run ‘act_on` on each file that is watched



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

#startObject

Defaults Calls #run_all if the :all_on_start option is present.



83
84
85
# File 'lib/guard/helpers/starter.rb', line 83

def start
  run_all if options[:all_on_start]
end

#target_filename(directory, file) ⇒ Object

The filename of the target file



53
54
55
# File 'lib/guard/helpers/starter.rb', line 53

def target_filename(directory, file)
  File.join(directory, File.basename(file))
end