Skip to content
Open
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
29 changes: 19 additions & 10 deletions source/Nuke.SourceGenerators/StronglyTypedSolutionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
}
}
Expand Down
Loading