Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 180686a24f
Fetching contributors…

Cannot retrieve contributors at this time

file 270 lines (215 sloc) 8.49 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.Routing;

namespace ServiceStack.Mvc
{
public enum BundleOptions
{
Normal,
Minified,
Combined,
MinifiedAndCombined
}

public static class Bundler
{
public static Func<bool> CachePaths = IsProduction;
public static Func<string, BundleOptions, string> DefaultUrlFilter = ProcessVirtualPathDefault;

// Logic to determine if the app is running in production or dev environment
public static bool IsProduction()
{
return (HttpContext.Current != null && !HttpContext.Current.IsDebuggingEnabled);
}

public static bool FileExists(string virtualPath)
{
if (!HostingEnvironment.IsHosted) return false;
var filePath = HostingEnvironment.MapPath(virtualPath);
return File.Exists(filePath);
}

static DateTime centuryBegin = new DateTime(2001, 1, 1);
public static string TimestampString(string virtualPath)
{
try
{
if (HostingEnvironment.IsHosted)
{
var filePath = HostingEnvironment.MapPath(virtualPath);
return Convert.ToString((File.GetLastWriteTimeUtc(filePath).Ticks - centuryBegin.Ticks) / 1000000000, 16);
}
}
catch { } //ignore
return string.Empty;
}

static readonly ConcurrentDictionary<string,string> VirutalPathCache = new ConcurrentDictionary<string, string>();
private static string ProcessVirtualPathDefault(string virtualPath, BundleOptions options)
{
if (!CachePaths()) VirutalPathCache.Clear();

return VirutalPathCache.GetOrAdd(virtualPath, str => {
// The path that comes in starts with ~/ and must first be made absolute

if (options == BundleOptions.Minified || options == BundleOptions.MinifiedAndCombined)
{
if (virtualPath.EndsWith(".js") && !virtualPath.EndsWith(".min.js"))
{
var minPath = virtualPath.Replace(".js", ".min.js");
if (FileExists(minPath))
virtualPath = minPath;
}
else if (virtualPath.EndsWith(".css") && !virtualPath.EndsWith(".min.css"))
{
var minPath = virtualPath.Replace(".css", ".min.css");
if (FileExists(minPath))
virtualPath = minPath;
}
}

var path = virtualPath;
if (virtualPath.IndexOf("://", StringComparison.Ordinal) == -1)
{
path = VirtualPathUtility.ToAbsolute(virtualPath);

var cacheBreaker = TimestampString(virtualPath);
if (!string.IsNullOrEmpty(cacheBreaker))
{
path += path.IndexOf('?') == -1
? "?" + cacheBreaker
: "&" + cacheBreaker;
}
}

// Add your own modifications here before returning the path
return path;
});
}

private static string RewriteUrl(this string relativePath, BundleOptions options=BundleOptions.Normal)
{
return DefaultUrlFilter(relativePath, options);
}

public static MvcHtmlString ToMvcHtmlString(this string s)
{
return MvcHtmlString.Create(s);
}

public static MvcHtmlString ToMvcHtmlString(this TagBuilder t)
{
return t.ToString().ToMvcHtmlString();
}

public static MvcHtmlString ToMvcHtmlString(this TagBuilder t, TagRenderMode mode)
{
return t.ToString(mode).ToMvcHtmlString();
}

public static MvcHtmlString Link(this HtmlHelper html, string rel, string href, object htmlAttributes = null, BundleOptions options = BundleOptions.Normal)
{
if (string.IsNullOrEmpty(href))
return MvcHtmlString.Empty;

if (href.StartsWith("~/"))
href = href.Replace("~/", VirtualPathUtility.ToAbsolute("~"));

var tag = new TagBuilder("link");
tag.MergeAttribute("rel", rel);

tag.MergeAttribute("href", href.RewriteUrl(options));

if (htmlAttributes != null)
tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));

return tag.ToString(TagRenderMode.SelfClosing).ToMvcHtmlString();
}

public static MvcHtmlString Css(this HtmlHelper html, string href, string media = null, BundleOptions options = BundleOptions.Minified)
{
return media != null
? html.Link("stylesheet", href, new { media }, options)
: html.Link("stylesheet", href, null, options);
}

public static T If<T>(this HtmlHelper html, bool predicate, T whenTrue, T whenFalse)
{
return predicate ? whenTrue : whenFalse;
}

