Skip to content

Commit 72c1f42

Browse files
committed
Fixed CS0436 compiler error and used GeneratedCode sub-namespace
- Fixed "CS0436 : The type 'CliServiceProviderExtensions' in ..." compiler error when project is referenced by another project. - Generated Builder classes will be put in GeneratedCode sub-namespace of the definition class's namespace to prevent namespace pollution. Global GeneratedCode namespace was already being used by delegate-model, now class-based model will use GeneratedCode sub-namespace.
1 parent 859e5e3 commit 72c1f42

File tree

234 files changed

+5104
-5068
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+5104
-5068
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<!-- https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#generateassemblyinfo -->
5-
<VersionPrefix>1.8.2</VersionPrefix>
5+
<VersionPrefix>1.8.3</VersionPrefix>
66
<Product>DotMake Command-Line</Product>
77
<Company>DotMake</Company>
88
<!-- Copyright is also used for NuGet metadata -->

src/DotMake.CommandLine.SourceGeneration.Embedded/CliServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Microsoft.Extensions.DependencyInjection;
21
using System;
2+
using Microsoft.Extensions.DependencyInjection;
33

44
namespace DotMake.CommandLine
55
{
@@ -10,7 +10,7 @@ namespace DotMake.CommandLine
1010
/// <br/>Default implementation <see cref="ServiceCollection"/> is in <c>Microsoft.Extensions.DependencyInjection</c> assembly.
1111
/// </para>
1212
/// </summary>
13-
public static class CliServiceCollectionExtensions
13+
internal static class CliServiceCollectionExtensions
1414
{
1515
private static readonly IServiceCollection ServiceCollection = new ServiceCollection();
1616
private static IServiceProvider serviceProvider;

src/DotMake.CommandLine.SourceGeneration.Embedded/CliServiceProviderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace DotMake.CommandLine
1111
/// used class <see cref="ActivatorUtilities"/> is in <c>Microsoft.Extensions.DependencyInjection.Abstractions</c> assembly.
1212
/// </para>
1313
/// </summary>
14-
public static class CliServiceProviderExtensions
14+
internal static class CliServiceProviderExtensions
1515
{
1616
private static IServiceProvider serviceProvider;
1717

src/DotMake.CommandLine.SourceGeneration/CliCommandAsDelegateInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public CliCommandAsDelegateInfo(ISymbol symbol, SyntaxNode syntaxNode, SemanticM
5555

5656
Hash = GenerateHash();
5757
GeneratedClassName = "CliCommandAsDelegate_" + Hash;
58-
GeneratedClassNamespace = "GeneratedCode";
58+
GeneratedClassNamespace = CliCommandInfo.GeneratedSubNamespace;
5959
GeneratedClassFullName = string.IsNullOrEmpty(GeneratedClassNamespace)
6060
? GeneratedClassName
6161
: GeneratedClassNamespace + "." + GeneratedClassName;

src/DotMake.CommandLine.SourceGeneration/CliCommandInfo.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class CliCommandInfo : CliSymbolInfo, IEquatable<CliCommandInfo>
1515
public const string CommandClassName = "CliCommand";
1616
public const string CommandClassNamespace = "System.CommandLine";
1717
public const string DiagnosticName = "CLI command";
18+
public const string GeneratedSubNamespace = "GeneratedCode";
1819
public const string GeneratedClassSuffix = "Builder";
1920
public static readonly string CommandBuilderFullName = "DotMake.CommandLine.CliCommandBuilder";
2021
public static readonly Dictionary<string, string> PropertyMappings = new Dictionary<string, string>
@@ -45,15 +46,13 @@ public CliCommandInfo(ISymbol symbol, SyntaxNode syntaxNode, AttributeData attri
4546
IsExternalChild = (parent == null) && (Settings.ParentSymbol != null);
4647

4748
GeneratedClassName = symbol.Name + GeneratedClassSuffix;
48-
GeneratedClassNamespace = Settings.IsParentContaining
49-
? Settings.GetContainingTypeFullName(GeneratedClassSuffix)
50-
: (symbol.ContainingNamespace == null || symbol.ContainingNamespace.IsGlobalNamespace)
51-
? string.Empty
52-
: symbol.ContainingNamespace.ToReferenceString();
53-
GeneratedClassFullName = string.IsNullOrEmpty(GeneratedClassNamespace)
54-
? GeneratedClassName
55-
: GeneratedClassNamespace + "." + GeneratedClassName;
56-
49+
GeneratedClassNamespace = symbol.GetNamespaceOrEmpty();
50+
if (!GeneratedClassNamespace.EndsWith(GeneratedSubNamespace))
51+
GeneratedClassNamespace = SymbolExtensions.CombineNameParts(GeneratedClassNamespace, GeneratedSubNamespace);
52+
GeneratedClassFullName = Settings.IsParentContaining
53+
? SymbolExtensions.CombineNameParts(Settings.GetContainingTypeFullName(GeneratedSubNamespace, GeneratedClassSuffix), GeneratedClassName)
54+
: SymbolExtensions.CombineNameParts(GeneratedClassNamespace, GeneratedClassName);
55+
5756
ReferenceDependantInfo = (parent != null)
5857
? parent.ReferenceDependantInfo
5958
: new ReferenceDependantInfo(semanticModel.Compilation);

src/DotMake.CommandLine.SourceGeneration/CliCommandSettings.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,27 @@ public IEnumerable<CliCommandSettings> GetParentTree()
6363
}
6464
}
6565

66-
public string GetContainingTypeFullName(string classSuffix)
66+
public string GetContainingTypeFullName(string subNamespace, string classSuffix)
6767
{
6868
var parentTree = GetParentTree()
6969
.Prepend(this) //to include first ParentSymbol
7070
.TakeWhile(s => s.IsParentContaining)
7171
.Reverse()
7272
.Select((s, i) =>
73-
((i == 0) ? s.ParentSymbol.ToReferenceString() : s.ParentSymbol.Name) + classSuffix);
73+
{
74+
var className = s.ParentSymbol.Name + classSuffix;
75+
76+
if (i == 0)
77+
{
78+
var classNamespace = ParentSymbol.GetNamespaceOrEmpty();
79+
if (!classNamespace.EndsWith(classSuffix))
80+
classNamespace = SymbolExtensions.CombineNameParts(classNamespace, subNamespace);
81+
82+
return SymbolExtensions.CombineNameParts(classNamespace, className);
83+
}
84+
85+
return className;
86+
});
7487

7588
return string.Join(".", parentTree);
7689
}

src/DotMake.CommandLine.SourceGeneration/SymbolExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using Microsoft.CodeAnalysis;
43
using System.Linq;
@@ -126,5 +125,17 @@ public static IEnumerable<ISymbol> GetAllMembers(this ITypeSymbol type)
126125
}
127126
}
128127
}
128+
129+
public static string GetNamespaceOrEmpty(this ISymbol symbol)
130+
{
131+
return (symbol.ContainingNamespace == null || symbol.ContainingNamespace.IsGlobalNamespace)
132+
? string.Empty
133+
: symbol.ContainingNamespace.ToReferenceString();
134+
}
135+
136+
public static string CombineNameParts(params string[] nameParts)
137+
{
138+
return string.Join(".", nameParts.Where(n => !string.IsNullOrEmpty(n)));
139+
}
129140
}
130141
}

