Skip to content

Commit c48870b

Browse files
author
John Luo
authored
gRPC ServiceReference tool (#271)
1 parent 0fb9708 commit c48870b

34 files changed

+2544
-0
lines changed

Grpc.DotNet.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "testing", "testing", "{88AC
139139
EndProject
140140
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InteropTestsNativeServer", "testassets\InteropTestsNativeServer\InteropTestsNativeServer.csproj", "{D15CAEF6-0A5D-416E-B6F7-DE75C4B1D89B}"
141141
EndProject
142+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-grpc", "src\dotnet-grpc\dotnet-grpc.csproj", "{EBD5D978-1519-45ED-AC20-EC43C7FA552C}"
143+
EndProject
144+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-grpc.Tests", "test\dotnet-grpc.Tests\dotnet-grpc.Tests.csproj", "{265CD0AA-A1BD-40B8-A1E6-B3CB5F7591B8}"
145+
EndProject
142146
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.AspNetCore.Server.ClientFactory", "src\Grpc.AspNetCore.Server.ClientFactory\Grpc.AspNetCore.Server.ClientFactory.csproj", "{BF9F5674-2BC3-446E-BBEA-3055A4A2B889}"
143147
EndProject
144148
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ticketer", "examples\Clients\Ticketer\Ticketer.csproj", "{64CC84DE-C5A1-46B9-9A1B-8CDD730239A9}"
@@ -239,6 +243,14 @@ Global
239243
{D15CAEF6-0A5D-416E-B6F7-DE75C4B1D89B}.Debug|Any CPU.Build.0 = Debug|Any CPU
240244
{D15CAEF6-0A5D-416E-B6F7-DE75C4B1D89B}.Release|Any CPU.ActiveCfg = Release|Any CPU
241245
{D15CAEF6-0A5D-416E-B6F7-DE75C4B1D89B}.Release|Any CPU.Build.0 = Release|Any CPU
246+
{EBD5D978-1519-45ED-AC20-EC43C7FA552C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
247+
{EBD5D978-1519-45ED-AC20-EC43C7FA552C}.Debug|Any CPU.Build.0 = Debug|Any CPU
248+
{EBD5D978-1519-45ED-AC20-EC43C7FA552C}.Release|Any CPU.ActiveCfg = Release|Any CPU
249+
{EBD5D978-1519-45ED-AC20-EC43C7FA552C}.Release|Any CPU.Build.0 = Release|Any CPU
250+
{265CD0AA-A1BD-40B8-A1E6-B3CB5F7591B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
251+
{265CD0AA-A1BD-40B8-A1E6-B3CB5F7591B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
252+
{265CD0AA-A1BD-40B8-A1E6-B3CB5F7591B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
253+
{265CD0AA-A1BD-40B8-A1E6-B3CB5F7591B8}.Release|Any CPU.Build.0 = Release|Any CPU
242254
{BF9F5674-2BC3-446E-BBEA-3055A4A2B889}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
243255
{BF9F5674-2BC3-446E-BBEA-3055A4A2B889}.Debug|Any CPU.Build.0 = Debug|Any CPU
244256
{BF9F5674-2BC3-446E-BBEA-3055A4A2B889}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -320,6 +332,8 @@ Global
320332
{6DC078D2-8FB1-4685-B9BE-61F1C0ED8053} = {BF1393D4-6099-4EF9-85BB-7EE6CBEB920C}
321333
{88AC787E-603C-48C3-AEBA-D90F3E2142CA} = {6DC078D2-8FB1-4685-B9BE-61F1C0ED8053}
322334
{D15CAEF6-0A5D-416E-B6F7-DE75C4B1D89B} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
335+
{EBD5D978-1519-45ED-AC20-EC43C7FA552C} = {8C62055F-8CD7-4859-9001-634D544DF2AE}
336+
{265CD0AA-A1BD-40B8-A1E6-B3CB5F7591B8} = {CECC4AE8-9C4E-4727-939B-517CC2E58D65}
323337
{BF9F5674-2BC3-446E-BBEA-3055A4A2B889} = {8C62055F-8CD7-4859-9001-634D544DF2AE}
324338
{64CC84DE-C5A1-46B9-9A1B-8CDD730239A9} = {F6E0F9D7-64E5-4C7B-A9BC-3C2AD687710B}
325339
{4306E048-9D81-44A8-8069-2C294289FC00} = {CECC4AE8-9C4E-4727-939B-517CC2E58D65}

build/dependencies.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<ConfigureAwaitCheckerAnalyzerPackageVersion>3.0.0</ConfigureAwaitCheckerAnalyzerPackageVersion>
66
<GoogleProtobufPackageVersion>3.8.0</GoogleProtobufPackageVersion>
77
<GrpcPackageVersion>1.22.0-dev201906181005</GrpcPackageVersion>
8+
<MicrosoftBuildPackageVersion>16.0.461</MicrosoftBuildPackageVersion>
9+
<MicrosoftBuildLocatorPackageVersion>1.2.2</MicrosoftBuildLocatorPackageVersion>
810
<MicrosoftAspNetCorePackageVersion>3.0.0-preview6-19265-03</MicrosoftAspNetCorePackageVersion>
911
<MicrosoftExtensionsPackageVersion>2.1.1</MicrosoftExtensionsPackageVersion>
1012
<MicrosoftExtensions30PackageVersion>3.0.0-preview6.19304.6</MicrosoftExtensions30PackageVersion>
@@ -16,5 +18,6 @@
1618
<NewtonsoftJsonPackageVersion>12.0.2</NewtonsoftJsonPackageVersion>
1719
<NunitPackageVersion>3.11.0</NunitPackageVersion>
1820
<Nunit3TestAdapterPackageVersion>3.12.0</Nunit3TestAdapterPackageVersion>
21+
<SystemCommandLineExperimentalPackageVersion>0.2.0-alpha.19272.1</SystemCommandLineExperimentalPackageVersion>
1922
</PropertyGroup>
2023
</Project>

kokoro/build_nuget.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ build/expand_dev_version.sh
3434
(cd src/Grpc.Net.Client && dotnet pack --configuration Release --output ../../artifacts)
3535
(cd src/Grpc.Net.ClientFactory && dotnet pack --configuration Release --output ../../artifacts)
3636
(cd src/Grpc.AspNetCore && dotnet pack --configuration Release --output ../../artifacts)
37+
(cd src/dotnet-grpc && dotnet pack --configuration Release --output ../../artifacts)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 Grpc.Dotnet.Cli.Internal;
23+
using Grpc.Dotnet.Cli.Options;
24+
using Grpc.Dotnet.Cli.Properties;
25+
using Microsoft.Build.Evaluation;
26+
27+
namespace Grpc.Dotnet.Cli.Commands
28+
{
29+
internal class AddFileCommand : CommandBase
30+
{
31+
public AddFileCommand(IConsole console, FileInfo? projectPath)
32+
: base(console, projectPath) { }
33+
34+
public static Command Create()
35+
{
36+
var command = new Command(
37+
name: "add-file",
38+
description: CoreStrings.AddFileCommandDescription,
39+
argument: new Argument<string[]>
40+
{
41+
Name = "files",
42+
Description = CoreStrings.AddFileCommandArgumentDescription,
43+
});
44+
45+
command.AddOption(CommonOptions.ProjectOption());
46+
command.AddOption(CommonOptions.ServiceOption());
47+
command.AddOption(CommonOptions.AdditionalImportDirsOption());
48+
command.AddOption(CommonOptions.AccessOption());
49+
50+
command.Handler = CommandHandler.Create<IConsole, FileInfo, Services, Access, string, string[]>(
51+
(console, project, services, access, additionalImportDirs, files) =>
52+
{
53+
try
54+
{
55+
var command = new AddFileCommand(console, project);
56+
command.AddFile(services, access, additionalImportDirs, files);
57+
58+
return 0;
59+
}
60+
catch (CLIToolException e)
61+
{
62+
console.LogError(e);
63+
64+
return -1;
65+
}
66+
});
67+
68+
return command;
69+
}
70+
71+
public void AddFile(Services services, Access access, string additionalImportDirs, string[] files)
72+
{
73+
var resolvedServices = ResolveServices(services);
74+
EnsureNugetPackages(resolvedServices);
75+
files = GlobReferences(files);
76+
77+
foreach (var file in files)
78+
{
79+
Console.Log(CoreStrings.LogAddFileReference, file);
80+
AddProtobufReference(resolvedServices, additionalImportDirs, access, file, string.Empty);
81+
}
82+
83+
Project.Save();
84+
}
85+
}
86+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)