Debian Codegraph @

src/csharp/Grpc.Tools/Common.cs

raw
1 #region Copyright notice and license
2
3 // Copyright 2018 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;
20 using System.IO;
21 using System.Runtime.CompilerServices;
22 using System.Runtime.InteropServices;
23 using System.Security;
24 using Grpc.Core.Internal;
25
26 namespace Grpc.Tools
27 {
28 // Metadata names (MSBuild item attributes) that we refer to often.
29 static class Metadata
30 {
31 // On output dependency lists.
32 public static string Source = "Source";
33 // On Protobuf items.
34 public static string ProtoRoot = "ProtoRoot";
35 public static string OutputDir = "OutputDir";
36 public static string GrpcServices = "GrpcServices";
37 public static string GrpcOutputDir = "GrpcOutputDir";
38 };
39
40 // A few flags used to control the behavior under various platforms.
41 internal static class Platform
42 {
43 public static readonly CommonPlatformDetection.OSKind Os = CommonPlatformDetection.GetOSKind();
44
45 public static readonly CommonPlatformDetection.CpuArchitecture Cpu = CommonPlatformDetection.GetProcessArchitecture();
46
47 // This is not necessarily true, but good enough. BCL lacks a per-FS
48 // API to determine file case sensitivity.
49 public static bool IsFsCaseInsensitive => Os == CommonPlatformDetection.OSKind.Windows;
50 public static bool IsWindows => Os == CommonPlatformDetection.OSKind.Windows;
51 };
52
53 // Exception handling helpers.
54 static class Exceptions
55 {
56 // Returns true iff the exception indicates an error from an I/O call. See
57 // https://github.com/Microsoft/msbuild/blob/v15.4.8.50001/src/Shared/ExceptionHandling.cs#L101
58 static public bool IsIoRelated(Exception ex) =>
59 ex is IOException ||
60 (ex is ArgumentException && !(ex is ArgumentNullException)) ||
61 ex is SecurityException ||
62 ex is UnauthorizedAccessException ||
63 ex is NotSupportedException;
64 };
65
66 // String helpers.
67 static class Strings
68 {
69 // Compare string to argument using OrdinalIgnoreCase comparison.
70 public static bool EqualNoCase(this string a, string b) =>
71 string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
72 }
73 }