public Context(FileCache cache) { FileCache = cache; SpriteFiles = new List<string>(); InfoMessages = new List<string>(); Errors = new Dictionary<ErrorType, List<Error>>(); Warnings = new Dictionary<ErrorType, List<Error>>(); WriterMode = MoreInternals.WriterMode.Pretty; Options = MoreInternals.Options.None; PendingSpriteExports = new List<SpriteExport>(); Dependecies = new DependencyGraph(); }
internal DependencyGraph Merge(DependencyGraph other) { var ret = new Dictionary<string, HashSet<string>>(); foreach (var otherDs in other.InvertedDependencies) { HashSet<string> selfDs; if (!InvertedDependencies.TryGetValue(otherDs.Key, out selfDs)) { selfDs = new HashSet<string>(); } ret[otherDs.Key] = new HashSet<string>(selfDs.Union(otherDs.Value)); } return new DependencyGraph(ret); }
public void DependencyGraph() { Current.SetContext(new Context(new FileCache())); Current.SetFileLookup(new TestAllExistLookup()); var parser = MoreInternals.Parser.Parser.CreateParser(); var statements = parser.Parse("dummy-file.more", new StringReader("foo { bar: url('/ref.png'); }")); var sprite = new SpriteBlock( new QuotedStringValue("out.png"), new List<SpriteRule> { new SpriteRule("blah", new QuotedStringValue("/img/blah.png"), -1, -1, "fake.more") }, -1, -1, "fake.more" ); var graph = new DependencyGraph(); graph.UsingResolved("used-file.more", "second-dep.css"); graph.UsingResolved("dummy-file.more", "used-file.more"); graph.FileCompiled("dummy-file.more", statements); graph.SpritesResolved(sprite); var a = graph.NeedRecompilation(new[] { @"\second-dep.css" }, new[] { "used-file.more", "out.png" }); Assert.AreEqual(1, a.Count()); Assert.AreEqual("used-file.more", a.Single()); var b = graph.NeedRecompilation(new[] { @"\fizz.more", @"\img\blah.png" }, new[] { "fake.more", "used-file.more" }); Assert.AreEqual(1, b.Count()); Assert.AreEqual("fake.more", b.Single()); var c = graph.NeedRecompilation(new[] { @"\ref.png" }, new[] { "dummy-file.more" }); Assert.AreEqual(1, c.Count()); Assert.AreEqual("dummy-file.more", c.Single()); }
static bool MultiThreadedCompile(int maxParallelism, string workingDirectory, List<string> toCompile, bool overwrite, bool warnAsErrors, bool minify, bool verbose, string spriteProg, string spriteArguments, bool autoCacheBreak, bool autoPrefix, out DependencyGraph dependencies) { var @lock = new Semaphore(0, toCompile.Count); var contexts = new ConcurrentBag<Context>(); var outMsg = new ConcurrentBag<string>(); toCompile.AsParallel() .WithDegreeOfParallelism(maxParallelism) .WithExecutionMode(ParallelExecutionMode.ForceParallelism) .ForAll( delegate(string compile) { try { var threadContext = new Context(FileCache); contexts.Add(threadContext); var buffer = new StringBuilder(); var outputFile = OutputFileFor(compile, overwrite: overwrite); buffer.AppendLine("\t" + compile); buffer.Append("\tto " + outputFile); var timer = new Stopwatch(); timer.Start(); var result = Compile(workingDirectory, compile, outputFile, threadContext, minify, warnAsErrors, autoCacheBreak, autoPrefix); timer.Stop(); if (result) { buffer.AppendLine(" in " + timer.ElapsedMilliseconds + "ms"); } else { buffer.AppendLine(" failed after " + timer.ElapsedMilliseconds + "ms"); } outMsg.Add(buffer.ToString()); } finally { @lock.Release(); } } ); for (int i = 0; i < toCompile.Count; i++) @lock.WaitOne(); var mergedContext = contexts.ElementAt(0); for (int i = 1; i < contexts.Count; i++) { mergedContext = mergedContext.Merge(contexts.ElementAt(i)); } dependencies = mergedContext.Dependecies; var infoMessages = mergedContext.GetInfoMessages().ToList(); var errors = mergedContext.GetErrors(); if (spriteProg.HasValue()) { foreach (var sprite in mergedContext.GetSpriteFiles()) { var commandErrors = RunSpriteCommand(spriteProg, workingDirectory, sprite, spriteArguments, infoMessages); errors = errors.SelectMany(s => s.ToList()).Union(commandErrors).ToLookup(k => k.Type); } } if (verbose) { foreach (var msg in outMsg) { Console.Write(msg); } if (outMsg.Count > 0) Console.WriteLine(); } if (errors.Count > 0) { var parseErrors = errors.Where(e => e.Key == ErrorType.Parser).SelectMany(s => s.ToList()).Distinct().ToList(); var compileErrors = errors.Where(e => e.Key == ErrorType.Compiler).SelectMany(s => s.ToList()).Distinct().ToList(); Print("Errors", parseErrors, compileErrors); } if (mergedContext.GetWarnings().Count > 0) { var parseWarnings = mergedContext.GetWarnings().Where(e => e.Key == ErrorType.Parser).SelectMany(s => s.ToList()).Distinct().ToList(); var compileWarnings = mergedContext.GetWarnings().Where(e => e.Key == ErrorType.Compiler).SelectMany(s => s.ToList()).Distinct().ToList(); Print("Warnings", parseWarnings, compileWarnings); } if (verbose && infoMessages.Count > 0) { Console.WriteLine("INFO"); Console.WriteLine("===="); foreach (var i in infoMessages) { Console.WriteLine(i); } } return mergedContext.GetErrors().Count == 0; }