Skip to content

Commit

Permalink
Add Build command and unknown API behavior flags
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Nov 26, 2024
1 parent 5c70c16 commit 3aeb4de
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 5 deletions.
66 changes: 66 additions & 0 deletions containers/dotnet/MakeItSo/BuildCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 System.Diagnostics;

namespace MakeItSo;

internal class BuildCommand : ICommand
{
private readonly string _api;
private readonly string _outputRoot;
private readonly UnknownApiBehavior _unknownApiBehavior;

public BuildCommand(string api, string outputRoot, UnknownApiBehavior unknownApiBehavior)
{
_api = api;
_outputRoot = outputRoot;
_unknownApiBehavior = unknownApiBehavior;
}

public void Execute()
{
var apiCatalogJson = File.ReadAllText(Path.Combine(_outputRoot, "apis", "apis.json"));
var apiCatalog = JsonConvert.DeserializeObject<ApiCatalog>(apiCatalogJson)!;
var api = apiCatalog.Apis.FirstOrDefault(api => api.ProtoPath == _api);
if (api is null)
{
switch (_unknownApiBehavior)
{
case UnknownApiBehavior.Create:
throw new InvalidOperationException($"Create for unknown API {_api} is not supported by the build command");
case UnknownApiBehavior.Error:
throw new InvalidOperationException($"No API configured with proto path {_api}, and unknown API behavior is 'error'");
case UnknownApiBehavior.Ignore:
return;
default:
throw new InvalidOperationException($"Unsupported unknown API behavior: {_unknownApiBehavior}");
}
}

var psi = new ProcessStartInfo
{
FileName = "/bin/bash",
ArgumentList = { "./build.sh", api.Id },
WorkingDirectory = _outputRoot
};
var process = Process.Start(psi)!;
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception($"Generation ended with exit code {process.ExitCode}");
}
}
}
4 changes: 3 additions & 1 deletion containers/dotnet/MakeItSo/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ internal class CreateCommand : ICommand
private readonly string _apiRoot;
private readonly string _api;
private readonly string _outputRoot;
private readonly UnknownApiBehavior _unknownApiBehavior;

public CreateCommand(string apiRoot, string api, string outputRoot)
public CreateCommand(string apiRoot, string api, string outputRoot, UnknownApiBehavior unknownApiBehavior)
{
_apiRoot = apiRoot;
_api = api;
_outputRoot = outputRoot;
_unknownApiBehavior = unknownApiBehavior;
}

public void Execute() => throw new NotImplementedException();
Expand Down
17 changes: 15 additions & 2 deletions containers/dotnet/MakeItSo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@
return 1;
}

UnknownApiBehavior unknownApiBehavior = namedArgs.GetValueOrDefault("unknown", "error") switch
{
"error" => UnknownApiBehavior.Error,
"ignore" => UnknownApiBehavior.Ignore,
"create" => UnknownApiBehavior.Create,
_ => throw new ArgumentException($"Invalid value for --unknown: '{namedArgs["unknown"]}'")
};

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"]),
"create" => new CreateCommand(namedArgs["api-root"], namedArgs["api"], namedArgs["output-root"], unknownApiBehavior),
"update" => new UpdateCommand(namedArgs["api-root"], namedArgs["api"], namedArgs["output-root"], unknownApiBehavior),
"build" => new BuildCommand(namedArgs["api"], namedArgs["output-root"], unknownApiBehavior),
_ => null
};
}
Expand Down Expand Up @@ -87,4 +96,8 @@ 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");
Console.WriteLine("--command=build --api=relative-path-to-api --output-root=path-to-dotnet-repo");
Console.WriteLine();
Console.WriteLine("Common optional arguments:");
Console.WriteLine("--unknown=error|ignore|create");
}
8 changes: 8 additions & 0 deletions containers/dotnet/MakeItSo/UnknownApiBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MakeItSo;

internal enum UnknownApiBehavior
{
Create,
Ignore,
Error
}
16 changes: 14 additions & 2 deletions containers/dotnet/MakeItSo/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ internal class UpdateCommand : ICommand
private readonly string _apiRoot;
private readonly string _api;
private readonly string _outputRoot;
private readonly UnknownApiBehavior _unknownApiBehavior;

public UpdateCommand(string apiRoot, string api, string outputRoot)
public UpdateCommand(string apiRoot, string api, string outputRoot, UnknownApiBehavior unknownApiBehavior)
{
_apiRoot = apiRoot;
_api = api;
_outputRoot = outputRoot;
_unknownApiBehavior = unknownApiBehavior;
}

public void Execute()
Expand All @@ -38,7 +40,17 @@ public void Execute()
var api = apiCatalog.Apis.FirstOrDefault(api => api.ProtoPath == _api);
if (api is null)
{
throw new Exception($"No API configured with proto path {_api}");
switch (_unknownApiBehavior)
{
case UnknownApiBehavior.Create:
throw new NotImplementedException($"Create for unknown API {_api} is not yet supported");
case UnknownApiBehavior.Error:
throw new InvalidOperationException($"No API configured with proto path {_api}, and unknown API behavior is 'error'");
case UnknownApiBehavior.Ignore:
return;
default:
throw new InvalidOperationException($"Unsupported unknown API behavior: {_unknownApiBehavior}");
}
}

var psi = new ProcessStartInfo
Expand Down

0 comments on commit 3aeb4de

Please sign in to comment.