From a90a292414c16f8129fe1479add79b7d257fb076 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Wed, 16 Oct 2013 18:57:27 +0300 Subject: [PATCH] Overload for RenderRequireJsSetup to accept a list of config files, will merge all paths and shims, will throw error if duplicates are found --- RequireJsNet.Docs/RequireJS.Shared.config | 21 +++ RequireJsNet.Docs/RequireJS.config | 14 -- RequireJsNet.Docs/RequireJsNet.Docs.csproj | 1 + RequireJsNet.Docs/Views/Shared/_Layout.cshtml | 8 +- RequireJsNet/RequireJsHtmlHelpers.cs | 153 ++++++++++++++++-- 5 files changed, 166 insertions(+), 31 deletions(-) create mode 100644 RequireJsNet.Docs/RequireJS.Shared.config diff --git a/RequireJsNet.Docs/RequireJS.Shared.config b/RequireJsNet.Docs/RequireJS.Shared.config new file mode 100644 index 0000000..c9ab3ee --- /dev/null +++ b/RequireJsNet.Docs/RequireJS.Shared.config @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/RequireJsNet.Docs/RequireJS.config b/RequireJsNet.Docs/RequireJS.config index 96382b0..09544ba 100644 --- a/RequireJsNet.Docs/RequireJS.config +++ b/RequireJsNet.Docs/RequireJS.config @@ -1,10 +1,6 @@  - - - - @@ -12,16 +8,6 @@ - - - - - - - - - - diff --git a/RequireJsNet.Docs/RequireJsNet.Docs.csproj b/RequireJsNet.Docs/RequireJsNet.Docs.csproj index f06d835..68ba223 100644 --- a/RequireJsNet.Docs/RequireJsNet.Docs.csproj +++ b/RequireJsNet.Docs/RequireJsNet.Docs.csproj @@ -198,6 +198,7 @@ + diff --git a/RequireJsNet.Docs/Views/Shared/_Layout.cshtml b/RequireJsNet.Docs/Views/Shared/_Layout.cshtml index 4d0b160..ce965ce 100644 --- a/RequireJsNet.Docs/Views/Shared/_Layout.cshtml +++ b/RequireJsNet.Docs/Views/Shared/_Layout.cshtml @@ -51,14 +51,14 @@ @if (HttpContext.Current.IsDebuggingEnabled) { @Html.RenderRequireJsSetup(Url.Content("~/Scripts"), - Url.Content("~/Scripts/require.js"), - Server.MapPath("~/RequireJS.config")) + Url.Content("~/Scripts/require.js"), + new List {Server.MapPath("~/RequireJS.shared.config"), Server.MapPath("~/RequireJS.config")}) } else { @Html.RenderRequireJsSetup(Url.Content("~/Scripts"), - Url.Content("~/Scripts/require.js"), - Server.MapPath("~/RequireJS.release.config")) + Url.Content("~/Scripts/require.js"), + Server.MapPath("~/RequireJS.release.config")) } diff --git a/RequireJsNet/RequireJsHtmlHelpers.cs b/RequireJsNet/RequireJsHtmlHelpers.cs index 843721b..e70441d 100644 --- a/RequireJsNet/RequireJsHtmlHelpers.cs +++ b/RequireJsNet/RequireJsHtmlHelpers.cs @@ -6,6 +6,8 @@ * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html */ + +using System.Data; using System.IO; using System.Xml.Linq; using System.Collections.Generic; @@ -27,16 +29,17 @@ public static class RequireJsHtmlHelpers /// Scrips folder /// requirejs.js url /// RequireJS.config server local path - public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl, string configPath = "") + public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl, + string configPath = "") { var setupHtml = new StringBuilder(); var entryPointPath = html.RequireJsEntryPoint(); - if(entryPointPath != null) + if (entryPointPath != null) { setupHtml.AppendLine(""); + + setupHtml.AppendLine(""); + } + + return new MvcHtmlString(setupHtml.ToString()); + } + public static MvcHtmlString RequireJsEntryPoint(this HtmlHelper html) { var area = html.ViewContext.RouteData.DataTokens["area"] != null - ? html.ViewContext.RouteData.DataTokens["area"].ToString() - : "Root"; + ? html.ViewContext.RouteData.DataTokens["area"].ToString() + : "Root"; var controller = html.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue as string; var action = html.ViewContext.Controller.ValueProvider.GetValue("action").RawValue as string; - var entryPointTmpl = "Controllers/{0}/" + controller + "/" + controller + "-" + action; + var entryPointTmpl = "Controllers/{0}/" + controller + "/" + controller + "-" + action; var entryPoint = string.Format(entryPointTmpl, area); var filePath = html.ViewContext.HttpContext.Server.MapPath("~/Scripts/" + entryPoint + ".js"); - if (File.Exists(filePath)) + if (File.Exists(filePath)) { return new MvcHtmlString(entryPoint); } @@ -99,13 +144,48 @@ public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html, string confi result.Append("{"); foreach (var item in paths) { - result.AppendFormat("\"{0}\":\"{1}\"{2}", item.Attribute("key").Value.Trim(), item.Attribute("value").Value.Trim(), paths.Last() == item ? "" : ","); + result.AppendFormat("\"{0}\":\"{1}\"{2}", item.Attribute("key").Value.Trim(), + item.Attribute("value").Value.Trim(), paths.Last() == item ? "" : ","); } result.Append("}"); return new MvcHtmlString(result.ToString()); } + public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html, IList configsList) + { + var pathList = new List(); + + var result = new StringBuilder(); + result.Append("{"); + foreach (var configPath in configsList) + { + if (!File.Exists(configPath)) + { + throw new FileNotFoundException("RequireJS config not found", configPath); + } + + var paths = XDocument.Load(configPath).Descendants("paths").Descendants("path"); + foreach (var item in paths) + { + //check unique name + var name = item.Attribute("key").Value.Trim(); + if (pathList.Contains(name)) + { + throw new DuplicateNameException(name + " duplicate path found in " + configPath); + } + pathList.Add(name); + + result.AppendFormat("\"{0}\":\"{1}\"{2}", item.Attribute("key").Value.Trim(), + item.Attribute("value").Value.Trim(), + (paths.Last() == item && configsList.Last() == configPath) ? "" : ","); + } + + } + result.Append("}"); + return new MvcHtmlString(result.ToString()); + } + public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, string configPath = "") { if (string.IsNullOrEmpty(configPath)) @@ -128,12 +208,14 @@ public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, string config var deps = item.Descendants("add"); foreach (var dep in deps) { - result.AppendFormat("\"{0}\"{1}", dep.Attribute("dependency").Value.Trim(), deps.Last() == dep ? "" : ","); + result.AppendFormat("\"{0}\"{1}", dep.Attribute("dependency").Value.Trim(), + deps.Last() == dep ? "" : ","); } - var exports = item.Attribute("exports") != null && !string.IsNullOrEmpty(item.Attribute("exports").Value) - ? ", exports: '" + item.Attribute("exports").Value.Trim() + "'" - : string.Empty; + var exports = item.Attribute("exports") != null && + !string.IsNullOrEmpty(item.Attribute("exports").Value) + ? ", exports: '" + item.Attribute("exports").Value.Trim() + "'" + : string.Empty; result.AppendFormat("]{0} {1}{2} ", exports, "}", shims.Last() == item ? "" : ","); } @@ -142,6 +224,51 @@ public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, string config return new MvcHtmlString(result.ToString()); } + public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, IList configsList) + { + var shimList = new List(); + var result = new StringBuilder(); + result.Append("{"); + foreach (var configPath in configsList) + { + if (!File.Exists(configPath)) + { + throw new FileNotFoundException("RequireJS config not found", configPath); + } + + var shims = XDocument.Load(configPath).Descendants("shim").Descendants("dependencies"); + foreach (var item in shims) + { + //check unique name + var name = item.Attribute("for").Value.Trim(); + if (shimList.Contains(name)) + { + throw new DuplicateNameException(name + " duplicate shim found in " + configPath); + } + shimList.Add(name); + + result.AppendFormat(" \"{0}\": {1} deps: [", item.Attribute("for").Value.Trim(), "{"); + var deps = item.Descendants("add"); + foreach (var dep in deps) + { + result.AppendFormat("\"{0}\"{1}", dep.Attribute("dependency").Value.Trim(), + deps.Last() == dep ? "" : ","); + } + + var exports = item.Attribute("exports") != null && + !string.IsNullOrEmpty(item.Attribute("exports").Value) + ? ", exports: '" + item.Attribute("exports").Value.Trim() + "'" + : string.Empty; + + result.AppendFormat("]{0} {1}{2} ", exports, "}", + (shims.Last() == item && configsList.Last() == configPath) ? "" : ","); + } + } + result.Append("}"); + + return new MvcHtmlString(result.ToString()); + } + public static string CurrentCulture(this HtmlHelper html) { // split the ro-Ro string by '-' so it returns eg. ro / en @@ -150,7 +277,7 @@ public static string CurrentCulture(this HtmlHelper html) public static Dictionary ToJsonDictionary() { - var enumType = typeof(TEnum); + var enumType = typeof (TEnum); var names = Enum.GetNames(enumType); return Enum.GetNames(enumType).ToDictionary(r => r, r => Convert.ToInt32(Enum.Parse(enumType, r))); }