Merge pull request #6532 from abpframework/maliming/PhysicalFileProvider

Compatible with PhysicalFileProvider in the TextTemplating.
pull/6623/head
Halil İbrahim Kalkan 5 years ago committed by GitHub
commit 6d92c4a159
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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;
@ -52,7 +53,13 @@ namespace Volo.Abp.TextTemplating.VirtualFiles
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)

@ -11,18 +11,18 @@ namespace Volo.Abp.TextTemplating.VirtualFiles
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)
{
@ -43,4 +43,4 @@ namespace Volo.Abp.TextTemplating.VirtualFiles
return _dictionary.GetOrDefault(cultureName);
}
}
}
}

@ -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>

@ -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);
}
}
}
}

@ -4,46 +4,42 @@ using Xunit;
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!");
}
}
}

Loading…
Cancel
Save