private static AssetLocation[] ProcessInfo(AssetKey key, IEnumerable<AssetLocation> allLocations) { var locations = new List<AssetLocation>(); foreach (var assetLocation in allLocations) { var fileContent = File.ReadAllText(assetLocation.PhysicalPath); var resourceProcessors = key.AssetType == AssetType.JS ? javaScriptProcessors : cssProcessors; foreach (var processor in resourceProcessors) fileContent = processor.ProcessFile(fileContent, key.AssetType, assetLocation.PhysicalPath, assetLocation.ContentPath); var fileName = Path.GetFileName(assetLocation.PhysicalPath); var fileExtension = "." + key.AssetType.ToString().ToLower(); if (!fileName.EndsWith(fileExtension)) fileName += fileExtension; var physicalPath = Path.Combine(outputDirectoryPath, fileName); File.WriteAllText(physicalPath, fileContent, Encoding.UTF8); var contentPath = outputContentPath + fileName; locations.Add(new AssetLocation(contentPath, physicalPath)); } return locations.ToArray(); }
private static AssetLocation CompressAndCompile(AssetKey key, IEnumerable<AssetLocation> assetLocations) { var text = assetLocations.Select(x => { var fileContent = File.ReadAllText(x.PhysicalPath); var resourceProcessors = key.AssetType == AssetType.JS ? javaScriptProcessors : cssProcessors; foreach (var processor in resourceProcessors) fileContent = processor.ProcessFile(fileContent, key.AssetType, x.PhysicalPath, x.ContentPath); return fileContent; }); var compiled = CompileText(text); var compressed = string.Empty; if (key.AssetType == AssetType.JS) { compressed = new JavaScriptCompressor(compiled, false, Encoding.UTF8, CultureInfo.CurrentCulture).Compress(); } else if (key.AssetType == AssetType.CSS) { compressed = CssCompressor.Compress(compiled); } else throw new NotSupportedException(); var fileName = key.ToCompiledName() + "." + key.AssetType.ToString().ToLower(); var physicalPath = Path.Combine(outputDirectoryPath, fileName); File.WriteAllText(physicalPath, compressed, Encoding.UTF8); var contentPath = outputContentPath + fileName; return new AssetLocation(contentPath, physicalPath); }
private static string[] GetResourcesFor(AssetType assetType, params string[] tags) { var key = new AssetKey(assetType, tags); // In debug mode we don't cache the processed tags if (!debugMode && processedTags.ContainsKey(key)) return processedTags[key].Select(x => x.ContentPath).ToArray(); var assets = tags.Select(x => { var assetKey = new AssetKey(assetType, x); if (!configuredTags.ContainsKey(assetKey)) throw new InvalidOperationException("Tag not found: " + x); return configuredTags[assetKey]; }); var allLocations = new List<AssetLocation>(); foreach (var asset in assets) allLocations.AddRange(asset.AssetLocations); var resourceProcessors = assetType == AssetType.JS ? javaScriptProcessors : cssProcessors; if (assetType != AssetType.FILE && CompressAssets) { var processedFile = CompressAndCompile(key, allLocations); if (debugMode) // In debug mode we don't cache the processed tags return new string[] { processedFile.ContentPath }; processedTags.Add(key, new AssetLocation[] { processedFile }); } else if (assetType != AssetType.FILE && resourceProcessors.Any()) { var processedFiles = ProcessInfo(key, allLocations); if (debugMode) // In debug mode we don't cache the processed tags return processedFiles.Select(x => x.ContentPath).ToArray(); processedTags.Add(key, processedFiles); } else { if (debugMode) // In debug mode we don't cache the processed tags return allLocations.Select(x => x.ContentPath).ToArray(); processedTags.Add(key, allLocations.ToArray()); } return processedTags[key].Select(x => x.ContentPath).ToArray(); }
private static void AddTag(AssetType assetType, string tag, params string[] contentPath) { AssertNotConfigured(); var key = new AssetKey(assetType, tag); if (configuredTags.ContainsKey(key)) throw new InvalidOperationException("Tag already exists: " + tag); var assetLocations = contentPath.Select(x => new AssetLocation(x, CurrentHttpContext.Server.MapPath(x))).ToArray(); var assetTag = new AssetTag(assetType, tag, assetLocations); configuredTags.Add(key, assetTag); }