Class: Guard::Autoupload

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Autoupload.



11
12
13
14
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
# File 'lib/guard/autoupload.rb', line 11

def initialize(watchers = [], options = {})
    super

    @instance = self
    if options[:protocol] == :scp
        @session = SCPSession.new(
            options[:host],
            options[:port] || 22,
            options[:user],
            options[:password],
            self
        )
    elsif options[:protocol] == :sftp
        @session = SFTPSession.new(
            options[:host],
            options[:port] || 22,
            options[:user],
            options[:password],
            @instance
        )
    elsif options[:protocol] == :ftp
        @session = FTPSession.new(
            options[:host],
            options[:port] || 21,
            options[:user],
            options[:password]
        )
    else
        throw :task_has_failed
    end

    @remote = options[:remote]
    @local = Dir.pwd
    @verbose = options[:verbose]
    @quiet = options[:quiet] unless verbose?
    output = options.dup
    output[:password] = options[:password].gsub(/./, '*') if options.include? :password
    log "Initialized with watchers #{watchers.inspect}" if verbose?
    log "Initialized with options #{output.inspect}" unless quiet?
end

Instance Method Details

#log(message) ⇒ Object



106
107
108
# File 'lib/guard/autoupload.rb', line 106

def log(message)
    puts "[#{Time.now}] #{message}"
end

#quiet?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/guard/autoupload.rb', line 102

def quiet?
    @quiet || false
end

#run_on_change(paths) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/guard/autoupload.rb', line 52

def run_on_change(paths)
    paths.each do |path|
        local_file = File.join(@local, path)
        remote_file = File.join(@remote, path)

        attempts = 0

        begin
            log "Upload #{local_file} => #{remote_file}" if verbose?
            @session.upload!(local_file, remote_file)
            log "Uploaded #{path}" unless quiet?
        rescue => ex
            log "Exception on uploading #{path}\n#{ex.inspect}"
            log ex.backtrace.join("\n") if verbose?
            attempts += 1
            remote_dir = File.dirname(remote_file)
            recursively_create_dirs(remote_dir)
            retry if attempts < 3
            log "Exceeded 3 attempts to upload #{path}"
            throw :task_has_failed
        end
    end

    msg = "Uploaded:\n#{paths.join("\n")}"
    ::Guard::Notifier.notify msg, :title => "Uploaded"
end

#run_on_removals(paths) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/guard/autoupload.rb', line 79

def run_on_removals(paths)
    paths.each do |path|
        remote_file = File.join(@remote, path)

        begin
            log "Delete #{remote_file}" if verbose?
            @session.remove!(remote_file)
        rescue => ex
            log "Exception on deleting #{path}\n#{ex.inspect}"
            log ex.backtrace.join("\n") if verbose?
        end

        log "Deleted #{path}" unless quiet?
    end

    msg = "Deleted:\n#{paths.join("\n")}"
    ::Guard::Notifier.notify msg, :title => "Deleted"
end

#stopObject



110
111
112
113
114
115
# File 'lib/guard/autoupload.rb', line 110

def stop
    log "Tearing down connections" unless quiet?
    if @session.is_a? SCPSession
        @session.close
    end
end

#verbose?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/guard/autoupload.rb', line 98

def verbose?
    @verbose || false
end