public static async Task RunAsync()

in src/AutoRest.CSharp/Common/AutoRest/Communication/StandaloneGeneratorRunner.cs [20:104]


        public static async Task RunAsync(CommandLineOptions options)
        {
            string? projectPath = null;
            string outputPath;
            string generatedTestOutputPath;
            bool wasProjectPathPassedIn = options.ProjectPath is not null;
            if (options.Standalone is not null)
            {
                //TODO this is only here for back compat we should consider removing it
                outputPath = options.Standalone;
            }
            else
            {
                projectPath = options.ProjectPath!;
                if (!projectPath!.EndsWith("src", StringComparison.Ordinal))
                    projectPath = Path.Combine(projectPath, "src");
                outputPath = Path.Combine(projectPath, "Generated");
            }
            generatedTestOutputPath = Path.Combine(outputPath, "..", "..", "tests", "Generated");
            var configurationPath = options.ConfigurationPath;
            if (configurationPath == null)
            {
                configurationPath = Path.Combine(outputPath, "Configuration.json");
                if (!File.Exists(configurationPath))
                {
                    configurationPath = Path.Combine(outputPath, "..", "..", "Configuration.json");
                }
            }
            LoadConfiguration(projectPath, outputPath, options.ExistingProjectFolder, File.ReadAllText(configurationPath));

            var codeModelInputPath = Path.Combine(outputPath, "CodeModel.yaml");
            var tspInputFile = Path.Combine(outputPath, "tspCodeModel.json");
            if (!File.Exists(tspInputFile))
            {
                tspInputFile = Path.Combine(outputPath, "..", "..", "tspCodeModel.json");
            }

            GeneratedCodeWorkspace workspace;
            if (File.Exists(tspInputFile))
            {
                var json = await File.ReadAllTextAsync(tspInputFile);
                var rootNamespace = TypeSpecSerialization.Deserialize(json) ?? throw new InvalidOperationException($"Deserializing {tspInputFile} has failed.");
                workspace = await new CSharpGen().ExecuteAsync(rootNamespace);
                if (options.IsNewProject)
                {
                    bool needAzureKeyAuth = rootNamespace.Auth?.ApiKey != null;
                    // TODO - add support for DataFactoryElement lookup
                    await new NewProjectScaffolding(needAzureKeyAuth, false).Execute();
                }
            }
            else if (File.Exists(codeModelInputPath))
            {
                var yaml = await File.ReadAllTextAsync(codeModelInputPath);
                var codeModel = CodeModelSerialization.DeserializeCodeModel(yaml);
                workspace = await new CSharpGen().ExecuteAsync(codeModel);
                if (options.IsNewProject)
                {
                    bool needAzureKeyAuth = codeModel.Security.Schemes.Any(scheme => scheme is KeySecurityScheme);
                    bool includeDfe = yaml.Contains("x-ms-format: dfe-", StringComparison.Ordinal);
                    await new NewProjectScaffolding(needAzureKeyAuth, includeDfe).Execute();
                }
            }
            else
            {
                throw new InvalidOperationException($"Neither CodeModel.yaml nor tspCodeModel.json exist in {outputPath} folder.");
            }

            if (options.ClearOutputFolder)
            {
                DeleteDirectory(outputPath, keepFiles);
                DeleteDirectory(generatedTestOutputPath, keepFiles);
            }

            await foreach (var file in workspace.GetGeneratedFilesAsync())
            {
                if (string.IsNullOrEmpty(file.Text))
                {
                    continue;
                }
                var filename = Path.Combine(outputPath, file.Name);
                Console.WriteLine($"Writing {filename}");
                Directory.CreateDirectory(Path.GetDirectoryName(filename)!);
                await File.WriteAllTextAsync(filename, file.Text);
            }
        }