guard-rsync 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +21 -0
  2. data/README.md +56 -0
  3. data/lib/guard/rsync.rb +95 -0
  4. metadata +70 -0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011 Kristofor Selden
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a
4
+ copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included
12
+ in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ Guard::Rsync
2
+ ===========
3
+
4
+ Rsync guard allows to automatically sync directories when source file
5
+ changes.
6
+
7
+ Focus of this guard task is to sync directories while excluding
8
+ autogenerated files and their sources.
9
+
10
+ Install
11
+ -------
12
+
13
+ Please be sure to have [Guard](https://github.com/guard/guard) installed before continue.
14
+
15
+ Install the gem:
16
+
17
+ $ gem install guard-rsync
18
+
19
+ Add it to your Gemfile (inside development group):
20
+
21
+ ``` ruby
22
+ gem 'guard-rsync'
23
+ ```
24
+
25
+ Usage
26
+ -----
27
+
28
+ Please read [Guard usage doc](https://github.com/guard/guard#readme)
29
+
30
+ Guardfile
31
+ ---------
32
+
33
+ The following example pairs the coffeescript guard with a rsync guard.
34
+
35
+ ``` ruby
36
+ group(:build_my_app) do
37
+ guard('rsync', {
38
+ :input => 'apps_src/my_app',
39
+ :output => 'apps',
40
+ :excludes => {
41
+ /(.+)\.coffee$/ => (lambda {|m| "#{m[1]}.js"})
42
+ },
43
+ :run_group_on_start => true
44
+ }) do
45
+ watch(%r{^apps_src/my_app/(.+\.(?!coffee)(.*)|[^.]+)$})
46
+ end
47
+
48
+ guard 'coffeescript', :input => 'apps_src/my_app', :output => 'apps/my_app'
49
+ end
50
+ ```
51
+
52
+ Author
53
+ ------
54
+
55
+ [Kristofor Selden](https://github.com/kselden)
56
+
@@ -0,0 +1,95 @@
1
+ require 'guard'
2
+ require 'guard/guard'
3
+ require 'guard/watcher'
4
+ require 'tempfile'
5
+
6
+ module Guard
7
+
8
+ # The CoffeeScript guard that gets notifications about the following
9
+ # Guard events: `start`, `stop`, `reload`, `run_all` and `run_on_change`.
10
+ #
11
+ class Rsync < Guard
12
+ # Initialize Guard::Sync.
13
+ #
14
+ # @param [Array<Guard::Watcher>] watchers the watchers in the Guard block
15
+ # @param [Hash] options the options for the Guard
16
+ # @option options [String] :input the input directory
17
+ # @option options [String] :output the output directory
18
+ # @option options [Hash] :excludes the map of excludes patterns in the
19
+ # input directory to exclude files in the output directory.
20
+ def initialize(watchers = [], options = { })
21
+ @input = ensure_no_trailing_slash(options[:input])
22
+ @output = options[:output]
23
+ raise 'input must be a directory' unless File.directory? @input
24
+ raise 'output must be a directory' unless File.directory? @output or @output =~ /^.*@.*:.*$/
25
+ @dirname = File.basename(@input)
26
+ @excludes = options[:excludes]
27
+ @run_group_on_start = options[:run_group_on_start]
28
+ super
29
+ end
30
+
31
+ # Call once when guard starts
32
+ def start
33
+ run_all
34
+ if @run_group_on_start
35
+ ::Guard.guards.each do |guard|
36
+ guard.run_all if self != guard && group == guard.group
37
+ end
38
+ end
39
+ end
40
+
41
+ # Gets called when rsync should be run.
42
+ #
43
+ # @return [Boolean] rsync was successful
44
+ def run_all
45
+ run_on_change([])
46
+ end
47
+
48
+ # Gets called when watched paths and files have changes.
49
+ #
50
+ # @param [Array<String>] paths the changed paths and files
51
+ # @return [Boolean] rsync was successful
52
+ def run_on_change(paths)
53
+ input_excludes = []
54
+ output_excludes = []
55
+ Dir.chdir(@input) do
56
+ Dir.glob('**/*').each do |file|
57
+ @excludes.each do |pattern, transform|
58
+ matches = file.match(pattern)
59
+ if matches
60
+ input_excludes << File.join('/', @dirname, file)
61
+ output_excludes << File.join('/', @dirname, transform.call(matches)) if transform
62
+ end
63
+ end
64
+ end
65
+ end
66
+ exclude_file = Tempfile.new('exclude')
67
+ begin
68
+ exclude_file.puts(input_excludes)
69
+ exclude_file.puts(output_excludes)
70
+ ::Guard.listener.ignore_paths.each do |dir|
71
+ next if dir == '.' or dir == '..'
72
+ exclude_file.puts(ensure_trailing_slash(File.basename(dir)))
73
+ end
74
+ exclude_file.flush
75
+ UI.info `rsync -av --delete --exclude-from "#{exclude_file.path}" "#{@input}" "#{@output}"`
76
+ success = $?.success?
77
+ return success
78
+ ensure
79
+ exclude_file.close
80
+ exclude_file.unlink
81
+ end
82
+ end
83
+
84
+ private
85
+ def ensure_trailing_slash(path)
86
+ path.gsub(/(.*[^\/])\Z/,'\1/')
87
+ end
88
+
89
+ def ensure_no_trailing_slash(path)
90
+ path.gsub(/\/\Z/,'')
91
+ end
92
+ end
93
+
94
+ end
95
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-rsync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kris Selden
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard
16
+ requirement: &70303394472940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70303394472940
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70303394472380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70303394472380
36
+ description: Guard::Rsync automatically syncs directories.
37
+ email:
38
+ - kris.selden@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - lib/guard/rsync.rb
44
+ - LICENSE
45
+ - README.md
46
+ homepage: http://github.com/kselden/guard-rsync
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: 1.3.6
64
+ requirements: []
65
+ rubyforge_project: guard-rsync
66
+ rubygems_version: 1.8.6
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Guard gem for syncing directories
70
+ test_files: []