Skip to content

Commit 229f1d4

Browse files
committed
Use SDK style projects for Dev18
1 parent a5998d8 commit 229f1d4

File tree

6 files changed

+121
-45
lines changed

6 files changed

+121
-45
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ ValidationSuiteResults
88
Assets/Plugins**
99
obj/
1010
/UserSettings/
11+
/ProjectSettings/*.asset
1112
/*.csproj
1213
/*.sln
14+
/*.csproj.user
15+
/*.slnx
1316
/Temp
1417
*.bak
1518
*.bak.meta
1619
.DS_Store
1720

21+
Packages/packages-lock.json
1822
Packages/com.unity.ide.visualstudio/Editor/COMIntegration/Release/build/
1923
Packages/com.unity.ide.visualstudio/Editor/COMIntegration/Release/COMIntegration.exe

Packages/com.unity.ide.visualstudio/Editor/ProjectGeneration/GeneratorFactory.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*--------------------------------------------------------------------------------------------*/
66

77
using System;
8-
using System.Collections.Generic;
98

109
namespace Microsoft.Unity.VisualStudio.Editor
1110
{
@@ -17,22 +16,19 @@ internal enum GeneratorStyle
1716

1817
internal static class GeneratorFactory
1918
{
20-
private static readonly Dictionary<GeneratorStyle, IGenerator> _generators = new Dictionary<GeneratorStyle, IGenerator>();
21-
22-
static GeneratorFactory()
23-
{
24-
_generators.Add(GeneratorStyle.SDK, new SdkStyleProjectGeneration());
25-
_generators.Add(GeneratorStyle.Legacy, new LegacyStyleProjectGeneration());
26-
}
19+
private static readonly SdkStyleProjectGeneration _sdkStyleProjectGeneration = new SdkStyleProjectGeneration();
20+
private static readonly LegacyStyleProjectGeneration _legacyStyleProjectGeneration = new LegacyStyleProjectGeneration();
2721

2822
public static IGenerator GetInstance(GeneratorStyle style)
2923
{
3024
var forceStyleString = OnSelectingCSProjectStyle();
3125
if (forceStyleString != null && Enum.TryParse<GeneratorStyle>(forceStyleString, out var forceStyle))
3226
style = forceStyle;
3327

34-
if (_generators.TryGetValue(style, out var result))
35-
return result;
28+
if (style == GeneratorStyle.SDK)
29+
return _sdkStyleProjectGeneration;
30+
if (style == GeneratorStyle.Legacy)
31+
return _legacyStyleProjectGeneration;
3632

3733
throw new ArgumentException("Unknown generator style");
3834
}

Packages/com.unity.ide.visualstudio/Editor/ProjectGeneration/ProjectGeneration.cs

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,11 @@ public string ProjectFile(Assembly assembly)
586586
#endif
587587

588588
public string SolutionFile()
589+
{
590+
return SolutionFileImpl();
591+
}
592+
593+
internal virtual string SolutionFileImpl()
589594
{
590595
return Path.Combine(ProjectDirectory.NormalizePathSeparators(), $"{InvalidCharactersRegexPattern.Replace(m_ProjectName, "_")}.sln");
591596
}
@@ -821,6 +826,26 @@ internal virtual void GetProjectFooter(StringBuilder footerBuilder)
821826
{
822827
}
823828

829+
private static string GetSolutionText()
830+
{
831+
return string.Join(k_WindowsNewline,
832+
@"",
833+
@"Microsoft Visual Studio Solution File, Format Version {0}",
834+
@"# Visual Studio {1}",
835+
@"{2}",
836+
@"Global",
837+
@" GlobalSection(SolutionConfigurationPlatforms) = preSolution",
838+
@" Debug|Any CPU = Debug|Any CPU",
839+
@" Release|Any CPU = Release|Any CPU",
840+
@" EndGlobalSection",
841+
@" GlobalSection(ProjectConfigurationPlatforms) = postSolution",
842+
@"{3}",
843+
@" EndGlobalSection",
844+
@"{4}",
845+
@"EndGlobal",
846+
@"").Replace(" ", "\t");
847+
}
848+
824849
private void SyncSolution(IEnumerable<Assembly> assemblies)
825850
{
826851
if (InvalidCharactersRegexPattern.IsMatch(ProjectDirectory))
@@ -831,16 +856,29 @@ private void SyncSolution(IEnumerable<Assembly> assemblies)
831856
SyncSolutionFileIfNotChanged(solutionFile, SolutionText(assemblies, previousSolution));
832857
}
833858

834-
private string SolutionText(IEnumerable<Assembly> assemblies, Solution previousSolution = null)
859+
internal virtual string SolutionText(IEnumerable<Assembly> assemblies, Solution previousSolution = null)
835860
{
836861
const string fileversion = "12.00";
837862
const string vsversion = "15";
838863

864+
var projects = GetSolutionProjects(assemblies, previousSolution);
865+
var properties = previousSolution != null ? previousSolution.Properties : null;
866+
867+
string propertiesText = GetPropertiesText(properties);
868+
string projectEntriesText = GetProjectEntriesText(projects);
869+
870+
// do not generate configurations for SolutionFolders
871+
var configurableProjects = projects.Where(p => !p.IsSolutionFolderProjectFactory());
872+
string projectConfigurationsText = string.Join(k_WindowsNewline, configurableProjects.Select(p => GetProjectActiveConfigurations(p.ProjectGuid)).ToArray());
873+
874+
return string.Format(GetSolutionText(), fileversion, vsversion, projectEntriesText, projectConfigurationsText, propertiesText);
875+
}
876+
877+
internal List<SolutionProjectEntry> GetSolutionProjects(IEnumerable<Assembly> assemblies, Solution previousSolution = null)
878+
{
839879
var relevantAssemblies = RelevantAssembliesForMode(assemblies);
840880
var generatedProjects = ToProjectEntries(relevantAssemblies).ToList();
841881

842-
SolutionProperties[] properties = null;
843-
844882
// First, add all projects generated by Unity to the solution
845883
var projects = new List<SolutionProjectEntry>();
846884
projects.AddRange(generatedProjects);
@@ -853,34 +891,9 @@ private string SolutionText(IEnumerable<Assembly> assemblies, Solution previousS
853891
.Where(p => generatedProjects.All(gp => gp.FileName != p.FileName));
854892

855893
projects.AddRange(externalProjects);
856-
properties = previousSolution.Properties;
857894
}
858895

859-
string propertiesText = GetPropertiesText(properties);
860-
string projectEntriesText = GetProjectEntriesText(projects);
861-
862-
// do not generate configurations for SolutionFolders
863-
var configurableProjects = projects.Where(p => !p.IsSolutionFolderProjectFactory());
864-
string projectConfigurationsText = string.Join(k_WindowsNewline, configurableProjects.Select(p => GetProjectActiveConfigurations(p.ProjectGuid)).ToArray());
865-
866-
const string solutionText =
867-
"" + k_WindowsNewline
868-
+ "Microsoft Visual Studio Solution File, Format Version {0}" + k_WindowsNewline
869-
+ "# Visual Studio {1}" + k_WindowsNewline
870-
+ "{2}" + k_WindowsNewline
871-
+ "Global" + k_WindowsNewline
872-
+ "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution" + k_WindowsNewline
873-
+ "\t\tDebug|Any CPU = Debug|Any CPU" + k_WindowsNewline
874-
+ "\t\tRelease|Any CPU = Release|Any CPU" + k_WindowsNewline
875-
+ "\tEndGlobalSection" + k_WindowsNewline
876-
+ "\tGlobalSection(ProjectConfigurationPlatforms) = postSolution" + k_WindowsNewline
877-
+ "{3}" + k_WindowsNewline
878-
+ "\tEndGlobalSection" + k_WindowsNewline
879-
+ "{4}" + k_WindowsNewline
880-
+ "EndGlobal" + k_WindowsNewline
881-
+ "";
882-
883-
return string.Format(solutionText, fileversion, vsversion, projectEntriesText, projectConfigurationsText, propertiesText);
896+
return projects;
884897
}
885898

886899
private static IEnumerable<Assembly> RelevantAssembliesForMode(IEnumerable<Assembly> assemblies)
@@ -942,6 +955,7 @@ private string GetProjectEntriesText(IEnumerable<SolutionProjectEntry> entries)
942955
private IEnumerable<SolutionProjectEntry> ToProjectEntries(IEnumerable<Assembly> assemblies)
943956
{
944957
foreach (var assembly in assemblies)
958+
{
945959
yield return new SolutionProjectEntry()
946960
{
947961
ProjectFactoryGuid = SolutionGuid(assembly),
@@ -950,6 +964,7 @@ private IEnumerable<SolutionProjectEntry> ToProjectEntries(IEnumerable<Assembly>
950964
ProjectGuid = ProjectGuid(assembly),
951965
Metadata = k_WindowsNewline
952966
};
967+
}
953968
}
954969

955970
/// <summary>

Packages/com.unity.ide.visualstudio/Editor/ProjectGeneration/SdkStyleProjectGeneration.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66

7-
using System;
7+
using System.Collections.Generic;
88
using System.IO;
99
using System.Text;
1010
using UnityEditor.Compilation;
@@ -122,5 +122,27 @@ internal static void GetCapabilityBlock(StringBuilder footerBuilder, string impo
122122
}
123123
footerBuilder.Append(@" </ItemGroup>").Append(k_WindowsNewline);
124124
}
125+
126+
internal override string SolutionFileImpl()
127+
{
128+
return base.SolutionFileImpl() + "x";
129+
}
130+
131+
internal override string SolutionText(IEnumerable<Assembly> assemblies, Solution previousSolution = null)
132+
{
133+
var projects = GetSolutionProjects(assemblies, previousSolution);
134+
135+
var content = new StringBuilder();
136+
content.Append("<Solution>").Append(k_WindowsNewline);
137+
138+
foreach (var project in projects)
139+
{
140+
content.Append(" ").Append("<Project Path=\"").Append(project.FileName).Append("\" />").Append(k_WindowsNewline);
141+
}
142+
143+
content.Append("</Solution>").Append(k_WindowsNewline);
144+
145+
return content.ToString();
146+
}
125147
}
126148
}

