From 7918455434be9985d5446b4283c32dcfd3a8eb9e Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Tue, 2 Dec 2025 10:30:04 +0100 Subject: [PATCH] Fix wrong prefix calculation for solution folders starting with digits --- .../StronglyTypedSolutionGenerator.cs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/source/Nuke.SourceGenerators/StronglyTypedSolutionGenerator.cs b/source/Nuke.SourceGenerators/StronglyTypedSolutionGenerator.cs index 60649aafe..a627ef11c 100644 --- a/source/Nuke.SourceGenerators/StronglyTypedSolutionGenerator.cs +++ b/source/Nuke.SourceGenerators/StronglyTypedSolutionGenerator.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using JetBrains.Annotations; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -68,7 +69,7 @@ public void Execute(GeneratorExecutionContext context) continue; [CanBeNull] - string GetDeclaration(IProjectContainer container) + string GetDeclaration(IProjectContainer container, string folderName = null) { var prefix = new string('_', container.Descendants(x => x.Parent).Count() + 1); var model = new @@ -78,21 +79,21 @@ string GetDeclaration(IProjectContainer container) Name = GetEscapedName(container switch { Solution => member.Name, - SolutionFolder folder => prefix.Substring(1) + folder.Name, + SolutionFolder folder => folderName ?? folder.Name, _ => throw new ArgumentOutOfRangeException(nameof(container), container, null) }), Projects = container.Projects.OrderBy(x => x.Name).Select(x => new { x.Name, - EscapedName = GetEscapedName(x.Name), + EscapedName = GetEscapedName(x.Name) }), Folders = container.SolutionFolders.OrderBy(x => x.Name).Select(x => new { x.Name, - TypeName = prefix + GetEscapedName(x.Name), - EscapedName = GetEscapedName(x.Name), + TypeName = folderName ?? $"{prefix}{GetEscapedName(x.Name)}", + EscapedName = GetEscapedName(x.Name) }), - Declarations = container.SolutionFolders.OrderBy(x => x.Name).Select(GetDeclaration).ToArray(), + Declarations = container.SolutionFolders.OrderBy(x => x.Name).Select(x => GetDeclaration(x, $"{prefix}{GetEscapedName(x.Name)}")).ToArray(), }; // lang=csharp @@ -118,10 +119,18 @@ internal class {{ name }}(SolutionFolderModel model, Nuke.Common.ProjectModel.So """); return template.Render(model); - string GetEscapedName(string name) => name - // .Replace(".", fancyNaming ? "丨" : "_") - .Replace(".", fancyNaming ? "٠" : "_") - .ReplaceRegex(@"(^[\W^\d]|[\W])", _ => "_"); + string GetEscapedName(string name) + { + name = name + // .Replace(".", fancyNaming ? "丨" : "_") + .Replace(".", fancyNaming ? "٠" : "_") + .ReplaceRegex(@"[^A-Za-z0-9_]", _ => "_"); + + if (Regex.IsMatch(name, @"^[0-9]")) + return "_" + name; + + return name; + } } } }