Module: Synchrotron

Defined in:
lib/synchrotron/scanner.rb,
lib/synchrotron.rb,
lib/synchrotron/ignore.rb,
lib/synchrotron/logger.rb,
lib/synchrotron/stream.rb,
lib/synchrotron/version.rb

Overview

Not currently used.

Defined Under Namespace

Classes: Ignore, Logger, Scanner, Stream

Constant Summary collapse

APP_NAME =
'Synchrotron'
APP_VERSION =
'0.0.7'
APP_AUTHOR =
'Ryan Grove'
APP_EMAIL =
'[email protected]'
APP_URL =
'http://github.com/rgrove/synchrotron/'
'Copyright (c) 2013 Ryan Grove <[email protected]>. All ' <<
'rights reserved.'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/synchrotron.rb', line 13

def config
  @config
end

.ignoreObject (readonly)

Returns the value of attribute ignore.



13
14
15
# File 'lib/synchrotron.rb', line 13

def ignore
  @ignore
end

.logObject (readonly)

Returns the value of attribute log.



13
14
15
# File 'lib/synchrotron.rb', line 13

def log
  @log
end

.scannerObject (readonly)

Returns the value of attribute scanner.



13
14
15
# File 'lib/synchrotron.rb', line 13

def scanner
  @scanner
end

Class Method Details

.coalesce_changes(changes) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/synchrotron.rb', line 53

def coalesce_changes(changes)
  coalesced = {}

  changes.each do |path|
    next if coalesced.include?(path)

    pn = Pathname.new(path)

    coalesced[pn.to_s] = true unless catch :matched do
      pn.descend {|p| throw(:matched, true) if coalesced.include?(p.to_s) }
      false
    end
  end

  coalesced.keys.sort
end

.init(config = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/synchrotron.rb', line 15

def init(config = {})
  @config = {
    :dry_run      => false,
    :exclude      => [],
    :exclude_from => [],
    :local_path   => File.expand_path('.'),
    :remote_path  => nil,

    :rsync_options => [
      '--compress',
      '--delete',
      '--human-readable',
      '--links',
      '--out-format="--> [%o] %n %L"',
      '--perms',
      '--recursive',
      '--times'
    ],

    :rsync_path => '/usr/bin/rsync',
    :verbosity  => :info
  }.merge(config)

  @log       = Logger.new(@config[:verbosity])
  @ignore    = Ignore.new(@config[:exclude], @log)
  @regex_rel = Regexp.new("^#{Regexp.escape(@config[:local_path].chomp('/'))}/?")
  @queue     = Queue.new

  @config[:rsync_options] << '--dry-run' if @config[:dry_run]

  local_exclude_file = File.join(@config[:local_path], '.synchrotron-exclude')
  @config[:exclude_from] << local_exclude_file if File.exist?(local_exclude_file)
  @config[:exclude_from].each {|filename| @ignore.add_file(filename) }

  @log.info "Local path : #{@config[:local_path]}"
  @log.info "Remote path: #{@config[:remote_path]}"
end

.monitorObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/synchrotron.rb', line 70

def monitor
  @log.info "Watching for changes"

  @sync_thread = Thread.new do
    while changed = @queue.pop do
      @log.verbose "Change detected"

      changed.each {|path| @log.verbose "--> #{path}" }
      changed.each {|path| sync(path) if File.exist?(path) }
    end
  end

  fsevent = FSEvent.new
  fsevent.watch(@config[:local_path], {:latency => 1}) do |paths|
    changed = coalesce_changes(paths.reject {|path| @ignore.match(path) })
    @queue << changed unless changed.empty?
  end

  fsevent.run
end

.relative_path(path) ⇒ Object



91
92
93
# File 'lib/synchrotron.rb', line 91

def relative_path(path)
  path.sub(@regex_rel, '')
end

.sync(path = ) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/synchrotron.rb', line 95

def sync(path = @config[:local_path])
  rsync_local   = escape_arg(path)
  rsync_options = @config[:rsync_options].join(' ')
  rsync_remote  = escape_arg(File.join(@config[:remote_path], relative_path(path)))

  if rsync_remote.include?('GIT_BRANCH')
    git_branch = `git symbolic-ref HEAD`[/([^\/])*$/].chomp
    @log.info "git branch is '#{git_branch}'."
    rsync_remote.gsub!('GIT_BRANCH',git_branch)
  end

  # Build exclusion list.
  @config[:exclude].each {|p| rsync_options << " --exclude #{escape_arg(p)}" }
  @config[:exclude_from].each {|f| rsync_options << " --exclude-from #{escape_arg(f)}"}

  rsync_cmd = "#{@config[:rsync_path]} #{rsync_options} #{rsync_local} #{rsync_remote}"

  @log.debug rsync_cmd

  lines = 0
  `#{rsync_cmd}`.each_line {|line| @log.info line; lines = lines + 1 }

  if @config[:notify] && lines > 0
    require 'terminal-notifier'

    TerminalNotifier.notify("Synced #{lines} files to #{rsync_remote}",
        :title  => 'Synchrotron',
        :sender => 'com.apple.Terminal',
        :group  => Process.pid)
  end
end