Packages/com.unity.ide.visualstudio/Editor/SolutionParser.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5+
using System;
56
using System.Collections.Generic;
7+
using System.IO;
68
using System.Text.RegularExpressions;
9+
using System.Xml.Linq;
710

811
namespace Microsoft.Unity.VisualStudio.Editor
912
{
@@ -16,10 +19,20 @@ internal static class SolutionParser
1619

1720
public static Solution ParseSolutionFile(string filename, IFileIO fileIO)
1821
{
19-
return ParseSolutionContent(fileIO.ReadAllText(filename));
22+
var extension = Path.GetExtension(filename).ToLower();
23+
if (extension == ".sln")
24+
{
25+
return ParseSolutionContent(fileIO.ReadAllText(filename));
26+
}
27+
if (extension == ".slnx")
28+
{
29+
return ParseSolutionXmlContent(fileIO.ReadAllText(filename));
30+
}
31+
32+
throw new NotSupportedException();
2033
}
2134

22-
public static Solution ParseSolutionContent(string content)
35+
private static Solution ParseSolutionContent(string content)
2336
{
2437
return new Solution
2538
{
@@ -76,5 +89,30 @@ private static SolutionProperties[] ParseSolutionProperties(string content)
7689

7790
return properties.ToArray();
7891
}
92+
93+
private static Solution ParseSolutionXmlContent(string content)
94+
{
95+
return new Solution
96+
{
97+
Projects = ParseSolutionXmlProjects(content),
98+
Properties = Array.Empty<SolutionProperties>(),
99+
};
100+
}
101+
102+
private static SolutionProjectEntry[] ParseSolutionXmlProjects(string content)
103+
{
104+
var document = XDocument.Parse(content);
105+
var projects = new List<SolutionProjectEntry>();
106+
107+
foreach (var project in document.Descendants("Project"))
108+
{
109+
projects.Add(new SolutionProjectEntry
110+
{
111+
FileName = project.Attribute("Path")?.Value,
112+
});
113+
}
114+
115+
return projects.ToArray();
116+
}
79117
}
80118
}

