OWASP O2 Platform Blog

Adding file comparison to O2 using (using WinMerge)

In order to quickly be able to compare files from an O2 script (using the great WinMerge tool), I’ve just added support for it.

You can now compare two files by simply making a call to the method file_Compare like this:

file1.file_Compare(file2);

That call will open up the WinMerge tool with those two files as parameters:

To enable this, there is a new API_WinMerge which can be used like this (the above screenshot is the result of this script execution):

var apiWinMerge = new API_WinMerge();
var file1 =  "WinMerge.cs".local(); 
var file2 =  "WinMerge.cs".local()
                          .fileContents()
                          .replace("WinMerge", "WineMerge_Changed!")
                          .save();

//apiWinMerge.openGui(); 
//apiWinMerge.compareTwoFiles(testFile1,testFile2);
file1.file_Compare(file2);
 
//O2File:WinMerge.cs

Note that the first time the API_WinMerge is used, WinMerge will be downloaded and  installed in the folder C:\O2\_tempDir\_ToolsOrAPIs\WinMerge\WinMerge-2.12.4-exe

Here is the source code of the API_WinMerge API (currently located at C:\O2\O2Scripts_Database\_Scripts\3rdParty_Tools\_Installers\WinMerge.cs)

using System;
using System.Diagnostics;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
//O2File:Tool_API.cs
//O2File:_Extra_methods_To_Add_to_Main_CodeBase.cs

using O2.XRules.Database.Utils;
namespace O2.XRules.Database.APIs
{
    public class Install_WinMerge_Test
    {
        public void test()
        {
            new WinMerge().start();
        }
    }
   
    public class WinMerge : Tool_API
    {   
        public WinMerge() : this(true)
        {
        }
       
        public WinMerge(bool installNow)
        {
            config("WinMerge", "WinMerge v2.12.4", "WinMerge-2.12.4-exe.zip");           
            this.Install_Uri = "http://downloads.sourceforge.net/winmerge/WinMerge-2.12.4-exe.zip".uri();
            this.Executable = Install_Dir.pathCombine(@"WinMerge-2.12.4-exe\WinMergeU.exe");
            if (installNow)
                install();       
        }
       
       
        public bool install()
        {
            "Installing {0}".info(ToolName);
            return installFromZip_Web();                        
        }
       
        public Process start()
        {
            return start("");
        }
        public Process start(string parameters)
        {
            if (install())
                return this.Executable.startProcess(parameters);
            return null;
        }       
    }
   
    public class API_WinMerge
    {
        public WinMerge winMerge;
        public Process WinMergeProcess;
       
        public API_WinMerge()
        {
            winMerge = new WinMerge();
        }               
    }
   
    public static class API_WinMerge_ExtensionMethods
    {
        public static API_WinMerge openGui(this API_WinMerge apiWinMerge)
        {
            apiWinMerge.WinMergeProcess = apiWinMerge.winMerge.start();
            return apiWinMerge;
        }
       
        public static API_WinMerge compareTwoFiles(this API_WinMerge apiWinMerge, string file1, string file2)
        {
            var parameters = "\"{0}\" \"{1}".format(file1,file2);
            apiWinMerge.WinMergeProcess = apiWinMerge.winMerge.start(parameters);           
            return apiWinMerge;
        }
               
        public static API_WinMerge file_Compare(this string file1, string file2)
        {
            var winMerge = new API_WinMerge();
            return winMerge.compareTwoFiles(file1,file2);
        }
       
        public static API_WinMerge file_Merge(this string file1, string file2)
        {
            return file1.file_Compare(file2);
        }
    }
}

November 13, 2011 - Posted by | Tools

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: