Click here to Skip to main content
Click here to Skip to main content
Alternative Article

Solving the .resx Merge Problem in TFS

, 6 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Solving the .resx Merge Problem "

 Introduction

This expands on the original article on how to solve the merging problem of .resx files with an TFS implementation. But could be easily tweaked to work with ant diff merge application. 

Using the code  

In Visual Studio do the following: Tools -> Options ->Source Control -> Visual Studio Team foundation server -> Configure user tools

Click Add and write:
Extension: .resx
Operation: Compare
Command : C:\SortRESX.exe
Arguments: compare %1 %2 %6 %7 %5

Click Ok and then Add and write:
Extension: .resx
Operation: Merge
Command : C:\SortRESX.exe
Arguments: merge %1 %2 %3 %4 %6 %7 %8 

Code

The program uses the sort from the original article before launching the TFS merge utility. 

class Program
{
    private const string DiffMergeLocation = 
      @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\diffmerge.exe";

    static void Main(string[] args)
    {
        //Are we mergeing or comparing
        if (String.Equals(args[0], "merge", StringComparison.CurrentCultureIgnoreCase))
        {
            HandleMerge(args);
        }
        else if (String.Equals(args[0], "compare", StringComparison.CurrentCultureIgnoreCase))
        {
            HandleCompare(args);
        }
        else
        {
            throw new NotSupportedException("Unknown mode, use either merge or compare");
        }
    }

    private static void HandleCompare(string[] args)
    {
        var theirsSource = args[1];
        var theirs = Path.GetTempFileName();

        var yoursSource = args[2];
        var yours = Path.GetTempFileName();

        try
        {
            //Sort input files
            var doc = XDocument.Load(theirsSource);
            var sortedDoc = SortDataByName(doc);
            sortedDoc.Save(theirs);

            doc = XDocument.Load(yoursSource);
            sortedDoc = SortDataByName(doc);
            sortedDoc.Save(yours);

            var theirLabel = args[3];
            var yoursLabel = args[4];
            //var options = args.Length >= 6 ? "\"" + args[5] + "\"" : "";

            //Launch diffmerge and wait for exit
            var start = new ProcessStartInfo
            {
                Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\" 
                  \"{3}\" /ignorespace", theirs, yours, theirLabel, yoursLabel),
                FileName = DiffMergeLocation,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden
            };

            // Run the external process & wait for it to finish
            using (var proc = Process.Start(start))
            {
                proc.WaitForExit();
            }
        }
        finally
        {
            //Clean up temp files
            File.Delete(theirs);
            File.Delete(yours);
        }
    }

    private static void HandleMerge(string[] args)
    {
        var theirsSource = args[1];
        var theirs = Path.GetTempFileName();

        var yoursSource = args[2];
        var yours = Path.GetTempFileName();

        var baseFileSource = args[3];
        var baseFile = Path.GetTempFileName();

        var merged = args[4];
        var theirLabel = args[5];
        var yoursLabel = args[6];

        try
        {
            //Sort input files
            var doc = XDocument.Load(theirsSource);
            var sortedDoc = SortDataByName(doc);
            sortedDoc.Save(theirs);

            doc = XDocument.Load(yoursSource);
            sortedDoc = SortDataByName(doc);
            sortedDoc.Save(yours);

            doc = XDocument.Load(baseFileSource);
            sortedDoc = SortDataByName(doc);
            sortedDoc.Save(baseFile);

            //Launch diffmerge and wait for exit
            var start = new ProcessStartInfo
            {
                Arguments = string.Format("/merge \"{0}\" \"{1}\" \"{2}\" 
                  \"{3}\" \"{4}\" \"{5}\"", theirs, yours, baseFile, merged, theirLabel, yoursLabel),
                FileName = DiffMergeLocation,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden
            };

            // Run the external process & wait for it to finish
            using (var proc = Process.Start(start))
            {
                proc.WaitForExit();
            }
        }
        finally
        {
            //Clean up temp files
            File.Delete(theirs);
            File.Delete(yours);
            File.Delete(baseFile);
        }
    }

    /// <summary>
    /// Use Linq to sort the elements.  The comment, schema, resheader, assembly, metadata, data appear in that order, 
    /// with resheader, assembly, metadata and data elements sorted by name attribute.
    /// </summary>
    /// <param name="resx"></param>
    /// <returns></returns>
    private static XDocument SortDataByName(XDocument resx)
    {
        return new XDocument(
            new XElement(resx.Root.Name,
                 resx.Root.Nodes().Where(comment => comment.NodeType == XmlNodeType.Comment),
                 resx.Root.Elements().Where(schema => schema.Name.LocalName == "schema"),
                 resx.Root.Elements("resheader").OrderBy(resheader => (string) resheader.Attribute("name")),
                 resx.Root.Elements("assembly").OrderBy(assembly => (string) assembly.Attribute("name")),
                 resx.Root.Elements("metadata").OrderBy(metadata => (string) metadata.Attribute("name")),
                 resx.Root.Elements("data").OrderBy(data => (string) data.Attribute("name"))
            )
        );
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Magnus_
Web Developer
Sweden Sweden
Software developer

Comments and Discussions

 
QuestionVisual Studio 2012 PinmemberStefanH22-Jan-14 6:28 
AnswerRe: Visual Studio 2012 PinmemberStefanH27-Jan-14 1:36 
GeneralRe: Visual Studio 2012 PinmemberMagnus_30-Jan-14 4:45 
GeneralMy vote of 5 PinmentorTom Clement6-Sep-13 5:17 
GeneralRe: My vote of 5 PinmemberMagnus_6-Sep-13 5:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 6 Sep 2013
Article Copyright 2013 by Magnus_
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid