Class: Epdiff
- Inherits:
-
Object
- Object
- Epdiff
- Defined in:
- lib/epdiff.rb,
lib/epdiff/version.rb
Overview
EPUB diff class
Constant Summary collapse
- TEXT_EXT =
%w[xhtml html xml opf css txt].freeze
- VERSION =
'2.0.0'
Class Method Summary collapse
Instance Method Summary collapse
- #binary_diff(path, path1, path2) ⇒ Object
- #execute(*args) ⇒ Object
-
#initialize ⇒ Epdiff
constructor
A new instance of Epdiff.
- #show_diff(_file1, _file2, work_dir) ⇒ Object
- #text_diff(_path, path1, path2) ⇒ Object
- #text_file?(path) ⇒ Boolean
- #unzip(filename, dir) ⇒ Object
Constructor Details
#initialize ⇒ Epdiff
Returns a new instance of Epdiff.
19 20 21 22 23 24 25 |
# File 'lib/epdiff.rb', line 19 def initialize @pastel = Pastel.new(enabled: true) @green = @pastel.green.detach @red = @pastel.red.detach @cyan = @pastel.cyan.detach @color = true end |
Class Method Details
.execute(*args) ⇒ Object
15 16 17 |
# File 'lib/epdiff.rb', line 15 def self.execute(*args) new.execute(*args) end |
Instance Method Details
#binary_diff(path, path1, path2) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/epdiff.rb', line 86 def binary_diff(path, path1, path2) content1 = File.binread(path1) content2 = File.binread(path2) return if content1 == content2 @exit_code = 1 = "DIFF: #{path} has some differences.\n" print @color ? @cyan.call() : end |
#execute(*args) ⇒ Object
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 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/epdiff.rb', line 97 def execute(*args) tmpdir = work_dir = nil = OptionParser.new do |opts| opts. = "Usage: epdiff [options] [filename] [filename]\n" opts.on('-t', '--tmpdir DIR', 'Set tepmorary directory') do |dir| tmpdir = dir end opts.on('-h', '--help', 'Show this help message') do puts opts exit end opts.on('-C', '--no-color', 'Not use color diff') do @color = false end opts.on('-v', '--version', 'Show version number') do puts Epdiff::VERSION exit end end begin .parse!(args) if args.size != 2 # invalid option puts exit end work_dir = tmpdir || Dir.mktmpdir file1, file2 = *args FileUtils.mkdir_p("#{work_dir}/file1") FileUtils.mkdir_p("#{work_dir}/file2") unzip(file1, "#{work_dir}/file1") unzip(file2, "#{work_dir}/file2") @exit_code = 0 show_diff(file1, file2, work_dir) @exit_code rescue StandardError => e warn e ensure FileUtils.rm_rf(work_dir) if work_dir && !tmpdir end end |
#show_diff(_file1, _file2, work_dir) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/epdiff.rb', line 39 def show_diff(_file1, _file2, work_dir) Dir.chdir(work_dir) do files1 = Dir.glob('file1/**/*') files2 = Dir.glob('file2/**/*') files1_s = files1.map { |d| d.sub(%r{^file1/}, '') } files2_s = files2.map { |d| d.sub(%r{^file2/}, '') } files_common = files1_s & files2_s files_common.each do |path| if File.file?("file1/#{path}") if text_file?("file1/#{path}") text_diff(path, "file1/#{path}", "file2/#{path}") else binary_diff(path, "file1/#{path}", "file2/#{path}") end end end (files1_s - files2_s).each do |diff1| @exit_code = 1 = "DIFF: #{diff1} only exists in 1st.\n" print @color ? @red.call() : end (files2_s - files1_s).each do |diff2| @exit_code = 1 = "DIFF: #{diff2} only exists in 2nd.\n" print @color ? @green.call() : end end end |
#text_diff(_path, path1, path2) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/epdiff.rb', line 73 def text_diff(_path, path1, path2) diff = if @color TTY::File.diff(path1, path2, verbose: false) else TTY::File.diff(path1, path2, verbose: false, color: nil) end return if diff == "No differences found\n" || diff.strip == '' @exit_code = 1 print diff end |
#text_file?(path) ⇒ Boolean
68 69 70 71 |
# File 'lib/epdiff.rb', line 68 def text_file?(path) extname = File.extname(path).sub(/\A\./, '') extname if TEXT_EXT.include?(extname) end |
#unzip(filename, dir) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/epdiff.rb', line 27 def unzip(filename, dir) Zip::InputStream.open(filename) do |zio| while entry = zio.get_next_entry # rubocop:disable Lint/AssignmentInCondition next unless entry.file? entry_filename = File.join(dir, entry.name) FileUtils.mkdir_p File.dirname(entry_filename) File.binwrite(entry_filename, zio.read) end end end |