Skip to content

Commit

Permalink
Merge pull request #893 from christianhelle/kiota-multiple-files-output
Browse files Browse the repository at this point in the history
Add support for generating multiples using Microsoft Kiota
  • Loading branch information
christianhelle authored May 8, 2024
2 parents d188aa6 + 952ab46 commit 910578b
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 32 deletions.
Binary file added images/kiota-options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions src/CLI/ApiClientCodeGen.CLI/Commands/CSharp/KiotaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using Rapicgen.Core.Generators.Kiota;
using Rapicgen.Core.Installer;
using Rapicgen.Core.Logging;
using Rapicgen.Core.Options.Kiota;

namespace Rapicgen.CLI.Commands.CSharp;

[Command("kiota", Description = "Microsoft Kiota (v1.14.0)")]
public class KiotaCommand : CodeGeneratorCommand
public class KiotaCommand : CodeGeneratorCommand, IKiotaOptions
{
private readonly IProcessLauncher processLauncher;
private readonly IDependencyInstaller dependencyInstaller;
Expand All @@ -29,5 +30,12 @@ public override ICodeGenerator CreateGenerator() =>
SwaggerFile,
DefaultNamespace,
processLauncher,
dependencyInstaller);
dependencyInstaller,
this);

[Option(
ShortName = "m",
LongName = "generate-multiple-files",
Description = "Set this to TRUE to generate multiple files (default: FALSE)")]
public bool GenerateMultipleFiles { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,29 @@
using System.IO;
using Rapicgen.Core.Installer;
using Rapicgen.Core.Logging;
using Rapicgen.Core.Options.Kiota;

namespace Rapicgen.Core.Generators.Kiota;

public class KiotaCodeGenerator : ICodeGenerator
public class KiotaCodeGenerator(
string swaggerFile,
string defaultNamespace,
IProcessLauncher processLauncher,
IDependencyInstaller dependencyInstaller,
IKiotaOptions options) : ICodeGenerator
{
private readonly string swaggerFile;
private readonly string defaultNamespace;
private readonly IProcessLauncher processLauncher;
private readonly IDependencyInstaller dependencyInstaller;

public KiotaCodeGenerator(
string swaggerFile,
string defaultNamespace,
IProcessLauncher processLauncher,
IDependencyInstaller dependencyInstaller)
{
this.swaggerFile = swaggerFile;
this.defaultNamespace = defaultNamespace;
this.processLauncher = processLauncher;
this.dependencyInstaller = dependencyInstaller;
}

public string GenerateCode(IProgressReporter? pGenerateProgress)
{
pGenerateProgress?.Progress(10);
dependencyInstaller.InstallKiota();

pGenerateProgress?.Progress(30);
var outputFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(outputFolder);
string outputFolder = options.GenerateMultipleFiles
? Path.GetDirectoryName(swaggerFile)
: Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);

pGenerateProgress?.Progress(40);
const string command = "kiota";
Expand All @@ -41,9 +34,17 @@ public string GenerateCode(IProgressReporter? pGenerateProgress)
context.Succeeded();

pGenerateProgress?.Progress(80);
var output = CSharpFileMerger.MergeFiles(outputFolder);
string output = string.Empty;

if (!options.GenerateMultipleFiles)
{
output = CSharpFileMerger.MergeFiles(outputFolder);
}

pGenerateProgress?.Progress(100);
return GeneratedCode.PrefixAutogeneratedCodeHeader(output, "Kiota", "v1.14.0");
return GeneratedCode.PrefixAutogeneratedCodeHeader(
output,
"Kiota",
"v1.14.0");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Rapicgen.Core.Options.Kiota
{
public class DefaultKiotaOptions : IKiotaOptions
{
public bool GenerateMultipleFiles => false;
}
}
7 changes: 7 additions & 0 deletions src/Core/ApiClientCodeGen.Core/Options/Kiota/IKiotaOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Rapicgen.Core.Options.Kiota
{
public interface IKiotaOptions
{
bool GenerateMultipleFiles { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Rapicgen.Core.Generators;
using Rapicgen.Core.Generators.Kiota;
using Rapicgen.Core.Installer;
using Rapicgen.Core.Options.Kiota;
using Xunit;

namespace ApiClientCodeGen.Tests.Common.Fixtures
Expand All @@ -26,7 +27,8 @@ protected override async Task OnInitializeAsync()
new DependencyInstaller(
new NpmInstaller(new ProcessLauncher()),
new FileDownloader(new WebDownloader()),
new ProcessLauncher()));
new ProcessLauncher()),
new DefaultKiotaOptions());

Code = codeGenerator.GenerateCode(ProgressReporterMock.Object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Rapicgen.Core.Generators;
using Rapicgen.Core.Generators.Kiota;
using Rapicgen.Core.Installer;
using Rapicgen.Core.Options.Kiota;
using Xunit;

namespace ApiClientCodeGen.Tests.Common.Fixtures.OpenApi3
Expand All @@ -16,7 +17,7 @@ public class KiotaCodeGeneratorFixture : TestWithResources

public string Code { get; private set; }

protected override async Task OnInitializeAsync()
protected override Task OnInitializeAsync()
{
const string defaultNamespace = "GeneratedCode";
var codeGenerator = new KiotaCodeGenerator(
Expand All @@ -26,9 +27,11 @@ protected override async Task OnInitializeAsync()
new DependencyInstaller(
new NpmInstaller(new ProcessLauncher()),
new FileDownloader(new WebDownloader()),
new ProcessLauncher()));
new ProcessLauncher()),
new DefaultKiotaOptions());

Code = codeGenerator.GenerateCode(ProgressReporterMock.Object);
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Rapicgen.Core.Generators;
using Rapicgen.Core.Generators.Kiota;
using Rapicgen.Core.Installer;
using Rapicgen.Core.Options.Kiota;
using Xunit;

namespace ApiClientCodeGen.Tests.Common.Fixtures.OpenApi3.Yaml
Expand All @@ -26,7 +27,8 @@ protected override async Task OnInitializeAsync()
new DependencyInstaller(
new NpmInstaller(new ProcessLauncher()),
new FileDownloader(new WebDownloader()),
new ProcessLauncher()));
new ProcessLauncher()),
new DefaultKiotaOptions());

