Skip to content

Commit 588a5f4

Browse files
committed
Add IAbpGlobalAssetsBundleService to get styles&scripts.
1 parent 4a62be7 commit 588a5f4

File tree

9 files changed

+255
-220
lines changed

9 files changed

+255
-220
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.FileProviders;
5+
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Logging.Abstractions;
7+
using Microsoft.Extensions.Options;
8+
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
9+
using Volo.Abp.Bundling.Styles;
10+
using Volo.Abp.DependencyInjection;
11+
12+
namespace Volo.Abp.AspNetCore.Bundling;
13+
14+
public abstract class AbpGlobalAssetsBundleServiceBase<TGlobalAssetsBundleService> : IAbpGlobalAssetsBundleService, ITransientDependency
15+
where TGlobalAssetsBundleService : class, IAbpGlobalAssetsBundleService
16+
{
17+
public ILogger<TGlobalAssetsBundleService> Logger { get; set; }
18+
19+
protected IOptions<AbpBundlingOptions> AbpBundlingOptions { get; }
20+
protected IBundleManager BundleManager { get; }
21+
22+
protected AbpGlobalAssetsBundleServiceBase(
23+
IOptions<AbpBundlingOptions> abpBundlingOptions,
24+
IBundleManager bundleManager)
25+
{
26+
AbpBundlingOptions = abpBundlingOptions;
27+
BundleManager = bundleManager;
28+
29+
Logger = NullLogger<TGlobalAssetsBundleService>.Instance;
30+
}
31+
32+
public async Task<string> GetStylesAsync()
33+
{
34+
var styleFiles = await BundleManager.GetStyleBundleFilesAsync(AbpBundlingOptions.Value.GlobalAssets.GlobalStyleBundleName!);
35+
var styles = string.Empty;
36+
37+
foreach (var file in styleFiles)
38+
{
39+
var fileInfo = GetFileInfo(file.FileName);
40+
if (fileInfo == null || !fileInfo.Exists)
41+
{
42+
Logger.LogError($"Could not find the file: {file.FileName}");
43+
continue;
44+
}
45+
46+
var fileContent = await fileInfo.ReadAsStringAsync();
47+
if (!BundleManager.As<BundleManagerBase>().IsBundlingEnabled())
48+
{
49+
fileContent = CssRelativePath.Adjust(fileContent,
50+
file.FileName,
51+
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"));
52+
53+
styles += $"/*{file.FileName}*/{Environment.NewLine}{fileContent}{Environment.NewLine}{Environment.NewLine}";
54+
}
55+
else
56+
{
57+
styles += $"{fileContent}{Environment.NewLine}{Environment.NewLine}";
58+
}
59+
}
60+
61+
return styles;
62+
}
63+
64+
public virtual async Task<string> GetScriptsAsync()
65+
{
66+
var scriptFiles = await BundleManager.GetScriptBundleFilesAsync(AbpBundlingOptions.Value.GlobalAssets.GlobalScriptBundleName!);
67+
var scripts = string.Empty;
68+
69+
foreach (var file in scriptFiles)
70+
{
71+
var fileInfo = GetFileInfo(file.FileName);
72+
if (fileInfo == null || !fileInfo.Exists)
73+
{
74+
Logger.LogError($"Could not find the file: {file.FileName}");
75+
continue;
76+
}
77+
78+
var fileContent = await fileInfo.ReadAsStringAsync();
79+
if (!BundleManager.As<BundleManagerBase>().IsBundlingEnabled())
80+
{
81+
scripts += $"{fileContent.EnsureEndsWith(';')}{Environment.NewLine}{Environment.NewLine}";
82+
}
83+
else
84+
{
85+
scripts += $"//{file.FileName}{Environment.NewLine}{fileContent.EnsureEndsWith(';')}{Environment.NewLine}{Environment.NewLine}";
86+
}
87+
}
88+
89+
return scripts;
90+
}
91+
92+
protected abstract IFileInfo? GetFileInfo(string fileName);
93+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
using System;
2-
using System.IO;
3-
using System.Text;
4-
using System.Threading.Tasks;
5-
using Microsoft.Extensions.DependencyInjection;
6-
using Microsoft.Extensions.FileProviders;
7-
using Microsoft.Extensions.Logging;
8-
using Microsoft.Extensions.Options;
91
using Volo.Abp.AspNetCore.Bundling;
10-
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
11-
using Volo.Abp.Bundling.Styles;
2+
using Volo.Abp.EventBus;
123
using Volo.Abp.Modularity;
134
using Volo.Abp.Threading;
14-
using Volo.Abp.VirtualFileSystem;
155

166
namespace Volo.Abp.AspNetCore.Components.MauiBlazor.Bundling;
177

@@ -25,86 +15,4 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
2515
{
2616
AsyncHelper.RunSync(() => OnApplicationInitializationAsync(context));
2717
}
28-
29-
public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
30-
{
31-
await InitialGlobalAssetsAsync(context);
32-
}
33-
34-
protected virtual async Task InitialGlobalAssetsAsync(ApplicationInitializationContext context)
35-
{
36-
var bundlingOptions = context.ServiceProvider.GetRequiredService<IOptions<AbpBundlingOptions>>().Value;
37-
var logger = context.ServiceProvider.GetRequiredService<ILogger<AbpAspNetCoreComponentsMauiBlazorBundlingModule>>();
38-
if (!bundlingOptions.GlobalAssets.Enabled)
39-
{
40-
return;
41-
}
42-
43-
var bundleManager = context.ServiceProvider.GetRequiredService<BundleManager>();
44-
var mauiBlazorContentFileProvider = context.ServiceProvider.GetRequiredService<IMauiBlazorContentFileProvider>();
45-
var dynamicFileProvider = context.ServiceProvider.GetRequiredService<IDynamicFileProvider>();
46-
if (!bundlingOptions.GlobalAssets.GlobalStyleBundleName.IsNullOrWhiteSpace())
47-
{
48-
var styleFiles = await bundleManager.GetStyleBundleFilesAsync(bundlingOptions.GlobalAssets.GlobalStyleBundleName);
49-
var styles = string.Empty;
50-
foreach (var file in styleFiles)
51-
{
52-
var fileInfo = mauiBlazorContentFileProvider.GetFileInfo(file.FileName);
53-
if (!fileInfo.Exists)
54-
{
55-
logger.LogError($"Could not find the file: {file.FileName}");
56-
continue;
57-
}
58-
59-
var fileContent = await fileInfo.ReadAsStringAsync();
60-
if (!bundleManager.IsBundlingEnabled())
61-
{
62-
fileContent = CssRelativePath.Adjust(fileContent,
63-
file.FileName,
64-
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"));
65-
66-
styles += $"/*{file.FileName}*/{Environment.NewLine}{fileContent}{Environment.NewLine}{Environment.NewLine}";
67-
}
68-
else
69-
{
70-
styles += $"{fileContent}{Environment.NewLine}{Environment.NewLine}";
71-
}
72-
}
73-
74-
dynamicFileProvider.AddOrUpdate(
75-
new InMemoryFileInfo("/wwwroot/" + bundlingOptions.GlobalAssets.CssFileName,
76-
Encoding.UTF8.GetBytes(styles),
77-
bundlingOptions.GlobalAssets.CssFileName));
78-
}
79-
80-
if (!bundlingOptions.GlobalAssets.GlobalScriptBundleName.IsNullOrWhiteSpace())
81-
{
82-
var scriptFiles = await bundleManager.GetScriptBundleFilesAsync(bundlingOptions.GlobalAssets.GlobalScriptBundleName);
83-
var scripts = string.Empty;
84-
foreach (var file in scriptFiles)
85-
{
86-
var fileInfo = mauiBlazorContentFileProvider.GetFileInfo(file.FileName);
87-
if (!fileInfo.Exists)
88-
{
89-
logger.LogError($"Could not find the file: {file.FileName}");
90-
continue;
91-
}
92-
93-
var fileContent = await fileInfo.ReadAsStringAsync();
94-
if (!bundleManager.IsBundlingEnabled())
95-
{
96-
scripts += $"{fileContent.EnsureEndsWith(';')}{Environment.NewLine}{Environment.NewLine}";
97-
}
98-
else
99-
{
100-
scripts += $"//{file.FileName}{Environment.NewLine}{fileContent.EnsureEndsWith(';')}{Environment.NewLine}{Environment.NewLine}";
101-
}
102-
}
103-
104-
dynamicFileProvider.AddOrUpdate(
105-
new InMemoryFileInfo("/wwwroot/" + bundlingOptions.GlobalAssets.JavaScriptFileName,
106-
Encoding.UTF8.GetBytes(scripts),
107-
bundlingOptions.GlobalAssets.JavaScriptFileName));
108-
}
109-
}
110-
}
18+
}

framework/src/Volo.Abp.AspNetCore.Components.MauiBlazor.Bundling/Volo/Abp/AspNetCore/Components/MauiBlazor/Bundling/AbpBlazorWebView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public class AbpBlazorWebView : BlazorWebView
88
{
99
public override IFileProvider CreateFileProvider(string contentRootDir)
1010
{
11-
return new CompositeFileProvider(Handler!.GetRequiredService<IMauiBlazorContentFileProvider>(), base.CreateFileProvider(contentRootDir));
11+
return new CompositeFileProvider(base.CreateFileProvider(contentRootDir), Handler!.GetRequiredService<IMauiBlazorContentFileProvider>());
1212
}
13-
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.IO;
2+
using Microsoft.Extensions.FileProviders;
3+
using Microsoft.Extensions.Options;
4+
using Microsoft.Maui.Storage;
5+
using Volo.Abp.AspNetCore.Bundling;
6+
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
7+
8+
namespace Volo.Abp.AspNetCore.Components.MauiBlazor.Bundling;
9+
10+
public class AbpGlobalAssetsBundleService : AbpGlobalAssetsBundleServiceBase<AbpGlobalAssetsBundleService>
11+
{
12+
protected IFileProvider MauiBlazorContentFileProvider { get; }
13+
protected string RootPath = "/wwwroot";
14+
15+
public AbpGlobalAssetsBundleService(
16+
IOptions<AbpBundlingOptions> abpBundlingOptions,
17+
IBundleManager bundleManager)
18+
: base(abpBundlingOptions, bundleManager)
19+
{
20+
21+
MauiBlazorContentFileProvider = CreateMauiBlazorContentFileProvider();
22+
}
23+
24+
protected virtual IFileProvider CreateMauiBlazorContentFileProvider()
25+
{
26+
var assetsDirectory = Path.Combine(FileSystem.Current.AppDataDirectory, "wwwroot");
27+
if (!Path.Exists(assetsDirectory))
28+
{
29+
Directory.CreateDirectory(assetsDirectory);
30+
}
31+
32+
return new PhysicalFileProvider(assetsDirectory);
33+
}
34+
35+
protected override IFileInfo? GetFileInfo(string fileName)
36+
{
37+
return MauiBlazorContentFileProvider.GetFileInfo(fileName);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
using System;
2-
using System.IO;
2+
using System.Text;
33
using Microsoft.Extensions.FileProviders;
4+
using Microsoft.Extensions.Options;
45
using Microsoft.Extensions.Primitives;
5-
using Microsoft.Maui.Controls.PlatformConfiguration;
66
using Microsoft.Maui.Storage;
7+
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
78
using Volo.Abp.DependencyInjection;
9+
using Volo.Abp.Threading;
810
using Volo.Abp.VirtualFileSystem;
911

1012
namespace Volo.Abp.AspNetCore.Components.MauiBlazor.Bundling;
1113

1214
public class MauiBlazorContentFileProvider : IMauiBlazorContentFileProvider, ISingletonDependency
1315
{
14-
private readonly IVirtualFileProvider _virtualFileProvider;
15-
private readonly IFileProvider _fileProvider;
16-
private string _rootPath = "/wwwroot";
16+
protected IAbpGlobalAssetsBundleService AbpGlobalAssetsBundleService { get; }
17+
protected IOptions<AbpBundlingOptions> AbpBundlingOptions { get; }
1718

18-
public MauiBlazorContentFileProvider(IVirtualFileProvider virtualFileProvider)
19+
public MauiBlazorContentFileProvider(
20+
IAbpGlobalAssetsBundleService abpGlobalAssetsBundleService,
21+
IOptions<AbpBundlingOptions> abpBundlingOptions)
1922
{
20-
_virtualFileProvider = virtualFileProvider;
21-
_fileProvider = CreateFileProvider();
23+
AbpGlobalAssetsBundleService = abpGlobalAssetsBundleService;
24+
AbpBundlingOptions = abpBundlingOptions;
2225
}
2326

2427
public string ContentRootPath => FileSystem.Current.AppDataDirectory;
@@ -30,39 +33,28 @@ public IFileInfo GetFileInfo(string subpath)
3033
return new NotFoundFileInfo(subpath);
3134
}
3235

33-
var fileInfo = _fileProvider.GetFileInfo(subpath);
34-
return fileInfo.Exists ? fileInfo : _fileProvider.GetFileInfo( _rootPath + subpath.EnsureStartsWith('/'));
35-
}
36+
if (string.Equals(subpath, AbpBundlingOptions.Value.GlobalAssets.GlobalStyleBundleName!, StringComparison.OrdinalIgnoreCase))
37+
{
38+
var styles = AsyncHelper.RunSync(() => AbpGlobalAssetsBundleService.GetStylesAsync());
39+
return new InMemoryFileInfo(subpath, Encoding.UTF8.GetBytes(styles), AbpBundlingOptions.Value.GlobalAssets.GlobalStyleBundleName!);
40+
}
3641

37-
public IDirectoryContents GetDirectoryContents(string subpath)
38-
{
39-
if (string.IsNullOrEmpty(subpath))
42+
if (string.Equals(subpath, AbpBundlingOptions.Value.GlobalAssets.GlobalScriptBundleName!, StringComparison.OrdinalIgnoreCase))
4043
{
41-
return NotFoundDirectoryContents.Singleton;
44+
var scripts = AsyncHelper.RunSync(() => AbpGlobalAssetsBundleService.GetScriptsAsync());
45+
return new InMemoryFileInfo(subpath, Encoding.UTF8.GetBytes(scripts), AbpBundlingOptions.Value.GlobalAssets.GlobalScriptBundleName!);
4246
}
4347

44-
var directory = _fileProvider.GetDirectoryContents(subpath);
45-
return directory.Exists ? directory : _fileProvider.GetDirectoryContents( _rootPath + subpath.EnsureStartsWith('/'));
48+
return new NotFoundFileInfo(subpath);
4649
}
4750

48-
public IChangeToken Watch(string filter)
51+
public IDirectoryContents GetDirectoryContents(string subpath)
4952
{
50-
return new CompositeChangeToken(
51-
[
52-
_fileProvider.Watch(_rootPath + filter),
53-
_fileProvider.Watch(filter)
54-
]
55-
);
53+
return NotFoundDirectoryContents.Singleton;
5654
}
5755

58-
protected virtual IFileProvider CreateFileProvider()
56+
public IChangeToken Watch(string filter)
5957
{
60-
var assetsDirectory = Path.Combine(ContentRootPath, _rootPath.TrimStart('/'));
61-
if (!Path.Exists(assetsDirectory))
62-
{
63-
Directory.CreateDirectory(assetsDirectory);
64-
}
65-
66-
return new CompositeFileProvider(new PhysicalFileProvider(assetsDirectory), _virtualFileProvider);
58+
return NullChangeToken.Singleton;
6759
}
68-
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Threading.Tasks;
2+
3+
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling;
4+
5+
public interface IAbpGlobalAssetsBundleService
6+
{
7+
Task<string> GetStylesAsync();
8+
9+
Task<string> GetScriptsAsync();
10+
}

0 commit comments

Comments
 (0)