Пример #1
0
        public static void Main(string[] args)
        {
            if (args.Length < 5)
            {
                Console.WriteLine("Usage cluster <PathToSln> <repositoryPath> <gitHubLink> <OutputFileName> <typeToCluster>");
                Console.WriteLine("Usage rewrite <PathToSln> <repositoryPath> <gitHubLink> <SerializedClustersJSON> <typeThasWasRefined>");
                return;
            }

            MSBuildLocator.RegisterDefaults();

            var workspace = MSBuildWorkspace.Create(new Dictionary <string, string> {
                { "DebugSymbols", "False" }
            });

            workspace.SkipUnrecognizedProjects = true;

            workspace.WorkspaceFailed += (e, o) =>
                                         Console.WriteLine(o.Diagnostic.ToString());
            var solution = workspace.OpenSolutionAsync(args[1]).Result;

            switch (args[0])
            {
            case "cluster":
                ClusteringExtractor.ExtractFromSolution(args[2], args[3], solution, args[4], args[5]);
                break;

            case "rewrite":
                SubtypeMiner.Rewrite(args[2], args[3], solution, args[4], args[5]);
                break;

            default:
                throw new Exception($"Unrecognized option {args[0]}");
            }
        }
Пример #2
0
        public static void ExtractFromSolution(string repositoryPath, string githubPath, Solution solution,
                                               string saveDir, string typeToCluster = "string")
        {
            Func <string, int, string> pathProcessor = (fullPath, lineNumber) =>
            {
                var basePath     = repositoryPath;
                var relativePath = fullPath.Substring(basePath.Length);
                var githubLink   = githubPath + relativePath.Replace('\\', '/') + "#L" + (lineNumber + 1);
                return(githubLink);
            };

            Console.WriteLine("Collecting type constraint graph...");
            var typeRelations = new TypeConstraints(pathProcessor);

            var projectGraph = solution.GetProjectDependencyGraph();
            var compilations = new List <CSharpCompilation>();

            foreach (var projectId in projectGraph.GetTopologicallySortedProjects())
            {
                Compilation compilation;
                try
                {
                    var project = solution.GetProject(projectId);

                    if (project.FilePath.ToLower().Contains("test"))
                    {
                        Console.WriteLine($"Excluding {project.FilePath} since it seems to be test-related");
                        continue;
                    }
                    compilation = project.GetCompilationAsync().Result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception while compiling project {0}: {1}", projectId, ex);
                    continue;
                }
                foreach (var error in compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error))
                {
                    Console.WriteLine(error.GetMessage());
                }
                if (compilation is CSharpCompilation cSharpCompilation)
                {
                    typeRelations.AddFromCompilation(cSharpCompilation);
                    compilations.Add(cSharpCompilation);
                }
            }

            var extractor = new ClusteringExtractor(new HashSet <string> {
                typeToCluster
            }, typeRelations);

            var(clusters, clusterParents) = extractor.InferColors();
            ClusteringSerializerUtil.SerializeClustering(saveDir, repositoryPath, clusters, clusterParents);
        }