public static MvcHtmlString Img(this HtmlHelper html, string src, string alt, string link = null, object htmlAttributes = null)
{
if (string.IsNullOrEmpty(src))
return MvcHtmlString.Empty;

if (src.StartsWith("~/"))
src = src.Replace("~/", VirtualPathUtility.ToAbsolute("~"));

var tag = new TagBuilder("img");

tag.MergeAttribute("src", src.RewriteUrl());
tag.MergeAttribute("alt", alt);
if (htmlAttributes != null)
tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));

if (!string.IsNullOrEmpty(link))
{
var a = new TagBuilder("a");
a.MergeAttribute("href", link);
a.InnerHtml = tag.ToString(TagRenderMode.Normal);
return a.ToMvcHtmlString();
}

return tag.ToString(TagRenderMode.SelfClosing).ToMvcHtmlString();
}

public static MvcHtmlString Js(this HtmlHelper html, string src, BundleOptions options = BundleOptions.Minified)
{
if (string.IsNullOrEmpty(src))
return MvcHtmlString.Empty;

if (src.StartsWith("~/"))
src = src.Replace("~/", VirtualPathUtility.ToAbsolute("~"));

var tag = new TagBuilder("script");
tag.MergeAttribute("type", "text/javascript");

tag.MergeAttribute("src", src.RewriteUrl(options));

return tag.ToString(TagRenderMode.Normal).ToMvcHtmlString();
}

public static MvcHtmlString Img(this HtmlHelper html, Uri url, string alt, Uri link = null, object htmlAttributes = null)
{
return html.Img(url.ToString(), alt, link != null ? link.ToString() : "", htmlAttributes);
}

public static string ToJsBool(this bool value)
{
return value.ToString(CultureInfo.InvariantCulture).ToLower();
}

static readonly ConcurrentDictionary<string, MvcHtmlString> BundleCache = new ConcurrentDictionary<string, MvcHtmlString>();

public static MvcHtmlString RenderJsBundle(this HtmlHelper html, string bundlePath, BundleOptions options = BundleOptions.Minified)
{
if (string.IsNullOrEmpty(bundlePath))
return MvcHtmlString.Empty;

if (!CachePaths()) BundleCache.Clear();

return BundleCache.GetOrAdd(bundlePath, str => {
var filePath = HttpContext.Current.Server.MapPath(bundlePath);

var baseUrl = VirtualPathUtility.GetDirectory(bundlePath);

if (options == BundleOptions.Combined)
return html.Js(bundlePath.Replace(".bundle", ""), options);
if (options == BundleOptions.MinifiedAndCombined)
return html.Js(bundlePath.Replace(".js.bundle", ".min.js"), options);

var jsFiles = File.ReadAllLines(filePath);

var scripts = new StringBuilder();
foreach (var file in jsFiles)
{
var jsFile = file.Trim().Replace(".coffee", ".js");
var jsSrc = Path.Combine(baseUrl, jsFile);

scripts.AppendLine(
html.Js(jsSrc, options).ToString()
);
}

return scripts.ToString().ToMvcHtmlString();
});
}

public static MvcHtmlString RenderCssBundle(this HtmlHelper html, string bundlePath, BundleOptions options = BundleOptions.Minified)
{
if (string.IsNullOrEmpty(bundlePath))
return MvcHtmlString.Empty;

if (!CachePaths()) BundleCache.Clear();

return BundleCache.GetOrAdd(bundlePath, str => {
var filePath = HttpContext.Current.Server.MapPath(bundlePath);

var baseUrl = VirtualPathUtility.GetDirectory(bundlePath);

if (options == BundleOptions.Combined)
return html.Css(bundlePath.Replace(".bundle", ""), null, options);
if (options == BundleOptions.MinifiedAndCombined)
return html.Css(bundlePath.Replace(".css.bundle", ".min.css"), null, options);

var cssFiles = File.ReadAllLines(filePath);

var styles = new StringBuilder();
foreach (var file in cssFiles)
{
var cssFile = file.Trim().Replace(".less", ".css");
var cssSrc = Path.Combine(baseUrl, cssFile);

styles.AppendLine(
html.Css(cssSrc, null, options).ToString()
);
}

return styles.ToString().ToMvcHtmlString();
});
}
}
}
Something went wrong with that request. Please try again.