Created
November 22, 2017 22:01
-
-
Save rquackenbush/8d3b39683dc6dfe9876bc228eacbe53f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Xml; | |
using System.Xml.Linq; | |
using System.Xml.XPath; | |
using System.Linq; | |
using System.Text; | |
namespace CsProjectMigrator | |
{ | |
public class Migrator | |
{ | |
public void Migrate(string csProjectFilePath) | |
{ | |
string projectDirectory = Path.GetDirectoryName(csProjectFilePath); | |
if (string.IsNullOrWhiteSpace(projectDirectory)) | |
throw new InvalidOperationException(); | |
//Get the project references | |
var project = new CsProject(csProjectFilePath); | |
string packagesConfigPath = Path.Combine(projectDirectory, "packages.config"); | |
PackagesConfig packagesConfig = null; | |
if (File.Exists(packagesConfigPath)) | |
{ | |
packagesConfig = new PackagesConfig(packagesConfigPath); | |
} | |
StringBuilder text = new StringBuilder(); | |
text.AppendLine("<Project Sdk=\"Microsoft.NET.Sdk\">"); | |
text.AppendLine(" <PropertyGroup>"); | |
text.AppendLine(" <TargetFrameworks>netstandard2.0;net46</TargetFrameworks>"); | |
text.AppendLine(" </PropertyGroup>"); | |
text.AppendLine(); | |
if (project.ProjectReferences.Any()) | |
{ | |
text.AppendLine(" <ItemGroup>"); | |
foreach (var projectReference in project.ProjectReferences) | |
{ | |
text.AppendLine($" <ProjectReference Include=\"{projectReference}\" />"); | |
} | |
text.AppendLine(" </ItemGroup>"); | |
text.AppendLine(); | |
} | |
if (packagesConfig != null && packagesConfig.Packages.Any()) | |
{ | |
text.AppendLine(" <ItemGroup>"); | |
foreach (var package in packagesConfig.Packages) | |
{ | |
text.AppendLine($" <PackageReference Include=\"{package.Id}\" Version=\"{package.Version}\" />"); | |
} | |
text.AppendLine(" </ItemGroup>"); | |
} | |
text.AppendLine("</Project>"); | |
//Back up the project file | |
File.Move(csProjectFilePath, csProjectFilePath + ".bak"); | |
//Write out the new one | |
File.WriteAllText(csProjectFilePath, text.ToString()); | |
//Console.WriteLine(text.ToString()); | |
} | |
public class CsProject | |
{ | |
public List<string> ProjectReferences { get; } = new List<string>(); | |
public CsProject(string path) | |
{ | |
//Load up the project | |
XDocument projectDocument = XDocument.Load(path); | |
XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable()); | |
xnm.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); | |
const string query = "/x:Project/x:ItemGroup/x:ProjectReference"; | |
var projectReferenceElements = projectDocument.XPathSelectElements(query, xnm); | |
foreach (var reference in projectReferenceElements) | |
{ | |
ProjectReferences.Add(reference.Attribute("Include")?.Value); | |
} | |
} | |
} | |
public class Package | |
{ | |
public string Id { get; set; } | |
public string Version { get; set; } | |
} | |
public class PackagesConfig | |
{ | |
public List<Package> Packages { get; } = new List<Package>(); | |
public PackagesConfig(string path) | |
{ | |
//Load up the project | |
XDocument configDocument = XDocument.Load(path); | |
var packages = configDocument.XPathSelectElements("/packages/package"); | |
foreach (var package in packages) | |
{ | |
string id = package.Attribute("id").Value; | |
string version = package.Attribute("version").Value; | |
Packages.Add(new Package() | |
{ | |
Id = id, | |
Version = version | |
}); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment