Class: Guard::Copy

Inherits:
Plugin
  • 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(options = {}) ⇒ Copy

Initializes a Guard plugin. Don’t do any work here, especially as Guard plugins get initialized even if they are not in an active group!

Parameters:

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

    the Guard plugin options

Options Hash (options):

  • watchers (Array<Guard::Watcher>)

    the Guard plugin file watchers

  • group (Symbol)

    the group this Guard plugin belongs to

  • any_return (Boolean)

    allow any object to be returned from a watcher



23
24
25
26
27
# File 'lib/guard/copy.rb', line 23

def initialize(options = {})
  inject_watchers(options)
  super
  @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



47
48
49
# File 'lib/guard/copy.rb', line 47

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



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/guard/copy.rb', line 54

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



71
72
73
74
75
76
77
78
79
# File 'lib/guard/copy.rb', line 71

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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/guard/copy.rb', line 31

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