Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverts changes in #4914 and #4993 #5185

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@ -81,7 +77,7 @@
pluginDocument.Write(writer);
break;
case PluginType.APIManifest:
var apiManifest = new ApiManifestDocument("application"); //TODO add application name

Check warning on line 80 in src/Kiota.Builder/Plugins/PluginsGenerationService.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)

Check warning on line 80 in src/Kiota.Builder/Plugins/PluginsGenerationService.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
// pass empty config hash so that its not included in this manifest.
apiManifest.ApiDependencies.AddOrReplace(Configuration.ClientClassName, Configuration.ToApiDependency(string.Empty, TreeNode?.GetRequestInfo().ToDictionary(static x => x.Key, static x => x.Value) ?? [], WorkingDirectory));
var publisherName = string.IsNullOrEmpty(OAIDocument.Info?.Contact?.Name)
Expand All @@ -102,112 +98,11 @@
}
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 Expand Up @@ -267,7 +162,7 @@
Schema = "https://aka.ms/json-schemas/copilot-extensions/v2.1/plugin.schema.json",
SchemaVersion = "v2.1",
NameForHuman = OAIDocument.Info?.Title.CleanupXMLString(),
// TODO name for model ???

Check warning on line 165 in src/Kiota.Builder/Plugins/PluginsGenerationService.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)

Check warning on line 165 in src/Kiota.Builder/Plugins/PluginsGenerationService.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
DescriptionForHuman = descriptionForHuman,
DescriptionForModel = manifestInfo.DescriptionForModel ?? descriptionForHuman,
ContactEmail = manifestInfo.ContactEmail,
Expand Down
Loading
Loading