示例#1
0
        private static async Task <int> ExecuteCodeGenerator(Func <Task <ApiModel> > retrieveModel, Func <Task <string?> > retrieveVersion)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var apiModel = await retrieveModel();

            var version = await retrieveVersion();

            // Remove old code
            var generatedCodePath = Path.GetFullPath(OutputPath);

            if (Directory.Exists(generatedCodePath))
            {
                Directory.Delete(generatedCodePath, recursive: true);
            }

            // Build code
            var codeGenerationContext = CodeGenerationContext.CreateFrom(apiModel);
            var csharpApiModelVisitor = new CSharpApiModelGenerator(codeGenerationContext);

            csharpApiModelVisitor.GenerateFiles(
                new CSharpDocumentWriter(
                    new DirectoryInfo(Path.GetFullPath(OutputPath))));

            // Report
            stopwatch.Stop();
            Console.WriteLine($"Code generation completed in: {stopwatch.Elapsed}");
            Console.WriteLine($"  Number of DTO: {codeGenerationContext.GetDtos().Count()}");
            Console.WriteLine($"  Number of Enums: {codeGenerationContext.GetEnums().Count()}");
            Console.WriteLine($"  Number of Resources (top level): {codeGenerationContext.GetResources().Count()}");

            // Write version marker
            if (string.IsNullOrEmpty(version))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"ERROR: Version information is not available.");
                return(-1);
            }
            await File.WriteAllTextAsync("../../../../../version-info.txt", version);

            return(0);
        }