Class: Guard::Copy

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/copy.rb,
lib/guard/copy/target.rb

Defined Under Namespace

Classes: Target

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Copy

Initialize a Guard.

Parameters:

  • watchers (Array<Guard::Watcher>) (defaults to: [])

    the Guard file watchers

  • options (Hash) (defaults to: {})

    the custom Guard options



15
16
17
18
19
20
21
22
23
# File 'lib/guard/copy.rb', line 15

def initialize(watchers = [], options = {})
  super
  if watchers.empty?
    watchers << ::Guard::Watcher.new(%r{^#{options[:from]}/.*$})
  else
    watchers.each { |w| normalize_watcher(w, options[:from]) }
  end
  @targets = Array(options[:to]).map { |to| Target.new(to, options) }
end

Instance Attribute Details

#targetsObject (readonly)

Returns the value of attribute targets.



10
11
12
# File 'lib/guard/copy.rb', line 10

def targets
  @targets
end

Instance Method Details

#run_allObject

Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…

Raises:

  • (:task_has_failed)

    when run_all has failed



43
44
45
# File 'lib/guard/copy.rb', line 43

def run_all
  run_on_changes(Watcher.match_files(self, Dir.glob("**/*.*")))
end

#run_on_changes(paths) ⇒ Object

Called on file(s) modifications that the Guard watches.

Parameters:

  • paths (Array<String>)

    the changes files or paths

Raises:

  • (:task_has_failed)

    when run_on_changes has failed



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/guard/copy.rb', line 50

def run_on_changes(paths)
  validate_at_least_one_target('copy')
  with_all_target_paths(paths) do |from_path, to_path|
    to_dir = File.dirname(to_path)
    if !File.directory?(to_dir) && options[:mkpath]
      UI.info("creating directory #{to_dir}") if options[:verbose]
      FileUtils.mkpath(to_dir)
    end
    validate_to_path(to_path)
    UI.info("copying to #{to_path}") if options[:verbose]
    copy(from_path, to_path)
  end
end

#run_on_removals(paths) ⇒ Object

Called on file(s) deletions that the Guard watches.

Parameters:

  • paths (Array<String>)

    the deleted files or paths

Raises:

  • (:task_has_failed)

    when run_on_removals has failed



67
68
69
70
71
72
73
74
75
# File 'lib/guard/copy.rb', line 67

def run_on_removals(paths)
  return unless options[:delete]
  validate_at_least_one_target('delete')
  with_all_target_paths(paths) do |_, to_path|
    validate_to_file(to_path)
    UI.info("deleting #{to_path}") if options[:verbose]
    FileUtils.rm(to_path)
  end
end

#startObject

Call once when Guard starts. Please override initialize method to init stuff.

Raises:

  • (:task_has_failed)

    when start has failed



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/guard/copy.rb', line 27

def start
  validate_presence_of(:from)
  validate_from_is_directory
  validate_presence_of(:to)
  validate_to_patterns_are_not_absolute
  validate_to_does_not_start_with_from
  resolve_targets!
  validate_no_targets_are_files
  display_target_paths

  run_all if options[:run_at_start]
end