예제 #1
0
        private IEnumerable <(string pathPostfix, string contents)> GetDotNotations(
            FileInfo projectFile,
            ImmutableDictionary <string, string> globalProperties,
            string[] targets,
            string[] endNodes)
        {
            Console.WriteLine("Loading graph...");

            var sw    = Stopwatch.StartNew();
            var graph = new ProjectGraph(new ProjectGraphEntryPoint(projectFile.FullName, globalProperties), ProjectCollection.GlobalProjectCollection);

            sw.Stop();

            Console.WriteLine($@"{projectFile} loaded {graph.ProjectNodes.Count} node(s) in {sw.ElapsedMilliseconds}ms.");

            var entryTargetsPerNode = graph.GetTargetLists(targets);

            if (endNodes != null)
            {
                var endGraphNodes = graph.ProjectNodes.Where(n => endNodes.Any(en => n.ProjectInstance.FullPath.Contains(en)));
                var paths         = GraphPaths.FindAllPathsBetween(graph.GraphRoots, endGraphNodes);

                var deduplicatedNodes = paths.SelectMany(p => p).ToHashSet();
                yield return($"_PathsEndingIn_{string.Join(",", endNodes)}", GraphVis.Create(deduplicatedNodes, entryTargetsPerNode));
            }

            yield return("", GraphVis.Create(graph, entryTargetsPerNode));
        }
        public static string Create(ProjectGraph graphNodes, GraphVisOptions options)
        {
            // I don't really remember why I did the hash thing. I think I was concerned with duplicate nodes?
            var projects = new ConcurrentDictionary <string, ProjectGraphNode>();

            foreach (var node in graphNodes.ProjectNodes)
            {
                var propsHash = GraphVis.HashGlobalProps(node.ProjectInstance.GlobalProperties);
                projects.TryAdd(node.ProjectInstance.FullPath + propsHash, node);
            }

            return(Create(projects, options));
        }
예제 #3
0
        private static int Run(CommandLineArguments args)
        {
            try
            {
                var outFile = Path.GetFullPath(args.OutputFile);

                var directory = Path.GetDirectoryName(outFile);
                var filename  = Path.GetFileNameWithoutExtension(outFile);
                var extension = Path.GetExtension(outFile);

                var projectFile = args.InputFile;

                if (string.IsNullOrEmpty(args.MSBuildBinDirectory))
                {
                    MSBuildLocatorUtils.RegisterMSBuild();
                }
                else
                {
                    MSBuildLocator.RegisterMSBuildPath(args.MSBuildBinDirectory);
                }

                var dotNotations = new Program().GetDotNotations(new FileInfo(projectFile), string.IsNullOrEmpty(args.EndNodes) ? null : args.EndNodes.Split(';'));

                var renderingFunction = extension.EndsWith(".txt")
                    ? (Action <string, string>)((path, dotNotation) => File.WriteAllText(path, dotNotation))
                    : (Action <string, string>)((path, dotNotation) => GraphVis.Save(dotNotation, path));

                foreach (var dotNotation in dotNotations)
                {
                    var outputFile = Path.Combine(directory, $"{filename}{dotNotation.pathPostfix}{extension}");
                    renderingFunction(outputFile, dotNotation.contents);
                }

                return(0);
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(e);
                Console.ResetColor();
                return(1);
            }
        }
예제 #4
0
        private string LoadGraph(FileInfo projectFile)
        {
            var files = new List <string>();

            if (projectFile.Extension == ".sln")
            {
                files.AddRange(SolutionParser.GetProjectFiles(projectFile.FullName));
            }
            else
            {
                files.Add(projectFile.FullName);
            }

            Console.WriteLine("Loading graph...");
            var sw    = Stopwatch.StartNew();
            var graph = new ProjectGraph(files, ProjectCollection.GlobalProjectCollection);

            Console.WriteLine($@"{projectFile} loaded {graph.ProjectNodes.Count} node(s) in {sw.ElapsedMilliseconds}ms.");

            return(GraphVis.Create(graph));
        }
예제 #5
0
        static void Main(string[] args)
        {
            try
            {
                var outFile = args.Length > 1 ? args[1] : "out.png";

                // GraphGen.exe <proj-file> ?<out.png> ?<msbuild-path>
                if (args.Length == 0)
                {
                    Console.WriteLine("GraphGen.exe <proj-file> ?<out.png> ?<msbuild-path>");
                    Environment.Exit(1);
                }

                var projectFile = args[0];

                if (args.Length < 3)
                {
                    MSBuildLocatorUtils.RegisterMSBuild();
                }
                else
                {
                    MSBuildLocator.RegisterMSBuildPath(args[2]);
                }

                var graphText = new Program().LoadGraph(new FileInfo(projectFile));

                GraphVis.Save(graphText, outFile);
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(e);
                Console.ResetColor();
                Environment.Exit(1);
            }
        }
예제 #6
0
        private static int Run(CommandLineArguments args)
        {
            try
            {
                var outFile = Path.GetFullPath(args.OutputFile);

                var directory = Path.GetDirectoryName(outFile);
                var filename  = Path.GetFileNameWithoutExtension(outFile);
                var extension = Path.GetExtension(outFile);

                var projectFile = args.InputFile;

                if (string.IsNullOrEmpty(args.MSBuildBinDirectory))
                {
                    MSBuildLocatorUtils.RegisterMSBuild();
                }
                else
                {
                    MSBuildLocator.RegisterMSBuildPath(args.MSBuildBinDirectory);
                }

                var globalProperties = string.IsNullOrEmpty(args.GlobalProperties)
                    ? ImmutableDictionary <string, string> .Empty
                    : args.GlobalProperties.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(
                    propertyPair =>
                {
                    var kvp = propertyPair.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

                    Trace.Assert(kvp.Length == 2, $"Expected <key>=<value> format in {propertyPair}");

                    return(kvp[0], kvp[1]);
                }).ToImmutableDictionary(kvp => kvp.Item1, kvp => kvp.Item2);

                var targets = string.IsNullOrEmpty(args.Targets)
                    ? null
                    : args.Targets.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                var endNodes = string.IsNullOrEmpty(args.EndNodes)
                    ? null
                    : args.EndNodes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                var dotNotations = new Program().GetDotNotations(new FileInfo(projectFile), globalProperties, targets, endNodes);

                var renderingFunction = extension.EndsWith(".txt")
                    ? (path, dotNotation) => File.WriteAllText(path, dotNotation)
                    : (Action <string, string>)((path, dotNotation) => GraphVis.Save(dotNotation, path));

                foreach (var dotNotation in dotNotations)
                {
                    var outputFile = Path.Combine(directory, $"{filename}{dotNotation.pathPostfix}{extension}");
                    renderingFunction(outputFile, dotNotation.contents);
                }

                return(0);
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(e);
                Console.ResetColor();
                return(1);
            }
        }