Class: Synchronizer
- Inherits:
-
Object
- Object
- Synchronizer
- Defined in:
- lib/jekyll/image_optimizer/synchronizer.rb
Direct Known Subclasses
Instance Method Summary collapse
- #copy_file(src, dest) ⇒ Object
- #create_symlink(name, param) ⇒ Object
- #delete_dir(dir) ⇒ Object
- #delete_file(file) ⇒ Object
- #delete_targets_without_source(target_path) ⇒ Object
- #do_synchronize_file(source, target, param) ⇒ Object
-
#initialize(source_path, target_path) ⇒ Synchronizer
constructor
A new instance of Synchronizer.
- #synchronize(param) ⇒ Object
- #synchronize_file(source, target, param) ⇒ Object
- #target_name(path, param) ⇒ Object
Constructor Details
#initialize(source_path, target_path) ⇒ Synchronizer
Returns a new instance of Synchronizer.
2 3 4 5 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 2 def initialize(source_path, target_path) @source_path=source_path @target_path=target_path end |
Instance Method Details
#copy_file(src, dest) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 47 def copy_file(src, dest) File.open(src, 'rb') { |r| File.open(dest, 'wb') { |w| while true w.syswrite r.sysread(1024) end } } rescue IOError # ignored end |
#create_symlink(name, param) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 7 def create_symlink(name, param) target=target_name(@target_path, param) if File::symlink? name return if File::readlink(name)==File::(target) File::delete name else raise "File #{name} already exists" if File::exist? name end puts "creating symlink (#{name}->#{target})" File::symlink(File::(target), name) end |
#delete_dir(dir) ⇒ Object
71 72 73 74 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 71 def delete_dir(dir) puts "deleting #{dir}" Dir.delete(dir) end |
#delete_file(file) ⇒ Object
76 77 78 79 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 76 def delete_file(file) puts "deleting #{file}" File.delete(file) end |
#delete_targets_without_source(target_path) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 59 def delete_targets_without_source(target_path) files_and_dirs_by_length=Dir["#{target_path}/**/*"].sort_by { |dir| File.file?(dir) ? ('a'+dir) : ('b'+(1000-dir.size).to_s) } for target in files_and_dirs_by_length source=target.sub(target_path, @source_path) if File.directory? target delete_dir(target) unless Dir.exists? source else delete_file(target) unless File.file? source end end end |
#do_synchronize_file(source, target, param) ⇒ Object
42 43 44 45 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 42 def do_synchronize_file(source, target, param) puts "copying #{source} -> #{target}" copy_file(source, target) end |
#synchronize(param) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 23 def synchronize(param) target_path=target_name(@target_path, param) for source in Dir["#{@source_path}/**/*"] target=source.sub(@source_path, target_path) synchronize_file(source, target, param) if File::file? source end delete_targets_without_source(target_path) end |
#synchronize_file(source, target, param) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 32 def synchronize_file(source, target, param) source_time = File.mtime(source) if !File.file?(target) or File.mtime(target) != source_time target_dir=File.dirname(target) Dir.mkdir(target_dir) unless File.directory? target_dir do_synchronize_file(source, target, param) File.utime(source_time, source_time, target) if File.file?(target) end end |
#target_name(path, param) ⇒ Object
19 20 21 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 19 def target_name(path, param) path+param end |