Exemplo n.º 1
0
        private static void ExportToMSE(string input, string output)
        {
            try
            {
                string path = Assembly.GetAssembly(typeof(MainClass)).Location;
                Console.WriteLine("Current executable location " + path);
                path = path.Replace("RoslynMonoFamix.exe", "");

                var metamodel = FamixModel.Metamodel();

                var msWorkspace = MSBuildWorkspace.Create();

                var solution = msWorkspace.OpenSolutionAsync(input).Result;
                Uri uri      = null;
                try
                {
                    uri = new Uri(input);;
                }
                catch (UriFormatException e)
                {
                    var currentFolder = new Uri(Environment.CurrentDirectory + "\\");
                    uri = new Uri(currentFolder, input.Replace("\\", "/"));
                    Console.WriteLine(e.StackTrace);
                }

                var ignoreFolder = Path.GetDirectoryName(uri.AbsolutePath);
                var importer     = new InCSharp.InCSharpImporter(metamodel, ignoreFolder);
                var documents    = new List <Document>();
                Console.WriteLine("Solution with id " + solution.Id.Id + " opened and contains " + solution.Projects.Count <Project>() + " projects.");

                var diagnostics = msWorkspace.Diagnostics;
                foreach (var diagnostic in diagnostics)
                {
                    Console.WriteLine(diagnostic.Message);
                }

                for (int i = 0; i < solution.Projects.Count <Project>(); i++)
                {
                    var project = solution.Projects.ElementAt <Project>(i);

                    System.Console.WriteLine("(project " + (i + 1) + " / " + solution.Projects.Count <Project>() + ") contains " + project.Documents.Count <Document>() + " documents.");

                    var projectCompilation = project.GetCompilationAsync().Result;
                    for (int j = 0; j < project.Documents.Count <Document>(); j++)
                    {
                        var document = project.Documents.ElementAt <Document>(j);
                        if (document.SupportsSyntaxTree)
                        {
                            System.Console.Write("(project " + (i + 1) + " / " + solution.Projects.Count <Project>() + ")");
                            System.Console.WriteLine("(document " + (j + 1) + " / " + project.Documents.Count <Document>() + " " + document.FilePath + ")");
                            var syntaxTree = document.GetSyntaxTreeAsync().Result;

                            var compilationAsync = project.GetCompilationAsync().Result;
                            var semanticModel    = compilationAsync.GetSemanticModel(syntaxTree);

                            semanticModel.ToString();

                            if (semanticModel.Language == "C#")
                            {
                                var visitor = new ASTVisitor(semanticModel, importer);
                                visitor.Visit(syntaxTree.GetRoot());
                            }
                            else
                            {
                                var visitor = new VBASTVisitor(semanticModel, importer);
                                visitor.Visit(syntaxTree.GetRoot());
                            }
                        }
                    }
                }
                metamodel.ExportMSEFile(output);
            }
            catch (ReflectionTypeLoadException ex)
            {
                StringBuilder sb = new StringBuilder();
                foreach (System.Exception exSub in ex.LoaderExceptions)
                {
                    sb.AppendLine(exSub.Message);
                    FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
                    if (exFileNotFound != null)
                    {
                        if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                        {
                            sb.AppendLine("Fusion Log:");
                            sb.AppendLine(exFileNotFound.FusionLog);
                        }
                    }
                    sb.AppendLine();
                }
                string errorMessage = sb.ToString();
                Console.WriteLine(errorMessage);
                //Display or log the error based on your application.
            }
        }
Exemplo n.º 2
0
        public void LoadSampleSystem()
        {
            string path = Assembly.GetAssembly(typeof(SampleSystemLoader)).Location;

            path = path.Replace("FamixTest.dll", "");
            string solutionPath = path + "../../../SampleCode/SampleCode.sln";

            importer = new Roslyn2Famix.InCSharp.InCSharpImporter(metamodel, Path.GetDirectoryName(new Uri(solutionPath).AbsolutePath));
            var msWorkspace = MSBuildWorkspace.Create();

            msWorkspace.WorkspaceFailed += (o, e) =>
            {
                System.Console.WriteLine(e.Diagnostic.Message);
            };

            var solution    = msWorkspace.OpenSolutionAsync(solutionPath).Result;
            var diagnostics = msWorkspace.Diagnostics;

            foreach (var diagnostic in diagnostics)
            {
                Console.WriteLine(diagnostic.Message);
            }
            Boolean fileWasFound = false;

            foreach (var project in solution.Projects)
            {
                foreach (var document in project.Documents)
                {
                    var targetFile = this.GetType().Name.Replace("Test", ".cs");
                    targetFile = targetFile.Replace("VB.cs", ".vb");
                    targetFile.Replace("2.cs", "1.cs");
                    targetFile.Replace("2.vb", "1.vb");

                    if (document.SupportsSyntaxTree && (document.FilePath.Replace("2.cs", "1.cs").EndsWith(targetFile) || document.FilePath.Replace("2.vb", "1.vb").EndsWith(targetFile)))
                    {
                        var syntaxTree = document.GetSyntaxTreeAsync().Result;
                        System.Console.WriteLine(syntaxTree.ToString());
                        var compilationAsync = project.GetCompilationAsync().Result;
                        var semanticModel    = compilationAsync.GetSemanticModel(syntaxTree);

                        var syntax = syntaxTree.GetRoot().NormalizeWhitespace().ToFullString();

                        if (document.FilePath.EndsWith(".vb"))
                        {
                            var visitor = new VBASTVisitor(semanticModel, importer);
                            var printer = new VBPrettyPrinter();
                            visitor.Visit(syntaxTree.GetRoot());
                            printer.Visit(syntaxTree.GetRoot());
                            Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            // WriteAllText creates a file, writes the specified string to the file,
                            // and then closes the file.    You do NOT need to call Flush() or Close().
                            System.IO.File.WriteAllText(@targetFile + ".txt", printer.PrettyText);

                            System.Console.WriteLine(printer.PrettyText);
                        }
                        else
                        {
                            var visitor = new ASTVisitor(semanticModel, importer);
                            visitor.Visit(syntaxTree.GetRoot());
                        }
                        fileWasFound = true;
                    }
                }
            }
            if (!fileWasFound)
            {
                throw new Exception("File was not found!");
            }
            metamodel.ExportMSEFile("SampleCode.mse");
        }