xref: /aosp_15_r20/external/grpc-grpc/src/csharp/Grpc.Tools/Common.cs (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1*cc02d7e2SAndroid Build Coastguard Worker #region Copyright notice and license
2*cc02d7e2SAndroid Build Coastguard Worker 
3*cc02d7e2SAndroid Build Coastguard Worker // Copyright 2018 gRPC authors.
4*cc02d7e2SAndroid Build Coastguard Worker //
5*cc02d7e2SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
6*cc02d7e2SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
7*cc02d7e2SAndroid Build Coastguard Worker // You may obtain a copy of the License at
8*cc02d7e2SAndroid Build Coastguard Worker //
9*cc02d7e2SAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
10*cc02d7e2SAndroid Build Coastguard Worker //
11*cc02d7e2SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
12*cc02d7e2SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
13*cc02d7e2SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*cc02d7e2SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
15*cc02d7e2SAndroid Build Coastguard Worker // limitations under the License.
16*cc02d7e2SAndroid Build Coastguard Worker 
17*cc02d7e2SAndroid Build Coastguard Worker #endregion
18*cc02d7e2SAndroid Build Coastguard Worker 
19*cc02d7e2SAndroid Build Coastguard Worker using System;
20*cc02d7e2SAndroid Build Coastguard Worker using System.IO;
21*cc02d7e2SAndroid Build Coastguard Worker using System.Runtime.CompilerServices;
22*cc02d7e2SAndroid Build Coastguard Worker using System.Runtime.InteropServices;
23*cc02d7e2SAndroid Build Coastguard Worker using System.Security;
24*cc02d7e2SAndroid Build Coastguard Worker using Grpc.Core.Internal;
25*cc02d7e2SAndroid Build Coastguard Worker 
26*cc02d7e2SAndroid Build Coastguard Worker namespace Grpc.Tools
27*cc02d7e2SAndroid Build Coastguard Worker {
28*cc02d7e2SAndroid Build Coastguard Worker     // Metadata names (MSBuild item attributes) that we refer to often.
29*cc02d7e2SAndroid Build Coastguard Worker     static class Metadata
30*cc02d7e2SAndroid Build Coastguard Worker     {
31*cc02d7e2SAndroid Build Coastguard Worker         // On output dependency lists.
32*cc02d7e2SAndroid Build Coastguard Worker         public static string Source = "Source";
33*cc02d7e2SAndroid Build Coastguard Worker         // On Protobuf items.
34*cc02d7e2SAndroid Build Coastguard Worker         public static string ProtoRoot = "ProtoRoot";
35*cc02d7e2SAndroid Build Coastguard Worker         public static string OutputDir = "OutputDir";
36*cc02d7e2SAndroid Build Coastguard Worker         public static string GrpcServices = "GrpcServices";
37*cc02d7e2SAndroid Build Coastguard Worker         public static string GrpcOutputDir = "GrpcOutputDir";
38*cc02d7e2SAndroid Build Coastguard Worker     };
39*cc02d7e2SAndroid Build Coastguard Worker 
40*cc02d7e2SAndroid Build Coastguard Worker     // A few flags used to control the behavior under various platforms.
41*cc02d7e2SAndroid Build Coastguard Worker     internal static class Platform
42*cc02d7e2SAndroid Build Coastguard Worker     {
43*cc02d7e2SAndroid Build Coastguard Worker         public static readonly CommonPlatformDetection.OSKind Os = CommonPlatformDetection.GetOSKind();
44*cc02d7e2SAndroid Build Coastguard Worker 
45*cc02d7e2SAndroid Build Coastguard Worker         public static readonly CommonPlatformDetection.CpuArchitecture Cpu = CommonPlatformDetection.GetProcessArchitecture();
46*cc02d7e2SAndroid Build Coastguard Worker 
47*cc02d7e2SAndroid Build Coastguard Worker         // This is not necessarily true, but good enough. BCL lacks a per-FS
48*cc02d7e2SAndroid Build Coastguard Worker         // API to determine file case sensitivity.
49*cc02d7e2SAndroid Build Coastguard Worker         public static bool IsFsCaseInsensitive => Os == CommonPlatformDetection.OSKind.Windows;
50*cc02d7e2SAndroid Build Coastguard Worker         public static bool IsWindows => Os == CommonPlatformDetection.OSKind.Windows;
51*cc02d7e2SAndroid Build Coastguard Worker     };
52*cc02d7e2SAndroid Build Coastguard Worker 
53*cc02d7e2SAndroid Build Coastguard Worker     // Exception handling helpers.
54*cc02d7e2SAndroid Build Coastguard Worker     static class Exceptions
55*cc02d7e2SAndroid Build Coastguard Worker     {
56*cc02d7e2SAndroid Build Coastguard Worker         // Returns true iff the exception indicates an error from an I/O call. See
57*cc02d7e2SAndroid Build Coastguard Worker         // https://github.com/Microsoft/msbuild/blob/v15.4.8.50001/src/Shared/ExceptionHandling.cs#L101
58*cc02d7e2SAndroid Build Coastguard Worker         static public bool IsIoRelated(Exception ex) =>
59*cc02d7e2SAndroid Build Coastguard Worker             ex is IOException ||
60*cc02d7e2SAndroid Build Coastguard Worker             (ex is ArgumentException && !(ex is ArgumentNullException)) ||
61*cc02d7e2SAndroid Build Coastguard Worker             ex is SecurityException ||
62*cc02d7e2SAndroid Build Coastguard Worker             ex is UnauthorizedAccessException ||
63*cc02d7e2SAndroid Build Coastguard Worker             ex is NotSupportedException;
64*cc02d7e2SAndroid Build Coastguard Worker     };
65*cc02d7e2SAndroid Build Coastguard Worker 
66*cc02d7e2SAndroid Build Coastguard Worker     // String helpers.
67*cc02d7e2SAndroid Build Coastguard Worker     static class Strings
68*cc02d7e2SAndroid Build Coastguard Worker     {
69*cc02d7e2SAndroid Build Coastguard Worker         // Compare string to argument using OrdinalIgnoreCase comparison.
EqualNoCase(this string a, string b)70*cc02d7e2SAndroid Build Coastguard Worker         public static bool EqualNoCase(this string a, string b) =>
71*cc02d7e2SAndroid Build Coastguard Worker             string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
72*cc02d7e2SAndroid Build Coastguard Worker     }
73*cc02d7e2SAndroid Build Coastguard Worker }
74