Skip to content

Instantly share code, notes, and snippets.

@Tornhoof
Created August 21, 2017 15:14
Show Gist options
  • Save Tornhoof/3710df4711900339d69a1cc99abf3ec2 to your computer and use it in GitHub Desktop.
Save Tornhoof/3710df4711900339d69a1cc99abf3ec2 to your computer and use it in GitHub Desktop.
Converts Project references
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
namespace FixProjectFormat
{
class Program
{
static void Main(string[] args)
{
var projects = Directory.GetFiles(@"C:\Development\Branches\FixProject\Backend", "*.csproj",
SearchOption.AllDirectories);
foreach (var project in projects)
{
FixProject(project);
}
}
private static void FixProject(string project)
{
var packages = ReadPackagesConfig(Path.GetDirectoryName(project));
var document = new XmlDocument();
document.Load(project);
var ns = new XmlNamespaceManager(document.NameTable);
ns.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");
//1. delete packages.config
var node = document.SelectSingleNode("//*[@Include='packages.config']", ns);
node?.ParentNode.RemoveChild(node);
//2. change app.config
CleanAppConfig(Path.GetDirectoryName(project));
// 3. fix packages
var allReferences = document.SelectNodes("//x:ItemGroup//x:Reference", ns);
foreach (var allReference in allReferences.OfType<XmlElement>())
{
var name = allReference.GetAttribute("Include");
var split = name.Split(',');
if (split.Length > 0)
{
var asmName = split[0];
if(packages.TryGetValue(asmName, out string version))
{
var newNode = allReference.OwnerDocument.CreateElement("PackageReference",
"http://schemas.microsoft.com/developer/msbuild/2003");
newNode.SetAttribute("Include", asmName);
newNode.SetAttribute("Version", version);
allReference.ParentNode.AppendChild(newNode);
allReference.ParentNode.RemoveChild(allReference);
}
else
{
var hintPath = allReference.SelectSingleNode("x:HintPath",ns)?.InnerText;
if (hintPath != null)
{
// this assumes your packages are in a directory called packages
var match = Regex.Match(hintPath, @".*?\\packages\\([\w\.]+?)\.\d+.*");
if (match.Success)
{
asmName = match.Groups[1].Value;
if (packages.TryGetValue(asmName, out version))
{
var newNode = allReference.OwnerDocument.CreateElement("PackageReference",
"http://schemas.microsoft.com/developer/msbuild/2003");
newNode.SetAttribute("Include", asmName);
newNode.SetAttribute("Version", version);
allReference.ParentNode.AppendChild(newNode);
allReference.ParentNode.RemoveChild(allReference);
}
}
}
}
}
}
document.Save(project);
}
private static void CleanAppConfig(string projectDirectory)
{
var document = new XmlDocument();
var appConfig = Path.Combine(projectDirectory, "app.config");
if (File.Exists(appConfig))
{
document.Load(appConfig);
var ns = new XmlNamespaceManager(document.NameTable);
ns.AddNamespace("y", "urn:schemas-microsoft-com:asm.v1");
var asmBindings = document.SelectSingleNode("/configuration/runtime/y:assemblyBinding", ns);
if (asmBindings != null)
{
asmBindings.ParentNode.RemoveChild(asmBindings);
document.Save(appConfig);
}
}
}
private static Dictionary<string,string> ReadPackagesConfig(string projectDirectory)
{
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var document = new XmlDocument();
var packagesConfigFile = Path.Combine(projectDirectory, "packages.config");
if (File.Exists(packagesConfigFile))
{
document.Load(packagesConfigFile);
var nodes = document.SelectNodes("/packages/package");
foreach (var node in nodes.OfType<XmlElement>())
{
result.Add(node.GetAttribute("id"), node.GetAttribute("version"));
}
}
return result;
}
}
}
@rickydutton
Copy link

This is genius, worked perfectly for me! Thanks a lot for sharing!

@haligasd
Copy link

Ok thank you so much bro. was about to start hurting someone. this works like a charm

@equitel
Copy link

equitel commented Feb 8, 2018

Many thanks, this is PERFECT!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment