|
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The gRPC Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#endregion |
| 18 | + |
| 19 | +using System.CommandLine; |
| 20 | +using System.CommandLine.Invocation; |
| 21 | +using System.IO; |
| 22 | +using System.Net.Http; |
| 23 | +using System.Threading.Tasks; |
| 24 | +using Grpc.Dotnet.Cli.Internal; |
| 25 | +using Grpc.Dotnet.Cli.Options; |
| 26 | +using Grpc.Dotnet.Cli.Properties; |
| 27 | + |
| 28 | +namespace Grpc.Dotnet.Cli.Commands |
| 29 | +{ |
| 30 | + internal class AddUrlCommand : CommandBase |
| 31 | + { |
| 32 | + public AddUrlCommand(IConsole console, FileInfo? projectPath) |
| 33 | + : base(console, projectPath) { } |
| 34 | + |
| 35 | + // Internal for testing |
| 36 | + internal AddUrlCommand(IConsole console, HttpClient client) |
| 37 | + : base(console, client) { } |
| 38 | + |
| 39 | + public static Command Create() |
| 40 | + { |
| 41 | + var command = new Command( |
| 42 | + name: "add-url", |
| 43 | + description: CoreStrings.AddUrlCommandDescription, |
| 44 | + argument: new Argument<string> |
| 45 | + { |
| 46 | + Name = "url", |
| 47 | + Description = CoreStrings.AddUrlCommandArgumentDescription, |
| 48 | + Arity = ArgumentArity.ExactlyOne |
| 49 | + }); |
| 50 | + |
| 51 | + command.AddOption(new Option( |
| 52 | + aliases: new[] { "-o", "--output" }, |
| 53 | + description: CoreStrings.OutputOptionDescription, |
| 54 | + argument: new Argument<string> { Name = "path", Arity = ArgumentArity.ExactlyOne })); |
| 55 | + command.AddOption(CommonOptions.ProjectOption()); |
| 56 | + command.AddOption(CommonOptions.ServiceOption()); |
| 57 | + command.AddOption(CommonOptions.AdditionalImportDirsOption()); |
| 58 | + command.AddOption(CommonOptions.AccessOption()); |
| 59 | + |
| 60 | + command.Handler = CommandHandler.Create<IConsole, FileInfo, Services, Access, string, string, string>( |
| 61 | + async (console, project, services, access, additionalImportDirs, url, output) => |
| 62 | + { |
| 63 | + try |
| 64 | + { |
| 65 | + if (string.IsNullOrEmpty(output)) |
| 66 | + { |
| 67 | + throw new CLIToolException(CoreStrings.ErrorNoOutputProvided); |
| 68 | + } |
| 69 | + |
| 70 | + var command = new AddUrlCommand(console, project); |
| 71 | + await command.AddUrlAsync(services, access, additionalImportDirs, url, output); |
| 72 | + |
| 73 | + return 0; |
| 74 | + } |
| 75 | + catch (CLIToolException e) |
| 76 | + { |
| 77 | + console.LogError(e); |
| 78 | + |
| 79 | + return -1; |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + return command; |
| 84 | + } |
| 85 | + |
| 86 | + public async Task AddUrlAsync(Services services, Access access, string additionalImportDirs, string url, string output) |
| 87 | + { |
| 88 | + var resolvedServices = ResolveServices(services); |
| 89 | + EnsureNugetPackages(resolvedServices); |
| 90 | + |
| 91 | + if (!IsUrl(url)) |
| 92 | + { |
| 93 | + throw new CLIToolException(CoreStrings.ErrorReferenceNotUrl); |
| 94 | + } |
| 95 | + |
| 96 | + await DownloadFileAsync(url, output); |
| 97 | + |
| 98 | + Console.Log(CoreStrings.LogAddUrlReference, output, url); |
| 99 | + AddProtobufReference(resolvedServices, additionalImportDirs, access, output, url); |
| 100 | + |
| 101 | + Project.Save(); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments