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
Expand Up @@ -24,7 +24,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<Version>5.2.6</Version>
<Version>5.3.0</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
Expand Down
19 changes: 17 additions & 2 deletions AutomaticInterface/AutomaticInterface/GeneratedSymbolDetails.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -16,9 +17,8 @@ ClassDeclarationSyntax classSyntax
/// to the containing namespace of the type symbol.
/// </summary>
public string NamespaceName { get; } =
PrepareValue(
PrepareNamespaceName(
generationAttribute,
AutomaticInterfaceGenerator.NamespaceParameterName,
typeSymbol.ContainingNamespace.ToDisplayString()
);

Expand Down Expand Up @@ -78,4 +78,19 @@ private static T PrepareValue<T>(AttributeData? generationAttribute, string key,

return defaultValue;
}

private static string PrepareNamespaceName(AttributeData? generationAttribute, string containingNamespaceName)
{
var namespacePattern = PrepareValue(
generationAttribute,
AutomaticInterfaceGenerator.NamespaceParameterName,
containingNamespaceName
);

Debug.Assert(namespacePattern != null);

var namespaceName = namespacePattern!.Replace("*", containingNamespaceName);

return namespaceName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal sealed class {{AutomaticInterfaceGenerator.DefaultAttributeName}}Attrib
/// <summary>
/// Use source generator to automatically create a Interface from this class
/// </summary>
/// <param name="namespaceName">Namespace name for the generated interface. Defaults to the same namespace as the class.</param>
/// <param name="namespaceName">Namespace name for the generated interface. Defaults to the same namespace as the class. The '*' character may be used as a placeholder for the containing type's namespace, allowing you to specify only a prefix for the target namespace instead of its full name.</param>
/// <param name="interfaceName">Interface name for the generated interface. Defaults to an interface version of the class name, e.g ExampleClass -> IExampleClass.</param>
/// <param name="asInternal">If true, the generated interface will be internal, otherwise public</param>
internal {{AutomaticInterfaceGenerator.DefaultAttributeName}}Attribute(string {{AutomaticInterfaceGenerator.NamespaceParameterName}} = default(string), string {{AutomaticInterfaceGenerator.InterfaceParameterName}} = default(string), bool {{AutomaticInterfaceGenerator.AsInternalParameterName}} = false) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------

namespace AutomaticInterfaceExample.CustomNameSpaceSuffix
{
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.DemoMethod()" />
int DemoMethod();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------

namespace AutomaticInterfaceExample.CustomNameSpaceSuffix
{
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.DemoMethod()" />
int DemoMethod();

}
}
42 changes: 42 additions & 0 deletions AutomaticInterface/Tests/Misc/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,48 @@ class DemoClass
await Verify(Infrastructure.GenerateCode(code));
}


[Fact]
public async Task CustomNameSpaceWithWildcardWithinBrackedNamespace()
{
const string code = """

using AutomaticInterface;

namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface("*.CustomNameSpaceSuffix")]
class DemoClass
{
public int DemoMethod() => 5;
}
}

""";

await Verify(Infrastructure.GenerateCode(code));
}

[Fact]
public async Task CustomNameSpaceWithWildcardWithinFileNamespace()
{
const string code = """

using AutomaticInterface;

namespace AutomaticInterfaceExample;

[GenerateAutomaticInterface("*.CustomNameSpaceSuffix")]
class DemoClass
{
public int DemoMethod() => 5;
}

""";

await Verify(Infrastructure.GenerateCode(code));
}

[Fact]
public async Task AsInternal()
{
Expand Down