Skip to content

Commit 0f6a6ca

Browse files
committed
Added preview plugin command
1 parent fd4c768 commit 0f6a6ca

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-11
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"preLaunchTask": "build",
1212
// If you have changed target frameworks, make sure to update the program path.
1313
"program": "${workspaceFolder}/src/Microsoft.OpenApi.Hidi/bin/Debug/net7.0/Microsoft.OpenApi.Hidi.dll",
14-
"args": ["transform",
14+
"args": ["plugin",
1515
"-m","C:\\Users\\darrmi\\src\\github\\microsoft\\openapi.net\\test\\Microsoft.OpenApi.Hidi.Tests\\UtilityFiles\\exampleapimanifest.json",
16-
"-o","minimoose.yaml"],
16+
"--of","./output"],
1717
"cwd": "${workspaceFolder}/src/Microsoft.OpenApi.Hidi",
1818
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
1919
"console": "internalConsole",

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
using Microsoft.Extensions.Configuration;
3030
using Microsoft.OpenApi.Hidi.Utilities;
3131
using Microsoft.OpenApi.Hidi.Formatters;
32-
using Microsoft.OpenApi.ApiManifest;
3332
using System.Linq;
33+
using Microsoft.OpenApi.ApiManifest.OpenAI;
34+
using Microsoft.OpenApi.ApiManifest;
3435

3536
namespace Microsoft.OpenApi.Hidi
3637
{
@@ -710,6 +711,7 @@ internal async static Task PluginManifest(HidiOptions options, ILogger<OpenApiSe
710711
options.OpenApi = apiDependency.ApiDescripionUrl;
711712
}
712713

714+
713715
// Load OpenAPI document
714716
OpenApiDocument document = await GetOpenApi(options.OpenApi, options.Csdl, options.CsdlFilter, options.SettingsConfig, options.InlineExternal, logger, cancellationToken, options.MetadataVersion);
715717

@@ -718,11 +720,34 @@ internal async static Task PluginManifest(HidiOptions options, ILogger<OpenApiSe
718720
document = ApplyFilters(options, logger, apiDependency, null, document, cancellationToken);
719721
}
720722

723+
// Ensure path in options.OutputFolder exists
724+
var outputFolder = new DirectoryInfo(options.OutputFolder);
725+
if (!outputFolder.Exists)
726+
{
727+
outputFolder.Create();
728+
}
729+
var slicedOpenApi = new FileInfo(Path.Combine(options.OutputFolder,"openapi.json"));
730+
// Write OpenAPI to Output folder
731+
WriteOpenApi(slicedOpenApi, true, options.InlineLocal, options.InlineExternal, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_0, document, logger);
732+
721733
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
734+
var manifest = new OpenAIPluginManifest() {
735+
NameForHuman = document.Info.Title,
736+
DescriptionForHuman = document.Info.Description,
737+
Api = new() {
738+
Type = "openapi",
739+
Url = "./openapi.json"
740+
}
741+
};
742+
manifest.NameForModel = manifest.NameForHuman;
743+
manifest.DescriptionForModel = manifest.DescriptionForHuman;
722744

723-
724-
// Save OpenAI Plugin manifest and filtered OpenAI to output folder
725-
745+
// Write OpenAIPluginManifest to Output folder
746+
var manifestFile = new FileInfo(Path.Combine(options.OutputFolder,"ai-plugin.json"));
747+
using var file = new FileStream(manifestFile.FullName, FileMode.Create);
748+
var jsonWriter = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true });
749+
manifest.Write(jsonWriter);
750+
jsonWriter.Flush();
726751
}
727752
}
728753
}

src/Microsoft.OpenApi.Hidi/Options/CommandOptions.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ internal class CommandOptions
1111
public Option<string> OpenApiDescriptionOption = new("--openapi", "Input OpenAPI description file path or URL");
1212
public Option<string> CsdlOption = new("--csdl", "Input CSDL file path or URL");
1313
public Option<string> CsdlFilterOption = new("--csdl-filter", "Comma delimited list of EntitySets or Singletons to filter CSDL on. e.g. tasks,accounts");
14-
public Option<FileInfo> OutputOption = new("--output", "The output directory path for the generated file.") { Arity = ArgumentArity.ZeroOrOne };
14+
public Option<FileInfo> OutputOption = new("--output", "The output file path for the generated file.") { Arity = ArgumentArity.ZeroOrOne };
15+
public Option<string> OutputFolderOption = new("--output-folder", "The output directory path for the generated files.") { Arity = ArgumentArity.ZeroOrOne };
1516
public Option<bool> CleanOutputOption = new("--clean-output", "Overwrite an existing file");
1617
public Option<string> VersionOption = new("--version", "OpenAPI specification version");
1718
public Option<string> MetadataVersionOption = new("--metadata-version", "Graph metadata version to use.");
@@ -22,7 +23,7 @@ internal class CommandOptions
2223
public Option<string> FilterByOperationIdsOption = new("--filter-by-operationids", "Filters OpenApiDocument by comma delimited list of OperationId(s) provided");
2324
public Option<string> FilterByTagsOption = new("--filter-by-tags", "Filters OpenApiDocument by comma delimited list of Tag(s) provided. Also accepts a single regex.");
2425
public Option<string> FilterByCollectionOption = new("--filter-by-collection", "Filters OpenApiDocument by Postman collection provided. Provide path to collection file.");
25-
public Option<string> FilterByApiManifestOption = new("--filter-by-manifest", "Filters OpenApiDocument by API Manifest provided. Provide path to API Manifest file.");
26+
public Option<string> ManifestOption = new("--manifest", "Path to API manifest file to locate and filter an OpenApiDocument");
2627
public Option<bool> InlineLocalOption = new("--inline-local", "Inline local $ref instances");
2728
public Option<bool> InlineExternalOption = new("--inline-external", "Inline external $ref instances");
2829

