static string ParseFile(string path, string solutionPath, bool doIndent) { Console.WriteLine("Parsing file " + path); var output = "// Converted with SharpSwift - https://github.com/matthewsot/SharpSwift" + ConvertToSwift.NewLine; var doc = GetDocumentFromSolution(solutionPath, path); var tree = doc.GetSyntaxTreeAsync().Result; ConvertToSwift.Model = doc.GetSemanticModelAsync().Result; var root = (CompilationUnitSyntax)tree.GetRoot(); var rootNamespace = root.Members.OfType <NamespaceDeclarationSyntax>().FirstOrDefault(); // Search for "//import XYZ" in the usings to output in the final Swift code output = root.Usings.Aggregate(output, (current, usingDir) => current + GetImportsFromTrivia(usingDir.GetLeadingTrivia())); output += GetImportsFromTrivia(root.Usings.Last().GetTrailingTrivia()); output += GetImportsFromTrivia(rootNamespace.GetLeadingTrivia()); output += "" + ConvertToSwift.NewLine; // Parses each class in the file into output. output = rootNamespace.Members.OfType <ClassDeclarationSyntax>() .Aggregate(output, (current, childClass) => current + ConvertToSwift.SyntaxNode(childClass)); return(doIndent ? Indenter.IndentDocument(output) : output); }
static string ParseFile(string path, string solutionPath, bool doIndent = true) { Console.WriteLine("Parsing file " + path); var output = "//Converted with SharpSwift - https://github.com/matthewsot/SharpSwift" + ConvertToSwift.NewLine; output += "//See https://github.com/matthewsot/DNSwift FMI about these imports" + ConvertToSwift.NewLine + ConvertToSwift.NewLine; output += "import DNSwift;" + ConvertToSwift.NewLine; Document doc = null; if (solutionPath != null) { doc = GetDocumentFromSolution(solutionPath, path); } var tree = CSharpSyntaxTree.ParseFile(path); if (doc != null) { SemanticModel model = doc.GetSemanticModelAsync().Result; if (model != null) { ConvertToSwift.model = model; } tree = doc.GetSyntaxTreeAsync().Result; if (tree == null) { tree = CSharpSyntaxTree.ParseFile(path); } } var root = (CompilationUnitSyntax)tree.GetRoot(); var rootNamespace = root.Members.OfType <NamespaceDeclarationSyntax>().FirstOrDefault(); var classes = rootNamespace.Members.OfType <ClassDeclarationSyntax>(); foreach (var usingDir in root.Usings) { if (!usingDir.Name.ToString().StartsWith("SharpSwift.")) { output += "import " + usingDir.Name.ToString().Replace(".", "") + ";\r\n"; } output += GetImportsFromTrivia(usingDir.GetLeadingTrivia()); } output += GetImportsFromTrivia(root.Usings.Last().GetTrailingTrivia()); //in case they added imports to the bottom output += GetImportsFromTrivia(rootNamespace.GetLeadingTrivia()); output += "" + ConvertToSwift.NewLine; foreach (var childClass in classes) { output += ConvertToSwift.SyntaxNode(childClass); } return(doIndent ? Indenter.IndentDocument(output) : output); }