Click here to Skip to main content
11,572,828 members (58,470 online)
Click here to Skip to main content

Solving the .resx Merge Problem in TFS

, 6 Sep 2013 CPOL 8.2K 124 7
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

You may also be interested in...

Comments and Discussions

 
QuestionPerforce Merge Pin
The Chairman6-Feb-15 3:23
memberThe Chairman6-Feb-15 3:23 
QuestionVisual Studio 2012 Pin
StefanH22-Jan-14 6:28
memberStefanH22-Jan-14 6:28 
AnswerRe: Visual Studio 2012 Pin
StefanH27-Jan-14 1:36
memberStefanH27-Jan-14 1:36 
GeneralRe: Visual Studio 2012 Pin
Magnus_30-Jan-14 4:45
memberMagnus_30-Jan-14 4:45 
GeneralMy vote of 5 Pin
Tom Clement6-Sep-13 5:17
mentorTom Clement6-Sep-13 5:17 
GeneralRe: My vote of 5 Pin
Magnus_6-Sep-13 5:27
memberMagnus_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 | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 6 Sep 2013
Article Copyright 2013 by Magnus_
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid