guard-copy 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module CopyVersion
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/guard/copy.rb CHANGED
@@ -1,24 +1,35 @@
1
1
  require 'guard'
2
2
  require 'guard/guard'
3
3
  require 'guard/copy/version'
4
+ require 'fileutils'
4
5
 
5
6
  module Guard
6
7
  class Copy < Guard
7
8
 
9
+ attr_reader :targets
10
+
8
11
  # Initialize a Guard.
9
12
  # @param [Array<Guard::Watcher>] watchers the Guard file watchers
10
13
  # @param [Hash] options the custom Guard options
11
14
  def initialize(watchers = [], options = {})
12
15
  # watchers are currently ignored
13
16
  watchers << ::Guard::Watcher.new(%r{#{options[:from]}/.*})
14
- @targets = options[:to].is_a?(Array) ? options[:to] : [options[:to]]
15
- UI.info("Guard::Copy will copy files from '#{options[:from]}' to '#{@targets.first}'")
17
+ options[:to] = Array(options[:to]).freeze
16
18
  super
17
19
  end
18
20
 
19
21
  # Call once when Guard starts. Please override initialize method to init stuff.
20
22
  # @raise [:task_has_failed] when start has failed
21
23
  def start
24
+ validate_from
25
+ validate_to
26
+ @targets = resolve_targets
27
+ if @targets.any?
28
+ UI.info("Guard::Copy will copy files from:")
29
+ UI.info(" #{options[:from]}")
30
+ UI.info("to:")
31
+ @targets.each { |target| UI.info(" #{target}") }
32
+ end
22
33
  end
23
34
 
24
35
  # Called when just `enter` is pressed
@@ -31,41 +42,82 @@ module Guard
31
42
  # @param [Array<String>] paths the changes files or paths
32
43
  # @raise [:task_has_failed] when run_on_change has failed
33
44
  def run_on_change(paths)
45
+ validate_targets
34
46
  paths.each do |from_path|
35
47
  @targets.each do |target|
36
- target_dirs = target_dirs(target)
37
- if target_dirs.empty?
38
- UI.error("'#{target}' does not match any directories")
39
- else
40
- copy(from_path, target_dirs)
41
- end
48
+ to_path = from_path.sub(@options[:from], target)
49
+ validate_to_path(to_path)
50
+ FileUtils.cp(from_path, to_path)
42
51
  end
43
52
  end
44
53
  end
45
54
 
46
55
  # Called on file(s) deletions that the Guard watches.
47
56
  # @param [Array<String>] paths the deleted files or paths
48
- # @raise [:task_has_failed] when run_on_change has failed
57
+ # @raise [:task_has_failed] when run_on_deletion has failed
49
58
  def run_on_deletion(paths)
50
59
  end
51
60
 
52
61
  private
53
62
 
54
- def target_dirs(target)
55
- if @options[:glob] == :newest
56
- Dir[target].sort_by { |f| File.mtime(f) }.last(1)
57
- else
58
- Dir[target]
63
+ def validate_from
64
+ unless options[:from]
65
+ UI.error('Guard::Copy - :from option is required')
66
+ throw :task_has_failed
67
+ end
68
+ if File.file?(options[:from])
69
+ UI.error("Guard::Copy - '#{options[:from]}' is a file and must be a directory")
70
+ throw :task_has_failed
71
+ end
72
+ unless File.directory?(options[:from])
73
+ UI.error('Guard::Copy - :from option does not contain a valid directory')
74
+ throw :task_has_failed
59
75
  end
60
76
  end
61
77
 
62
- def copy(from_path, target_dirs)
63
- target_dirs.each do |target_dir|
64
- if File.directory?(target_dir)
65
- to_path = from_path.sub(@options[:from], target_dir)
66
- FileUtils.cp(from_path, to_path)
67
- else
68
- UI.error("'#{target_dir}' is not a directory")
78
+ def validate_to
79
+ if options[:to].empty?
80
+ UI.error('Guard::Copy - :to option is required')
81
+ throw :task_has_failed
82
+ end
83
+ if options[:to].include?(options[:from])
84
+ UI.error('Guard::Copy - :to must not include :from')
85
+ throw :task_has_failed
86
+ end
87
+ end
88
+
89
+ def validate_targets
90
+ if @targets.empty?
91
+ UI.error('Guard::Copy - cannot copy, no valid :to directories')
92
+ throw :task_has_failed
93
+ end
94
+ end
95
+
96
+ def validate_to_path(to_path)
97
+ to_dir = File.dirname(to_path)
98
+ unless File.directory?(to_dir)
99
+ UI.error('Guard::Copy - cannot copy, directory path does not exist:')
100
+ UI.error(" #{to_dir}")
101
+ throw :task_has_failed
102
+ end
103
+ end
104
+
105
+ def resolve_targets
106
+ Array.new.tap do |targets|
107
+ options[:to].each do |to|
108
+ if @options[:glob] == :newest
109
+ dirs = Dir[to].sort_by { |f| File.mtime(f) }.last(1)
110
+ else
111
+ dirs = Dir[to]
112
+ end
113
+ if dirs.any?
114
+ #dirs.each do |dir|
115
+ #throw :task_has_failed if File.file?(dir)
116
+ #end
117
+ targets.concat(dirs)
118
+ else
119
+ UI.warning("Guard::Copy - '#{to}' does not match a valid directory")
120
+ end
69
121
  end
70
122
  end
71
123
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-copy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-15 00:00:00.000000000 Z
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: guard
@@ -91,6 +91,54 @@ dependencies:
91
91
  - - ! '>='
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0.8'
94
+ - !ruby/object:Gem::Dependency
95
+ name: guard-rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 0.7.2
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 0.7.2
110
+ - !ruby/object:Gem::Dependency
111
+ name: fakefs
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.4.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 0.4.0
126
+ - !ruby/object:Gem::Dependency
127
+ name: mocha
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: 0.11.4
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: 0.11.4
94
142
  description: Guard::Copy automatically copies files.
95
143
  email:
96
144
  - marc.schwieterman@gmail.com
@@ -115,12 +163,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
163
  - - ! '>='
116
164
  - !ruby/object:Gem::Version
117
165
  version: '0'
166
+ segments:
167
+ - 0
168
+ hash: 964757571042390819
118
169
  required_rubygems_version: !ruby/object:Gem::Requirement
119
170
  none: false
120
171
  requirements:
121
172
  - - ! '>='
122
173
  - !ruby/object:Gem::Version
123
174
  version: '0'
175
+ segments:
176
+ - 0
177
+ hash: 964757571042390819
124
178
  requirements: []
125
179
  rubyforge_project:
126
180
  rubygems_version: 1.8.24