Exemplo n.º 1
0
        static int Clean(FileInfo[] templates, int lockTimeout)
        {
            using (var locker = new Lock($"Global\\T4.Build.clean.{ComputeHash(templates)}.lock", lockTimeout))
            {
                var hasErrors = false;

                foreach (var t in templates)
                {
                    var generator = new BuildTemplateGenerator(t.FullName);
                    var output    = generator.OutputFile;
                    if (!string.IsNullOrEmpty(output) && File.Exists(output))
                    {
                        try
                        {
                            Console.WriteLine($"Deleting file \"{output}\"");
                            File.Delete(output);
                        }
                        catch (Exception e)
                        {
                            hasErrors = true;
                            Console.Error.WriteLine(e.Message);
                        }
                    }
                }

                return(hasErrors ? 1 : 0);
            }
        }
Exemplo n.º 2
0
        static int Transform(FileInfo[] templates, int lockTimeout, bool skipUpToDate, bool parallel)
        {
            using (var locker = new Lock($"Global\\T4.Build.transform.{ComputeHash(templates)}.lock", lockTimeout))
            {
                var outputQueue = new ConcurrentQueue <String>();
                var errorQueue  = new ConcurrentQueue <CompilerErrorCollection>();
                var didSomeWork = false;
                var stopwatch   = new Stopwatch();

                stopwatch.Start();
                Parallel.ForEach(templates,
                                 new ParallelOptions {
                    MaxDegreeOfParallelism = parallel ? -1 : 1
                },
                                 t =>
                {
                    var generator = new BuildTemplateGenerator(t.ToString());
                    try
                    {
                        bool skipped;
                        generator.ProcessTemplate(skipUpToDate, out skipped);
                        if (!skipped)
                        {
                            didSomeWork = true;
                        }
                        if (generator.Errors.HasErrors)
                        {
                            errorQueue.Enqueue(generator.Errors);
                        }
                        else
                        {
                            outputQueue.Enqueue(generator.OutputFile);
                        }
                    }
                    catch (Exception e)
                    {
                        errorQueue.Enqueue(new CompilerErrorCollection {
                            new CompilerError {
                                ErrorText = $"Could not process template '{t}':\n{e}"
                            }
                        });
                    }
                });
                stopwatch.Stop();

                // Sort the generated file names so that the MSBuild compilation cache hash remains stable
                var outputs = outputQueue.ToArray();
                Array.Sort(outputs, new CaseInsensitiveComparer());
                foreach (var output in outputs)
                {
                    Console.WriteLine(output);
                }

                if (didSomeWork && errorQueue.IsEmpty)
                {
                    Console.Error.WriteLine($"Templates transformed for {Path.GetFileName(Directory.GetCurrentDirectory())} (in {stopwatch.ElapsedMilliseconds / 1000.0} sec).");
                }

                foreach (var errors in errorQueue)
                {
                    LogErrors(errors);
                }

                return(errorQueue.IsEmpty ? 0 : 1);
            }
        }