src/DotMake.CommandLine/DotMake.CommandLine.csproj

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
<Description>Declarative syntax for System.CommandLine via attributes for easy, fast, strongly-typed (no reflection) usage. Includes a source generator which automagically converts your classes to CLI commands and properties to CLI options or CLI arguments.</Description>
2121
<PackageTags>command-line CLI console System.CommandLine declarative attributes parsing command argument option generator</PackageTags>
2222
<PackageReleaseNotes>
23-
- Updated to latest daily build `2.0.0-beta4.24123.1` of System.CommandLine.
24-
- Better error message when a passed class does not have a `[CliCommand]` attribute ot it's a nested class and one of its parents
25-
does not have a `[CliCommand]` attribute.
26-
- On Windows platform, backslash + double quote (`\&quot;`) at the end of an argument,
27-
is usually a path separator and not an escape for double quote, so it will be trimmed to prevent unnecessary path errors.
23+
- Fixed "CS0436 : The type 'CliServiceProviderExtensions' in ..." compiler error when project is referenced by another project.
24+
- Generated Builder classes will be put in GeneratedCode sub-namespace of the definition class's namespace to prevent namespace pollution.
25+
Global GeneratedCode namespace was already being used by delegate-model, now class-based model will use GeneratedCode sub-namespace.
2826
</PackageReleaseNotes>
2927
</PropertyGroup>
3028

src/TestApp.Nuget/GeneratedFiles/net472/DotMake.CommandLine.SourceGeneration/DotMake.CommandLine.SourceGeneration.CliCommandGenerator/(ModuleInitializerAttribute).g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <auto-generated />
2-
// Generated by DotMake.CommandLine.SourceGeneration v1.8.2.0
2+
// Generated by DotMake.CommandLine.SourceGeneration v1.8.3.0
33
// Roslyn (Microsoft.CodeAnalysis) v4.900.24.8111
44
// Generation: 1
55

src/TestApp.Nuget/GeneratedFiles/net472/DotMake.CommandLine.SourceGeneration/DotMake.CommandLine.SourceGeneration.CliCommandGenerator/(RequiredMemberAttribute).g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <auto-generated />
2-
// Generated by DotMake.CommandLine.SourceGeneration v1.8.2.0
2+
// Generated by DotMake.CommandLine.SourceGeneration v1.8.3.0
33
// Roslyn (Microsoft.CodeAnalysis) v4.900.24.8111
44
// Generation: 1
55

0 commit comments

Comments
 (0)