/// <summary> /// Gets a bundle file by virtual path /// </summary> /// <param name="bundle">Bundle</param> /// <param name="virtualPath">Virtual path</param> /// <returns>Bundle</returns> protected virtual BundleFile GetBundleFileByVirtualPath(Bundle bundle, string virtualPath) { BundleFile file = null; string url = _virtualFileSystemWrapper.ToAbsolutePath(virtualPath); url = UrlHelpers.ProcessBackSlashes(url); url = RemoveAdditionalFileExtension(url); var bundleContext = new BundleContext(_context, BundleTable.Bundles, bundle.Path); IEnumerable <BundleFile> bundleFiles = bundle.EnumerateFiles(bundleContext); foreach (BundleFile bundleFile in bundleFiles) { string bundleFileUrl = _virtualFileSystemWrapper.ToAbsolutePath(bundleFile.VirtualFile.VirtualPath); bundleFileUrl = UrlHelpers.ProcessBackSlashes(bundleFileUrl); bundleFileUrl = RemoveAdditionalFileExtension(bundleFileUrl); if (string.Equals(bundleFileUrl, url, StringComparison.OrdinalIgnoreCase)) { file = bundleFile; break; } } return(file); }
/// <summary> /// Returns virtual paths of included files in <paramref name="virtualPath" /> file, according to /// <paramref name="context" />. /// If not added yet and <see cref="LessTransform" /> included in bundle transforms, executes /// <see cref="LessTransform" /> transformation for the specified <paramref name="bundle" /> /// and ensures the dependencies are saved. /// </summary> /// <param name="bundle">Bundle to process, if not yet</param> /// <param name="virtualPath">Root file to get dependencies for.</param> /// <param name="context">Current context.</param> /// <returns>Virtual paths of included files.</returns> private static IEnumerable<string> GetFileDependencies(Bundle bundle, string virtualPath, BundleContext context) { string key = BundleTable.VirtualPathProvider.GetCacheKey(virtualPath) ?? virtualPath; Func<string, IList<string>> process; if (bundle.Transforms.Any(transform => transform is LessTransform)) { process = s => { IEnumerable<BundleFile> files = bundle.EnumerateFiles(context); LessTransform.Process(ref files); return files.Select(file => file.IncludedVirtualPath).ToArray(); }; } else { process = s => new string[0]; } _fileDependencies.GetOrAdd(key, process); // returns more specific dependencies by the key containing transient file paths return _fileDependencies.GetOrAdd(GetTransientFileKey(virtualPath), process); }
public void BundlePublicArgumentNullChecks() { Bundle bundle = new Bundle("~/whatever"); ExceptionHelper.ExpectArgumentNullException(() => bundle.EnumerateFiles(null), "context"); ExceptionHelper.ExpectArgumentNullException(() => bundle.CacheLookup(null), "context"); ExceptionHelper.ExpectArgumentNullException(() => bundle.UpdateCache(null, null), "context"); ExceptionHelper.ExpectArgumentNullException(() => bundle.UpdateCache(new BundleContext(), null), "response"); ExceptionHelper.ExpectArgumentNullException(() => bundle.GenerateBundleResponse(null), "context"); ExceptionHelper.ExpectArgumentNullException(() => bundle.GetCacheKey(null), "context"); ExceptionHelper.ExpectArgumentNullException(() => bundle.ApplyTransforms(null, "", null), "context"); }
/// <summary>Gets a bundle file by virtual path</summary> /// <param name="bundle">Bundle</param> /// <param name="virtualPath">Virtual path</param> /// <returns>Bundle</returns> protected virtual BundleFile GetBundleFileByVirtualPath( Bundle bundle, string virtualPath) { BundleFile bundleFile = (BundleFile)null; string b = this.RemoveAdditionalFileExtension(UrlHelpers.ProcessBackSlashes(this._virtualFileSystemWrapper.ToAbsolutePath(virtualPath))); BundleContext context = new BundleContext(this._context, BundleTable.Bundles, bundle.Path); foreach (BundleFile enumerateFile in bundle.EnumerateFiles(context)) { if (string.Equals(this.RemoveAdditionalFileExtension(UrlHelpers.ProcessBackSlashes(this._virtualFileSystemWrapper.ToAbsolutePath(enumerateFile.VirtualFile.VirtualPath))), b, StringComparison.OrdinalIgnoreCase)) { bundleFile = enumerateFile; break; } } return(bundleFile); }
private static string GetTimeStamp(Bundle bundle, BundleContext bundleContext) { var lastdate = DateTime.MinValue; foreach (var file in bundle.EnumerateFiles(bundleContext)) { var mappedPath = HostingEnvironment.MapPath(file.IncludedVirtualPath); if (mappedPath != null && File.Exists(mappedPath)) { var fileDate = File.GetLastWriteTime(mappedPath); if (fileDate > lastdate) { lastdate = fileDate; } } } return(lastdate.ToString("yyyyMMddHHmmss")); }
private BundleResponse GetSingleFileBundleResponse(Bundle bundle, BundleContext bundleContext, string filepath) { var files = bundle.EnumerateFiles(bundleContext); var file = files.FirstOrDefault(f => f.VirtualFile.VirtualPath.TrimStart(new[] { '/' }) == filepath); if (file == null) { throw new FileNotFoundException(string.Format("File not found '{0}'", filepath)); } string contents; var virtualPathProvider = HostingEnvironment.VirtualPathProvider; var virtualFile = virtualPathProvider.GetFile(VirtualPathUtility.ToAppRelative(file.VirtualFile.VirtualPath)); using (var streamReader = new StreamReader(virtualFile.Open())) { contents = streamReader.ReadToEnd(); } return bundle.ApplyTransforms(bundleContext, contents, new List<BundleFile> { file }); }
public async Task <BundleResponse> BuildBundleAsync(Bundle bundle, IDictionary <string, string> fragments, HttpContext httpContext, BundlingOptions options) { Guard.NotNull(bundle, nameof(bundle)); Guard.NotNull(httpContext, nameof(httpContext)); Guard.NotNull(options, nameof(options)); Logger.Debug("Building bundle '{0}'.", bundle.Route); using var chronometer = _chronometer.Step($"Bundle '{bundle.Route}'"); var bundleFiles = bundle.EnumerateFiles(httpContext, options) .Where(x => x.File.Exists) .ToArray(); if (bundleFiles.Length == 0) { throw new InvalidOperationException($"The bundle '{bundle.Route}' does not contain any files."); } var context = new BundleContext { Bundle = bundle, Fragments = fragments, HttpContext = httpContext, Options = options, Files = bundleFiles }; foreach (var bundleFile in bundleFiles) { context.Content.Add(await bundle.LoadContentAsync(bundleFile)); } context.IncludedFiles.AddRange(context.Content.Select(x => x.Path)); var response = await bundle.GenerateBundleResponseAsync(context); return(response); }
private BundleResponse GetSingleFileBundleResponse(Bundle bundle, BundleContext bundleContext, string filepath) { var files = bundle.EnumerateFiles(bundleContext); var file = files.FirstOrDefault(f => f.VirtualFile.VirtualPath.TrimStart(new[] { '/' }) == filepath); if (file == null) { throw new FileNotFoundException(string.Format("File not found '{0}'", filepath)); } string contents; var virtualPathProvider = HostingEnvironment.VirtualPathProvider; var virtualFile = virtualPathProvider.GetFile(VirtualPathUtility.ToAppRelative(file.VirtualFile.VirtualPath)); using (var streamReader = new StreamReader(virtualFile.Open())) { contents = streamReader.ReadToEnd(); } return(bundle.ApplyTransforms(bundleContext, contents, new List <BundleFile> { file })); }
private static string GetTimeStamp(Bundle bundle, BundleContext bundleContext) { var lastdate = DateTime.MinValue; foreach (var file in bundle.EnumerateFiles(bundleContext)) { var mappedPath = HostingEnvironment.MapPath(file.IncludedVirtualPath); if (mappedPath != null && File.Exists(mappedPath)) { var fileDate = File.GetLastWriteTime(mappedPath); if (fileDate > lastdate) { lastdate = fileDate; } } } return lastdate.ToString("yyyyMMddHHmmss"); }
public override IEnumerable <FileInfo> EnumerateFiles(BundleContext context) { return(_bundle.EnumerateFiles(context)); }