Module: Epdiff

Extended by:
Epdiff
Included in:
Epdiff
Defined in:
lib/epdiff.rb,
lib/epdiff/version.rb

Constant Summary collapse

VERSION =
"1.0.2"

Instance Method Summary collapse

Instance Method Details

#execute(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/epdiff.rb', line 26

def execute(*args)

  tmpdir = Dir.mktmpdir
  diff_path = "diff"
  unzip_path = nil

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: epdiff [options] [filename] [filename]\n"

    opts.on('-t','--tmpdir DIR', 'Set tepmorary directory') do |dir|
      tmpdir = dir
    end

    opts.on('--diff-command PATH', 'Specify diff command path') do |path|
      diff_path = path
    end

    opts.on('--unzip-command PATH', 'Specify unzip command path') do |path|
      unzip_path = path
    end

    opts.on('-h', '--help', 'Show this help message') do
      puts opts
      exit
    end

    opts.on('-v', '--version', 'Show version number') do
      puts Epdiff::VERSION
      exit
    end
  end

  begin

    opts.parse!(args)

    if args.size != 2
      # invalid option
      puts opts
      exit
    end

    file1, file2 = *args
    FileUtils.mkdir_p(tmpdir+"/file1")
    FileUtils.mkdir_p(tmpdir+"/file2")

    if unzip_path
      %x("#{unzip_path}" "#{file1}" -d "#{tmpdir}/file1")
      %x("#{unzip_path}" "#{file2}" -d "#{tmpdir}/file2")
    else
      unzip(file1, "#{tmpdir}/file1")
      unzip(file2, "#{tmpdir}/file2")
    end

    IO.popen("'#{diff_path}' -r -u '#{tmpdir}/file1' '#{tmpdir}/file2'") do |io|
      print io.read.gsub(/#{Regexp.escape(tmpdir)}/, "")
    end

  rescue => e
    warn e
    puts opts
  end
end

#unzip(filename, dir) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/epdiff.rb', line 10

def unzip(filename, dir)
  Zip::InputStream.open(filename) do |zio|
    while entry = zio.get_next_entry
      entry_filename = File.join(dir, entry.name)
      if  entry.name_is_directory?
        FileUtils.mkdir_p File.dirname(entry_filename)
      elsif entry.file?
        FileUtils.mkdir_p File.dirname(entry_filename)
        File.open(entry_filename, "wb") do |f|
          f.write zio.read
        end
      end
    end
  end
end