Skip to content

Commit

Permalink
Merge branch 'main' into elinor/add-kiota-workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
ElinorW authored Aug 19, 2024
2 parents 4705d68 + 07818e5 commit 5eb1083
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 854 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed an issue where models would be missing when they had no properties and a single allOf entry. [#5014](https://github.com/microsoft/kiota/issues/5014)
- Reverts modification of responses in output openApi file when generating plugins [#4945](https://github.com/microsoft/kiota/issues/4945)
- Expand properties types with null type for Typescript. [#4993](https://github.com/microsoft/kiota-typescript/issues/1188)
- Added Collection, HashMap, Map, Objects, InputStream, BigDecimal to the list of reserved names for Java generation. [#5135](https://github.com/microsoft/kiota/issues/5135)

## [1.17.0] - 2024-08-09

Expand Down
Binary file removed resources/color.png
Binary file not shown.
Binary file removed resources/outline.png
Binary file not shown.
6 changes: 0 additions & 6 deletions src/Kiota.Builder/Kiota.Builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<AssemblyOriginatorKeyFile>..\Microsoft.OpenApi.snk</AssemblyOriginatorKeyFile>
<IsTrimmable>true</IsTrimmable>
<AnalysisMode>All</AnalysisMode>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
<PropertyGroup>
<!-- the source generators emit warnings -->
Expand All @@ -50,7 +49,6 @@
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.17" />
<PackageReference Include="Microsoft.Plugins.Manifest" Version="0.0.7-preview" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.8" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
<ProjectReference Include="..\Kiota.Generated\KiotaGenerated.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Expand All @@ -60,8 +58,4 @@
<ItemGroup>
<None Include="../../README.md" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="../../resources/color.png" />
<EmbeddedResource Include="../../resources/outline.png" />
</ItemGroup>
</Project>
181 changes: 0 additions & 181 deletions src/Kiota.Builder/Plugins/Models/AppManifestModel.cs

This file was deleted.

105 changes: 0 additions & 105 deletions src/Kiota.Builder/Plugins/PluginsGenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
Expand All @@ -11,8 +10,6 @@
using Kiota.Builder.Configuration;
using Kiota.Builder.Extensions;
using Kiota.Builder.OpenApiExtensions;
using Kiota.Builder.Plugins.Models;
using Microsoft.Extensions.FileProviders;
using Microsoft.Kiota.Abstractions.Extensions;
using Microsoft.OpenApi.ApiManifest;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -43,7 +40,6 @@ public PluginsGenerationService(OpenApiDocument document, OpenApiUrlTreeNode ope
private const string ManifestFileNameSuffix = ".json";
private const string DescriptionPathSuffix = "openapi.yml";
private const string OpenAIManifestFileName = "openai-plugins";
private const string AppManifestFileName = "manifest.json";
public async Task GenerateManifestAsync(CancellationToken cancellationToken = default)
{
// 1. cleanup any namings to be used later on.
Expand Down Expand Up @@ -102,112 +98,11 @@ public async Task GenerateManifestAsync(CancellationToken cancellationToken = de
}
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
}

// 4. write the app manifest if its an Api Plugin
if (Configuration.PluginTypes.Any(static plugin => plugin == PluginType.APIPlugin))
{
var manifestFullPath = Path.Combine(Configuration.OutputPath, AppManifestFileName);
var pluginFileName = $"{Configuration.ClientClassName.ToLowerInvariant()}-{PluginType.APIPlugin.ToString().ToLowerInvariant()}{ManifestFileNameSuffix}";
var appManifestModel = await GetAppManifestModelAsync(pluginFileName, manifestFullPath, cancellationToken).ConfigureAwait(false);
#pragma warning disable CA2007
await using var appManifestStream = File.Open(manifestFullPath, FileMode.Create);
#pragma warning restore CA2007
await JsonSerializer.SerializeAsync(appManifestStream, appManifestModel, AppManifestModelGenerationContext.AppManifestModel, cancellationToken).ConfigureAwait(false);
}
}

private const string ColorFileName = "color.png";
private const string OutlineFileName = "outline.png";
[GeneratedRegex(@"[^a-zA-Z0-9_]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, 2000)]
private static partial Regex PluginNameCleanupRegex();

private async Task<AppManifestModel> GetAppManifestModelAsync(string pluginFileName, string manifestFullPath, CancellationToken cancellationToken)
{
var manifestInfo = ExtractInfoFromDocument(OAIDocument.Info);
// create default model
var manifestModel = new AppManifestModel
{
Id = Guid.NewGuid().ToString(),
Developer = new Developer
{
Name = !string.IsNullOrEmpty(OAIDocument.Info?.Contact?.Name) ? OAIDocument.Info?.Contact?.Name : "Microsoft Kiota.",
WebsiteUrl = !string.IsNullOrEmpty(OAIDocument.Info?.Contact?.Url?.OriginalString) ? OAIDocument.Info?.Contact?.Url?.OriginalString : "https://www.example.com/contact/",
PrivacyUrl = !string.IsNullOrEmpty(manifestInfo.PrivacyUrl) ? manifestInfo.PrivacyUrl : "https://www.example.com/privacy/",
TermsOfUseUrl = !string.IsNullOrEmpty(OAIDocument.Info?.TermsOfService?.OriginalString) ? OAIDocument.Info?.TermsOfService?.OriginalString : "https://www.example.com/terms/",
},
PackageName = $"com.microsoft.kiota.plugin.{Configuration.ClientClassName}",
Name = new Name
{
ShortName = Configuration.ClientClassName,
FullName = $"API Plugin {Configuration.ClientClassName} for {OAIDocument.Info?.Title.CleanupXMLString() ?? "OpenApi Document"}"
},
Description = new Description
{
ShortName = !string.IsNullOrEmpty(OAIDocument.Info?.Description.CleanupXMLString()) ? $"API Plugin for {OAIDocument.Info?.Description.CleanupXMLString()}." : OAIDocument.Info?.Title.CleanupXMLString() ?? "OpenApi Document",
FullName = !string.IsNullOrEmpty(OAIDocument.Info?.Description.CleanupXMLString()) ? $"API Plugin for {OAIDocument.Info?.Description.CleanupXMLString()}." : OAIDocument.Info?.Title.CleanupXMLString() ?? "OpenApi Document"
},
Icons = new Icons(),
AccentColor = "#FFFFFF"
};

if (File.Exists(manifestFullPath)) // No need for default, try to update the model from the file
{
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
await using var fileStream = File.OpenRead(manifestFullPath);
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
var manifestModelFromFile = await JsonSerializer.DeserializeAsync(fileStream, AppManifestModelGenerationContext.AppManifestModel, cancellationToken).ConfigureAwait(false);
if (manifestModelFromFile != null)
manifestModel = manifestModelFromFile;
}
else
{
// The manifest file did not exist, so setup any dependencies needed.
// If it already existed, the user has setup them up in another way.

// 1. Check if icons exist and write them out.
var embeddedProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly());
await CopyResourceFileToDirectoryIfNotExistsAsync(ColorFileName, embeddedProvider, cancellationToken).ConfigureAwait(false);
await CopyResourceFileToDirectoryIfNotExistsAsync(OutlineFileName, embeddedProvider, cancellationToken).ConfigureAwait(false);
}

manifestModel.CopilotExtensions ??= new CopilotExtensions();// ensure its not null.

if (manifestModel.CopilotExtensions.Plugins is not null && manifestModel.CopilotExtensions.Plugins.FirstOrDefault(pluginItem => Configuration.ClientClassName.Equals(pluginItem.Id, StringComparison.OrdinalIgnoreCase)) is { } plugin)
{
plugin.File = pluginFileName; // id is already consistent so make sure the file name is ok
}
else
{
manifestModel.CopilotExtensions.Plugins ??= [];
// Add a new plugin entry
manifestModel.CopilotExtensions.Plugins.Add(new Plugin
{
File = pluginFileName,
Id = Configuration.ClientClassName
});
}

return manifestModel;
}
private async Task CopyResourceFileToDirectoryIfNotExistsAsync(string fileName, EmbeddedFileProvider embeddedProvider, CancellationToken cancellationToken)
{
var targetPath = Path.Combine(Configuration.OutputPath, fileName);
if (!File.Exists(targetPath))
{
#pragma warning disable CA2007
await using var reader = embeddedProvider.GetFileInfo(fileName).CreateReadStream();
await using var defaultColorFile = File.Open(targetPath, FileMode.Create);
#pragma warning restore CA2007
await reader.CopyToAsync(defaultColorFile, cancellationToken).ConfigureAwait(false);
}
}
internal static readonly AppManifestModelGenerationContext AppManifestModelGenerationContext = new(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
});

private OpenApiDocument GetDocumentWithTrimmedComponentsAndResponses(OpenApiDocument doc)
{
// ensure the info and components are not null
Expand Down
6 changes: 6 additions & 0 deletions src/Kiota.Builder/Refiners/JavaReservedNamesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class JavaReservedNamesProvider : IReservedNamesProvider
"int",
"interface",
"long",
"map",
"native",
"new",
"notify",
Expand Down Expand Up @@ -68,6 +69,11 @@ public class JavaReservedNamesProvider : IReservedNamesProvider
"while",
"BaseRequestBuilder",
"MultipartBody",
"HashMap",
"Collection",
"Objects",
"InputStream",
"BigDecimal",
});
public HashSet<string> ReservedNames => _reservedNames.Value;
}
Loading

0 comments on commit 5eb1083

Please sign in to comment.