Skip to content

Commit

Permalink
Use slngen in Nuke
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonC9018 committed Dec 13, 2023
1 parent d72ad9d commit 79dd046
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 68 deletions.
6 changes: 3 additions & 3 deletions .build/Build.Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ partial class Build
readonly int DegreeOfParallelism = 2;

AbsolutePath SourceDirectory => RootDirectory / "src";
AbsolutePath AllSolutionFile => SourceDirectory / "All.sln";
AbsolutePath AllSolutionFile => RootDirectory / "All.sln";
AbsolutePath TestSolutionFile => TemporaryDirectory / "Build.Test.sln";
AbsolutePath PackSolutionFile => SourceDirectory / "Build.Pack.sln";

AbsolutePath OutputDirectory => RootDirectory / "output";
AbsolutePath TestResultDirectory => OutputDirectory / "test-results";
AbsolutePath CoverageReportDirectory => OutputDirectory / "coberage-reports";
AbsolutePath CoverageReportDirectory => OutputDirectory / "coverage-reports";
AbsolutePath PackageDirectory => OutputDirectory / "packages";
AbsolutePath StarWarsTemplateNuSpec => RootDirectory / "templates" / "StarWars" / "HotChocolate.Templates.StarWars.nuspec";
AbsolutePath HotChocolateDirectoryBuildProps => SourceDirectory / "HotChocolate" / "Directory.Build.Props";
Expand All @@ -30,5 +30,5 @@ partial class Build
AbsolutePath EmptyAzfUp12Proj => RootDirectory / "templates" / "v12" / "function-isolated" / "HotChocolate.Template.AzureFunctions.Isolated.csproj";
AbsolutePath Gateway13Proj => RootDirectory / "templates" / "v12" / "gateway" / "HotChocolate.Template.Gateway.csproj";
AbsolutePath GatewayManaged13Proj => RootDirectory / "templates" / "v12" / "gateway-bcp" / "HotChocolate.Template.Gateway.Managed.csproj";
AbsolutePath FSharpTypes => SourceDirectory/"HotChocolate" /"Core" / "src" / "Types.FSharp" / "HotChocolate.Types.FSharp.fsproj";
AbsolutePath FSharpTypes => SourceDirectory / "HotChocolate" / "Core" / "src" / "Types.FSharp" / "HotChocolate.Types.FSharp.fsproj";
}
68 changes: 68 additions & 0 deletions .build/Build.GenerateSolution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Nuke.Common.Tooling;
using Nuke.Common.ProjectModel;

partial class Build
{
[PackageExecutable(
packageId: "Microsoft.VisualStudio.SlnGen.Tool",
packageExecutable: "slngen.exe",
// Must be set for tools shipping multiple versions
Framework = "net8.0")]
readonly Tool SlnGen;

IReadOnlyCollection<Output> DotNetBuildSonarSolution(
string solutionOutputFile,
IEnumerable<string> directories = null,
Func<string, bool> include = null)
{
var projects = Helpers.GetAllProjects(SourceDirectory, directories, include);
var result = CreateSolution(solutionOutputFile, projects);
return result;
}

IReadOnlyCollection<Output> DotNetBuildTestSolution(
string solutionOutputFile,
IEnumerable<Project> projects)
{
var projectPaths = projects.Select(p => (string) p.Path);
var result = CreateSolution(solutionOutputFile, projectPaths);
return result;
}

IReadOnlyCollection<Output> CreateSolution(
string solutionOutputFile,
IEnumerable<string> projectAbsolutePaths)
{
if (File.Exists(solutionOutputFile))
{
return Array.Empty<Output>();
}

var solutionDirectory = Path.GetDirectoryName(solutionOutputFile)!;
var workingDirectory = solutionDirectory;

var arguments = new Arguments();

foreach (var projectAbsolutePath in projectAbsolutePaths)
{
var projectRelativePath = Path.GetRelativePath(workingDirectory, projectAbsolutePath);
arguments.Add($"{projectRelativePath}");
}

// https://microsoft.github.io/slngen/
arguments.Add("--solutionfile {value}", solutionOutputFile);
arguments.Add("--launch {value}", "false");
arguments.Add("--verbosity {value}", "minimal");
arguments.Add("--folders {value}", "true");

var result = SlnGen(
arguments: arguments.RenderForExecution(),
workingDirectory: workingDirectory);

return result;
}
}
2 changes: 1 addition & 1 deletion .build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ partial class Build : NukeBuild
.Select(p => new TestProject
{
Name = Path.GetFileNameWithoutExtension(p.Path),
Path = Path.GetRelativePath(RootDirectory, p.Path)
Path = Path.GetRelativePath(RootDirectory, p.Path!),
})
.OrderBy(p => p.Name)
.ToList();
Expand Down
1 change: 1 addition & 0 deletions .build/Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageDownload Include="Microsoft.VisualStudio.SlnGen.Tool" Version="[11.1.0]" />
<PackageDownload Include="NuGet.CommandLine" Version="[6.5.0]" />
<PackageDownload Include="dotnet-sonarscanner" Version="[5.13.0]" />
<PackageDownload Include="OpenCover" Version="[4.7.1221]" />
Expand Down
94 changes: 35 additions & 59 deletions .build/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using System.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.DotNet;

