Class: DirectoryPush::Cli
- Inherits:
-
Object
- Object
- DirectoryPush::Cli
- Defined in:
- lib/directory_push.rb
Constant Summary collapse
- FILTER_FILE_NAME =
'.rsync-filter'- DEFAULT_RSYNC_OPTIONS =
[ '--delete', '--verbose', '--archive', '--compress', '--progress', %Q{--exclude-from '#{FILTER_FILE_NAME}'}, %Q{--timeout '9999'} ]
- GUARDFILE_TEXT =
%q{ require 'yaml' require 'guard/compat/plugin' config = YAML.load(File.read(File.join(File.dirname(__FILE__), 'config.yml'))) rsync_options = ['rsync'] + config.delete(:rsync) source = config.delete(:source) ignore_pattern = config.delete(:ignore) remote = %Q{#{config.delete(:user)}@#{config.delete(:remote_address)}:"#{config.delete(:destination)}"} rsync_options << source << remote $rsync_command = rsync_options.join(' ') rsync_options_without_delete = rsync_options.reject { |o| o.match('--del') } $rsync_command_without_delete = rsync_options_without_delete.join(' ') module ::Guard class DirectoryPush < Plugin def start() sync end def reload() sync end def run_all() sync end def run_on_additions(paths) Compat::UI.info "Guard::DirectoryPush Files created: #{paths}." sync_without_delete end def run_on_modifications(paths) Compat::UI.info "Guard::DirectoryPush Files changed: #{paths}." sync_without_delete end def run_on_removals(paths) Compat::UI.info "Guard::DirectoryPush Files removed: #{paths}." sync_without_delete end private def sync() Compat::UI.info %Q{Guard::DirectoryPush `#{$rsync_command}`.} system $rsync_command end def sync_without_delete() Compat::UI.info %Q{Guard::DirectoryPush `#{$rsync_command_without_delete}`.} system $rsync_command_without_delete 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
-
#directory_path ⇒ Object
readonly
Returns the value of attribute directory_path.
-
#path_on_remote ⇒ Object
readonly
Returns the value of attribute path_on_remote.
-
#remote_address ⇒ Object
readonly
Returns the value of attribute remote_address.
-
#rsync_options ⇒ Object
readonly
Returns the value of attribute rsync_options.
-
#settings_dir_path ⇒ Object
readonly
Returns the value of attribute settings_dir_path.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #backup_dir_path(d) ⇒ Object
- #config ⇒ Object
- #config_path ⇒ Object
- #config_to_yml ⇒ Object
- #directory_name ⇒ Object
- #ensure_config_file_present ⇒ Object
- #ensure_filter_file_present ⇒ Object
- #ensure_guardfile_present ⇒ Object
- #ensure_setting_dir_present ⇒ Object
- #filter_path ⇒ Object
- #guardfile_path ⇒ Object
-
#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
constructor
A new instance of Cli.
- #pull? ⇒ Boolean
- #pull_from_remote(destination) ⇒ Object
- #sync(source, destination) ⇒ Object
- #watch ⇒ Object
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.
92 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 |
# 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.(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 = @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_path ⇒ Object (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_remote ⇒ Object (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_address ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute rsync_options.
11 12 13 |
# File 'lib/directory_push.rb', line 11 def @rsync_options end |
#settings_dir_path ⇒ Object (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 |
#user ⇒ Object (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
215 216 217 |
# File 'lib/directory_push.rb', line 215 def backup_dir_path(d) File.join settings_dir_path, "#{File.basename(d)}.bak" end |
#config ⇒ Object
167 168 169 170 171 172 173 174 175 176 |
# 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 ⇒ Object
150 |
# File 'lib/directory_push.rb', line 150 def config_path() File.join(settings_dir_path, 'config.yml') end |
#config_to_yml ⇒ Object
178 |
# File 'lib/directory_push.rb', line 178 def config_to_yml() YAML.dump(config) end |
#directory_name ⇒ Object
140 |
# File 'lib/directory_push.rb', line 140 def directory_name() File.basename(@directory_path) end |
#ensure_config_file_present ⇒ Object
180 181 182 183 184 185 186 |
# 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 ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# 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 ⇒ Object
188 189 190 191 192 193 194 |
# 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 ⇒ Object
142 143 144 145 146 147 |
# 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 ⇒ Object
149 |
# File 'lib/directory_push.rb', line 149 def filter_path() File.join(settings_dir_path, FILTER_FILE_NAME) end |
#guardfile_path ⇒ Object
151 |
# File 'lib/directory_push.rb', line 151 def guardfile_path() File.join(settings_dir_path, 'Guardfile') end |
#pull? ⇒ Boolean
138 |
# File 'lib/directory_push.rb', line 138 def pull?() @pull end |
#pull_from_remote(destination) ⇒ Object
196 197 198 199 200 201 202 |
# 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) ⇒ Object
204 205 206 207 208 209 210 211 212 213 |
# 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 ⇒ Object
219 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 |
# 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 |