예제 #1
0
    private static Docs?ParseDocs(GeneratorExecutionContext context)
    {
        Docs?docs = null;

        if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWin32InputDocPaths", out string?delimitedApiDocsPaths) &&
            !string.IsNullOrWhiteSpace(delimitedApiDocsPaths))
        {
            string[] apiDocsPaths = delimitedApiDocsPaths !.Split('|');
            if (apiDocsPaths.Length > 0)
            {
                List <Docs> docsList = new(apiDocsPaths.Length);
                foreach (string path in apiDocsPaths)
                {
                    try
                    {
                        docsList.Add(Docs.Get(path));
                    }
                    catch (Exception e)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(DocParsingError, null, path, e.Message));
                    }
                }

                docs = Docs.Merge(docsList);
            }
        }

        return(docs);
    }
예제 #2
0
        private static void Main(string[] args)
        {
            using var cts           = new CancellationTokenSource();
            Console.CancelKeyPress += (s, e) =>
            {
                Console.WriteLine("Canceling...");
                cts.Cancel();
                e.Cancel = true;
            };

            Console.WriteLine("Initializing generator...");

            try
            {
                string outputDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) !, "output");
                if (Directory.Exists(outputDirectory))
                {
                    foreach (string file in Directory.EnumerateFiles(outputDirectory, "*", SearchOption.AllDirectories))
                    {
                        File.Delete(file);
                    }
                }
                else
                {
                    Directory.CreateDirectory(outputDirectory);
                }

                var    sw           = Stopwatch.StartNew();
                string metadataPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location !) !, "Windows.Win32.winmd");
                string apiDocsPath  = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location !) !, "apidocs.msgpack");
                using var generator = new Generator(
                          metadataPath,
                          Docs.Get(apiDocsPath),
                          new GeneratorOptions
                {
                    WideCharOnly   = true,
                    EmitSingleFile = true,
                },
                          parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9));
                Console.WriteLine("Generating code... (press Ctrl+C to cancel)");
                if (args.Length > 0)
                {
                    foreach (string name in args)
                    {
                        cts.Token.ThrowIfCancellationRequested();
                        if (!generator.TryGenerate(name, cts.Token))
                        {
                            Console.Error.WriteLine("WARNING: No match for " + name);
                        }
                    }
                }
                else
                {
                    generator.GenerateAll(cts.Token);
                }

                Console.WriteLine("Gathering source files...");
                var compilationUnits = generator.GetCompilationUnits(cts.Token);
                Console.WriteLine("Emitting source files...");
                compilationUnits.AsParallel().WithCancellation(cts.Token).ForAll(unit =>
                {
                    string outputPath = Path.Combine(outputDirectory, unit.Key);
                    Console.WriteLine("Writing output file: {0}", outputPath);
                    using var generatedSourceStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read);
                    using var generatedSourceWriter = new StreamWriter(generatedSourceStream, Encoding.UTF8);
                    unit.Value.WriteTo(generatedSourceWriter);
                });

                Console.WriteLine("Generation time: {0}", sw.Elapsed);
            }
            catch (OperationCanceledException oce) when(oce.CancellationToken == cts.Token)
            {
                Console.Error.WriteLine("Canceled.");
            }
        }