Skip to content

Commit

Permalink
Settings file scafolding
Browse files Browse the repository at this point in the history
  • Loading branch information
Evans Aboge (from Dev Box) committed Jan 15, 2025
1 parent a99c4a8 commit a96a7e8
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<KiotaBuilder> logger, GenerationConfiguration config, HttpClient client, bool useKiotaConfig = false)
Expand Down Expand Up @@ -285,6 +287,11 @@ public async Task<bool> 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);
}
Expand Down
42 changes: 42 additions & 0 deletions src/Kiota.Builder/Settings/ISettingsManagementService.cs
Original file line number Diff line number Diff line change
@@ -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;
/// <summary>
/// A service that manages the settings file for http language snippets.
/// </summary>
public interface ISettingsManagementService
{
/// <summary>
/// Gets the settings file for a Kiota project by crawling the directory tree.
/// </summary>
/// <param name="searchDirectory"></param>
/// <returns></returns>
string GetDirectoryContainingSettingsFile(string searchDirectory);

/// <summary>
/// Gets the settings from a directory.
/// </summary>
/// <param name="directoryPath"></param>
/// <returns></returns>
Task<SettingsFile> GetSettingsFromDirectoryAsync(string directoryPath, CancellationToken cancellationToken);

/// <summary>
/// Gets the settings from a stream.
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
Task<SettingsFile> GetSettingsFromStreamAsync(Stream stream);

/// <summary>
/// Writes the settings file to a directory.
/// </summary>
/// <param name="directoryPath"></param>
/// <param name="openApiDocument">OpenApi document</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task WriteSettingsFileAsync(string directoryPath, OpenApiDocument openApiDocument, CancellationToken cancellationToken);
}
71 changes: 71 additions & 0 deletions src/Kiota.Builder/Settings/SettingsFile.cs
Original file line number Diff line number Diff line change
@@ -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 = "";
}
}
12 changes: 12 additions & 0 deletions src/Kiota.Builder/Settings/SettingsFileGenerationContext.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
60 changes: 60 additions & 0 deletions src/Kiota.Builder/Settings/SettingsFileManagementService.cs
Original file line number Diff line number Diff line change
@@ -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<SettingsFile> GetSettingsFromDirectoryAsync(string directoryPath, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task<SettingsFile> 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);
}
}
6 changes: 3 additions & 3 deletions src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Kiota.Builder.Writers.Http;
public class CodeClassDeclarationWriter(HttpConventionService conventionService) : CodeProprietableBlockDeclarationWriter<ClassDeclaration>(conventionService)
{
private const string BaseUrlPropertyName = "url";
private const string BaseUrlPropertyName = "hostAddress";

protected override void WriteTypeDeclaration(ClassDeclaration codeElement, LanguageWriter writer)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit a96a7e8

Please sign in to comment.