Skip to content

Commit

Permalink
Rename Namespace to HintNamespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Dec 22, 2024
1 parent 8a22794 commit 5df3936
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ private static GeneratorDriverRunResult Generate(string input,
var analyzerConfigOptionsProvider = new AnalyzerConfigOptionsProviderMock
{
{ "IsRazorBlade", "True" },
{ "Namespace", "TestNamespace" }
{ "HintNamespace", "TestNamespace" }
};

if (embeddedLibrary)
Expand Down
14 changes: 7 additions & 7 deletions src/RazorBlade.Analyzers/RazorBladeSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
if (!string.Equals(isTargetFile, bool.TrueString, StringComparison.OrdinalIgnoreCase))
return null;

if (!options.TryGetValue("build_metadata.AdditionalFiles.Namespace", out var ns))
ns = null;
if (!options.TryGetValue("build_metadata.AdditionalFiles.HintNamespace", out var hintNamespace))
hintNamespace = null;

return new InputFile(
additionalText,
ns,
hintNamespace,
CSharpIdentifier.SanitizeIdentifier(Path.GetFileNameWithoutExtension(additionalText.Path))
);
}
Expand All @@ -92,14 +92,14 @@ private static void Generate(SourceProductionContext context, InputFile file, Gl
context.ReportDiagnostic(diagnostic.ToDiagnostic());

context.AddSource(
$"{file.Namespace}.{file.ClassName}.Razor.g.cs",
$"{file.HintNamespace}.{file.ClassName}.Razor.g.cs",
csharpDoc.GeneratedCode
);

if (!string.IsNullOrEmpty(libraryCode))
{
context.AddSource(
$"{file.Namespace}.{file.ClassName}.RazorBlade.g.cs",
$"{file.HintNamespace}.{file.ClassName}.RazorBlade.g.cs",
libraryCode
);
}
Expand All @@ -122,7 +122,7 @@ private static RazorCSharpDocument GenerateRazorCode(SourceText sourceText, Inpu
configurationFeature.ConfigureNamespace.Add((codeDoc, node) =>
{
node.Content = NamespaceVisitor.GetNamespaceDirectiveContent(codeDoc)
?? file.Namespace
?? file.HintNamespace
?? "Razor";
});

Expand Down Expand Up @@ -177,7 +177,7 @@ private static string GenerateLibrarySpecificCode(RazorCSharpDocument generatedD

static partial void OnGenerate();

private record InputFile(AdditionalText AdditionalText, string? Namespace, string ClassName);
private record InputFile(AdditionalText AdditionalText, string? HintNamespace, string ClassName);

private record GlobalOptions(CSharpParseOptions ParseOptions, ImmutableArray<SyntaxTree> AdditionalSyntaxTrees);
}
7 changes: 7 additions & 0 deletions src/RazorBlade.IntegrationTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ private static void WriteTemplate(RazorTemplate template)

private static void WriteSeparator()
=> Console.WriteLine("--------------------------------------------------");

private static void CheckNamespaces()
{
_ = typeof(global::RazorBlade.IntegrationTest.TestTemplate);
_ = typeof(global::RazorBlade.IntegrationTest.Examples.ExampleTemplate);
_ = typeof(global::FooBar.OtherNamespace.TestTemplateWithNamespace);
}
}

public class FooBarModelClass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@inherits RazorBlade.HtmlTemplate
@namespace FooBar.OtherNamespace
4 changes: 2 additions & 2 deletions src/RazorBlade/RazorBlade.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<CompilerVisibleProperty Include="RazorBladeEmbeddedLibrary" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="IsRazorBlade" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="Namespace" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="HintNamespace" />

<None Remove="**/*.cshtml" Condition="'$(EnableDefaultItems)' == 'true' and '$(EnableDefaultRazorBladeItems)' == 'true'" />
</ItemGroup>
Expand All @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<RazorBlade Update="@(RazorBlade)" Namespace="$([MSBuild]::ValueOrDefault('$(RootNamespace).%(RelativeDir)', '').Replace('\', '.').Replace('/', '.').Trim('.'))" />
<RazorBlade Update="@(RazorBlade)" HintNamespace="$([MSBuild]::ValueOrDefault('$(RootNamespace).%(RelativeDir)', '').Replace('\', '.').Replace('/', '.').Trim('.'))" />
<RazorBlade Update="@(RazorBlade)" IsRazorBlade="True" />

<AdditionalFiles Include="@(RazorBlade)" />
Expand Down

0 comments on commit 5df3936

Please sign in to comment.