2018-06-15 02:57:03 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|
|
|
|
using Microsoft.AspNetCore.Razor.TagHelpers;
|
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2019-08-04 23:13:40 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-06-15 02:57:03 +02:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2018-10-26 07:22:53 +02:00
|
|
|
|
namespace Teknik.Utilities.TagHelpers
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
[HtmlTargetElement("bundle")]
|
|
|
|
|
public class BundleTagHelper : TagHelper
|
|
|
|
|
{
|
2018-06-17 05:01:33 +02:00
|
|
|
|
private const string VirtualFolder = "./wwwroot/";
|
2018-06-15 02:57:03 +02:00
|
|
|
|
private const string ConfigPath = "bundleconfig.json";
|
|
|
|
|
|
2021-06-26 07:30:48 +02:00
|
|
|
|
private readonly IWebHostEnvironment _env;
|
2018-06-15 02:57:03 +02:00
|
|
|
|
|
|
|
|
|
private readonly IMemoryCache _cache;
|
|
|
|
|
|
|
|
|
|
[HtmlAttributeNotBound]
|
|
|
|
|
[ViewContext]
|
|
|
|
|
public ViewContext ViewContext { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool AppendVersion { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
public string Src { get; set; }
|
|
|
|
|
|
2021-06-26 07:30:48 +02:00
|
|
|
|
public BundleTagHelper(IWebHostEnvironment env, IMemoryCache cache)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
_env = env;
|
|
|
|
|
_cache = cache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
|
|
|
|
{
|
|
|
|
|
// Clear the initial wrap tag
|
|
|
|
|
output.TagName = string.Empty;
|
|
|
|
|
|
|
|
|
|
// Clean up the src
|
|
|
|
|
Src = Src.TrimStart(new [] { '~', '/' });
|
|
|
|
|
|
|
|
|
|
List<string> files = new List<string>();
|
|
|
|
|
|
|
|
|
|
switch (_env.EnvironmentName)
|
|
|
|
|
{
|
2018-10-26 07:22:53 +02:00
|
|
|
|
case "Test":
|
2018-06-15 02:57:03 +02:00
|
|
|
|
case "Production":
|
|
|
|
|
files.Add(Src);
|
|
|
|
|
break;
|
|
|
|
|
case "Development":
|
|
|
|
|
var configFile = Path.Combine(_env.ContentRootPath, ConfigPath);
|
|
|
|
|
var bundle = GetBundle(configFile, Src);
|
|
|
|
|
if (bundle == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Clean up the bundle to remove the virtual folder that aspnetcore provides.
|
|
|
|
|
files.AddRange(bundle.InputFiles.Select(file =>
|
|
|
|
|
{
|
|
|
|
|
if (file.StartsWith(VirtualFolder))
|
|
|
|
|
return file.Substring(VirtualFolder.Length);
|
|
|
|
|
return file;
|
|
|
|
|
}).ToList());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
// Get the file version for this file
|
|
|
|
|
string fullPath = "/" + file;
|
|
|
|
|
|
|
|
|
|
if (AppendVersion)
|
|
|
|
|
{
|
2019-08-04 23:13:40 +02:00
|
|
|
|
var _fileVersionProvider = ViewContext.HttpContext.RequestServices.GetRequiredService<IFileVersionProvider>();
|
|
|
|
|
fullPath = _fileVersionProvider.AddFileVersionToPath(ViewContext.HttpContext.Request.Path, fullPath);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (file.EndsWith(".js"))
|
|
|
|
|
{
|
|
|
|
|
output.Content.AppendHtml($"<script src='{fullPath}' type='text/javascript'></script>");
|
|
|
|
|
}
|
|
|
|
|
else if (file.EndsWith(".css"))
|
|
|
|
|
{
|
|
|
|
|
output.Content.AppendHtml($"<link rel='stylesheet' href='{fullPath}'></link>");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Bundle GetBundle(string configFile, string bundlePath)
|
|
|
|
|
{
|
|
|
|
|
var file = new FileInfo(configFile);
|
|
|
|
|
if (!file.Exists)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var bundles = JsonConvert.DeserializeObject<IEnumerable<Bundle>>(File.ReadAllText(configFile));
|
|
|
|
|
return (from b in bundles
|
|
|
|
|
where b.OutputFileName.EndsWith(bundlePath, StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
|
select b).FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Bundle
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("outputFileName")]
|
|
|
|
|
public string OutputFileName { get; set; }
|
|
|
|
|
|
|
|
|
|
[JsonProperty("inputFiles")]
|
|
|
|
|
public List<string> InputFiles { get; set; } = new List<string>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|