Code = codeGenerator.GenerateCode(ProgressReporterMock.Object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Rapicgen.Core.Generators;
using Rapicgen.Core.Generators.Kiota;
using Rapicgen.Core.Installer;
using Rapicgen.Core.Options.Kiota;
using Xunit;

namespace ApiClientCodeGen.Tests.Common.Fixtures.Yaml
Expand All @@ -16,7 +17,7 @@ public class KiotaCodeGeneratorFixture : TestWithResources

public string Code { get; private set; }

protected override async Task OnInitializeAsync()
protected override Task OnInitializeAsync()
{
const string defaultNamespace = "GeneratedCode";
var codeGenerator = new KiotaCodeGenerator(
Expand All @@ -26,9 +27,11 @@ protected override async Task OnInitializeAsync()
new DependencyInstaller(
new NpmInstaller(new ProcessLauncher()),
new FileDownloader(new WebDownloader()),
new ProcessLauncher()));
new ProcessLauncher()),
new DefaultKiotaOptions());

Code = codeGenerator.GenerateCode(ProgressReporterMock.Object);
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<Compile Include="$(MSBuildThisFileDirectory)Options\General\GeneralOptionPage.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Options\Kiota\KiotaOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Options\Kiota\KiotaOptionsPage.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Options\NSwagStudio\NSwagStudioOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Options\NSwagStudio\NSwagStudioOptionsPage.cs">
<SubType>Component</SubType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
using Rapicgen.Core.Options;
using Rapicgen.Core.Options.AutoRest;
using Rapicgen.Core.Options.General;
using Rapicgen.Core.Options.Kiota;
using Rapicgen.Core.Options.NSwag;
using Rapicgen.Core.Options.OpenApiGenerator;
using Rapicgen.Core.Options.Refitter;
using Rapicgen.Options;
using Rapicgen.Options.AutoRest;
using Rapicgen.Options.General;
using Rapicgen.Options.Kiota;
using Rapicgen.Options.NSwag;
using Rapicgen.Options.OpenApiGenerator;
using Rapicgen.Options.Refitter;
Expand Down Expand Up @@ -102,7 +104,8 @@ public ICodeGenerator Create(
inputFilePath,
defaultNamespace,
processLauncher,
dependencyInstaller);
dependencyInstaller,
optionsFactory.Create<IKiotaOptions, KiotaOptionsPage, DefaultKiotaOptions>());

case SupportedCodeGenerator.Refitter:
return new RefitterCodeGenerator(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Rapicgen.Core.Logging;
using Rapicgen.Core.Options.Kiota;

namespace Rapicgen.Options.Kiota
{
public class KiotaOptions
: OptionsBase<IKiotaOptions, KiotaOptionsPage>, IKiotaOptions
{
public KiotaOptions(IKiotaOptions? options = null)
{
try
{
options ??= GetFromDialogPage();
GenerateMultipleFiles = options.GenerateMultipleFiles;
}
catch (Exception e)
{
Logger.Instance.TrackError(e);

Logger.Instance.WriteLine(Environment.NewLine);
Logger.Instance.WriteLine("Error reading user options. Reverting to default values");
Logger.Instance.WriteLine("GenerateMultipleFiles = false");

GenerateMultipleFiles = false;
}
}

public bool GenerateMultipleFiles { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Rapicgen.Core.Options.Kiota;
using Microsoft.VisualStudio.Shell;
using System.Runtime.InteropServices;

namespace Rapicgen.Options.Kiota
{
[ExcludeFromCodeCoverage]
[ComVisible(true)]
public class KiotaOptionsPage : DialogPage, IKiotaOptions
{
public const string Name = "Kiota";

[Category(Name)]
[DisplayName("Generate Multiple Files")]
[Description("Generate multiple files for each operation. This only works for SDK style projects")]
public bool GenerateMultipleFiles { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/VSIX/ApiClientCodeGen.VSIX.Shared/VsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Task = System.Threading.Tasks.Task;
using Rapicgen.Options.Refitter;
using Rapicgen.Commands.Refitter;
using Rapicgen.Options.Kiota;

namespace Rapicgen
{
Expand Down Expand Up @@ -94,6 +95,13 @@ namespace Rapicgen
0,
0,
true)]
[ProvideOptionPage(
typeof(KiotaOptionsPage),
VsixName,
KiotaOptionsPage.Name,
0,
0,
true)]
public sealed class VsPackage : AsyncPackage
{
public const string VsixName = "REST API Client Code Generator";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Rapicgen.Core.Generators;
using Rapicgen.Core.Generators.Kiota;
using Rapicgen.Core.Installer;
using Rapicgen.Core.Options.Kiota;

namespace ApiClientCodeGen.VSMac.CustomTools.Kiota
{
Expand All @@ -18,6 +19,7 @@ protected override ICodeGenerator GetCodeGenerator(
new DependencyInstaller(
new NpmInstaller(new ProcessLauncher()),
new FileDownloader(new WebDownloader()),
new ProcessLauncher()));
new ProcessLauncher()),
new DefaultKiotaOptions());
}
}

0 comments on commit 910578b

Please sign in to comment.