Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Solution>
<Project Path="TestProject1/TestProject1.csproj" />
<Project Path="RELATIVE">
<Build Project="false" />
</Project>
</Solution>
34 changes: 34 additions & 0 deletions source/Nuke.GlobalTool.Tests/UpdateSolutionFileContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Nuke.Common.Utilities;
using VerifyXunit;
using Xunit;
Expand Down Expand Up @@ -140,4 +143,35 @@ public Task Test(int number, string input, string expected)
return Verifier.Verify(expected)
.UseParameters(number);
}

[Theory]
[InlineData(
1,
"""
<Solution>
<Project Path="TestProject1/TestProject1.csproj" />
</Solution>
""",
"""
<Solution>
<Project Path="TestProject1/TestProject1.csproj" />
<Project Path="RELATIVE">
<Build Project="false" />
</Project>
</Solution>
""")]
public Task TestXml(int number, string input, string expected)
{
var content = XDocument.Load(new StringReader(input));
Program.UpdateSolutionXmlFileContent(content, "RELATIVE");

var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };
var stringStream = new StringWriter();
using var writer = XmlWriter.Create(stringStream, settings);
content.Save(writer);
writer.Flush();

return Verifier.Verify(stringStream.ToString())
.UseParameters(number);
}
}
43 changes: 37 additions & 6 deletions source/Nuke.GlobalTool/Program.Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using JetBrains.Annotations;
using Nuke.Common;
using Nuke.Common.Execution;
Expand All @@ -27,7 +29,7 @@ partial class Program
{
// ReSharper disable InconsistentNaming

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

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

if (solutionFile != null)
{
var solutionFileContent = solutionFile.ReadAllLines().ToList();
var buildProjectFileRelative = solutionFile.Parent.GetWinRelativePathTo(buildProjectFile);
UpdateSolutionFileContent(solutionFileContent, buildProjectFileRelative, buildProjectGuid, buildProjectName);
solutionFile.WriteAllLines(solutionFileContent, Encoding.UTF8);
if (solutionFile.Extension.EqualsOrdinalIgnoreCase(".slnx"))
{
var solutionDocument = XDocument.Load(solutionFile);
UpdateSolutionXmlFileContent(solutionDocument, buildProjectFileRelative);

var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };
using var writer = XmlWriter.Create(solutionFile, settings);
solutionDocument.Save(writer);
}
else
{
var solutionFileContent = solutionFile.ReadAllLines().ToList();
UpdateSolutionFileContent(solutionFileContent, buildProjectFileRelative, buildProjectGuid, buildProjectName);
solutionFile.WriteAllLines(solutionFileContent, Encoding.UTF8);
}
}

buildProjectFile.WriteAllLines(
Expand Down Expand Up @@ -186,12 +200,29 @@ internal static void UpdateSolutionFileContent(
"EndProject");
}

internal static void UpdateSolutionXmlFileContent(XDocument content, string buildProjectFileRelative)
{
var solutionElement = content.Root;
Assert.True(solutionElement?.Name == "Solution", "Could not find a root 'Solution' element in solution file");

// file uses forward slashes for paths on every platform
var path = buildProjectFileRelative.Replace(oldChar: '\\', newChar: '/');

if (solutionElement.Elements("Project").Any(x => x.GetAttributeValue("Path").EqualsOrdinalIgnoreCase(path)))
{
return;
}

var projectElement = new XElement("Project", new XAttribute("Path", path));
projectElement.Add(new XElement("Build", new XAttribute("Project", value: false)));
solutionElement.Add(projectElement);
}

private static string[] GetTemplate(string templateName)
{
return ResourceUtility.GetResourceAllLines<Program>($"templates.{templateName}");
}


private static void WriteBuildScripts(
AbsolutePath scriptDirectory,
AbsolutePath rootDirectory,
Expand Down
2 changes: 2 additions & 0 deletions source/Nuke.GlobalTool/templates/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<NukeScriptDirectory>_SCRIPT_DIRECTORY_</NukeScriptDirectory>
<NukeTelemetryVersion>_TELEMETRY_VERSION_</NukeTelemetryVersion>
<IsPackable>false</IsPackable>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
<UseArtifactsOutput>false</UseArtifactsOutput>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading