Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / Windows Forms
Alternative
Article

Solving the .resx Merge Problem in TFS

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
6 Sep 2013CPOL 26.7K   256   7   7
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. 

C#
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)


Written By
Web Developer
Sweden Sweden
Software developer

Comments and Discussions

 
QuestionPerforce Merge Pin
The Chairman6-Feb-15 3:23
The Chairman6-Feb-15 3:23 
QuestionVisual Studio 2012 Pin
StefanH22-Jan-14 6:28
StefanH22-Jan-14 6:28 
AnswerRe: Visual Studio 2012 Pin
StefanH27-Jan-14 1:36
StefanH27-Jan-14 1:36 
GeneralRe: Visual Studio 2012 Pin
Magnus_30-Jan-14 4:45
Magnus_30-Jan-14 4:45 
GeneralRe: Visual Studio 2012 Pin
EM3R50N30-Jun-16 0:13
EM3R50N30-Jun-16 0:13 
GeneralMy vote of 5 Pin
Tom Clement6-Sep-13 5:17
professionalTom Clement6-Sep-13 5:17 
Hi Magnus, Thanks for contributing this to the community. It's a great addition to the original article!
GeneralRe: My vote of 5 Pin
Magnus_6-Sep-13 5:27
Magnus_6-Sep-13 5:27 

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

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