Class: DirectoryPush::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/directory_push.rb

Constant Summary collapse

FILTER_FILE_NAME =
'.rsync-filter'
DEFAULT_RSYNC_OPTIONS =
[
  '--verbose',
  '--archive',
  '--compress',
  '--progress',
  %Q{--exclude-from '#{FILTER_FILE_NAME}'},
  %Q{--timeout '9999'}
]
GUARDFILE_TEXT =
%q{
require 'yaml'
require 'guard/compat/plugin'
require 'rsync'

config = YAML.load(File.read(File.join(File.dirname(__FILE__), 'config.yml')))
$rsync_options = config.delete(:rsync)
$rsync_options << '--delete'
$source = config.delete(:source)
ignore_pattern = config.delete(:ignore)
$remote = %Q{#{config.delete(:user)}@#{config.delete(:remote_address)}:"#{config.delete(:destination)}"}

module ::Guard
  class DirectoryPush < Plugin
    private
    def sync()
      Compat::UI.info %Q{Guard::DirectoryPush source: "#{$source}", destination: "#{$remote}", options: #{$rsync_options}}
      result = Rsync.run $source, $remote, $rsync_options
      if result.success?
        result.changes.each do |change|
          Compat::UI.info "#{change.filename} (#{change.summary})"
        end
      else
        Compat::UI.error result.error
        raise result.error
      end
    end

    public

    alias_method :start, :sync
    alias_method :stop, :sync
    alias_method :reload, :sync
    alias_method :run_all, :sync
    def run_on_additions(paths)
      Compat::UI.info "Guard::DirectoryPush Files created: #{paths}"
      sync
    end
    def run_on_modifications(paths)
      Compat::UI.info "Guard::DirectoryPush Files changed: #{paths}"
      sync
    end
    def run_on_removals(paths)
      Compat::UI.info "Guard::DirectoryPush Files removed: #{paths}"
      sync
    end
  end
end

config[:verbose] = true
config[:cli] = '--color'
config[:sync_on_start] = true

directories [$source]
guard 'directory-push', config do
  watch %r{^.+$}
  if ignore_pattern
    ignore ignore_pattern
  end
end
}
DEFAULT_USER =
Etc.getlogin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory_path, remote_address, user: DEFAULT_USER, path_on_remote: nil, pull: false, rsync_options: DEFAULT_RSYNC_OPTIONS, guard_ignore_pattern: nil, preserve_settings: false) ⇒ Cli

Returns a new instance of Cli.



93
94
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
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/directory_push.rb', line 93

def initialize(
    directory_path,
    remote_address,
    user: DEFAULT_USER,
    path_on_remote: nil,
    pull: false,
    rsync_options: DEFAULT_RSYNC_OPTIONS,
    guard_ignore_pattern: nil,
    preserve_settings: false
)
  if (
    remote_address.nil? ||
    remote_address.empty? ||
    remote_address == '~' ||
    remote_address == '$HOME'
  )
    raise ArgumentError.new(
      %Q{Remote address, "#{remote_address}", is invalid!}
    )
  end

  @directory_path = File.expand_path(directory_path, Dir.pwd)
  unless File.exists?(@directory_path)
    raise ArgumentError.new(
      %Q{Directory "#{@directory_path}" does not exist!}
    )
  end

  @user = user
  if @user.nil? || @user.empty?
    raise ArgumentError.new(%Q{User "#{@user}" is invalid!.})
  end

  @remote_address = remote_address
  @path_on_remote = path_on_remote || @directory_path
  @terminal = HighLine.new
  @pull = pull
  @rsync_options = rsync_options
  @guard_ignore_pattern = guard_ignore_pattern
  @preserve_settings = preserve_settings
  @settings_dir_path = File.join(
    Dir.pwd,
    ".#{directory_name}.directory_push-settings"
  )
end

Instance Attribute Details

#directory_pathObject (readonly)

Returns the value of attribute directory_path.



11
12
13
# File 'lib/directory_push.rb', line 11

def directory_path
  @directory_path
end

#path_on_remoteObject (readonly)

Returns the value of attribute path_on_remote.



11
12
13
# File 'lib/directory_push.rb', line 11

def path_on_remote
  @path_on_remote
end

#remote_addressObject (readonly)

Returns the value of attribute remote_address.



11
12
13
# File 'lib/directory_push.rb', line 11

def remote_address
  @remote_address
end

#rsync_optionsObject (readonly)

Returns the value of attribute rsync_options.



11
12
13
# File 'lib/directory_push.rb', line 11

def rsync_options
  @rsync_options
end

#settings_dir_pathObject (readonly)

Returns the value of attribute settings_dir_path.



11
12
13
# File 'lib/directory_push.rb', line 11

def settings_dir_path
  @settings_dir_path
end

#userObject (readonly)

Returns the value of attribute user.



11
12
13
# File 'lib/directory_push.rb', line 11

def user
  @user
end

Instance Method Details

#backup_dir_path(d) ⇒ Object



216
217
218
# File 'lib/directory_push.rb', line 216

def backup_dir_path(d)
  File.join settings_dir_path, "#{File.basename(d)}.bak"
end

#configObject



168
169
170
171
172
173
174
175
176
177
# File 'lib/directory_push.rb', line 168

def config()
  {
    source: "#{@directory_path}/",
    user: @user,
    remote_address: @remote_address,
    destination: @path_on_remote,
    rsync: @rsync_options,
    ignore: @guard_ignore_pattern
  }
end

#config_pathObject



151
# File 'lib/directory_push.rb', line 151

def config_path() File.join(settings_dir_path, 'config.yml') end

#config_to_ymlObject



179
# File 'lib/directory_push.rb', line 179

def config_to_yml() YAML.dump(config) end

#directory_nameObject



141
# File 'lib/directory_push.rb', line 141

def directory_name() File.basename(@directory_path) end

#ensure_config_file_presentObject



181
182
183
184
185
186
187
# File 'lib/directory_push.rb', line 181

def ensure_config_file_present()
  ensure_setting_dir_present
  unless File.exist?(config_path)
    @terminal.say %Q{Creating config file in "#{config_path}".}
    File.open(config_path, 'w') { |f| f.print config_to_yml }
  end
end

#ensure_filter_file_presentObject



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/directory_push.rb', line 154

def ensure_filter_file_present()
  ensure_setting_dir_present
  @terminal.say("Creating rsync-filter file")
  gitignore = File.join(@directory_path, '.gitignore')
  if File.exists?(gitignore)
     @terminal.say(
      %Q{Using "#{gitignore}" as rsync-filter since they have the same format.}
    )
    FileUtils.cp(gitignore, filter_path)
  else
    FileUtils.touch(filter_path) unless File.exist?(filter_path)
  end
end

#ensure_guardfile_presentObject



189
190
191
192
193
194
195
# File 'lib/directory_push.rb', line 189

def ensure_guardfile_present()
  ensure_setting_dir_present
  unless File.exist?(guardfile_path)
    @terminal.say %Q{Creating Guardfile in "#{guardfile_path}".}
    File.open(guardfile_path, 'w') { |f| f.print GUARDFILE_TEXT }
  end
end

#ensure_setting_dir_presentObject



143
144
145
146
147
148
# File 'lib/directory_push.rb', line 143

def ensure_setting_dir_present()
  unless File.directory?(settings_dir_path)
    @terminal.say("Creating settings directory, \"#{settings_dir_path}\".")
    FileUtils.mkdir(settings_dir_path)
  end
end

#filter_pathObject



150
# File 'lib/directory_push.rb', line 150

def filter_path() File.join(settings_dir_path, FILTER_FILE_NAME) end

#guardfile_pathObject



152
# File 'lib/directory_push.rb', line 152

def guardfile_path() File.join(settings_dir_path, 'Guardfile') end

#pull?Boolean

Returns:

  • (Boolean)


139
# File 'lib/directory_push.rb', line 139

def pull?() @pull end

#pull_from_remote(destination) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/directory_push.rb', line 197

def pull_from_remote(destination)
  source = "#{@user}@#{@remote_address}:#{@path_on_remote}"

  @terminal.say %Q{Pulling from "#{source}" and replacing the contents of "#{destination}".}

  sync source, destination
end

#sync(source, destination) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/directory_push.rb', line 205

def sync(source, destination)
  result = Rsync.run source, destination, @rsync_options
  if result.success?
    result.changes.each do |change|
      @terminal.say "#{change.filename} (#{change.summary})"
    end
  else
    @terminal.say result.error
  end
end

#watchObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/directory_push.rb', line 220

def watch()
  ensure_config_file_present
  ensure_guardfile_present
  ensure_filter_file_present
  Dir.chdir settings_dir_path do |d|
    if pull?
      directory_path_bak = backup_dir_path(@directory_path)
      @terminal.say %Q{Backing up "#{@directory_path}" in "#{directory_path_bak}".}
      FileUtils.cp_r @directory_path, directory_path_bak

      pull_from_remote @directory_path
    else
      source = "#{@user}@#{@remote_address}:#{@path_on_remote}/"
      source_bak = backup_dir_path(@path_on_remote)

      @terminal.say %Q{Backing up "#{source}" in "#{source_bak}".}
      sync source, source_bak
    end

    @terminal.say "Starting Guard"
    command = "guard"
    @terminal.say command
    system command
  end

  unless @preserve_settings
    @terminal.say %Q{Removing settings directory, "#{settings_dir_path}".}
    FileUtils.rm_rf(settings_dir_path)
  end
end