Class: Guard::Autoupload

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Autoupload

Returns a new instance of Autoupload.



10
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
51
52
# File 'lib/guard/autoupload.rb', line 10

def initialize(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
  @local_subpath = options[:local] || ''
  @verbose = options[:verbose]
  @quiet = options[:quiet] unless verbose?
  @remote_delete = options[:remote_delete].nil? ? true : options[:remote_delete]
  output = options.dup
  output[:password] = options[:password].gsub(/./, '*') if options.include? :password

  UI.info("Initialized with watchers #{watchers.inspect}") if verbose?
  UI.info("Initialized with options #{output.inspect}") unless quiet?
end

Instance Method Details

#quiet?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/guard/autoupload.rb', line 116

def quiet?
  @quiet || false
end

#run_on_changes(paths) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/guard/autoupload.rb', line 54

def run_on_changes(paths)
  local_file = nil
  paths.each do |path|
    path = path.encode(Kconv::UTF8, Encoding::UTF8_MAC) if RUBY_PLATFORM.include? "darwin"

    local_file = File.join(@local, path)
    path.sub!(/^#{@local_subpath}/, '')
    remote_file = File.join(@remote, path)

    if(File.directory?(local_file))
      next
    end

    attempts = 0

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

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

#run_on_removals(paths) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/guard/autoupload.rb', line 91

def run_on_removals(paths)
  paths.each do |path|
    path.sub!(/^#{@local_subpath}/, '')
    remote_file = File.join(@remote, path)

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

    UI.info("Deleted #{path}") unless quiet?
  end

  ::Guard::Notifier.notify "Deleted", :title => "Deleted"
end

#stopObject



120
121
122
123
124
125
# File 'lib/guard/autoupload.rb', line 120

def stop
  UI.info("Tearing down connections") unless quiet?
  if @session.is_a? SCPSession
    @session.close
  end
end

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
  @verbose || false
end