|
|
|
@ -29,10 +29,11 @@ namespace ICSharpCode.Decompiler.Solution
@@ -29,10 +29,11 @@ namespace ICSharpCode.Decompiler.Solution
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class SolutionCreator |
|
|
|
|
{ |
|
|
|
|
private static readonly XNamespace ProjectFileNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); |
|
|
|
|
static readonly XNamespace NonSDKProjectFileNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); |
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a solution file to the specified <paramref name="targetFile"/>.
|
|
|
|
|
/// Also fixes intra-solution project references in the project files.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="targetFile">The full path of the file to write.</param>
|
|
|
|
|
/// <param name="projects">The projects contained in this solution.</param>
|
|
|
|
@ -40,7 +41,7 @@ namespace ICSharpCode.Decompiler.Solution
@@ -40,7 +41,7 @@ namespace ICSharpCode.Decompiler.Solution
|
|
|
|
|
/// <exception cref="ArgumentException">Thrown when <paramref name="targetFile"/> is null or empty.</exception>
|
|
|
|
|
/// <exception cref="ArgumentNullException">Thrown when <paramref name="projects"/> is null.</exception>
|
|
|
|
|
/// <exception cref="InvalidOperationException">Thrown when <paramref name="projects"/> contains no items.</exception>
|
|
|
|
|
public static void WriteSolutionFile(string targetFile, IEnumerable<ProjectItem> projects) |
|
|
|
|
public static void WriteSolutionFile(string targetFile, List<ProjectItem> projects) |
|
|
|
|
{ |
|
|
|
|
if (string.IsNullOrWhiteSpace(targetFile)) |
|
|
|
|
{ |
|
|
|
@ -62,10 +63,10 @@ namespace ICSharpCode.Decompiler.Solution
@@ -62,10 +63,10 @@ namespace ICSharpCode.Decompiler.Solution
|
|
|
|
|
WriteSolutionFile(writer, projects, targetFile); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
FixProjectReferences(projects); |
|
|
|
|
FixAllProjectReferences(projects); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void WriteSolutionFile(TextWriter writer, IEnumerable<ProjectItem> projects, string solutionFilePath) |
|
|
|
|
static void WriteSolutionFile(TextWriter writer, List<ProjectItem> projects, string solutionFilePath) |
|
|
|
|
{ |
|
|
|
|
WriteHeader(writer); |
|
|
|
|
WriteProjects(writer, projects, solutionFilePath); |
|
|
|
@ -90,7 +91,7 @@ namespace ICSharpCode.Decompiler.Solution
@@ -90,7 +91,7 @@ namespace ICSharpCode.Decompiler.Solution
|
|
|
|
|
writer.WriteLine("MinimumVisualStudioVersion = 10.0.40219.1"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void WriteProjects(TextWriter writer, IEnumerable<ProjectItem> projects, string solutionFilePath) |
|
|
|
|
static void WriteProjects(TextWriter writer, List<ProjectItem> projects, string solutionFilePath) |
|
|
|
|
{ |
|
|
|
|
foreach (var project in projects) |
|
|
|
|
{ |
|
|
|
@ -103,7 +104,7 @@ namespace ICSharpCode.Decompiler.Solution
@@ -103,7 +104,7 @@ namespace ICSharpCode.Decompiler.Solution
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static IEnumerable<string> WriteSolutionConfigurations(TextWriter writer, IEnumerable<ProjectItem> projects) |
|
|
|
|
static List<string> WriteSolutionConfigurations(TextWriter writer, List<ProjectItem> projects) |
|
|
|
|
{ |
|
|
|
|
var platforms = projects.GroupBy(p => p.PlatformName).Select(g => g.Key).ToList(); |
|
|
|
|
|
|
|
|
@ -125,10 +126,10 @@ namespace ICSharpCode.Decompiler.Solution
@@ -125,10 +126,10 @@ namespace ICSharpCode.Decompiler.Solution
|
|
|
|
|
return platforms; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void WriteProjectConfigurations( |
|
|
|
|
static void WriteProjectConfigurations( |
|
|
|
|
TextWriter writer, |
|
|
|
|
IEnumerable<ProjectItem> projects, |
|
|
|
|
IEnumerable<string> solutionPlatforms) |
|
|
|
|
List<ProjectItem> projects, |
|
|
|
|
List<string> solutionPlatforms) |
|
|
|
|
{ |
|
|
|
|
writer.WriteLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution"); |
|
|
|
|
|
|
|
|
@ -152,47 +153,80 @@ namespace ICSharpCode.Decompiler.Solution
@@ -152,47 +153,80 @@ namespace ICSharpCode.Decompiler.Solution
|
|
|
|
|
writer.WriteLine("\tEndGlobalSection"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void FixProjectReferences(IEnumerable<ProjectItem> projects) |
|
|
|
|
static void FixAllProjectReferences(List<ProjectItem> projects) |
|
|
|
|
{ |
|
|
|
|
var projectsMap = projects.ToDictionary(p => p.ProjectName, p => p); |
|
|
|
|
var projectsMap = projects.ToDictionary( |
|
|
|
|
p => p.ProjectName, |
|
|
|
|
p => p); |
|
|
|
|
|
|
|
|
|
foreach (var project in projects) |
|
|
|
|
{ |
|
|
|
|
XDocument projectDoc = XDocument.Load(project.FilePath); |
|
|
|
|
|
|
|
|
|
if (projectDoc.Root?.Name.LocalName != "Project") |
|
|
|
|
{ |
|
|
|
|
throw new InvalidOperationException( |
|
|
|
|
$"The file {project.FilePath} is not a valid project file, " + |
|
|
|
|
$"no <Project> at the root; could not fix project references."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// sdk style projects don't use a namespace for the elements,
|
|
|
|
|
// but we still need to use the namespace for non-sdk style projects.
|
|
|
|
|
var sdkStyle = projectDoc.Root.Attribute("Sdk") != null; |
|
|
|
|
var itemGroupTagName = sdkStyle ? "ItemGroup" : NonSDKProjectFileNamespace + "ItemGroup"; |
|
|
|
|
var referenceTagName = sdkStyle ? "Reference" : NonSDKProjectFileNamespace + "Reference"; |
|
|
|
|
|
|
|
|
|
var referencesItemGroups = projectDoc.Root |
|
|
|
|
.Elements(ProjectFileNamespace + "ItemGroup") |
|
|
|
|
.Where(e => e.Elements(ProjectFileNamespace + "Reference").Any()); |
|
|
|
|
.Elements(itemGroupTagName) |
|
|
|
|
.Where(e => e.Elements(referenceTagName).Any()) |
|
|
|
|
.ToList(); |
|
|
|
|
|
|
|
|
|
foreach (var itemGroup in referencesItemGroups) |
|
|
|
|
{ |
|
|
|
|
FixProjectReferences(project.FilePath, itemGroup, projectsMap); |
|
|
|
|
FixProjectReferences(project.FilePath, itemGroup, projectsMap, sdkStyle); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
projectDoc.Save(project.FilePath); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void FixProjectReferences(string projectFilePath, XElement itemGroup, IDictionary<string, ProjectItem> projects) |
|
|
|
|
static void FixProjectReferences(string projectFilePath, XElement itemGroup, |
|
|
|
|
Dictionary<string, ProjectItem> projects, bool sdkStyle) |
|
|
|
|
{ |
|
|
|
|
foreach (var item in itemGroup.Elements(ProjectFileNamespace + "Reference").ToList()) |
|
|
|
|
|
|
|
|
|
XName GetElementName(string name) => sdkStyle ? name : NonSDKProjectFileNamespace + name; |
|
|
|
|
|
|
|
|
|
var referenceTagName = GetElementName("Reference"); |
|
|
|
|
var projectReferenceTagName = GetElementName("ProjectReference"); |
|
|
|
|
|
|
|
|
|
foreach (var item in itemGroup.Elements(referenceTagName).ToList()) |
|
|
|
|
{ |
|
|
|
|
var assemblyName = item.Attribute("Include")?.Value; |
|
|
|
|
if (assemblyName != null && projects.TryGetValue(assemblyName, out var referencedProject)) |
|
|
|
|
{ |
|
|
|
|
item.Remove(); |
|
|
|
|
|
|
|
|
|
var projectReference = new XElement(ProjectFileNamespace + "ProjectReference", |
|
|
|
|
new XElement(ProjectFileNamespace + "Project", referencedProject.Guid.ToString("B").ToUpperInvariant()), |
|
|
|
|
new XElement(ProjectFileNamespace + "Name", referencedProject.ProjectName)); |
|
|
|
|
projectReference.SetAttributeValue("Include", GetRelativePath(projectFilePath, referencedProject.FilePath)); |
|
|
|
|
var projectReference = new XElement( |
|
|
|
|
projectReferenceTagName, |
|
|
|
|
new XAttribute("Include", GetRelativePath(projectFilePath, referencedProject.FilePath))); |
|
|
|
|
|
|
|
|
|
// SDK-style projects do not use the <Project> and <Name> elements for project references.
|
|
|
|
|
// (Instead, those get read from the .csproj file in "Include".)
|
|
|
|
|
if (!sdkStyle) |
|
|
|
|
{ |
|
|
|
|
projectReference.Add( |
|
|
|
|
// no ToUpper() for uuids, most Microsoft tools seem to emit them in lowercase
|
|
|
|
|
// (no .ToLower() as .ToString("B") already outputs lowercase)
|
|
|
|
|
new XElement(NonSDKProjectFileNamespace + "Project", referencedProject.Guid.ToString("B")), |
|
|
|
|
new XElement(NonSDKProjectFileNamespace + "Name", referencedProject.ProjectName)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
itemGroup.Add(projectReference); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static string GetRelativePath(string fromFilePath, string toFilePath) |
|
|
|
|
static string GetRelativePath(string fromFilePath, string toFilePath) |
|
|
|
|
{ |
|
|
|
|
Uri fromUri = new Uri(fromFilePath); |
|
|
|
|
Uri toUri = new Uri(toFilePath); |
|
|
|
|