From 7655b2dab577be490c2c78433da9d921e29c80c2 Mon Sep 17 00:00:00 2001 From: Julie Qiu Date: Mon, 25 Nov 2024 14:55:29 -0500 Subject: [PATCH] impl(containers/dotnet): add Copied container from https://github.com/jskeet/google-cloud-dotnet/tree/docker-generate/picard --- containers/dotnet/Dockerfile | 37 +++++++++ containers/dotnet/MakeItSo/ApiCatalog.cs | 31 +++++++ containers/dotnet/MakeItSo/ApiMetadata.cs | 22 +++++ containers/dotnet/MakeItSo/CreateCommand.cs | 31 +++++++ containers/dotnet/MakeItSo/ICommand.cs | 20 +++++ containers/dotnet/MakeItSo/MakeItSo.csproj | 14 ++++ containers/dotnet/MakeItSo/Program.cs | 90 +++++++++++++++++++++ containers/dotnet/MakeItSo/UpdateCommand.cs | 58 +++++++++++++ 8 files changed, 303 insertions(+) create mode 100644 containers/dotnet/Dockerfile create mode 100644 containers/dotnet/MakeItSo/ApiCatalog.cs create mode 100644 containers/dotnet/MakeItSo/ApiMetadata.cs create mode 100644 containers/dotnet/MakeItSo/CreateCommand.cs create mode 100644 containers/dotnet/MakeItSo/ICommand.cs create mode 100644 containers/dotnet/MakeItSo/MakeItSo.csproj create mode 100644 containers/dotnet/MakeItSo/Program.cs create mode 100644 containers/dotnet/MakeItSo/UpdateCommand.cs diff --git a/containers/dotnet/Dockerfile b/containers/dotnet/Dockerfile new file mode 100644 index 0000000..fc6a17a --- /dev/null +++ b/containers/dotnet/Dockerfile @@ -0,0 +1,37 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM mcr.microsoft.com/dotnet/sdk:6.0.414-jammy AS build + +# TODO: We're currently bundling MakeItSo into the Docker image +# itself. This avoids having to make assumptions based on the +# command-line arguments, but leaves scope for inconsistency. At the +# moment, nothing *else* is in the Docker image... we could +# potentially include "everything needed for an unconfigured API" in +# the image, so that we could generate (but probably not build) an +# arbitrary library. + +WORKDIR /src +COPY . ./ +RUN dotnet build MakeItSo/MakeItSo.csproj -c Release + +FROM mcr.microsoft.com/dotnet/sdk:6.0.414-jammy + +# Additional tooling required to regenerate libraries and project files. +RUN apt-get update +RUN apt-get install -y unzip + +WORKDIR /app +COPY --from=build /src/MakeItSo/bin/Release/net6.0 . +ENTRYPOINT ["dotnet", "MakeItSo.dll"] diff --git a/containers/dotnet/MakeItSo/ApiCatalog.cs b/containers/dotnet/MakeItSo/ApiCatalog.cs new file mode 100644 index 0000000..e59ca6a --- /dev/null +++ b/containers/dotnet/MakeItSo/ApiCatalog.cs @@ -0,0 +1,31 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace MakeItSo; + +/// +/// // A very cut-down version of the code in Google.Cloud.Tools.Common. +/// +internal class ApiCatalog +{ + /// + /// The APIs within the catalog. + /// + public List Apis { get; set; } = null!; + + /// + /// Proto paths for APIs we knowingly don't generate. The values are the reasons for not generating. + /// + public Dictionary IgnoredPaths { get; set; } = null!; +} diff --git a/containers/dotnet/MakeItSo/ApiMetadata.cs b/containers/dotnet/MakeItSo/ApiMetadata.cs new file mode 100644 index 0000000..de8b2ab --- /dev/null +++ b/containers/dotnet/MakeItSo/ApiMetadata.cs @@ -0,0 +1,22 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"): +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace MakeItSo; + +// A very cut-down version of the code in Google.Cloud.Tools.Common. +internal class ApiMetadata +{ + public string Id { get; set; } = null!; + public string ProtoPath { get; set; } = null!; +} diff --git a/containers/dotnet/MakeItSo/CreateCommand.cs b/containers/dotnet/MakeItSo/CreateCommand.cs new file mode 100644 index 0000000..8c90564 --- /dev/null +++ b/containers/dotnet/MakeItSo/CreateCommand.cs @@ -0,0 +1,31 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"): +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace MakeItSo; + +internal class CreateCommand : ICommand +{ + private readonly string _apiRoot; + private readonly string _api; + private readonly string _outputRoot; + + public CreateCommand(string apiRoot, string api, string outputRoot) + { + _apiRoot = apiRoot; + _api = api; + _outputRoot = outputRoot; + } + + public void Execute() => throw new NotImplementedException(); +} diff --git a/containers/dotnet/MakeItSo/ICommand.cs b/containers/dotnet/MakeItSo/ICommand.cs new file mode 100644 index 0000000..420436d --- /dev/null +++ b/containers/dotnet/MakeItSo/ICommand.cs @@ -0,0 +1,20 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"): +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace MakeItSo; + +internal interface ICommand +{ + void Execute(); +} diff --git a/containers/dotnet/MakeItSo/MakeItSo.csproj b/containers/dotnet/MakeItSo/MakeItSo.csproj new file mode 100644 index 0000000..7d211a9 --- /dev/null +++ b/containers/dotnet/MakeItSo/MakeItSo.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/containers/dotnet/MakeItSo/Program.cs b/containers/dotnet/MakeItSo/Program.cs new file mode 100644 index 0000000..cac2a17 --- /dev/null +++ b/containers/dotnet/MakeItSo/Program.cs @@ -0,0 +1,90 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"). +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// Quick and dirty prototype for an entry point for containerized generation. + +using MakeItSo; + +var namedArgs = new Dictionary(); + +foreach (var arg in args) +{ + if (!arg.StartsWith("--")) + { + Console.WriteLine($"Invalid argument: {arg}"); + return 1; + } + else + { + var split = arg.Split('=', 2); + if (split.Length != 2) + { + Console.WriteLine($"Invalid argument: {arg}"); + return 1; + } + namedArgs[split[0][2..]] = split[1]; + } +} + +var commandName = namedArgs.GetValueOrDefault("command"); +if (commandName is null) +{ + Console.WriteLine("No command specified."); + ShowHelp(); + return 1; +} + +ICommand? command; +try +{ + command = namedArgs["command"] switch + { + "create" => new CreateCommand(namedArgs["api-root"], namedArgs["api"], namedArgs["output-root"]), + "update" => new UpdateCommand(namedArgs["api-root"], namedArgs["api"], namedArgs["output-root"]), + _ => null + }; +} +catch (KeyNotFoundException ex) +{ + Console.WriteLine(ex.Message); + ShowHelp(); + return 1; +} + +if (command is null) +{ + Console.WriteLine($"Unknown command: {namedArgs["command"]}"); + ShowHelp(); + return 1; +} + +try +{ + command.Execute(); +} +catch (Exception e) +{ + Console.WriteLine($"Error executing command: {e}"); + return 1; +} + +return 0; + +void ShowHelp() +{ + Console.WriteLine("Valid commands:"); + Console.WriteLine("--command=update --api-root=path-to-apis --api=relative-path-to-api --output-root=path-to-dotnet-repo"); + Console.WriteLine("--command=create --api-root=path-to-apis --api=relative-path-to-api --output-root=path-to-dotnet-repo"); +} diff --git a/containers/dotnet/MakeItSo/UpdateCommand.cs b/containers/dotnet/MakeItSo/UpdateCommand.cs new file mode 100644 index 0000000..38351be --- /dev/null +++ b/containers/dotnet/MakeItSo/UpdateCommand.cs @@ -0,0 +1,58 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"): +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Diagnostics; + +namespace MakeItSo; + +internal class UpdateCommand : ICommand +{ + private readonly string _apiRoot; + private readonly string _api; + private readonly string _outputRoot; + + public UpdateCommand(string apiRoot, string api, string outputRoot) + { + _apiRoot = apiRoot; + _api = api; + _outputRoot = outputRoot; + } + + public void Execute() + { + var apiCatalogJson = File.ReadAllText(Path.Combine(_outputRoot, "apis", "apis.json")); + var apiCatalog = JsonConvert.DeserializeObject(apiCatalogJson)!; + var api = apiCatalog.Apis.FirstOrDefault(api => api.ProtoPath == _api); + if (api is null) + { + throw new Exception($"No API configured with proto path {_api}"); + } + + var psi = new ProcessStartInfo + { + FileName = "/bin/bash", + ArgumentList = { "./generateapis.sh", api.Id }, + WorkingDirectory = _outputRoot, + EnvironmentVariables = { { "GOOGLEAPIS_DIR", _apiRoot } } + }; + var process = Process.Start(psi)!; + process.WaitForExit(); + if (process.ExitCode != 0) + { + throw new Exception($"Generation ended with exit code {process.ExitCode}"); + } + } +}