Ruby Patch to Zip

From EggeWiki
Jump to navigation Jump to search

I needed to take a patch file, which contained patches from more than one Eclipse project, and create a zip file with each file contained within.

The Eclipse patch file basically looks like:

<geshi lang="patch">

      1. Eclipse Workspace Patch 1.0
  1. P My Little Project

Index: src/com/MyClass.java

=======================================================

RCS file: /usr/dev/cvs/cvsroot/My Little Project/src/com/MyClass.java,v retrieving revision 1.11.2.2.8.3 diff -u -r1.11.2.2.8.3 MyClass.java --- src/com/MyClass.java 24 May 2007 01:20:56 -0000 1.11.2.2.8.3 +++ src/com/MyClass.java 5 Sep 2007 01:00:23 -0000 @@ -338,8 +338,6 @@

private void setupCustomers( StringBuffer errorBuf_ )
{

- // a comment I removed -

    System.out.println("Hello customer");

</geshi>

You have a project definition, followed by files in that project. You can have multiple project definitions. I created a Ruby script to parse the file and keep track of the project name. The script then outputs the qualified path to each file it encounters. Sure, I could have done this with awk, but Ruby is more enjoyable and readable.

<geshi lang="ruby">

  1. !/bin/env ruby

ARGV.each do |arg|

 if !File.exists?(arg) then
   raise(RuntimeError,"#{arg} does not exist!")
 end
 File.open(arg, 'r') do |infile|
   while(line = infile.gets)
     if line =~ /^#P ([^\r]*)/ then # ignore any new line characters

project = $1

     end
     if line =~ /^Index: ([^\r]*)/ then

puts '"' + project + '/' + $1 + '"'

     end
   end
 end

end </geshi>

Lastly, to zip up these files, I used bash/Cygwin.

<geshi lang="bash"> ./patchToFiles.rb patches/mypatch.patch | xargs zip mypatch.zip </geshi>