public bool LoadParsedSource()
        {
            if (SourceFile.Extension != ".cs")
            {
                Log.Warning("Source file ({Extension}) not supported", SourceFile.Extension);
                return(false);
            }

#if JSON_IMPORT_EXPORT
            if (ParsedJsonFile != null)
            {
                using var stream     = ParsedJsonFile.Open(FileMode.Open, FileAccess.Read);
                using var reader     = new StreamReader(stream);
                using var jsonReader = new JsonTextReader(reader);
                var serializer = new JsonSerializer();
                ParsedClasses = new List <ClassDefinition>(serializer.Deserialize <IList <ClassDefinition> >(jsonReader));
                return(true);
            }
#endif

            var tree = CSharpSyntaxTree.ParseText(SourceText.From(SourceContents, Encoding.UTF8));
            var root = tree.GetCompilationUnitRoot();

            ParsedClasses = CSharpParser.ProcessCSharpSyntaxTree(root);

#if JSON_IMPORT_EXPORT
            if (ParsedJsonFile != null)
            {
                var jsonSerializer = JsonSerializer.CreateDefault();
                jsonSerializer.PreserveReferencesHandling = PreserveReferencesHandling.All;
                jsonSerializer.TypeNameHandling           = TypeNameHandling.Objects;
                using var textWriter   = ParsedJsonFile.Open(FileMode.Create, FileAccess.Write);
                using var streamWriter = new StreamWriter(textWriter);
                var jsonWriter = new JsonTextWriter(streamWriter)
                {
                    Formatting = Formatting.Indented
                };
                jsonSerializer.Serialize(jsonWriter, ParsedClasses);
            }
#endif

            return(true);
        }
Пример #2
0
        private static void MainInternal(string[] args)
        {
            Guard.Argument(args.Length, nameof(args.Length)).Equal(3);

            var path = new FileInfo(args[0]);

            Guard.Argument(path.Exists, nameof(path.Exists)).True();

            var outputPath   = new FileInfo(args[1]);
            var outputCsPath = new FileInfo(args[2]);

            Log.Information("{Path} to {OutputPath} & {OutputCSPath}", path, outputPath, outputCsPath);

            var tree = CSharpSyntaxTree.ParseText(SourceText.From(path.OpenText().ReadToEnd(), Encoding.UTF8));
            var root = tree.GetCompilationUnitRoot();

            var classes = CSharpParser.ProcessCSharpSyntaxTree(root);

            {
                var jsonSerializer = JsonSerializer.CreateDefault();
                jsonSerializer.PreserveReferencesHandling = PreserveReferencesHandling.All;
                jsonSerializer.TypeNameHandling           = TypeNameHandling.Objects;
                using var textWriter   = outputPath.Open(FileMode.Create, FileAccess.Write);
                using var streamWriter = new StreamWriter(textWriter);
                using var jsonWriter   = new JsonTextWriter(streamWriter)
                      {
                          Formatting = Formatting.Indented
                      };
                jsonSerializer.Serialize(jsonWriter, classes);
            }
            {
                using var textWriter   = outputCsPath.Open(FileMode.Create, FileAccess.Write);
                using var streamWriter = new StreamWriter(textWriter);
                foreach (var classDefinition in classes)
                {
                    streamWriter.WriteLine(classDefinition);
                }
            }
        }