Skip to content

Commit 7f4306e

Browse files
committed
Added ability to alter the class modifier
1 parent bd6df18 commit 7f4306e

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

MSBuildRazorCompiler/CompileRazorFiles.targets

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
<MSBuildRazorCompilerExe>dotnet "$(BuildPath)netcoreapp3.1\MSBuildRazorCompiler.dll"</MSBuildRazorCompilerExe>
66
<MsBuildRazorCompilerPath>$(MSBuildProjectDirectory)</MsBuildRazorCompilerPath>
77
<MsBuildRazorCompilerRootNamespace>$(RootNamespace)</MsBuildRazorCompilerRootNamespace>
8-
</PropertyGroup>
8+
<MsBuildRazorCompilerClassModifier>internal</MsBuildRazorCompilerClassModifier>
9+
10+
</PropertyGroup>
911

1012
<Target Name="CompileRazorFiles" BeforeTargets="CoreCompile">
11-
<Exec Command="$(MSBuildRazorCompilerExe) &quot;$(MsBuildRazorCompilerPath)&quot; $(RootNamespace)" />
13+
<Exec Command="$(MSBuildRazorCompilerExe) &quot;$(MsBuildRazorCompilerPath)&quot; $(MsBuildRazorCompilerRootNamespace) $(MsBuildRazorCompilerClassModifier)" />
1214
<!-- We need to re-scan included source files in case this build has generated new ones https://stackoverflow.com/a/44829863 -->
1315
<ItemGroup>
1416
<Compile Include="**/*$(DefaultLanguageSourceExtension)"

MSBuildRazorCompiler/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static int Main(string[] args)
1616
{
1717
var directoryToApply = args.Length > 0 ? args[0] : null;
1818
var rootNamespace = args.Length > 1 ? args[1] : null;
19+
var classModifier = args.Length > 2 ? args[2] : "internal";
1920

2021
if (string.IsNullOrEmpty(directoryToApply) || !Directory.Exists(directoryToApply) || !Path.IsPathFullyQualified(directoryToApply))
2122
{
@@ -48,7 +49,7 @@ static int Main(string[] args)
4849
Console.WriteLine($" Classname will be `{className}` and namespace will be `{namespacePath}`");
4950

5051
var code = source.GeneratedCode
51-
.Replace("public class GeneratedTemplate", $"public class {className}")
52+
.Replace("public class GeneratedTemplate", $"{classModifier} class {className}")
5253
.Replace("namespace RazorLight.CompiledTemplates", $"namespace {namespacePath}")
5354
.Replace("typeof(RazorLight.CompiledTemplates.GeneratedTemplate)", $"typeof({namespacePath}.{className})");
5455

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ Because this library generates `.cs` files you don't need to worry about compila
2222
3. Compile - you should now see `SomeFile.cshtml.cs` next to your `SomeFile.cshtml`, this new file will contain a class `SomeFile` that extends `RazorLight.TemplatePage<TModel>` and will be in the namespace of your project at the folder level your file was in
2323
4. Execute the following code to get a rendered result (note: you'll need to add a `using RazorRenderer;`): `new SomeFile().Render(model, viewBag or null)` - there is also an async variant
2424

25+
## Configuration
26+
27+
If you want to configure the options for the compiler you can override any of these default variables in your `.csproj` file:
28+
29+
```xml
30+
<BuildPath Condition="'$(BuildPath)' == ''">$(MSBuildThisFileDirectory)</BuildPath>
31+
<MSBuildRazorCompilerExe>dotnet "$(BuildPath)netcoreapp3.1\MSBuildRazorCompiler.dll"</MSBuildRazorCompilerExe>
32+
<MsBuildRazorCompilerPath>$(MSBuildProjectDirectory)</MsBuildRazorCompilerPath>
33+
<MsBuildRazorCompilerRootNamespace>$(RootNamespace)</MsBuildRazorCompilerRootNamespace>
34+
<MsBuildRazorCompilerClassModifier>internal</MsBuildRazorCompilerClassModifier>
35+
```
36+
2537
## Intellisense
2638

2739
For intellisense to work it's recommended you add the following to the top of your file:

0 commit comments

Comments
 (0)