Skip to content

Commit

Permalink
Compatible with PhysicalFileProvider in the TextTemplating.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Dec 8, 2020
1 parent 0465073 commit d495686
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 48 deletions.
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
Expand Down Expand Up @@ -52,7 +53,13 @@ public virtual async Task<ILocalizedTemplateContentReader> CreateAsync(TemplateD
var fileInfo = VirtualFileProvider.GetFileInfo(virtualPath);
if (!fileInfo.Exists)
{
throw new AbpException("Could not find a file/folder at the location: " + virtualPath);
var directoryContents = VirtualFileProvider.GetDirectoryContents(virtualPath);
if (!directoryContents.Exists)
{
throw new AbpException("Could not find a file/folder at the location: " + virtualPath);
}

fileInfo = new VirtualDirectoryFileInfo(virtualPath, virtualPath, DateTimeOffset.UtcNow);
}

if (fileInfo.IsDirectory)
Expand Down
Expand Up @@ -11,18 +11,18 @@ public class VirtualFolderLocalizedTemplateContentReader : ILocalizedTemplateCon
private Dictionary<string, string> _dictionary;

public async Task ReadContentsAsync(
IVirtualFileProvider virtualFileProvider,
IVirtualFileProvider virtualFileProvider,
string virtualPath)
{
_dictionary = new Dictionary<string, string>();

var directoryInfo = virtualFileProvider.GetFileInfo(virtualPath);
if (!directoryInfo.IsDirectory)
var directoryContents = virtualFileProvider.GetDirectoryContents(virtualPath);
if (!directoryContents.Exists)
{
throw new AbpException("Given virtual path is not a folder: " + virtualPath);
throw new AbpException("Could not find a folder at the location: " + virtualPath);
}

foreach (var file in virtualFileProvider.GetDirectoryContents(virtualPath))
foreach (var file in directoryContents)
{
if (file.IsDirectory)
{
Expand All @@ -43,4 +43,4 @@ public string GetContentOrNull(string cultureName)
return _dictionary.GetOrDefault(cultureName);
}
}
}
}
Expand Up @@ -10,8 +10,9 @@
<ItemGroup>
<EmbeddedResource Include="Volo\Abp\TextTemplating\Localization\**\*.json" />
<None Remove="Volo\Abp\TextTemplating\Localization\**\*.json" />
<EmbeddedResource Include="Volo\Abp\TextTemplating\SampleTemplates\**\*.tpl" />
<None Remove="Volo\Abp\TextTemplating\SampleTemplates\**\*.tpl" />
<EmbeddedResource Include="Volo\Abp\TextTemplating\SampleTemplates\**\*.tpl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
Expand Down
@@ -0,0 +1,59 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Shouldly;
using Volo.Abp.VirtualFileSystem;
using Xunit;

namespace Volo.Abp.TextTemplating.VirtualFiles
{
public class LocalizedTemplateContentReaderFactory_Tests: AbpTextTemplatingTestBase
{
private readonly ITemplateDefinitionManager _templateDefinitionManager;

public LocalizedTemplateContentReaderFactory_Tests()
{
_templateDefinitionManager = GetRequiredService<ITemplateDefinitionManager>();
}

[Fact]
public async Task Create_Should_Work_With_PhysicalFileProvider()
{
var localizedTemplateContentReaderFactory = new LocalizedTemplateContentReaderFactory(
new PhysicalFileVirtualFileProvider(
new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(),
@"Volo\Abp\TextTemplating\"))));

var reader = await localizedTemplateContentReaderFactory.CreateAsync(_templateDefinitionManager.Get(TestTemplates.WelcomeEmail));

reader.GetContentOrNull("en").ShouldBe("Welcome {{model.name}} to the abp.io!");
reader.GetContentOrNull("tr").ShouldBe("Merhaba {{model.name}}, abp.io'ya hoşgeldiniz!");
}

class PhysicalFileVirtualFileProvider : IVirtualFileProvider
{
private readonly PhysicalFileProvider _physicalFileProvider;

public PhysicalFileVirtualFileProvider(PhysicalFileProvider physicalFileProvider)
{
_physicalFileProvider = physicalFileProvider;
}

public IFileInfo GetFileInfo(string subpath)
{
return _physicalFileProvider.GetFileInfo(subpath);
}

public IDirectoryContents GetDirectoryContents(string subpath)
{
return _physicalFileProvider.GetDirectoryContents(subpath);
}

public IChangeToken Watch(string filter)
{
return _physicalFileProvider.Watch(filter);
}
}
}
}
Expand Up @@ -4,46 +4,42 @@

namespace Volo.Abp.TextTemplating.VirtualFiles
{
//TODO: Make tests running again!
//public class VirtualFileTemplateContributor_Tests : AbpTextTemplatingTestBase
//{
// [Fact]
// public async Task Should_Get_Localized_Content_By_Culture()
// {
// var contributor = new VirtualFileTemplateContentContributor(
// "/SampleTemplates/WelcomeEmail"
// );
public class VirtualFileTemplateContributor_Tests : AbpTextTemplatingTestBase
{
private readonly ITemplateDefinitionManager _templateDefinitionManager;
private readonly VirtualFileTemplateContentContributor _virtualFileTemplateContentContributor;

// contributor.Initialize(
// new TemplateContentContributorInitializationContext(
// new TemplateDefinition("Test"),
// ServiceProvider
// )
// );
public VirtualFileTemplateContributor_Tests()
{
_templateDefinitionManager = GetRequiredService<ITemplateDefinitionManager>();
_virtualFileTemplateContentContributor = GetRequiredService<VirtualFileTemplateContentContributor>();
}

// (await contributor
// .GetOrNullAsync("en")).ShouldBe("Welcome {{model.name}} to the abp.io!");
[Fact]
public async Task Should_Get_Localized_Content_By_Culture()
{
(await _virtualFileTemplateContentContributor.GetOrNullAsync(
new TemplateContentContributorContext(_templateDefinitionManager.Get(TestTemplates.WelcomeEmail),
ServiceProvider,
"en")))
.ShouldBe("Welcome {{model.name}} to the abp.io!");

// (await contributor
// .GetOrNullAsync("tr")).ShouldBe("Merhaba {{model.name}}, abp.io'ya hoşgeldiniz!");
// }
(await _virtualFileTemplateContentContributor.GetOrNullAsync(
new TemplateContentContributorContext(_templateDefinitionManager.Get(TestTemplates.WelcomeEmail),
ServiceProvider,
"tr")))
.ShouldBe("Merhaba {{model.name}}, abp.io'ya hoşgeldiniz!");
}

// [Fact]
// public async Task Should_Get_Non_Localized_Template_Content()
// {
// var contributor = new VirtualFileTemplateContentContributor(
// "/SampleTemplates/ForgotPasswordEmail.tpl"
// );

// contributor.Initialize(
// new TemplateContentContributorInitializationContext(
// new TemplateDefinition("Test"),
// ServiceProvider
// )
// );

// (await contributor
// .GetOrNullAsync()).ShouldBe("{{l \"HelloText\"}}. Please click to the following link to get an email to reset your password!");
// }
//}
[Fact]
public async Task Should_Get_Non_Localized_Template_Content()
{
(await _virtualFileTemplateContentContributor.GetOrNullAsync(
new TemplateContentContributorContext(
_templateDefinitionManager.Get(TestTemplates.ForgotPasswordEmail),
ServiceProvider,
null)))
.ShouldBe("{{L \"HelloText\" model.name}}, {{L \"HowAreYou\" }}. Please click to the following link to get an email to reset your password!");
}
}
}

0 comments on commit d495686

Please sign in to comment.