Skip to content

Commit

Permalink
Add xmldocs, add UseOptions to pass options directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzabroski committed Jun 30, 2020
1 parent cb6d28b commit 3652882
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/RazorLight/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using RazorLight.DependencyInjection;

Expand Down
79 changes: 57 additions & 22 deletions src/RazorLight/RazorLightEngineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public class RazorLightEngineBuilder

private bool disableEncoding = false;

private RazorLightOptions options;

/// <summary>
/// Configures RazorLight to use a project. Use UseEmbeddedResourcesProject
/// </summary>
/// <param name="project"></param>
/// <returns></returns>
public virtual RazorLightEngineBuilder UseProject(RazorLightProject project)
{
if (project == null)
Expand All @@ -42,13 +49,62 @@ public virtual RazorLightEngineBuilder UseProject(RazorLightProject project)
return this;
}

/// <summary>
/// Configures RazorLight to use a project whose persistent store is the file system.
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public RazorLightEngineBuilder UseFileSystemProject(string root)
{
project = new FileSystemRazorProject(root);

return this;
}

/// <summary>
/// Configures RazorLight to use a project whose persistent store is the file system.
/// </summary>
/// <param name="root">Directory path to the root folder containing your Razor markup files.</param>
/// <param name="extension">If you wish, you can use a different extension than .cshtml.</param>
/// <returns><see cref="RazorLightEngineBuilder"/></returns>
public RazorLightEngineBuilder UseFileSystemProject(string root, string extension)
{
project = new FileSystemRazorProject(root, extension);

return this;
}

/// <summary>
/// Configures RazorLight to use a project whose persistent store an assembly manifest resource stream.
/// </summary>
/// <param name="rootType">Any type in the root namespace (prefix) for your assembly manifest resource stream.</param>
/// <returns><see cref="EmbeddedRazorProject"/></returns>
public RazorLightEngineBuilder UseEmbeddedResourcesProject(Type rootType)
{
project = new EmbeddedRazorProject(rootType);

return this;
}

/// <summary>
/// Configures RazorLight to use a project whose persistent store an assembly manifest resource stream.
/// </summary>
/// <param name="assembly">Assembly containing embedded resources</param>
/// <param name="rootNamespace">The root namespace (prefix) for your assembly manifest resource stream.</param>
/// <returns></returns>
public RazorLightEngineBuilder UseEmbeddedResourcesProject(Assembly assembly, string rootNamespace = null)
{
project = new EmbeddedRazorProject(assembly, rootNamespace);

return this;
}

public RazorLightEngineBuilder UseOptions(RazorLightOptions razorLightOptions)
{
options = razorLightOptions;

return this;
}

/// <summary>
/// Disables encoding of HTML entities in variables.
Expand Down Expand Up @@ -88,28 +144,6 @@ public RazorLightEngineBuilder EnableEncoding()
return this;
}

public RazorLightEngineBuilder UseEmbeddedResourcesProject(Assembly assembly, string rootNamespace = null)
{
project = new EmbeddedRazorProject(assembly, rootNamespace);

return this;
}


public RazorLightEngineBuilder UseFileSystemProject(string root)
{
project = new FileSystemRazorProject(root);

return this;
}

public RazorLightEngineBuilder UseFileSystemProject(string root, string extension)
{
project = new FileSystemRazorProject(root, extension);

return this;
}

public virtual RazorLightEngineBuilder UseMemoryCachingProvider()
{
cachingProvider = new MemoryCachingProvider();
Expand Down Expand Up @@ -218,6 +252,7 @@ public virtual RazorLightEngineBuilder SetOperatingAssembly(Assembly assembly)

public virtual RazorLightEngine Build()
{
//options = options ?? new RazorLightOptions();
var options = new RazorLightOptions();

if (namespaces != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Xunit;
using RazorLight.Extensions;
using System;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Builder;

namespace RazorLight.Tests.Extensions
{
Expand All @@ -14,9 +16,9 @@ public class ServiceCollectionExtensionsTest
private IServiceCollection GetServices()
{
var services = new ServiceCollection();
var envMock = new Mock<IHostingEnvironment>();
var envMock = new Mock<Microsoft.AspNetCore.Hosting.IHostingEnvironment>();
envMock.Setup(m => m.ContentRootPath).Returns(rootPath);
services.AddSingleton<IHostingEnvironment>(envMock.Object);
services.AddSingleton<Microsoft.AspNetCore.Hosting.IHostingEnvironment>(envMock.Object);

return services;
}
Expand Down Expand Up @@ -45,6 +47,66 @@ public void Ensure_FactoryMethod_Is_Called()
var engine = provider.GetService<IRazorLightEngine>();

Assert.NotNull(engine);
Assert.IsType<RazorLightEngine>(engine);
Assert.True(called);
}

public class EmbeddedEngineStartup
{
public void Configure(IApplicationBuilder app)
{

}
public void ConfigureServices(IServiceCollection services)
{
var embeddedEngine = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(typeof(EmbeddedEngineStartup)) // exception without this (or another project type)
.UseMemoryCachingProvider()
.Build();

services.AddRazorLight(() => embeddedEngine);
}
}

#if !(NETCOREAPP2_0)
[Fact]
public void Ensure_Works_With_Generic_Host()
{
static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<EmbeddedEngineStartup>();
});
}

var hostBuilder = CreateHostBuilder(null);

Assert.NotNull(hostBuilder);
var host = hostBuilder.Build();
Assert.NotNull(host);
host.Services.GetService<IRazorLightEngine>();
}
#endif

[Fact]
public void Ensure_RazorLightEngineWithFileSystemFactory_Is_Called()
{
var services = GetServices();
var called = false;

services.AddRazorLight(() =>
{
called = true;
return new RazorLightEngineWithFileSystemProjectFactory().Create();
});

var provider = services.BuildServiceProvider();
var engine = provider.GetService<IRazorLightEngine>();

Assert.NotNull(engine);
Assert.IsType<RazorLightEngine>(engine);
Assert.True(called);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/RazorLight.Tests/RazorLight.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

<ItemGroup>
<PackageReference Include="MarkdownSnippets.MsBuild" Version="19.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.5" />
<PackageReference Include="Moq" Version="4.7.99" />
<PackageReference Include="Pose" Version="1.2.1" />
<PackageReference Include="Verify.Xunit" Version="5.0.2" />
Expand Down

0 comments on commit 3652882

Please sign in to comment.