<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using BuildDependencyReader.BuildDependencyResolver;
using BuildDependencyReader.ProjectFileParser;
using QuickGraph;
using QuickGraph.Algorithms;
using QuickGraph.Graphviz;
using Mono.Options;

namespace BuildDependencyReader.PrintProjectDependencies
{
    class DotEngine : IDotEngine
    {
        public string Run(QuickGraph.Graphviz.Dot.GraphvizImageType imageType, string dot, string outputFileName)
        {
            var tempFileName = System.IO.Path.GetTempFileName();
            File.AppendAllText(tempFileName, dot);
            var processStartInfo = new ProcessStartInfo(@"D:\Program Files (x86)\Graphviz 2.28\bin\dot.exe", 
                String.Format("-T{0} -o{1} {2}", 
                              imageType.ToString().ToLowerInvariant(), 
                              outputFileName, 
                              tempFileName));
            processStartInfo.UseShellExecute = false;
            processStartInfo.RedirectStandardError = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.WorkingDirectory = System.Environment.CurrentDirectory;

            Process process = Process.Start(processStartInfo);
            Console.Error.Write(process.StandardError.ReadToEnd());
            Console.Out.Write(process.StandardOutput.ReadToEnd());
            return outputFileName;
        }
    }

    class Program
    {
        static int Main(string[] args)
        {
            var exlcudedSlns = new List<string>();
            var inputFiles = new List<string>();
            bool verbose = false;
            bool generateGraphviz = false;
            string basePath;
            if (false == ParseOptions(args, exlcudedSlns, inputFiles, out basePath, out verbose, out generateGraphviz))
            {
                return 1;
            }

            PrintProjectDependencies(inputFiles, exlcudedSlns, basePath, verbose, generateGraphviz);

            return 0;
        }

        private static void PrintProjectDependencies(IEnumerable<string> _inputFiles, IEnumerable<string> _excludedSLNs, string basePath, bool verbose, bool generateGraphviz)
        {
            var projectFinder = new ProjectFinder(basePath, true);
            var dependencyInfo = BuildDependencyResolver.BuildDependencyResolver.DependencyInfo(projectFinder, _inputFiles, _excludedSLNs, verbose);

            if (generateGraphviz)
            {
                GenerateGraphViz(dependencyInfo.SolutionDependencyGraph);
            }

            foreach (var solutionFileName in dependencyInfo.TrimmedSolutionDependencyGraph.TopologicalSort())
            {
                Console.WriteLine(solutionFileName);
            }

            foreach (var solutionFileName in dependencyInfo.TrimmedSolutionDependencyGraph.TopologicalSort())
            {
                Console.WriteLine("Buildling: '{0}'", solutionFileName);
                Builder.CopyAssemblyReferencesFromBuiltProjects(projectFinder, 
                                                                projectFinder.GetProjectsOfSLN(solutionFileName)
                                                                             .SelectMany(x => x.AssemblyReferences)
                                                                             .Distinct());
                MSBuild(solutionFileName);
                Console.WriteLine("Done: '{0}'", solutionFileName);
            }
        }

        private static void MSBuild(string solutionFileName)
        {
            var process = new Process();
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.FileName = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe";
            process.StartInfo.Arguments = String.Format("/nologo /v:quiet \"{0}\"", solutionFileName);
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            Console.Error.Write(process.StandardOutput.ReadToEnd());
            Console.Error.Write(process.StandardError.ReadToEnd());
        }

