Skip to content

Commit ebd9415

Browse files
sailroGitHub Enterprise
authored andcommitted
Merge pull request #210 from unity/dev18-generator
Use SDK style projects for Dev18
2 parents a5998d8 + cf5c89f commit ebd9415

File tree

7 files changed

+111
-37
lines changed

7 files changed

+111
-37
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: 33 additions & 21 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
}
@@ -831,30 +836,13 @@ private void SyncSolution(IEnumerable<Assembly> assemblies)
831836
SyncSolutionFileIfNotChanged(solutionFile, SolutionText(assemblies, previousSolution));
832837
}
833838

834-
private string SolutionText(IEnumerable<Assembly> assemblies, Solution previousSolution = null)
839+
internal virtual string SolutionText(IEnumerable<Assembly> assemblies, Solution previousSolution = null)
835840
{
836841
const string fileversion = "12.00";
837842
const string vsversion = "15";
838843

839-
var relevantAssemblies = RelevantAssembliesForMode(assemblies);
840-
var generatedProjects = ToProjectEntries(relevantAssemblies).ToList();
841-
842-
SolutionProperties[] properties = null;
843-
844-
// First, add all projects generated by Unity to the solution
845-
var projects = new List<SolutionProjectEntry>();
846-
projects.AddRange(generatedProjects);
847-
848-
if (previousSolution != null)
849-
{
850-
// Add all projects that were previously in the solution and that are not generated by Unity, nor generated in the project root directory
851-
var externalProjects = previousSolution.Projects
852-
.Where(p => p.IsSolutionFolderProjectFactory() || !FileUtility.IsFileInProjectRootDirectory(p.FileName))
853-
.Where(p => generatedProjects.All(gp => gp.FileName != p.FileName));
854-
855-
projects.AddRange(externalProjects);
856-
properties = previousSolution.Properties;
857-
}
844+
var projects = GetSolutionProjects(assemblies, previousSolution);
845+
var properties = previousSolution != null ? previousSolution.Properties : null;
858846

859847
string propertiesText = GetPropertiesText(properties);
860848
string projectEntriesText = GetProjectEntriesText(projects);
@@ -863,7 +851,7 @@ private string SolutionText(IEnumerable<Assembly> assemblies, Solution previousS
863851
var configurableProjects = projects.Where(p => !p.IsSolutionFolderProjectFactory());
864852
string projectConfigurationsText = string.Join(k_WindowsNewline, configurableProjects.Select(p => GetProjectActiveConfigurations(p.ProjectGuid)).ToArray());
865853

866-
const string solutionText =
854+
const string solutionText =
867855
"" + k_WindowsNewline
868856
+ "Microsoft Visual Studio Solution File, Format Version {0}" + k_WindowsNewline
869857
+ "# Visual Studio {1}" + k_WindowsNewline
@@ -883,6 +871,28 @@ private string SolutionText(IEnumerable<Assembly> assemblies, Solution previousS
883871
return string.Format(solutionText, fileversion, vsversion, projectEntriesText, projectConfigurationsText, propertiesText);
884872
}
885873

874+
internal List<SolutionProjectEntry> GetSolutionProjects(IEnumerable<Assembly> assemblies, Solution previousSolution = null)
875+
{
876+
var relevantAssemblies = RelevantAssembliesForMode(assemblies);
877+
var generatedProjects = ToProjectEntries(relevantAssemblies).ToList();
878+
879+
// First, add all projects generated by Unity to the solution
880+
var projects = new List<SolutionProjectEntry>();
881+
projects.AddRange(generatedProjects);
882+
883+
if (previousSolution != null)
884+
{
885+
// Add all projects that were previously in the solution and that are not generated by Unity, nor generated in the project root directory
886+
var externalProjects = previousSolution.Projects
887+
.Where(p => p.IsSolutionFolderProjectFactory() || !FileUtility.IsFileInProjectRootDirectory(p.FileName))
888+
.Where(p => generatedProjects.All(gp => gp.FileName != p.FileName));
889+
890+
projects.AddRange(externalProjects);
891+
}
892+
893+
return projects;
894+
}
895+
886896
private static IEnumerable<Assembly> RelevantAssembliesForMode(IEnumerable<Assembly> assemblies)
887897
{
888898
return assemblies.Where(i => ScriptingLanguage.CSharp == ScriptingLanguageFor(i));
@@ -942,6 +952,7 @@ private string GetProjectEntriesText(IEnumerable<SolutionProjectEntry> entries)
942952
private IEnumerable<SolutionProjectEntry> ToProjectEntries(IEnumerable<Assembly> assemblies)
943953
{
944954
foreach (var assembly in assemblies)
955+
{
945956
yield return new SolutionProjectEntry()
946957
{
947958
ProjectFactoryGuid = SolutionGuid(assembly),
@@ -950,6 +961,7 @@ private IEnumerable<SolutionProjectEntry> ToProjectEntries(IEnumerable<Assembly>
950961
ProjectGuid = ProjectGuid(assembly),
951962
Metadata = k_WindowsNewline
952963
};
964+
}
953965
}
954966

955967
/// <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/VisualStudioCodeInstallation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ private void CreateSettingsFile(string vscodeDirectory, bool enablePatch)
382382
""explorer.fileNesting.enabled"": true,
383383
""explorer.fileNesting.patterns"": {
384384
""*.sln"": ""*.csproj"",
385+
""*.slnx"": ""*.csproj""
385386
},
386387
""dotnet.defaultSolution"": """ + IOPath.GetFileName(ProjectGenerator.SolutionFile()) + @"""
387388
}";

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)