string ReadCss(Resource resource) { var css = readFileText(rootDirectory + resource.Path); var currentDirectory = applicationRoot + resource.Path.Substring(0, resource.Path.LastIndexOf('/') + 1); css = ExpandRelativeUrls(css, currentDirectory); return css; }
public Given_a_new_Resource() { resource = new Resource( @"resources/test.js", new byte[] { 1, 2, 3 }, new string[] { @"resources/other.js" } ); }
public Module(string path, Resource[] resources, string[] moduleReferences, string location) { if (!resources.All(s => s.Path.StartsWith(path, StringComparison.OrdinalIgnoreCase))) throw new ArgumentException("Resource paths in this module must start with the path \"" + path + "\"."); this.path = path; this.resources = resources; this.location = location ?? ""; this.hash = HashResourceHashes(resources); this.moduleReferences = moduleReferences.Select(r => r).ToArray(); }
/// <summary> /// Creates a new Resource with only module-internal, application relative references. /// The tuple also contains any module-external references used by the resource. /// </summary> public Tuple<Resource, string[]> Resolve(Func<string, bool> isInternalPath) { var applicationRelativeReferences = CreateApplicationRelativeReferences().ToArray(); var newScript = new Resource( path, hash, applicationRelativeReferences.Where(isInternalPath).ToArray() ); var externalReferences = applicationRelativeReferences .Where(r => !isInternalPath(r)) .ToArray(); return Tuple.Create(newScript, externalReferences); }
byte[] HashResourceHashes(Resource[] resources) { using (var sha1 = SHA1.Create()) { return sha1.ComputeHash(resources.SelectMany(resource => resource.Hash).ToArray()); } }
Resource[] OrderScriptsByDependency(Resource[] resources) { var resourcesByPath = resources.ToDictionary(resource => resource.Path, StringComparer.OrdinalIgnoreCase); // Create a graph where each node is a resource path // and directed edges represent references between resources. var graph = new Graph<string>( resourcesByPath.Keys, path => resourcesByPath[path].References ); var sortedPaths = graph.TopologicalSort(); return sortedPaths.Select(path => resourcesByPath[path]).ToArray(); }