Packages/com.unity.ide.visualstudio/Editor/VisualStudioForWindowsInstallation.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ internal class VisualStudioForWindowsInstallation : VisualStudioInstallation
2626
// C# language version support for Visual Studio
2727
private static readonly VersionPair[] WindowsVersionTable =
2828
{
29+
new VersionPair(18, 0, /* => */ 14,0),
30+
2931
// VisualStudio 2022
3032
new VersionPair(17,12, /* => */ 13,0),
3133
new VersionPair(17, 8, /* => */ 12,0),
@@ -43,8 +45,7 @@ internal class VisualStudioForWindowsInstallation : VisualStudioInstallation
4345
new VersionPair(15,0, /* => */ 7,0),
4446
};
4547

46-
private static string _vsWherePath = null;
47-
private static readonly IGenerator _generator = GeneratorFactory.GetInstance(GeneratorStyle.Legacy);
48+
private static string _vsWherePath;
4849

4950
public override bool SupportsAnalyzers
5051
{
@@ -124,7 +125,7 @@ public override IGenerator ProjectGenerator
124125
{
125126
get
126127
{
127-
return _generator;
128+
return GeneratorFactory.GetInstance(Version.Major >= 18 ? GeneratorStyle.SDK : GeneratorStyle.Legacy);
128129
}
129130
}
130131

0 commit comments

Comments
 (0)