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, use_hash) ⇒ 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, use_hash) ⇒ Synchronizer
Returns a new instance of Synchronizer.
2 3 4 5 6 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 2 def initialize(source_path, target_path, use_hash) @source_path=source_path @target_path=target_path @hash=ImageHash.new(use_hash) end |
Instance Method Details
#copy_file(src, dest) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 54 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
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 8 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
81 82 83 84 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 81 def delete_dir(dir) puts "deleting #{dir}" Dir.delete(dir) end |
#delete_file(file) ⇒ Object
86 87 88 89 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 86 def delete_file(file) puts "deleting #{file}" File.delete(file) end |
#delete_targets_without_source(target_path) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 66 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) source=@hash.without_hash(source) if File.directory? target delete_dir(target) unless Dir.exists? source else unless File.file? source and File.mtime(source)==File.mtime(target) delete_file(target) end end end end |
#do_synchronize_file(source, target, param) ⇒ Object
49 50 51 52 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 49 def do_synchronize_file(source, target, param) puts "copying #{source} -> #{target}" copy_file(source, target) end |
#synchronize(param) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 24 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 33 def synchronize_file(source, target, param) source_time = File.mtime(source) target_found=Dir[@hash.file_search_pattern(target)] target=target_found[0] unless target_found.empty? if !File.file?(target) or File.mtime(target) != source_time or !@hash.hash?(target) target=@hash.without_hash(target) target_dir=File.dirname(target) Dir.mkdir(target_dir) unless File.directory? target_dir do_synchronize_file(source, target, param) if File.file?(target) target=@hash.with_hash(target) File.utime(source_time, source_time, target) end end end |
#target_name(path, param) ⇒ Object
20 21 22 |
# File 'lib/jekyll/image_optimizer/synchronizer.rb', line 20 def target_name(path, param) path+param end |