From ae4b9c3f42369a3b23bbb8547e0738981a211187 Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska Date: Tue, 17 Mar 2020 15:35:18 +0100 Subject: [PATCH] Moving CDF related files into its own folder --- .../JavaScript/AssetInitialization.cs | 59 ------ .../JavaScript/CDF/AssetInitialization.cs | 34 ++++ .../CDF}/ClientDependencyComponent.cs | 12 +- .../CDF}/ClientDependencyComposer.cs | 10 +- .../ClientDependencyConfiguration.cs | 20 +- .../JavaScript/{ => CDF}/CssInitialization.cs | 4 +- .../{ => CDF}/DependencyPathRenderer.cs | 3 +- .../JavaScript/CDF/JsInitialization.cs | 68 +++++++ .../UmbracoClientDependencyLoader.cs | 4 +- .../JavaScript/JsInitialization.cs | 171 ------------------ src/Umbraco.Web/Umbraco.Web.csproj | 20 +- 11 files changed, 133 insertions(+), 272 deletions(-) delete mode 100644 src/Umbraco.Web/JavaScript/AssetInitialization.cs create mode 100644 src/Umbraco.Web/JavaScript/CDF/AssetInitialization.cs rename src/Umbraco.Web/{Runtime => JavaScript/CDF}/ClientDependencyComponent.cs (91%) rename src/Umbraco.Web/{Runtime => JavaScript/CDF}/ClientDependencyComposer.cs (65%) rename src/Umbraco.Web/JavaScript/{ => CDF}/ClientDependencyConfiguration.cs (92%) rename src/Umbraco.Web/JavaScript/{ => CDF}/CssInitialization.cs (89%) rename src/Umbraco.Web/JavaScript/{ => CDF}/DependencyPathRenderer.cs (97%) create mode 100644 src/Umbraco.Web/JavaScript/CDF/JsInitialization.cs rename src/Umbraco.Web/JavaScript/{ => CDF}/UmbracoClientDependencyLoader.cs (94%) delete mode 100644 src/Umbraco.Web/JavaScript/JsInitialization.cs diff --git a/src/Umbraco.Web/JavaScript/AssetInitialization.cs b/src/Umbraco.Web/JavaScript/AssetInitialization.cs deleted file mode 100644 index 63f3cdf5f9..0000000000 --- a/src/Umbraco.Web/JavaScript/AssetInitialization.cs +++ /dev/null @@ -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 ScanPropertyEditors(AssetType assetType, HttpContextBase httpContext) - { - if (httpContext == null) throw new ArgumentNullException(nameof(httpContext)); - var attributes = Current.PropertyEditors - .SelectMany(x => x.GetType().GetCustomAttributes(false)) - .Where(x => x.AssetType == assetType) - .Select(x => x.DependencyFile) - .ToList(); - - return _runtimeMinifier.GetAssetPaths(assetType, attributes); - } - - internal static IEnumerable OptimizeAssetCollection(IEnumerable 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);; - } - } -} diff --git a/src/Umbraco.Web/JavaScript/CDF/AssetInitialization.cs b/src/Umbraco.Web/JavaScript/CDF/AssetInitialization.cs new file mode 100644 index 0000000000..d3794b24c0 --- /dev/null +++ b/src/Umbraco.Web/JavaScript/CDF/AssetInitialization.cs @@ -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 ScanPropertyEditors(AssetType assetType, HttpContextBase httpContext) + { + if (httpContext == null) throw new ArgumentNullException(nameof(httpContext)); + var attributes = Current.PropertyEditors + .SelectMany(x => x.GetType().GetCustomAttributes(false)) + .Where(x => x.AssetType == assetType) + .Select(x => x.DependencyFile) + .ToList(); + + return _runtimeMinifier.GetAssetPaths(assetType, attributes); + } + } +} diff --git a/src/Umbraco.Web/Runtime/ClientDependencyComponent.cs b/src/Umbraco.Web/JavaScript/CDF/ClientDependencyComponent.cs similarity index 91% rename from src/Umbraco.Web/Runtime/ClientDependencyComponent.cs rename to src/Umbraco.Web/JavaScript/CDF/ClientDependencyComponent.cs index 833c25e7d3..b4b61f5d8b 100644 --- a/src/Umbraco.Web/Runtime/ClientDependencyComponent.cs +++ b/src/Umbraco.Web/JavaScript/CDF/ClientDependencyComponent.cs @@ -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; diff --git a/src/Umbraco.Web/Runtime/ClientDependencyComposer.cs b/src/Umbraco.Web/JavaScript/CDF/ClientDependencyComposer.cs similarity index 65% rename from src/Umbraco.Web/Runtime/ClientDependencyComposer.cs rename to src/Umbraco.Web/JavaScript/CDF/ClientDependencyComposer.cs index 0e52a04cfe..7d2542e3ae 100644 --- a/src/Umbraco.Web/Runtime/ClientDependencyComposer.cs +++ b/src/Umbraco.Web/JavaScript/CDF/ClientDependencyComposer.cs @@ -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 { diff --git a/src/Umbraco.Web/JavaScript/ClientDependencyConfiguration.cs b/src/Umbraco.Web/JavaScript/CDF/ClientDependencyConfiguration.cs similarity index 92% rename from src/Umbraco.Web/JavaScript/ClientDependencyConfiguration.cs rename to src/Umbraco.Web/JavaScript/CDF/ClientDependencyConfiguration.cs index d0df020f33..9aacad013e 100644 --- a/src/Umbraco.Web/JavaScript/ClientDependencyConfiguration.cs +++ b/src/Umbraco.Web/JavaScript/CDF/ClientDependencyConfiguration.cs @@ -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 { /// /// 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); diff --git a/src/Umbraco.Web/JavaScript/CssInitialization.cs b/src/Umbraco.Web/JavaScript/CDF/CssInitialization.cs similarity index 89% rename from src/Umbraco.Web/JavaScript/CssInitialization.cs rename to src/Umbraco.Web/JavaScript/CDF/CssInitialization.cs index 550f7be8d6..625b6ec214 100644 --- a/src/Umbraco.Web/JavaScript/CssInitialization.cs +++ b/src/Umbraco.Web/JavaScript/CDF/CssInitialization.cs @@ -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 GetStylesheetFiles(HttpContextBase httpContext) { var stylesheets = new HashSet(); - 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); diff --git a/src/Umbraco.Web/JavaScript/DependencyPathRenderer.cs b/src/Umbraco.Web/JavaScript/CDF/DependencyPathRenderer.cs similarity index 97% rename from src/Umbraco.Web/JavaScript/DependencyPathRenderer.cs rename to src/Umbraco.Web/JavaScript/CDF/DependencyPathRenderer.cs index eebc2e6900..a79ee229d2 100644 --- a/src/Umbraco.Web/JavaScript/DependencyPathRenderer.cs +++ b/src/Umbraco.Web/JavaScript/CDF/DependencyPathRenderer.cs @@ -3,7 +3,7 @@ using System.Web; using ClientDependency.Core; using ClientDependency.Core.FileRegistration.Providers; -namespace Umbraco.Web.JavaScript +namespace Umbraco.Web.JavaScript.CDF { /// /// 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; } - } } diff --git a/src/Umbraco.Web/JavaScript/CDF/JsInitialization.cs b/src/Umbraco.Web/JavaScript/CDF/JsInitialization.cs new file mode 100644 index 0000000000..5c448ee03b --- /dev/null +++ b/src/Umbraco.Web/JavaScript/CDF/JsInitialization.cs @@ -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 +{ + /// + /// Reads from all defined manifests and ensures that any of their initialization is output with the + /// main Umbraco initialization output. + /// + internal class JsInitialization : AssetInitialization + { + private readonly IManifestParser _parser; + private readonly IRuntimeMinifier _runtimeMinifier; + + public JsInitialization(IManifestParser parser, IRuntimeMinifier runtimeMinifier) : base(runtimeMinifier) + { + _parser = parser; + _runtimeMinifier = runtimeMinifier; + } + + /// + /// Returns a list of optimized script paths for the back office + /// + /// + /// + /// + /// + /// Cache busted/optimized script paths for the back office including manifest and property editor scripts + /// + /// + /// Used to cache bust and optimize script paths for the back office + /// + public IEnumerable OptimizeBackOfficeScriptFiles(HttpContextBase httpContext, IEnumerable umbracoInit, IEnumerable additionalJsFiles = null) + { + var scripts = new HashSet(); + 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(JavaScriptHelper.OptimizeAssetCollection(scripts, AssetType.Javascript, httpContext, _runtimeMinifier)); + + foreach (var script in ScanPropertyEditors(AssetType.Javascript, httpContext)) + scripts.Add(script); + + return scripts.ToArray(); + } + + /// + /// Returns the default config as a JArray + /// + /// + internal static IEnumerable GetDefaultInitialization() + { + var resources = JsonConvert.DeserializeObject(Resources.JsInitialize); + return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString()); + } + } +} diff --git a/src/Umbraco.Web/JavaScript/UmbracoClientDependencyLoader.cs b/src/Umbraco.Web/JavaScript/CDF/UmbracoClientDependencyLoader.cs similarity index 94% rename from src/Umbraco.Web/JavaScript/UmbracoClientDependencyLoader.cs rename to src/Umbraco.Web/JavaScript/CDF/UmbracoClientDependencyLoader.cs index c98672416b..08f0a8c91a 100644 --- a/src/Umbraco.Web/JavaScript/UmbracoClientDependencyLoader.cs +++ b/src/Umbraco.Web/JavaScript/CDF/UmbracoClientDependencyLoader.cs @@ -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 { /// /// Used to load in all client dependencies for Umbraco. diff --git a/src/Umbraco.Web/JavaScript/JsInitialization.cs b/src/Umbraco.Web/JavaScript/JsInitialization.cs deleted file mode 100644 index 21774ea3bd..0000000000 --- a/src/Umbraco.Web/JavaScript/JsInitialization.cs +++ /dev/null @@ -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 -{ - /// - /// Reads from all defined manifests and ensures that any of their initialization is output with the - /// main Umbraco initialization output. - /// - 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); - - /// - /// Gets the JS initialization script to boot the back office application - /// - /// - /// - /// - /// The angular module name to boot - /// - /// - public static string GetJavascriptInitialization(HttpContextBase httpContext, IEnumerable 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); - } - - /// - /// Returns a list of optimized script paths for the back office - /// - /// - /// - /// - /// - /// Cache busted/optimized script paths for the back office including manifest and property editor scripts - /// - /// - /// Used to cache bust and optimize script paths for the back office - /// - public IEnumerable OptimizeBackOfficeScriptFiles(HttpContextBase httpContext, IEnumerable umbracoInit, IEnumerable additionalJsFiles = null) - { - var scripts = new HashSet(); - 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(OptimizeAssetCollection(scripts, AssetType.Javascript, httpContext, _runtimeMinifier)); - - foreach (var script in ScanPropertyEditors(AssetType.Javascript, httpContext)) - scripts.Add(script); - - return scripts.ToArray(); - } - - /// - /// Returns a list of optimized script paths - /// - /// - /// - /// - /// - /// - /// Used to cache bust and optimize script paths - /// - public static IEnumerable OptimizeScriptFiles(HttpContextBase httpContext, IEnumerable scriptFiles, IRuntimeMinifier runtimeMinifier) - { - var scripts = new HashSet(); - foreach (var script in scriptFiles) - scripts.Add(script); - - scripts = new HashSet(OptimizeAssetCollection(scripts, AssetType.Javascript, httpContext, runtimeMinifier)); - - return scripts.ToArray(); - } - - /// - /// Returns the default config as a JArray - /// - /// - internal static IEnumerable GetDefaultInitialization() - { - var resources = JsonConvert.DeserializeObject(Resources.JsInitialize); - return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString()); - } - - /// - /// Returns the default config as a JArray - /// - /// - internal static IEnumerable GetPreviewInitialization() - { - var resources = JsonConvert.DeserializeObject(Resources.PreviewInitialize); - return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString()); - } - - internal static IEnumerable GetTinyMceInitialization() - { - var resources = JsonConvert.DeserializeObject(Resources.TinyMceInitialize); - return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString()); - } - - internal static IEnumerable OptimizeTinyMceScriptFiles(HttpContextBase httpContext, IRuntimeMinifier runtimeMinifier) - { - return OptimizeScriptFiles(httpContext, GetTinyMceInitialization(), runtimeMinifier); - } - - /// - /// Parses the JsResources.Main and replaces the replacement tokens accordingly. - /// - /// - /// - 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"); - }); - } - } -} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 168c76d142..6ea9b98950 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -145,7 +145,8 @@ - + + @@ -193,8 +194,8 @@ - - + + @@ -243,7 +244,7 @@ - + @@ -338,7 +339,7 @@ - + @@ -399,9 +400,9 @@ - - - + + + @@ -449,7 +450,7 @@ - + @@ -588,5 +589,6 @@ Mvc\web.config + \ No newline at end of file