static class Helpers
{
Expand Down Expand Up @@ -33,81 +28,62 @@ static class Helpers
Path.Combine("StrawberryShake", "CodeGeneration"),
Path.Combine("StrawberryShake", "MetaPackages"),
Path.Combine("StrawberryShake", "Tooling"),
"CookieCrumble"
"CookieCrumble",
};

static IEnumerable<string> GetAllProjects(
static readonly string[] IgnoredProjectSegments = new[]
{
"benchmark",
"demo",
"sample",
"examples",
};

static bool ProjectContainsIgnoredSegment(ReadOnlySpan<char> path)
{
foreach (var ignoredSegment in IgnoredProjectSegments)
{
if (path.Contains(ignoredSegment, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}

public static IEnumerable<string> GetAllProjects(
string sourceDirectory,
IEnumerable<string> directories,
Func<string, bool> include = null)
{
directories ??= Directories;

foreach (var directory in directories)
{
var fullDirectory = Path.Combine(sourceDirectory, directory);
foreach (var file in Directory.EnumerateFiles(fullDirectory, "*.csproj", SearchOption.AllDirectories))
{
if (!(include?.Invoke(file) ?? true)
|| file.Contains("benchmark", StringComparison.OrdinalIgnoreCase)
|| file.Contains("demo", StringComparison.OrdinalIgnoreCase)
|| file.Contains("sample", StringComparison.OrdinalIgnoreCase)
|| file.Contains("examples", StringComparison.OrdinalIgnoreCase))
bool shouldInclude = include?.Invoke(file) ?? true;
if (!shouldInclude)
{
continue;
}
yield return file;
}
}
}

public static IReadOnlyCollection<Output> DotNetBuildSonarSolution(
string solutionFile,
IEnumerable<string> directories = null,
Func<string, bool> include = null)
{
if (File.Exists(solutionFile))
{
return Array.Empty<Output>();
}

directories ??= Directories;

var projects = GetAllProjects(Path.GetDirectoryName(solutionFile), directories, include);
var workingDirectory = Path.GetDirectoryName(solutionFile);
var list = new List<Output>();

list.AddRange(DotNetTasks.DotNet($"new sln -n {Path.GetFileNameWithoutExtension(solutionFile)}", workingDirectory));

var projectsArg = string.Join(" ", projects.Select(t => $"\"{t}\""));

list.AddRange(DotNetTasks.DotNet($"sln \"{solutionFile}\" add {projectsArg}", workingDirectory));

return list;
}
var relativePath = Path.GetRelativePath(sourceDirectory, file);
if (ProjectContainsIgnoredSegment(relativePath))
{
continue;
}

public static IReadOnlyCollection<Output> DotNetBuildTestSolution(
string solutionFile,
IEnumerable<Project> projects)
{
if (File.Exists(solutionFile))
{
return Array.Empty<Output>();
yield return file;
}
}

var workingDirectory = Path.GetDirectoryName(solutionFile);
var list = new List<Output>();

list.AddRange(DotNetTasks.DotNet($"new sln -n {Path.GetFileNameWithoutExtension(solutionFile)}", workingDirectory));

var projectsArg = string.Join(" ", projects.Select(t => $"\"{t}\""));

list.AddRange(DotNetTasks.DotNet($"sln \"{solutionFile}\" add {projectsArg}", workingDirectory));

return list;
}

public static void TryDelete(string fileName)
{
if(File.Exists(fileName))
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Expand Down
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,7 @@ artifacts/
__mismatch__
coverage.opencover.xml
output
src/All.sln
src/Build.CheckApi.sln
src/Build.Sonar.sln
src/Build.Test.sln
src/Build.Pack.sln
*.sln
*.DotSettings

# asp .net core
Expand Down

0 comments on commit 79dd046

Please sign in to comment.