Skip to content

Commit

Permalink
Added optional param configPath to RenderRequireJsSetup method
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Oct 14, 2013
1 parent b07ba95 commit 487773d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion RequireJsNet.Docs/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
</div>
</footer>

@Html.RenderRequireJsSetup(Url.Content("~/Scripts"), Url.Content("~/Scripts/require.js"))
@Html.RenderRequireJsSetup(Url.Content("~/Scripts"), Url.Content("~/Scripts/require.js"), Server.MapPath("~/RequireJS.release.config"))
</body>
</html>
59 changes: 29 additions & 30 deletions RequireJsNet/RequireJsHtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ namespace RequireJS
{
public static class RequireJsHtmlHelpers
{
public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl)
/// <summary>
/// Setup RequireJS to be used in layouts
/// </summary>
/// <example>
/// @Html.RenderRequireJsSetup(Url.Content("~/Scripts"), Url.Content("~/Scripts/require.js"), Server.MapPath("~/RequireJS.release.config"))
/// </example>
/// <param name="baseUrl">Scrips folder</param>
/// <param name="requireUrl">requirejs.js url</param>
/// <param name="configPath">RequireJS.config server local path</param>
public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl, string configPath = "")
{
var setupHtml = new StringBuilder();

Expand All @@ -39,9 +48,9 @@ public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string ba
setupHtml.AppendLine(",");
setupHtml.Append("baseUrl:'" + baseUrl + "'");
setupHtml.AppendLine(",");
setupHtml.Append("paths:" + html.GetRequireJsPaths());
setupHtml.Append("paths:" + html.GetRequireJsPaths(configPath));
setupHtml.AppendLine(",");
setupHtml.AppendLine("shim:" + html.GetRequireJsShim());
setupHtml.AppendLine("shim:" + html.GetRequireJsShim(configPath));
setupHtml.AppendLine("};");

setupHtml.AppendLine("</script>");
Expand All @@ -67,26 +76,20 @@ public static MvcHtmlString RequireJsEntryPoint(this HtmlHelper html)
return File.Exists(filePath) ? new MvcHtmlString(entryPoint) : null;
}

public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html)
public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html, string configPath = "")
{
var cfg = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.config");

if (!File.Exists(cfg))
if (string.IsNullOrEmpty(configPath))
{
throw new FileNotFoundException("RequireJS config not found", cfg);
configPath = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.config");
}

var result = new StringBuilder();
var paths = XDocument.Load(cfg).Descendants("paths").Descendants("path");

#if !DEBUG

var cfgRelease = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.Release.config");
if (File.Exists(cfgRelease))
if (!File.Exists(configPath))
{
paths = XDocument.Load(cfgRelease).Descendants("paths").Descendants("path");
throw new FileNotFoundException("RequireJS config not found", configPath);
}
#endif

var result = new StringBuilder();
var paths = XDocument.Load(configPath).Descendants("paths").Descendants("path");

result.Append("{");
foreach (var item in paths)
Expand All @@ -98,25 +101,21 @@ public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html)
return new MvcHtmlString(result.ToString());
}

public static MvcHtmlString GetRequireJsShim(this HtmlHelper html)
public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, string configPath = "")
{
var cfg = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.config");

if (!File.Exists(cfg))
if (string.IsNullOrEmpty(configPath))
{
throw new FileNotFoundException("RequireJS config not found", cfg);
configPath = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.config");
}

var result = new StringBuilder();
var shims = XDocument.Load(cfg).Descendants("shim").Descendants("dependencies");
#if !DEBUG

var cfgRelease = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.Release.config");
if (File.Exists(cfgRelease))
if (!File.Exists(configPath))
{
shims = XDocument.Load(cfg).Descendants("shim").Descendants("dependencies");
throw new FileNotFoundException("RequireJS config not found", configPath);
}
#endif

var result = new StringBuilder();
var shims = XDocument.Load(configPath).Descendants("shim").Descendants("dependencies");

result.Append("{");
foreach (var item in shims)
{
Expand Down

0 comments on commit 487773d

Please sign in to comment.