class DirectoryPush::Cli
Constants
- DEFAULT_RSYNC_OPTIONS
- DEFAULT_USER
- FILTER_FILE_NAME
- GUARDFILE_TEXT
Attributes
directory_path[R]
path_on_remote[R]
remote_address[R]
rsync_options[R]
settings_dir_path[R]
user[R]
Public Class Methods
new( 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 )
click to toggle source
# File lib/directory_push.rb, line 92 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
Public Instance Methods
backup_dir_path(d)
click to toggle source
# File lib/directory_push.rb, line 215 def backup_dir_path(d) File.join settings_dir_path, "#{File.basename(d)}.bak" end
config()
click to toggle source
# File lib/directory_push.rb, line 167 def config() { source: "#{@directory_path}/", user: @user, remote_address: @remote_address, destination: @path_on_remote, rsync: @rsync_options, ignore: @guard_ignore_pattern } end
config_path()
click to toggle source
# File lib/directory_push.rb, line 150 def config_path() File.join(settings_dir_path, 'config.yml') end
config_to_yml()
click to toggle source
# File lib/directory_push.rb, line 178 def config_to_yml() YAML.dump(config) end
directory_name()
click to toggle source
# File lib/directory_push.rb, line 140 def directory_name() File.basename(@directory_path) end
ensure_config_file_present()
click to toggle source
# File lib/directory_push.rb, line 180 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_present()
click to toggle source
# File lib/directory_push.rb, line 153 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_present()
click to toggle source
# File lib/directory_push.rb, line 188 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_present()
click to toggle source
# File lib/directory_push.rb, line 142 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_path()
click to toggle source
# File lib/directory_push.rb, line 149 def filter_path() File.join(settings_dir_path, FILTER_FILE_NAME) end
guardfile_path()
click to toggle source
# File lib/directory_push.rb, line 151 def guardfile_path() File.join(settings_dir_path, 'Guardfile') end
pull?()
click to toggle source
# File lib/directory_push.rb, line 138 def pull?() @pull end
pull_from_remote(destination)
click to toggle source
# File lib/directory_push.rb, line 196 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)
click to toggle source
# File lib/directory_push.rb, line 204 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
watch()
click to toggle source
# File lib/directory_push.rb, line 219 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