Skip to content

Commit 4d80bce

Browse files
committed
Add :setup support for slnx solution files
* add ManagePackageVersionsCentrally false directive to generated build.csproj
1 parent 335b930 commit 4d80bce

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Solution>
2+
<Project Path="TestProject1/TestProject1.csproj" />
3+
<Project Path="RELATIVE" />
4+
</Solution>

source/Nuke.GlobalTool.Tests/UpdateSolutionFileContentTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// https://github.com/nuke-build/nuke/blob/master/LICENSE
44

55
using System;
6+
using System.IO;
67
using System.Linq;
78
using System.Threading.Tasks;
9+
using System.Xml;
10+
using System.Xml.Linq;
811
using Nuke.Common.Utilities;
912
using VerifyXunit;
1013
using Xunit;
@@ -140,4 +143,33 @@ public Task Test(int number, string input, string expected)
140143
return Verifier.Verify(expected)
141144
.UseParameters(number);
142145
}
146+
147+
[Theory]
148+
[InlineData(
149+
1,
150+
"""
151+
<Solution>
152+
<Project Path="TestProject1/TestProject1.csproj" />
153+
</Solution>
154+
""",
155+
"""
156+
<Solution>
157+
<Project Path="TestProject1/TestProject1.csproj" />
158+
<Project Path="RELATIVE" />
159+
</Solution>
160+
""")]
161+
public Task TestXml(int number, string input, string expected)
162+
{
163+
var content = XDocument.Load(new StringReader(input));
164+
Program.UpdateSolutionXmlFileContent(content, "RELATIVE");
165+
166+
var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };
167+
var stringStream = new StringWriter();
168+
using var writer = XmlWriter.Create(stringStream, settings);
169+
content.Save(writer);
170+
writer.Flush();
171+
172+
return Verifier.Verify(stringStream.ToString())
173+
.UseParameters(number);
174+
}
143175
}

source/Nuke.GlobalTool/Program.Setup.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Linq;
99
using System.Reflection;
1010
using System.Text;
11+
using System.Xml;
12+
using System.Xml.Linq;
1113
using JetBrains.Annotations;
1214
using Nuke.Common;
1315
using Nuke.Common.Execution;
@@ -27,7 +29,7 @@ partial class Program
2729
{
2830
// ReSharper disable InconsistentNaming
2931

30-
private const string TARGET_FRAMEWORK = "net8.0";
32+
private const string TARGET_FRAMEWORK = "net10.0";
3133
private const string PROJECT_KIND = "9A19103F-16F7-4668-BE54-9A1E7A4F7556";
3234

3335
// ReSharper disable once CognitiveComplexity
@@ -84,7 +86,7 @@ public static int Setup(string[] args, [CanBeNull] AbsolutePath rootDirectory, [
8486
"Which solution should be the default?",
8587
choices: new DirectoryInfo(rootDirectory)
8688
.EnumerateFiles("*", SearchOption.AllDirectories)
87-
.Where(x => x.FullName.EndsWithOrdinalIgnoreCase(".sln"))
89+
.Where(x => x.FullName.EndsWithOrdinalIgnoreCase(".sln") || x.FullName.EndsWithOrdinalIgnoreCase(".slnx"))
8890
.OrderByDescending(x => x.FullName)
8991
.Select(x => (x, rootDirectory.GetRelativePathTo(x.FullName).ToString()))
9092
.Concat((null, "None")).ToArray())?.FullName;
@@ -110,10 +112,22 @@ public static int Setup(string[] args, [CanBeNull] AbsolutePath rootDirectory, [
110112

111113
if (solutionFile != null)
112114
{
113-
var solutionFileContent = solutionFile.ReadAllLines().ToList();
114115
var buildProjectFileRelative = solutionFile.Parent.GetWinRelativePathTo(buildProjectFile);
115-
UpdateSolutionFileContent(solutionFileContent, buildProjectFileRelative, buildProjectGuid, buildProjectName);
116-
solutionFile.WriteAllLines(solutionFileContent, Encoding.UTF8);
116+
if (solutionFile.Extension.EqualsOrdinalIgnoreCase(".slnx"))
117+
{
118+
var solutionDocument = XDocument.Load(solutionFile);
119+
UpdateSolutionXmlFileContent(solutionDocument, buildProjectFileRelative);
120+
121+
var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };
122+
using var writer = XmlWriter.Create(solutionFile, settings);
123+
solutionDocument.Save(writer);
124+
}
125+
else
126+
{
127+
var solutionFileContent = solutionFile.ReadAllLines().ToList();
128+
UpdateSolutionFileContent(solutionFileContent, buildProjectFileRelative, buildProjectGuid, buildProjectName);
129+
solutionFile.WriteAllLines(solutionFileContent, Encoding.UTF8);
130+
}
117131
}
118132

119133
buildProjectFile.WriteAllLines(
@@ -186,12 +200,27 @@ internal static void UpdateSolutionFileContent(
186200
"EndProject");
187201
}
188202

203+
internal static void UpdateSolutionXmlFileContent(XDocument content, string buildProjectFileRelative)
204+
{
205+
var solutionElement = content.Root;
206+
Assert.True(solutionElement?.Name == "Solution", "Could not find a root 'Solution' element in solution file");
207+
208+
// file uses forward slashes for paths on every platform
209+
var path = buildProjectFileRelative.Replace(oldChar: '\\', newChar: '/');
210+
211+
if (solutionElement.Elements("Project").Any(x => x.GetAttributeValue("Path").EqualsOrdinalIgnoreCase(path)))
212+
{
213+
return;
214+
}
215+
216+
solutionElement.Add(new XElement("Project", new XAttribute("Path", path)));
217+
}
218+
189219
private static string[] GetTemplate(string templateName)
190220
{
191221
return ResourceUtility.GetResourceAllLines<Program>($"templates.{templateName}");
192222
}
193223

194-
195224
private static void WriteBuildScripts(
196225
AbsolutePath scriptDirectory,
197226
AbsolutePath rootDirectory,

source/Nuke.GlobalTool/templates/_build.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<NukeScriptDirectory>_SCRIPT_DIRECTORY_</NukeScriptDirectory>
1010
<NukeTelemetryVersion>_TELEMETRY_VERSION_</NukeTelemetryVersion>
1111
<IsPackable>false</IsPackable>
12+
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
13+
<UseArtifactsOutput>false</UseArtifactsOutput>
1214
</PropertyGroup>
1315

1416
<ItemGroup>

0 commit comments

Comments
 (0)