@@ -32,6 +33,7 @@ public CommandOptions()
3233
CsdlOption.AddAlias("--cs");
3334
CsdlFilterOption.AddAlias("--csf");
3435
OutputOption.AddAlias("-o");
36+
OutputFolderOption.AddAlias("--of");
3537
CleanOutputOption.AddAlias("--co");
3638
VersionOption.AddAlias("-v");
3739
MetadataVersionOption.AddAlias("--mv");
@@ -42,7 +44,7 @@ public CommandOptions()
4244
FilterByOperationIdsOption.AddAlias("--op");
4345
FilterByTagsOption.AddAlias("--t");
4446
FilterByCollectionOption.AddAlias("-c");
45-
FilterByApiManifestOption.AddAlias("-m");
47+
ManifestOption.AddAlias("-m");
4648
InlineLocalOption.AddAlias("--il");
4749
InlineExternalOption.AddAlias("--ie");
4850
}
@@ -65,7 +67,6 @@ public IReadOnlyList<Option> GetAllCommandOptions()
6567
FilterByOperationIdsOption,
6668
FilterByTagsOption,
6769
FilterByCollectionOption,
68-
FilterByApiManifestOption,
6970
InlineLocalOption,
7071
InlineExternalOption
7172
};
@@ -92,5 +93,18 @@ public IReadOnlyList<Option> GetShowCommandOptions()
9293
LogLevelOption
9394
};
9495
}
96+
97+
public IReadOnlyList<Option> GetPluginCommandOptions()
98+
{
99+
return new List<Option>
100+
{
101+
102+
ManifestOption,
103+
OutputFolderOption,
104+
CleanOutputOption,
105+
LogLevelOption
106+
};
107+
}
108+
95109
}
96110
}

src/Microsoft.OpenApi.Hidi/Options/HidiOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class HidiOptions
1515
public string Csdl { get; set; }
1616
public string CsdlFilter { get; set; }
1717
public FileInfo Output { get; set; }
18+
public string OutputFolder { get; set; }
1819
public bool CleanOutput { get; set; }
1920
public string Version { get; set; }
2021
public string MetadataVersion { get; set; }
@@ -42,6 +43,7 @@ private void ParseHidiOptions(ParseResult parseResult, CommandOptions options)
4243
CsdlFilter = parseResult.GetValueForOption(options.CsdlFilterOption);
4344
Csdl = parseResult.GetValueForOption(options.CsdlOption);
4445
Output = parseResult.GetValueForOption(options.OutputOption);
46+
OutputFolder = parseResult.GetValueForOption(options.OutputFolderOption);
4547
CleanOutput = parseResult.GetValueForOption(options.CleanOutputOption);
4648
Version = parseResult.GetValueForOption(options.VersionOption);
4749
MetadataVersion = parseResult.GetValueForOption(options.MetadataVersionOption);
@@ -56,7 +58,7 @@ private void ParseHidiOptions(ParseResult parseResult, CommandOptions options)
5658
FilterByOperationIds = parseResult.GetValueForOption(options.FilterByOperationIdsOption),
5759
FilterByTags = parseResult.GetValueForOption(options.FilterByTagsOption),
5860
FilterByCollection = parseResult.GetValueForOption(options.FilterByCollectionOption),
59-
FilterByApiManifest = parseResult.GetValueForOption(options.FilterByApiManifestOption)
61+
FilterByApiManifest = parseResult.GetValueForOption(options.ManifestOption)
6062
};
6163
}
6264
}

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,29 @@ internal static RootCommand CreateRootCommand()
2727
var commandOptions = new CommandOptions();
2828

2929
var validateCommand = new Command("validate");
30+
validateCommand.Description = "Validate an OpenAPI document.";
3031
validateCommand.AddOptions(commandOptions.GetValidateCommandOptions());
3132
validateCommand.Handler = new ValidateCommandHandler(commandOptions);
3233

3334
var transformCommand = new Command("transform");
35+
transformCommand.Description = "Transform an OpenAPI or CSDL document into a JSON/YAML OpenAPI v2/v3 document.";
3436
transformCommand.AddOptions(commandOptions.GetAllCommandOptions());
3537
transformCommand.Handler = new TransformCommandHandler(commandOptions);
3638

3739
var showCommand = new Command("show");
40+
showCommand.Description = "Create a visual representation of the paths in an OpenAPI document.";
3841
showCommand.AddOptions(commandOptions.GetShowCommandOptions());
3942
showCommand.Handler = new ShowCommandHandler(commandOptions);
4043

44+
var pluginCommand = new Command("plugin");
45+
pluginCommand.Description = "Create manifest files for an OpenAI plugin [preview]";
46+
pluginCommand.AddOptions(commandOptions.GetPluginCommandOptions());
47+
pluginCommand.Handler = new PluginCommandHandler(commandOptions);
48+
4149
rootCommand.Add(showCommand);
4250
rootCommand.Add(transformCommand);
4351
rootCommand.Add(validateCommand);
52+
rootCommand.Add(pluginCommand);
4453
return rootCommand;
4554
}
4655
}

0 commit comments

Comments
 (0)