        private static void GenerateGraphViz(AdjacencyGraph<string, SEdge<string>> graph)
        {
            var graphviz = new GraphvizAlgorithm<String, SEdge<String>>(graph, "graph", QuickGraph.Graphviz.Dot.GraphvizImageType.Svg);
            graphviz.GraphFormat.RankSeparation = 2;
            //graphviz.GraphFormat.IsConcentrated = true;

            graphviz.FormatVertex += new FormatVertexEventHandler<String>(graphviz_FormatVertex);

            var fileName = System.IO.Path.GetTempFileName() + ".svg";
            var outFileName = graphviz.Generate(new DotEngine(), fileName);
            Console.Error.WriteLine("GraphViz Output to: " + fileName);<commit_msg>tools/BuildDependencyReader: Fix bug where failure of msbuild would be ignored<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using BuildDependencyReader.BuildDependencyResolver;
using BuildDependencyReader.ProjectFileParser;
using QuickGraph;
using QuickGraph.Algorithms;
using QuickGraph.Graphviz;
using Mono.Options;

namespace BuildDependencyReader.PrintProjectDependencies
{
    class DotEngine : IDotEngine
    {
        public string Run(QuickGraph.Graphviz.Dot.GraphvizImageType imageType, string dot, string outputFileName)
        {
            var tempFileName = System.IO.Path.GetTempFileName();
            File.AppendAllText(tempFileName, dot);
            var processStartInfo = new ProcessStartInfo(@"D:\Program Files (x86)\Graphviz 2.28\bin\dot.exe", 
                String.Format("-T{0} -o{1} {2}", 
                              imageType.ToString().ToLowerInvariant(), 
                              outputFileName, 
                              tempFileName));
            processStartInfo.UseShellExecute = false;
            processStartInfo.RedirectStandardError = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.WorkingDirectory = System.Environment.CurrentDirectory;

            Process process = Process.Start(processStartInfo);
            Console.Error.Write(process.StandardError.ReadToEnd());
            Console.Out.Write(process.StandardOutput.ReadToEnd());
            return outputFileName;
        }
    }

    class Program
    {
        static int Main(string[] args)
        {
            var exlcudedSlns = new List<string>();
            var inputFiles = new List<string>();
            bool verbose = false;
            bool generateGraphviz = false;
            string basePath;
            if (false == ParseOptions(args, exlcudedSlns, inputFiles, out basePath, out verbose, out generateGraphviz))
            {
                return 1;
            }

            PrintProjectDependencies(inputFiles, exlcudedSlns, basePath, verbose, generateGraphviz);

            return 0;
        }

        private static void PrintProjectDependencies(IEnumerable<string> _inputFiles, IEnumerable<string> _excludedSLNs, string basePath, bool verbose, bool generateGraphviz)
        {
            var projectFinder = new ProjectFinder(basePath, true);
            var dependencyInfo = BuildDependencyResolver.BuildDependencyResolver.DependencyInfo(projectFinder, _inputFiles, _excludedSLNs, verbose);

            if (generateGraphviz)
            {
                GenerateGraphViz(dependencyInfo.SolutionDependencyGraph);
            }

            foreach (var solutionFileName in dependencyInfo.TrimmedSolutionDependencyGraph.TopologicalSort())
            {
                Console.WriteLine(solutionFileName);
            }

            foreach (var solutionFileName in dependencyInfo.TrimmedSolutionDependencyGraph.TopologicalSort())
            {
                Console.WriteLine("Buildling: '{0}'", solutionFileName);
                Builder.CopyAssemblyReferencesFromBuiltProjects(projectFinder, 
                                                                projectFinder.GetProjectsOfSLN(solutionFileName)
                                                                             .SelectMany(x => x.AssemblyReferences)
                                                                             .Distinct());
                MSBuild(solutionFileName);
                Console.WriteLine("Done: '{0}'", solutionFileName);
            }
        }

        private static void MSBuild(string solutionFileName)
        {
            var process = new Process();
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.FileName = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe";
            process.StartInfo.Arguments = String.Format("/nologo /v:quiet \"{0}\"", solutionFileName);
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            Console.Error.Write(process.StandardOutput.ReadToEnd());
            Console.Error.Write(process.StandardError.ReadToEnd());
            process.WaitForExit();
            if (0 != process.ExitCode)
            {
                throw new Exception("Build failed: " + solutionFileName);
            }
        }

        private static void GenerateGraphViz(AdjacencyGraph<string, SEdge<string>> graph)
        {
            var graphviz = new GraphvizAlgorithm<String, SEdge<String>>(graph, "graph", QuickGraph.Graphviz.Dot.GraphvizImageType.Svg);
            graphviz.GraphFormat.RankSeparation = 2;
            //graphviz.GraphFormat.IsConcentrated = true;

            graphviz.FormatVertex += new FormatVertexEventHandler<String>(graphviz_FormatVertex);

            var fileName = System.IO.Path.GetTempFileName() + ".svg";
            var outFileName = graphviz.Generate(new DotEngine(), fileName);
            Console.Error.WriteLine("GraphViz Output to: " + fileName);
{'fraction_non_alphanumeric': 0.08593058696293383, 'fraction_numerical': 0.004227706223576836, 'mean_word_length': 2.1658885776532837, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4014554', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2364, 'n_words': 461}