diff --git a/.gitignore b/.gitignore index 6079306..cdf072a 100644 --- a/.gitignore +++ b/.gitignore @@ -598,3 +598,5 @@ fabric.properties # End of https://www.gitignore.io/api/csharp,jetbrains,visualstudio /examples/StreetlightsAPI/Properties/launchSettings.json + +*g.cs diff --git a/Saunter.sln b/Saunter.sln index 818b589..15fcbc0 100644 --- a/Saunter.sln +++ b/Saunter.sln @@ -61,12 +61,18 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncAPI.Saunter.Generator.Cli.Tests", "test\AsyncAPI.Saunter.Generator.Cli.Tests\AsyncAPI.Saunter.Generator.Cli.Tests.csproj", "{18AD0249-0436-4A26-9972-B97BA6905A54}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StreetlightsAPI.TopLevelStatement", "examples\StreetlightsAPI.TopLevelStatement\StreetlightsAPI.TopLevelStatement.csproj", "{6F6B8B03-9045-46EC-AE12-E7ADA492F9FA}" + ProjectSection(ProjectDependencies) = postProject + {A320E670-5CB0-4815-AF67-D8D09FC92A2A} = {A320E670-5CB0-4815-AF67-D8D09FC92A2A} + EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncAPI.Saunter.Generator.Build", "src\AsyncAPI.Saunter.Generator.Build\AsyncAPI.Saunter.Generator.Build.csproj", "{A320E670-5CB0-4815-AF67-D8D09FC92A2A}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncAPI.Saunter.Generator.Build.Tests", "test\AsyncAPI.Saunter.Generator.Build.Tests\AsyncAPI.Saunter.Generator.Build.Tests.csproj", "{61142B10-7B49-436E-AE32-2737658BD1E5}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StreetlightsAPI.AsyncApiSpecFirst", "examples\StreetlightsAPI.AsyncApiSpecFirst\StreetlightsAPI.AsyncApiSpecFirst.csproj", "{19A30A6D-1E91-44FD-BB5D-428D12D0160D}" + ProjectSection(ProjectDependencies) = postProject + {A320E670-5CB0-4815-AF67-D8D09FC92A2A} = {A320E670-5CB0-4815-AF67-D8D09FC92A2A} + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/examples/StreetlightsAPI.AsyncApiSpecFirst/StreetlightsAPI.AsyncApiSpecFirst.csproj b/examples/StreetlightsAPI.AsyncApiSpecFirst/StreetlightsAPI.AsyncApiSpecFirst.csproj index dab15d9..858e390 100644 --- a/examples/StreetlightsAPI.AsyncApiSpecFirst/StreetlightsAPI.AsyncApiSpecFirst.csproj +++ b/examples/StreetlightsAPI.AsyncApiSpecFirst/StreetlightsAPI.AsyncApiSpecFirst.csproj @@ -10,7 +10,8 @@ - + + @@ -45,7 +46,6 @@ - @@ -59,5 +59,9 @@ + + + + diff --git a/examples/StreetlightsAPI.AsyncApiSpecFirst/specs/streetlights.json b/examples/StreetlightsAPI.AsyncApiSpecFirst/specs/streetlights.json new file mode 100644 index 0000000..8a429cb --- /dev/null +++ b/examples/StreetlightsAPI.AsyncApiSpecFirst/specs/streetlights.json @@ -0,0 +1,94 @@ +{ + "asyncapi": "2.6.0", + "info": { + "title": "Streetlights API", + "version": "1.0.0", + "description": "The Smartylighting Streetlights API allows you to remotely manage the city lights.", + "license": { + "name": "Apache 2.0", + "url": "https://www.apache.org/licenses/LICENSE-2.0" + } + }, + "servers": { + "mosquitto": { + "url": "test.mosquitto.org", + "protocol": "mqtt" + }, + "webapi": { + "url": "localhost:5000", + "protocol": "http" + } + }, + "defaultContentType": "application/json", + "channels": { + "publish/light/measured": { + "servers": [ + "webapi" + ], + "publish": { + "operationId": "MeasureLight", + "summary": "Inform about environmental lighting conditions for a particular streetlight.", + "tags": [ + { + "name": "Light" + } + ], + "message": { + "$ref": "#/components/messages/lightMeasuredEvent" + } + } + }, + "subscribe/light/measured": { + "servers": [ + "mosquitto" + ], + "subscribe": { + "operationId": "PublishLightMeasurement", + "summary": "Subscribe to environmental lighting conditions for a particular streetlight.", + "tags": [ + { + "name": "Light" + } + ], + "message": { + "payload": { + "$ref": "#/components/schemas/lightMeasuredEvent" + } + } + } + } + }, + "components": { + "schemas": { + "lightMeasuredEvent": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int32", + "description": "Id of the streetlight." + }, + "lumens": { + "type": "integer", + "format": "int32", + "description": "Light intensity measured in lumens." + }, + "sentAt": { + "type": "string", + "format": "date-time", + "description": "Light intensity measured in lumens." + } + }, + "additionalProperties": false + } + }, + "messages": { + "lightMeasuredEvent": { + "payload": { + "$ref": "#/components/schemas/lightMeasuredEvent" + }, + "name": "lightMeasuredEvent" + } + } + } +} \ No newline at end of file diff --git a/examples/StreetlightsAPI.AsyncApiSpecFirst/specs/streetlights.yml b/examples/StreetlightsAPI.AsyncApiSpecFirst/specs/streetlights.yml new file mode 100644 index 0000000..3b6c0c9 --- /dev/null +++ b/examples/StreetlightsAPI.AsyncApiSpecFirst/specs/streetlights.yml @@ -0,0 +1,61 @@ +asyncapi: 2.6.0 +info: + title: Streetlights API 44 + version: 1.0.0 + description: The Smartylighting Streetlights API allows you to remotely manage the city lights. + license: + name: Apache 2.0 + url: https://www.apache.org/licenses/LICENSE-2.0 +servers: + mosquitto: + url: test.mosquitto.org + protocol: mqtt + webapi: + url: localhost:5000 + protocol: http +defaultContentType: application/json +channels: + publish/light/measured: + servers: + - webapi + publish: + operationId: MeasureLight + summary: Inform about environmental lighting conditions for a particular streetlight. + tags: + - name: Light + message: + $ref: '#/components/messages/lightMeasuredEvent' + subscribe/light/measured: + servers: + - mosquitto + subscribe: + operationId: PublishLightMeasurement + summary: Subscribe to environmental lighting conditions for a particular streetlight. + tags: + - name: Light + message: + payload: + $ref: '#/components/schemas/lightMeasuredEvent' +components: + schemas: + lightMeasuredEvent: + type: object + properties: + id: + type: integer + format: int32 + description: Id of the streetlight. + lumens: + type: integer + format: int32 + description: Light intensity measured in lumens. + sentAt: + type: string + format: date-time + description: Light intensity measured in lumens. + additionalProperties: false + messages: + lightMeasuredEvent: + payload: + $ref: '#/components/schemas/lightMeasuredEvent' + name: lightMeasuredEvent \ No newline at end of file diff --git a/src/AsyncAPI.Saunter.Generator.Build/build/AsyncAPI.Saunter.Generator.Build.props b/src/AsyncAPI.Saunter.Generator.Build/build/AsyncAPI.Saunter.Generator.Build.props index eebd4c5..9f8adb8 100644 --- a/src/AsyncAPI.Saunter.Generator.Build/build/AsyncAPI.Saunter.Generator.Build.props +++ b/src/AsyncAPI.Saunter.Generator.Build/build/AsyncAPI.Saunter.Generator.Build.props @@ -12,8 +12,8 @@ - - + + \ No newline at end of file diff --git a/src/AsyncAPI.Saunter.Generator.Build/build/AsyncAPI.Saunter.Generator.Build.targets b/src/AsyncAPI.Saunter.Generator.Build/build/AsyncAPI.Saunter.Generator.Build.targets index e2911d0..858b202 100644 --- a/src/AsyncAPI.Saunter.Generator.Build/build/AsyncAPI.Saunter.Generator.Build.targets +++ b/src/AsyncAPI.Saunter.Generator.Build/build/AsyncAPI.Saunter.Generator.Build.targets @@ -1,5 +1,11 @@ - + + + + + + + @@ -14,6 +20,7 @@ + @@ -39,16 +46,13 @@ WorkingDirectory="$(AsyncAPIBuildToolRoot)" /> - - - - + + Inputs="@(AsyncAPISpecs)" Outputs="@(AsyncAPISpecs->'%(DefiningProjectDirectory)%(OutputPath)/%(Filename).g.cs')"> - + - diff --git a/src/AsyncAPI.Saunter.Generator.Cli/FromSpec/FromSpecCommand.cs b/src/AsyncAPI.Saunter.Generator.Cli/FromSpec/FromSpecCommand.cs index 2b7f6be..fcb401a 100644 --- a/src/AsyncAPI.Saunter.Generator.Cli/FromSpec/FromSpecCommand.cs +++ b/src/AsyncAPI.Saunter.Generator.Cli/FromSpec/FromSpecCommand.cs @@ -3,18 +3,38 @@ using LEGO.AsyncAPI.Models; using Microsoft.Extensions.Logging; -namespace AsyncAPI.Saunter.Generator.Cli.FromSpecCommand; +namespace AsyncAPI.Saunter.Generator.Cli.FromSpec; internal class FromSpecCommand(ILogger logger) { /// /// Retrieves AsyncAPI spec from a startup assembly and writes to file. /// - /// The AsyncAPI specification to generate code for. Parameter should include the namespace: namespace,asyncapi.json + /// the AsyncAPI specification to generate code for. Parameter should include the namespace: namespace,outputPath,asyncapi.json [Command("fromspec")] public int FromSpec(params string[] specs) { - logger.LogInformation($"FromSpec(#{specs.Length}): {string.Join(';', specs)}"); + logger.LogInformation($"FromSpec(#{specs.Length}): --specs {string.Join(';', specs)}"); + + foreach (var (namespaceName, output, specName) in Split(specs)) + { + Directory.CreateDirectory(output); + var outputFile = Path.Combine(output, $"{Path.GetFileNameWithoutExtension(specName)}.g.cs"); + File.Create(outputFile); + logger.LogInformation($"Created {Path.GetFullPath(outputFile)}"); + } return 0; } + + private static IEnumerable<(string namespaceName, string output, string specName)> Split(IEnumerable input) + { + foreach (var spec in input) + { + var split = spec.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(); + if (split.Count == 3 && !split.Any(string.IsNullOrWhiteSpace)) + { + yield return (split[0], split[1], split[2]); + } + } + } } diff --git a/src/AsyncAPI.Saunter.Generator.Cli/Program.cs b/src/AsyncAPI.Saunter.Generator.Cli/Program.cs index 0766373..3c7018d 100644 --- a/src/AsyncAPI.Saunter.Generator.Cli/Program.cs +++ b/src/AsyncAPI.Saunter.Generator.Cli/Program.cs @@ -1,4 +1,4 @@ -using AsyncAPI.Saunter.Generator.Cli.FromSpecCommand; +using AsyncAPI.Saunter.Generator.Cli.FromSpec; using AsyncAPI.Saunter.Generator.Cli.ToFile; using ConsoleAppFramework; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Common.NugetPackage.props b/src/Common.NugetPackage.props index e3e5452..460f93e 100644 --- a/src/Common.NugetPackage.props +++ b/src/Common.NugetPackage.props @@ -2,7 +2,7 @@ - 999.$([System.DateTime]::Now.ToString("yy"))$([System.DateTime]::Now.DayOfYear).$([System.DateTime]::Now.ToString("HHmm")) + 999.$([System.DateTime]::Now.ToString("yy"))$([System.DateTime]::Now.DayOfYear).$([System.DateTime]::Now.ToString("HHmm.ss")) true ../../local-nuget-source