Class: Guard::Flopbox

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/flopbox.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Flopbox.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/guard/flopbox.rb', line 11

def initialize(watchers = [], options = {})
  @sftp_session = Net::SFTP.start(options[:hostname], options[:user], options[:sftp_opts])
  @remote       = options[:remote]
  @debug        = options[:debug]
  @growl        = options[:growl]
  @growl_image  = options[:growl_image]
  @pwd          = Dir.pwd
  
  log "Initialized with watchers = #{watchers.inspect}"
  log "Initialized with options  = #{options.inspect}"
  
  super
end

Instance Attribute Details

#growl_imageObject (readonly)

Returns the value of attribute growl_image.



9
10
11
# File 'lib/guard/flopbox.rb', line 9

def growl_image
  @growl_image
end

#pwdObject (readonly)

Returns the value of attribute pwd.



9
10
11
# File 'lib/guard/flopbox.rb', line 9

def pwd
  @pwd
end

#remoteObject (readonly)

Returns the value of attribute remote.



9
10
11
# File 'lib/guard/flopbox.rb', line 9

def remote
  @remote
end

#sftp_sessionObject (readonly)

Returns the value of attribute sftp_session.



9
10
11
# File 'lib/guard/flopbox.rb', line 9

def sftp_session
  @sftp_session
end

Instance Method Details

#run_on_change(paths) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/guard/flopbox.rb', line 25

def run_on_change(paths)
  paths.each do |path|
    local_file  = File.join(pwd, path)
    remote_file = File.join(remote, path)
    
    attempts = 0
    begin
      log "Upload #{local_file} => #{remote_file}"
      sftp_session.upload!(local_file, remote_file)

    rescue Net::SFTP::StatusException => ex
      log "Exception on upload #{path} - directory likely doesn't exist"

      attempts += 1
      remote_dir = File.dirname(remote_file)
      recursively_create_dirs( remote_dir )          

      retry if (attempts < 3)
      log "Exceeded 3 attempts to upload #{path}"
    end

    growl("Synced:\n#{paths.join("\n")}")
  end
end