From a96a7e8139019435c52fb8fd3760054a40e81d93 Mon Sep 17 00:00:00 2001 From: "Evans Aboge (from Dev Box)" Date: Wed, 15 Jan 2025 15:11:44 +0300 Subject: [PATCH] Settings file scafolding --- src/Kiota.Builder/KiotaBuilder.cs | 7 ++ .../Settings/ISettingsManagementService.cs | 42 +++++++++++ src/Kiota.Builder/Settings/SettingsFile.cs | 71 +++++++++++++++++++ .../Settings/SettingsFileGenerationContext.cs | 12 ++++ .../Settings/SettingsFileManagementService.cs | 60 ++++++++++++++++ .../HTTP/CodeClassDeclarationWriter.cs | 6 +- 6 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 src/Kiota.Builder/Settings/ISettingsManagementService.cs create mode 100644 src/Kiota.Builder/Settings/SettingsFile.cs create mode 100644 src/Kiota.Builder/Settings/SettingsFileGenerationContext.cs create mode 100644 src/Kiota.Builder/Settings/SettingsFileManagementService.cs diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index 450e392103..d9e39a4bef 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -26,6 +26,7 @@ using Kiota.Builder.OpenApiExtensions; using Kiota.Builder.Plugins; using Kiota.Builder.Refiners; +using Kiota.Builder.Settings; using Kiota.Builder.WorkspaceManagement; using Kiota.Builder.Writers; using Microsoft.Extensions.Logging; @@ -46,6 +47,7 @@ public partial class KiotaBuilder private readonly ParallelOptions parallelOptions; private readonly HttpClient httpClient; private OpenApiDocument? openApiDocument; + private readonly SettingsFileManagementService settingsFileManagementService = new(); internal void SetOpenApiDocument(OpenApiDocument document) => openApiDocument = document ?? throw new ArgumentNullException(nameof(document)); public KiotaBuilder(ILogger logger, GenerationConfiguration config, HttpClient client, bool useKiotaConfig = false) @@ -285,6 +287,11 @@ public async Task GenerateClientAsync(CancellationToken cancellationToken) sw.Start(); await CreateLanguageSourceFilesAsync(config.Language, generatedCode, cancellationToken).ConfigureAwait(false); StopLogAndReset(sw, $"step {++stepId} - writing files - took"); + + if (config.Language == GenerationLanguage.HTTP && openApiDocument is not null) + { + await settingsFileManagementService.WriteSettingsFileAsync(config.OutputPath, openApiDocument, cancellationToken).ConfigureAwait(false); + } return stepId; }, cancellationToken).ConfigureAwait(false); } diff --git a/src/Kiota.Builder/Settings/ISettingsManagementService.cs b/src/Kiota.Builder/Settings/ISettingsManagementService.cs new file mode 100644 index 0000000000..339e55ee4c --- /dev/null +++ b/src/Kiota.Builder/Settings/ISettingsManagementService.cs @@ -0,0 +1,42 @@ +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.Services; + +namespace Kiota.Builder.Settings; +/// +/// A service that manages the settings file for http language snippets. +/// +public interface ISettingsManagementService +{ + /// + /// Gets the settings file for a Kiota project by crawling the directory tree. + /// + /// + /// + string GetDirectoryContainingSettingsFile(string searchDirectory); + + /// + /// Gets the settings from a directory. + /// + /// + /// + Task GetSettingsFromDirectoryAsync(string directoryPath, CancellationToken cancellationToken); + + /// + /// Gets the settings from a stream. + /// + /// + /// + Task GetSettingsFromStreamAsync(Stream stream); + + /// + /// Writes the settings file to a directory. + /// + /// + /// OpenApi document + /// + /// + Task WriteSettingsFileAsync(string directoryPath, OpenApiDocument openApiDocument, CancellationToken cancellationToken); +} diff --git a/src/Kiota.Builder/Settings/SettingsFile.cs b/src/Kiota.Builder/Settings/SettingsFile.cs new file mode 100644 index 0000000000..f5751a605d --- /dev/null +++ b/src/Kiota.Builder/Settings/SettingsFile.cs @@ -0,0 +1,71 @@ +using System.Text.Json.Serialization; +using Kiota.Builder.Configuration; + +namespace Kiota.Builder.Settings; +public class SettingsFile +{ + [JsonPropertyName("rest-client.environmentVariables")] + public EnvironmentVariables EnvironmentVariables + { + get; set; + } + + public SettingsFile() + { + EnvironmentVariables = new EnvironmentVariables(); + } +} + +public class EnvironmentVariables +{ + [JsonPropertyName("$shared")] + public SharedAuth Shared + { + get; set; + } + + [JsonPropertyName("remote")] + public AuthenticationSettings Remote + { + get; set; + } + + [JsonPropertyName("development")] + public AuthenticationSettings Development + { + get; set; + } + + public EnvironmentVariables() + { + Shared = new SharedAuth(); + Remote = new AuthenticationSettings(); + Development = new AuthenticationSettings(); + } +} + +public class SharedAuth +{ + +} + +public class AuthenticationSettings +{ + public string HostAddress + { + get; set; + } + public string? BasicAuth + { + get; set; + } + public string? Bearer + { + get; set; + } + + public AuthenticationSettings() + { + HostAddress = ""; + } +} diff --git a/src/Kiota.Builder/Settings/SettingsFileGenerationContext.cs b/src/Kiota.Builder/Settings/SettingsFileGenerationContext.cs new file mode 100644 index 0000000000..d5dcb10899 --- /dev/null +++ b/src/Kiota.Builder/Settings/SettingsFileGenerationContext.cs @@ -0,0 +1,12 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Text.Json.Serialization.Metadata; +using Kiota.Builder.Lock; + +namespace Kiota.Builder.Settings; + +[JsonSerializable(typeof(SettingsFile))] +internal partial class SettingsFileGenerationContext : JsonSerializerContext +{ +} diff --git a/src/Kiota.Builder/Settings/SettingsFileManagementService.cs b/src/Kiota.Builder/Settings/SettingsFileManagementService.cs new file mode 100644 index 0000000000..15bca62b5e --- /dev/null +++ b/src/Kiota.Builder/Settings/SettingsFileManagementService.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.OpenApi.Models; + +namespace Kiota.Builder.Settings; + +public class SettingsFileManagementService : ISettingsManagementService +{ + internal const string SettingsFileName = "settings.json"; + public string GetDirectoryContainingSettingsFile(string searchDirectory) + { + throw new NotImplementedException(); + } + + public Task GetSettingsFromDirectoryAsync(string directoryPath, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task GetSettingsFromStreamAsync(Stream stream) + { + throw new NotImplementedException(); + } + + public Task WriteSettingsFileAsync(string directoryPath, OpenApiDocument openApiDocument, CancellationToken cancellationToken) + { + ArgumentException.ThrowIfNullOrEmpty(directoryPath); + ArgumentNullException.ThrowIfNull(openApiDocument); + var settings = GenerateSettingsFile(openApiDocument); + return WriteSettingsFileInternalAsync(directoryPath, settings, cancellationToken); + } + + private static SettingsFile GenerateSettingsFile(OpenApiDocument openApiDocument) + { + var settings = new SettingsFile(); + settings.EnvironmentVariables.Development.HostAddress = openApiDocument.Servers[0].Url; + settings.EnvironmentVariables.Remote.HostAddress = openApiDocument.Servers[0].Url; + return settings; + } + + private static readonly JsonSerializerOptions options = new() + { + WriteIndented = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + }; + + private static readonly SettingsFileGenerationContext context = new(options); + + private static async Task WriteSettingsFileInternalAsync(string directoryPath, SettingsFile settings, CancellationToken cancellationToken) + { + var filePath = Path.Combine(directoryPath, SettingsFileName); +#pragma warning disable CA2007 // Dispose objects before losing scope + await using var fileStream = File.Open(filePath, FileMode.Create); +#pragma warning disable CA2007 + await JsonSerializer.SerializeAsync(fileStream, settings, context.SettingsFile, cancellationToken).ConfigureAwait(false); + } +} diff --git a/src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs b/src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs index 279a02e295..4a033171b2 100644 --- a/src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs +++ b/src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs @@ -8,7 +8,7 @@ namespace Kiota.Builder.Writers.Http; public class CodeClassDeclarationWriter(HttpConventionService conventionService) : CodeProprietableBlockDeclarationWriter(conventionService) { - private const string BaseUrlPropertyName = "url"; + private const string BaseUrlPropertyName = "hostAddress"; protected override void WriteTypeDeclaration(ClassDeclaration codeElement, LanguageWriter writer) { @@ -30,7 +30,7 @@ protected override void WriteTypeDeclaration(ClassDeclaration codeElement, Langu var baseUrl = GetBaseUrl(requestBuilderClass); // Write the baseUrl variable - WriteBaseUrl(baseUrl, writer); + //WriteBaseUrl(baseUrl, writer); // Extract and write the URL template WriteUrlTemplate(urlTemplateProperty, writer); @@ -102,7 +102,7 @@ private static void WriteBaseUrl(string? baseUrl, LanguageWriter writer) { // Write the base URL variable to the writer writer.WriteLine($"# Base url for the server/host"); - writer.WriteLine($"@{BaseUrlPropertyName} = {baseUrl}"); + writer.WriteLine($"@{BaseUrlPropertyName} = {{hostAddress}}"); writer.WriteLine(); }