Moving CDF related files into its own folder

This commit is contained in:
Elitsa Marinovska
2020-03-17 15:35:18 +01:00
parent 755b52e136
commit ae4b9c3f42
11 changed files with 133 additions and 272 deletions
@@ -1,59 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Assets;
using Umbraco.Core.Runtime;
using Umbraco.Web.Composing;
using Umbraco.Web.PropertyEditors;
namespace Umbraco.Web.JavaScript
{
internal abstract class AssetInitialization
{
private readonly IRuntimeMinifier _runtimeMinifier;
public AssetInitialization(IRuntimeMinifier runtimeMinifier)
{
_runtimeMinifier = runtimeMinifier;
}
protected IEnumerable<string> ScanPropertyEditors(AssetType assetType, HttpContextBase httpContext)
{
if (httpContext == null) throw new ArgumentNullException(nameof(httpContext));
var attributes = Current.PropertyEditors
.SelectMany(x => x.GetType().GetCustomAttributes<PropertyEditorAssetAttribute>(false))
.Where(x => x.AssetType == assetType)
.Select(x => x.DependencyFile)
.ToList();
return _runtimeMinifier.GetAssetPaths(assetType, attributes);
}
internal static IEnumerable<string> OptimizeAssetCollection(IEnumerable<string> assets, AssetType assetType, HttpContextBase httpContext, IRuntimeMinifier runtimeMinifier)
{
if (httpContext == null) throw new ArgumentNullException(nameof(httpContext));
var requestUrl = httpContext.Request.Url;
if (requestUrl == null) throw new ArgumentException("HttpContext.Request.Url is null.", nameof(httpContext));
var dependencies = assets.Where(x => x.IsNullOrWhiteSpace() == false).Select(x =>
{
// most declarations with be made relative to the /umbraco folder, so things
// like lib/blah/blah.js so we need to turn them into absolutes here
if (x.StartsWith("/") == false && Uri.IsWellFormedUriString(x, UriKind.Relative))
{
return new AssetFile(assetType) { FilePath = new Uri(requestUrl, x).AbsolutePath };
}
return assetType == AssetType.Javascript
? new JavascriptFile(x)
: (IAssetFile) new CssFile(x);
}).ToList();
return runtimeMinifier.GetAssetPaths(assetType, dependencies);;
}
}
}
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Assets;
using Umbraco.Core.Runtime;
using Umbraco.Web.Composing;
using Umbraco.Web.PropertyEditors;
namespace Umbraco.Web.JavaScript.CDF
{
internal abstract class AssetInitialization
{
private readonly IRuntimeMinifier _runtimeMinifier;
public AssetInitialization(IRuntimeMinifier runtimeMinifier)
{
_runtimeMinifier = runtimeMinifier;
}
protected IEnumerable<string> ScanPropertyEditors(AssetType assetType, HttpContextBase httpContext)
{
if (httpContext == null) throw new ArgumentNullException(nameof(httpContext));
var attributes = Current.PropertyEditors
.SelectMany(x => x.GetType().GetCustomAttributes<PropertyEditorAssetAttribute>(false))
.Where(x => x.AssetType == assetType)
.Select(x => x.DependencyFile)
.ToList();
return _runtimeMinifier.GetAssetPaths(assetType, attributes);
}
}
}
@@ -1,20 +1,15 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClientDependency.Core.CompositeFiles.Providers;
using ClientDependency.Core.Config;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Hosting;
using Umbraco.Core.Runtime;
using Umbraco.Web.JavaScript;
using Umbraco.Web.Runtime;
namespace Umbraco.Web.Runtime
namespace Umbraco.Web.JavaScript.CDF
{
[ComposeAfter(typeof(WebInitialComponent))]
public sealed class ClientDependencyComponent : IComponent
@@ -26,8 +21,7 @@ namespace Umbraco.Web.Runtime
public ClientDependencyComponent(
IHostingSettings hostingSettings,
IHostingEnvironment hostingEnvironment,
IRuntimeSettings settings,
IRuntimeMinifier runtimeMinifier)
IRuntimeSettings settings)
{
_hostingSettings = hostingSettings;
_hostingEnvironment = hostingEnvironment;
@@ -1,14 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Runtime;
using Umbraco.Web.JavaScript;
namespace Umbraco.Web.Runtime
namespace Umbraco.Web.JavaScript.CDF
{
public sealed class ClientDependencyComposer : ComponentComposer<ClientDependencyComponent>
{
@@ -10,11 +10,9 @@ using ClientDependency.Core.CompositeFiles.Providers;
using ClientDependency.Core.Config;
using Semver;
using Umbraco.Core.IO;
using Umbraco.Web.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.Runtime;
namespace Umbraco.Web.JavaScript
namespace Umbraco.Web.JavaScript.CDF
{
/// <summary>
/// A utility class for working with CDF config and cache files - use sparingly!
@@ -22,14 +20,18 @@ namespace Umbraco.Web.JavaScript
public class ClientDependencyConfiguration
{
private readonly ILogger _logger;
private readonly IRuntimeMinifier _runtimeMinifier;
private readonly string _fileName;
public ClientDependencyConfiguration(ILogger logger, IIOHelper ioHelper, IRuntimeMinifier runtimeMinifier)
private string FileMapDefaultFolder
{
get => XmlFileMapper.FileMapDefaultFolder;
set => XmlFileMapper.FileMapDefaultFolder = value;
}
public ClientDependencyConfiguration(ILogger logger, IIOHelper ioHelper)
{
if (logger == null) throw new ArgumentNullException("logger");
_logger = logger;
_runtimeMinifier = runtimeMinifier;
_fileName = ioHelper.MapPath(string.Format("{0}/ClientDependency.config", Core.Constants.SystemDirectories.Config));
}
@@ -111,9 +113,9 @@ namespace Umbraco.Web.JavaScript
try
{
var fullPath = _runtimeMinifier.FileMapDefaultFolder.StartsWith("~/")
? currentHttpContext.Server.MapPath(_runtimeMinifier.FileMapDefaultFolder)
: _runtimeMinifier.FileMapDefaultFolder;
var fullPath = FileMapDefaultFolder.StartsWith("~/")
? currentHttpContext.Server.MapPath(FileMapDefaultFolder)
: FileMapDefaultFolder;
if (fullPath != null)
{
cdfTempDirectories.Add(fullPath);
@@ -7,7 +7,7 @@ using Umbraco.Core.Assets;
using Umbraco.Core.Manifest;
using Umbraco.Core.Runtime;
namespace Umbraco.Web.JavaScript
namespace Umbraco.Web.JavaScript.CDF
{
internal class CssInitialization : AssetInitialization
{
@@ -32,7 +32,7 @@ namespace Umbraco.Web.JavaScript
public IEnumerable<string> GetStylesheetFiles(HttpContextBase httpContext)
{
var stylesheets = new HashSet<string>();
var optimizedManifest = OptimizeAssetCollection(_parser.Manifest.Stylesheets, AssetType.Css, httpContext, _runtimeMinifier);
var optimizedManifest = JavaScriptHelper.OptimizeAssetCollection(_parser.Manifest.Stylesheets, AssetType.Css, httpContext, _runtimeMinifier);
foreach (var stylesheet in optimizedManifest)
stylesheets.Add(stylesheet);
@@ -3,7 +3,7 @@ using System.Web;
using ClientDependency.Core;
using ClientDependency.Core.FileRegistration.Providers;
namespace Umbraco.Web.JavaScript
namespace Umbraco.Web.JavaScript.CDF
{
/// <summary>
/// A custom renderer that only outputs a dependency path instead of script tags - for use with the js loader with yepnope
@@ -45,6 +45,5 @@ namespace Umbraco.Web.JavaScript
{
return css + Delimiter;
}
}
}
@@ -0,0 +1,68 @@
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core.Assets;
using Umbraco.Core.Manifest;
using Umbraco.Core.Runtime;
namespace Umbraco.Web.JavaScript.CDF
{
/// <summary>
/// Reads from all defined manifests and ensures that any of their initialization is output with the
/// main Umbraco initialization output.
/// </summary>
internal class JsInitialization : AssetInitialization
{
private readonly IManifestParser _parser;
private readonly IRuntimeMinifier _runtimeMinifier;
public JsInitialization(IManifestParser parser, IRuntimeMinifier runtimeMinifier) : base(runtimeMinifier)
{
_parser = parser;
_runtimeMinifier = runtimeMinifier;
}
/// <summary>
/// Returns a list of optimized script paths for the back office
/// </summary>
/// <param name="httpContext"></param>
/// <param name="umbracoInit"></param>
/// <param name="additionalJsFiles"></param>
/// <returns>
/// Cache busted/optimized script paths for the back office including manifest and property editor scripts
/// </returns>
/// <remarks>
/// Used to cache bust and optimize script paths for the back office
/// </remarks>
public IEnumerable<string> OptimizeBackOfficeScriptFiles(HttpContextBase httpContext, IEnumerable<string> umbracoInit, IEnumerable<string> additionalJsFiles = null)
{
var scripts = new HashSet<string>();
foreach (var script in umbracoInit)
scripts.Add(script);
foreach (var script in _parser.Manifest.Scripts)
scripts.Add(script);
if (additionalJsFiles != null)
foreach (var script in additionalJsFiles)
scripts.Add(script);
scripts = new HashSet<string>(JavaScriptHelper.OptimizeAssetCollection(scripts, AssetType.Javascript, httpContext, _runtimeMinifier));
foreach (var script in ScanPropertyEditors(AssetType.Javascript, httpContext))
scripts.Add(script);
return scripts.ToArray();
}
/// <summary>
/// Returns the default config as a JArray
/// </summary>
/// <returns></returns>
internal static IEnumerable<string> GetDefaultInitialization()
{
var resources = JsonConvert.DeserializeObject<JArray>(Resources.JsInitialize);
return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString());
}
}
}
@@ -1,12 +1,10 @@
using System.Web.UI;
using ClientDependency.Core.Controls;
using ClientDependency.Core.FileRegistration.Providers;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.IO;
using Umbraco.Web.Composing;
namespace Umbraco.Web.JavaScript
namespace Umbraco.Web.JavaScript.CDF
{
/// <summary>
/// Used to load in all client dependencies for Umbraco.
@@ -1,171 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Assets;
using Umbraco.Web.Composing;
using Umbraco.Core.Manifest;
using Umbraco.Core.Configuration;
using Umbraco.Core.IO;
using Umbraco.Core.Runtime;
namespace Umbraco.Web.JavaScript
{
/// <summary>
/// Reads from all defined manifests and ensures that any of their initialization is output with the
/// main Umbraco initialization output.
/// </summary>
internal class JsInitialization : AssetInitialization
{
private readonly IManifestParser _parser;
private readonly IRuntimeMinifier _runtimeMinifier;
public JsInitialization(IManifestParser parser, IRuntimeMinifier runtimeMinifier) : base(runtimeMinifier)
{
_parser = parser;
_runtimeMinifier = runtimeMinifier;
}
// deal with javascript functions inside of json (not a supported json syntax)
private const string PrefixJavaScriptObject = "@@@@";
private static readonly Regex JsFunctionParser = new Regex($"(\"{PrefixJavaScriptObject}(.*?)\")+",
RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
// replace tokens in the js main
private static readonly Regex Token = new Regex("(\"##\\w+?##\")", RegexOptions.Compiled);
/// <summary>
/// Gets the JS initialization script to boot the back office application
/// </summary>
/// <param name="httpContext"></param>
/// <param name="scripts"></param>
/// <param name="angularModule">
/// The angular module name to boot
/// </param>
/// <returns></returns>
public static string GetJavascriptInitialization(HttpContextBase httpContext, IEnumerable<string> scripts, string angularModule, IGlobalSettings globalSettings, IIOHelper ioHelper)
{
var jarray = new StringBuilder();
jarray.AppendLine("[");
var first = true;
foreach (var file in scripts)
{
if (first) first = false;
else jarray.AppendLine(",");
jarray.Append("\"");
jarray.Append(file);
jarray.Append("\"");
}
jarray.Append("]");
return WriteScript(jarray.ToString(), ioHelper.ResolveUrl(globalSettings.UmbracoPath), angularModule);
}
/// <summary>
/// Returns a list of optimized script paths for the back office
/// </summary>
/// <param name="httpContext"></param>
/// <param name="umbracoInit"></param>
/// <param name="additionalJsFiles"></param>
/// <returns>
/// Cache busted/optimized script paths for the back office including manifest and property editor scripts
/// </returns>
/// <remarks>
/// Used to cache bust and optimize script paths for the back office
/// </remarks>
public IEnumerable<string> OptimizeBackOfficeScriptFiles(HttpContextBase httpContext, IEnumerable<string> umbracoInit, IEnumerable<string> additionalJsFiles = null)
{
var scripts = new HashSet<string>();
foreach (var script in umbracoInit)
scripts.Add(script);
foreach (var script in _parser.Manifest.Scripts)
scripts.Add(script);
if (additionalJsFiles != null)
foreach (var script in additionalJsFiles)
scripts.Add(script);
scripts = new HashSet<string>(OptimizeAssetCollection(scripts, AssetType.Javascript, httpContext, _runtimeMinifier));
foreach (var script in ScanPropertyEditors(AssetType.Javascript, httpContext))
scripts.Add(script);
return scripts.ToArray();
}
/// <summary>
/// Returns a list of optimized script paths
/// </summary>
/// <param name="httpContext"></param>
/// <param name="scriptFiles"></param>
/// <param name="runtimeMinifier"></param>
/// <returns></returns>
/// <remarks>
/// Used to cache bust and optimize script paths
/// </remarks>
public static IEnumerable<string> OptimizeScriptFiles(HttpContextBase httpContext, IEnumerable<string> scriptFiles, IRuntimeMinifier runtimeMinifier)
{
var scripts = new HashSet<string>();
foreach (var script in scriptFiles)
scripts.Add(script);
scripts = new HashSet<string>(OptimizeAssetCollection(scripts, AssetType.Javascript, httpContext, runtimeMinifier));
return scripts.ToArray();
}
/// <summary>
/// Returns the default config as a JArray
/// </summary>
/// <returns></returns>
internal static IEnumerable<string> GetDefaultInitialization()
{
var resources = JsonConvert.DeserializeObject<JArray>(Resources.JsInitialize);
return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString());
}
/// <summary>
/// Returns the default config as a JArray
/// </summary>
/// <returns></returns>
internal static IEnumerable<string> GetPreviewInitialization()
{
var resources = JsonConvert.DeserializeObject<JArray>(Resources.PreviewInitialize);
return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString());
}
internal static IEnumerable<string> GetTinyMceInitialization()
{
var resources = JsonConvert.DeserializeObject<JArray>(Resources.TinyMceInitialize);
return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString());
}
internal static IEnumerable<string> OptimizeTinyMceScriptFiles(HttpContextBase httpContext, IRuntimeMinifier runtimeMinifier)
{
return OptimizeScriptFiles(httpContext, GetTinyMceInitialization(), runtimeMinifier);
}
/// <summary>
/// Parses the JsResources.Main and replaces the replacement tokens accordingly.
/// </summary>
/// <param name="replacements"></param>
/// <returns></returns>
internal static string WriteScript(string scripts, string umbracoPath, string angularModule)
{
var count = 0;
var replacements = new[] { scripts, umbracoPath, angularModule };
// replace, catering for the special syntax when we have
// js function() objects contained in the json
return Token.Replace(Resources.Main, match =>
{
var replacement = replacements[count++];
return JsFunctionParser.Replace(replacement, "$2");
});
}
}
}
+11 -9
View File
@@ -145,7 +145,8 @@
<Compile Include="Compose\BackOfficeUserAuditEventsComposer.cs" />
<Compile Include="Composing\CompositionExtensions\Installer.cs" />
<Compile Include="Composing\LightInject\LightInjectContainer.cs" />
<Compile Include="JavaScript\ClientDependencyRuntimeMinifier.cs" />
<Compile Include="JavaScript\CDF\ClientDependencyRuntimeMinifier.cs" />
<Compile Include="JavaScript\JavaScriptHelper.cs" />
<Compile Include="Models\NoNodesViewModel.cs" />
<Compile Include="Mvc\RenderNoContentController.cs" />
<Compile Include="Editors\BackOfficePreviewModel.cs" />
@@ -193,8 +194,8 @@
<Compile Include="Profiling\WebProfilingController.cs" />
<Compile Include="RoutableDocumentFilter.cs" />
<Compile Include="Runtime\AspNetUmbracoBootPermissionChecker.cs" />
<Compile Include="Runtime\ClientDependencyComponent.cs" />
<Compile Include="Runtime\ClientDependencyComposer.cs" />
<Compile Include="JavaScript\CDF\ClientDependencyComponent.cs" />
<Compile Include="JavaScript\CDF\ClientDependencyComposer.cs" />
<Compile Include="Security\BackOfficeUserStore.cs" />
<Compile Include="Security\BackOfficeUserValidator.cs" />
<Compile Include="Security\ConfiguredPasswordValidator.cs" />
@@ -243,7 +244,7 @@
<Compile Include="Security\SessionIdValidator.cs" />
<Compile Include="SignalR\PreviewHubComposer.cs" />
<Compile Include="Trees\FilesTreeController.cs" />
<Compile Include="JavaScript\ClientDependencyConfiguration.cs" />
<Compile Include="JavaScript\CDF\ClientDependencyConfiguration.cs" />
<Compile Include="Editors\Binders\BlueprintItemBinder.cs" />
<Compile Include="UmbracoApplicationBase.cs" />
<Compile Include="WebApi\Filters\HttpQueryStringModelBinder.cs" />
@@ -338,7 +339,7 @@
<Compile Include="Trees\MediaTypeTreeController.cs" />
<Compile Include="Trees\MemberTypeTreeController.cs" />
<Compile Include="Security\WebAuthExtensions.cs" />
<Compile Include="JavaScript\UmbracoClientDependencyLoader.cs" />
<Compile Include="JavaScript\CDF\UmbracoClientDependencyLoader.cs" />
<Compile Include="UmbracoDefaultOwinStartup.cs" />
<Compile Include="HtmlStringUtilities.cs" />
<Compile Include="IUmbracoComponentRenderer.cs" />
@@ -399,9 +400,9 @@
<Compile Include="Trees\MenuRenderingEventArgs.cs" />
<Compile Include="Trees\TemplatesTreeController.cs" />
<Compile Include="Trees\TreeControllerBase.cs" />
<Compile Include="JavaScript\AssetInitialization.cs" />
<Compile Include="JavaScript\CssInitialization.cs" />
<Compile Include="JavaScript\DependencyPathRenderer.cs" />
<Compile Include="JavaScript\CDF\AssetInitialization.cs" />
<Compile Include="JavaScript\CDF\CssInitialization.cs" />
<Compile Include="JavaScript\CDF\DependencyPathRenderer.cs" />
<Compile Include="UmbracoComponentRenderer.cs" />
<Compile Include="WebApi\AngularJsonMediaTypeFormatter.cs" />
<Compile Include="WebApi\AngularJsonOnlyConfigurationAttribute.cs" />
@@ -449,7 +450,7 @@
<Compile Include="Trees\ContentTreeController.cs" />
<Compile Include="Trees\TreeRenderingEventArgs.cs" />
<Compile Include="Trees\UrlHelperExtensions.cs" />
<Compile Include="JavaScript\JsInitialization.cs" />
<Compile Include="JavaScript\CDF\JsInitialization.cs" />
<Compile Include="HttpRequestExtensions.cs" />
<Compile Include="HttpUrlHelperExtensions.cs" />
<Compile Include="Install\FilePermissionHelper.cs" />
@@ -588,5 +589,6 @@
<Link>Mvc\web.config</Link>
</None>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>