Skip to content

Commit

Permalink
Merge pull request #5025 from microsoft/andrueastman/defaultIcons
Browse files Browse the repository at this point in the history
Generates default icons when not present
  • Loading branch information
baywet authored Jul 26, 2024
2 parents 1983ca4 + f3060f4 commit 44d61a4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Adds generation of default 'color.png` and `outline.png` files when generating plugins. [#4993](https://github.com/microsoft/kiota/issues/4993)

### Changed

- Fixed a bug in dotnet where CS1587 warnings are generated in generated enums with descriptions [#4957](https://github.com/microsoft/kiota/issues/4957)
Expand Down
Binary file added resources/color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/Kiota.Builder/Kiota.Builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<AssemblyOriginatorKeyFile>..\Microsoft.OpenApi.snk</AssemblyOriginatorKeyFile>
<IsTrimmable>true</IsTrimmable>
<AnalysisMode>All</AnalysisMode>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
<PropertyGroup>
<!-- the source generators emit warnings -->
Expand All @@ -49,6 +50,7 @@
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.14" />
<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.7" />
<PackageReference Include="YamlDotNet" Version="15.1.2" />
<ProjectReference Include="..\Kiota.Generated\KiotaGenerated.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Expand All @@ -58,4 +60,8 @@
<ItemGroup>
<None Include="../../README.md" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="../../resources/color.png"/>
<EmbeddedResource Include="../../resources/outline.png"/>
</ItemGroup>
</Project>
27 changes: 26 additions & 1 deletion src/Kiota.Builder/Plugins/PluginsGenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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,6 +12,7 @@
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 @@ -114,6 +116,8 @@ public async Task GenerateManifestAsync(CancellationToken cancellationToken = de
}
}

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();

Expand Down Expand Up @@ -155,6 +159,16 @@ private async Task<AppManifestModel> GetAppManifestModelAsync(string pluginFileN
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.

Expand All @@ -175,7 +189,18 @@ private async Task<AppManifestModel> GetAppManifestModelAsync(string pluginFileN

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public async Task GeneratesManifest(string inputPluginName, string expectedPlugi
Assert.True(File.Exists(Path.Combine(outputDirectory, OpenAIPluginFileName)));
Assert.True(File.Exists(Path.Combine(outputDirectory, $"{expectedPluginName.ToLower()}-openapi.yml")));
Assert.True(File.Exists(Path.Combine(outputDirectory, AppManifestFileName)));
Assert.True(File.Exists(Path.Combine(outputDirectory, "color.png")));
Assert.True(File.Exists(Path.Combine(outputDirectory, "outline.png")));

// Validate the v2 plugin
var manifestContent = await File.ReadAllTextAsync(Path.Combine(outputDirectory, $"{expectedPluginName.ToLower()}-apiplugin.json"));
Expand Down Expand Up @@ -507,6 +509,8 @@ public async Task GeneratesManifestAndUpdatesExistingAppManifest()

Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
Assert.True(File.Exists(Path.Combine(outputDirectory, OpenApiFileName)));
Assert.False(File.Exists(Path.Combine(outputDirectory, "color.png"))); // manifest already existed and specifed the path to a file, so we did not add it.
Assert.False(File.Exists(Path.Combine(outputDirectory, "outline.png")));// manifest already existed and specifed the path to a file, so we did not add it.
Assert.True(File.Exists(Path.Combine(outputDirectory, "manifest.json")));// Assert manifest exists after generation

// Validate the manifest file
Expand Down Expand Up @@ -640,6 +644,8 @@ public async Task GeneratesManifestAndUpdatesExistingAppManifestWithExistingPlug
Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
Assert.True(File.Exists(Path.Combine(outputDirectory, OpenApiFileName)));
Assert.True(File.Exists(Path.Combine(outputDirectory, "manifest.json")));// Assert manifest exists after generation
Assert.False(File.Exists(Path.Combine(outputDirectory, "color.png"))); // manifest already existed and specifed the path to a file, so we did not add it.
Assert.False(File.Exists(Path.Combine(outputDirectory, "outline.png")));// manifest already existed and specifed the path to a file, so we did not add it.

// Validate the manifest file
var appManifestFile = await File.ReadAllTextAsync(Path.Combine(outputDirectory, AppManifestFileName));
Expand Down Expand Up @@ -745,6 +751,8 @@ public async Task GeneratesManifestAndCleansUpInputDescription()

Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
Assert.True(File.Exists(Path.Combine(outputDirectory, OpenApiFileName)));
Assert.True(File.Exists(Path.Combine(outputDirectory, "color.png")));
Assert.True(File.Exists(Path.Combine(outputDirectory, "outline.png")));

// Validate the v2 plugin
var manifestContent = await File.ReadAllTextAsync(Path.Combine(outputDirectory, ManifestFileName));
Expand Down

0 comments on commit 44d61a4

Please sign in to comment.