Example #1
0
        public RewriterFromJson(string filename, string repositoryPath, TypeConstraints recollectedConstraints)
        {
            ClusteringSerializerUtil csu = new ClusteringSerializerUtil();

            (Clusters, Parents) = csu.Deserialize(filename, repositoryPath, recollectedConstraints);
            AncestorMap         = new Dictionary <int, List <int> >();
            SetAncestorMap();
        }
Example #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);
        }