public void GetFilesTest() { var bundler = new Bundler(); var files = bundler.GetFiles(@"Examples\BundleA.bundle").ToList(); Assert.IsNotNull(files); var expectedFiles = new List<string>() { @"Examples\css\foo.css", @"Examples\css\goo.css", @"Examples\css\SubFolder\too.css", @"Examples\css\SubFolder\new.css", @"Examples\css\SubFolder\chew.css", @"Examples\Less\Local.less", }.ConvertAll(s => Path.GetFullPath(s).ToLower()); Assert.AreEqual(expectedFiles, files.Select(f => f.ToLower())); }
public void GetFilesTest() { var bundler = new Bundler(); var files = bundler.GetFiles(@"Examples\BundleA.bundle").ToList(); Assert.IsNotNull(files); var expectedFiles = new List <string>() { @"Examples\css\foo.css", @"Examples\css\goo.css", @"Examples\css\SubFolder\too.css", @"Examples\css\SubFolder\new.css", @"Examples\css\SubFolder\chew.css", @"Examples\Less\Local.less", }.ConvertAll(s => Path.GetFullPath(s).ToLower()); Assert.AreEqual(expectedFiles, files.Select(f => f.ToLower())); }
/// <summary> /// Gets the urls to the bundle for the <c>context</c>. /// </summary> /// <param name="bundleAppPath">The app path to the bundle. The app path starts with '~/' and is relative to the web app.</param> /// <param name="context">The context of the urls. Defaults to the <see cref="System.Web.HttpContext.Current"/>.</param> /// <returns>The urls to the file or files within the bundle.</returns> public static List <string> GetUrls(string bundleAppPath, System.Web.HttpContextBase context = null) { var urls = new List <string>(); // ensure we have the current context context = context ?? new System.Web.HttpContextWrapper(System.Web.HttpContext.Current); var isSecureRequest = context.Request.IsSecureConnection; var server = context.Server; // ensure the bundle exists var bundleFile = server.MapPath(bundleAppPath); if (!System.IO.File.Exists(bundleFile)) { throw new System.IO.FileNotFoundException("The bundle file could not be found. The bundle file must exist.", bundleFile); } // build the root url var rootUrl = ""; if (Settings.Default.AbsolutePath || !string.IsNullOrWhiteSpace(Settings.Default.CdnHostName)) { // IF path should be absolute // OR we are using a CDN // THEN we're always going to return an absolute url if (isSecureRequest) { rootUrl = "https://"; } else { rootUrl = "http://"; } // determine whether or not to append the cdn host name if (!string.IsNullOrWhiteSpace(Settings.Default.CdnHostName)) { rootUrl += Settings.Default.CdnHostName; } else { rootUrl += context.Request.Url.Host; } } if (Settings.Default.BundleOutput) { // get the output file var bundleOutput = BundleFileRegex.Replace(bundleAppPath, @"${Name}.min${TypeExt}"); var bundleOutputFile = server.MapPath(bundleOutput); if (!System.IO.File.Exists(bundleOutputFile)) { throw new System.IO.FileNotFoundException(string.Format("The output file could not be found. The output file for the bundle must exist. Bundle: {0}", bundleFile), bundleOutputFile); } // create the url to the bundle output file var bundleOutputUrl = rootUrl + VirtualPathUtility.ToAbsolute(bundleOutput + "?v=" + System.IO.File.GetLastWriteTimeUtc(bundleOutputFile).ToString("yyyyMMddHHmmssfff")); urls.Add(bundleOutputUrl); } else { var bundler = new Bundler(); var webRootDir = server.MapPath("~/").ToLower(); var webRootUrlPath = VirtualPathUtility.ToAbsolute("/"); foreach (var includeFile in bundler.GetFiles(bundleFile)) { if (!System.IO.File.Exists(includeFile)) { throw new System.IO.FileNotFoundException(string.Format("A include file could not be found. All the files in the bundle must exist. Bundle: {0}", bundleFile), includeFile); } // create the url to the include file var includeUrl = rootUrl + webRootUrlPath + includeFile.ToLower().Replace(webRootDir, "").Replace("\\", "/") + "?v=" + System.IO.File.GetLastWriteTimeUtc(includeFile).ToString("yyyyMMddHHmmssfff"); urls.Add(includeUrl); } } return(urls); }
/// <summary> /// Gets the urls to the bundle for the <c>context</c>. /// </summary> /// <param name="bundleAppPath">The app path to the bundle. The app path starts with '~/' and is relative to the web app.</param> /// <param name="context">The context of the urls. Defaults to the <see cref="System.Web.HttpContext.Current"/>.</param> /// <returns>The urls to the file or files within the bundle.</returns> public static List<string> GetUrls(string bundleAppPath, System.Web.HttpContextBase context = null) { var urls = new List<string>(); // ensure we have the current context context = context ?? new System.Web.HttpContextWrapper(System.Web.HttpContext.Current); var isSecureRequest = context.Request.IsSecureConnection; var server = context.Server; // ensure the bundle exists var bundleFile = server.MapPath(bundleAppPath); if (!System.IO.File.Exists(bundleFile)) { throw new System.IO.FileNotFoundException("The bundle file could not be found. The bundle file must exist.", bundleFile); } // build the root url var rootUrl = ""; if (Settings.Default.AbsolutePath || !string.IsNullOrWhiteSpace(Settings.Default.CdnHostName)) { // IF path should be absolute // OR we are using a CDN // THEN we're always going to return an absolute url if (isSecureRequest) { rootUrl = "https://"; } else { rootUrl = "http://"; } // determine whether or not to append the cdn host name if (!string.IsNullOrWhiteSpace(Settings.Default.CdnHostName)) { rootUrl += Settings.Default.CdnHostName; } else { rootUrl += context.Request.Url.Host; } } if (Settings.Default.BundleOutput) { // get the output file var bundleOutput = BundleFileRegex.Replace(bundleAppPath, @"${Name}.min${TypeExt}"); var bundleOutputFile = server.MapPath(bundleOutput); if (!System.IO.File.Exists(bundleOutputFile)) { throw new System.IO.FileNotFoundException(string.Format("The output file could not be found. The output file for the bundle must exist. Bundle: {0}", bundleFile), bundleOutputFile); } // create the url to the bundle output file var bundleOutputUrl = rootUrl + VirtualPathUtility.ToAbsolute(bundleOutput + "?v=" + System.IO.File.GetLastWriteTimeUtc(bundleOutputFile).ToString("yyyyMMddHHmmssfff")); urls.Add(bundleOutputUrl); } else { var bundler = new Bundler(); var webRootDir = server.MapPath("~/").ToLower(); var webRootUrlPath = VirtualPathUtility.ToAbsolute("/"); foreach (var includeFile in bundler.GetFiles(bundleFile)) { if (!System.IO.File.Exists(includeFile)) { throw new System.IO.FileNotFoundException(string.Format("A include file could not be found. All the files in the bundle must exist. Bundle: {0}", bundleFile), includeFile); } // create the url to the include file var includeUrl = rootUrl + webRootUrlPath + includeFile.ToLower().Replace(webRootDir, "").Replace("\\", "/") + "?v=" + System.IO.File.GetLastWriteTimeUtc(includeFile).ToString("yyyyMMddHHmmssfff"); urls.Add(includeUrl); } } return urls; }