|
6 | 6 | using Microsoft.DotNet.Configurer; |
7 | 7 | using Microsoft.Extensions.EnvironmentAbstractions; |
8 | 8 |
|
9 | | -namespace Microsoft.DotNet.BuildServer |
| 9 | +namespace Microsoft.DotNet.Cli.BuildServer; |
| 10 | + |
| 11 | +internal class BuildServerProvider( |
| 12 | + IFileSystem fileSystem = null, |
| 13 | + IEnvironmentProvider environmentProvider = null, |
| 14 | + IReporter reporter = null) : IBuildServerProvider |
10 | 15 | { |
11 | | - internal class BuildServerProvider : IBuildServerProvider |
12 | | - { |
13 | | - public const string PidFileDirectoryVariableName = "DOTNET_BUILD_PIDFILE_DIRECTORY"; |
14 | | - private readonly IFileSystem _fileSystem; |
15 | | - private readonly IEnvironmentProvider _environmentProvider; |
16 | | - private readonly IReporter _reporter; |
| 16 | + public const string PidFileDirectoryVariableName = "DOTNET_BUILD_PIDFILE_DIRECTORY"; |
| 17 | + private readonly IFileSystem _fileSystem = fileSystem ?? FileSystemWrapper.Default; |
| 18 | + private readonly IEnvironmentProvider _environmentProvider = environmentProvider ?? new EnvironmentProvider(); |
| 19 | + private readonly IReporter _reporter = reporter ?? Reporter.Error; |
17 | 20 |
|
18 | | - public BuildServerProvider( |
19 | | - IFileSystem fileSystem = null, |
20 | | - IEnvironmentProvider environmentProvider = null, |
21 | | - IReporter reporter = null) |
| 21 | + public IEnumerable<IBuildServer> EnumerateBuildServers(ServerEnumerationFlags flags = ServerEnumerationFlags.All) |
| 22 | + { |
| 23 | + if ((flags & ServerEnumerationFlags.MSBuild) == ServerEnumerationFlags.MSBuild) |
22 | 24 | { |
23 | | - _fileSystem = fileSystem ?? FileSystemWrapper.Default; |
24 | | - _environmentProvider = environmentProvider ?? new EnvironmentProvider(); |
25 | | - _reporter = reporter ?? Reporter.Error; |
| 25 | + // Yield a single MSBuild server (handles server discovery itself) |
| 26 | + // TODO: use pid file enumeration when supported by the server (https://github.com/dotnet/cli/issues/9113) |
| 27 | + yield return new MSBuildServer(); |
26 | 28 | } |
27 | 29 |
|
28 | | - public IEnumerable<IBuildServer> EnumerateBuildServers(ServerEnumerationFlags flags = ServerEnumerationFlags.All) |
| 30 | + if ((flags & ServerEnumerationFlags.VBCSCompiler) == ServerEnumerationFlags.VBCSCompiler) |
29 | 31 | { |
30 | | - if ((flags & ServerEnumerationFlags.MSBuild) == ServerEnumerationFlags.MSBuild) |
31 | | - { |
32 | | - // Yield a single MSBuild server (handles server discovery itself) |
33 | | - // TODO: use pid file enumeration when supported by the server (https://github.com/dotnet/cli/issues/9113) |
34 | | - yield return new MSBuildServer(); |
35 | | - } |
36 | | - |
37 | | - if ((flags & ServerEnumerationFlags.VBCSCompiler) == ServerEnumerationFlags.VBCSCompiler) |
38 | | - { |
39 | | - // Yield a single VB/C# compiler (handles server discovery itself) |
40 | | - // TODO: use pid file enumeration when supported by the server (https://github.com/dotnet/cli/issues/9112) |
41 | | - yield return new VBCSCompilerServer(); |
42 | | - } |
| 32 | + // Yield a single VB/C# compiler (handles server discovery itself) |
| 33 | + // TODO: use pid file enumeration when supported by the server (https://github.com/dotnet/cli/issues/9112) |
| 34 | + yield return new VBCSCompilerServer(); |
| 35 | + } |
43 | 36 |
|
44 | | - // TODO: remove or amend this check when the following issues are resolved: |
45 | | - // https://github.com/dotnet/cli/issues/9112 |
46 | | - // https://github.com/dotnet/cli/issues/9113 |
47 | | - if ((flags & ServerEnumerationFlags.Razor) != ServerEnumerationFlags.Razor) |
48 | | - { |
49 | | - yield break; |
50 | | - } |
| 37 | + // TODO: remove or amend this check when the following issues are resolved: |
| 38 | + // https://github.com/dotnet/cli/issues/9112 |
| 39 | + // https://github.com/dotnet/cli/issues/9113 |
| 40 | + if ((flags & ServerEnumerationFlags.Razor) != ServerEnumerationFlags.Razor) |
| 41 | + { |
| 42 | + yield break; |
| 43 | + } |
51 | 44 |
|
52 | | - var directory = GetPidFileDirectory(); |
| 45 | + var directory = GetPidFileDirectory(); |
53 | 46 |
|
54 | | - if (!_fileSystem.Directory.Exists(directory.Value)) |
55 | | - { |
56 | | - yield break; |
57 | | - } |
| 47 | + if (!_fileSystem.Directory.Exists(directory.Value)) |
| 48 | + { |
| 49 | + yield break; |
| 50 | + } |
58 | 51 |
|
59 | | - foreach (var path in _fileSystem.Directory.EnumerateFiles(directory.Value)) |
| 52 | + foreach (var path in _fileSystem.Directory.EnumerateFiles(directory.Value)) |
| 53 | + { |
| 54 | + if ((flags & ServerEnumerationFlags.Razor) == ServerEnumerationFlags.Razor && |
| 55 | + Path.GetFileName(path).StartsWith(RazorPidFile.FilePrefix)) |
60 | 56 | { |
61 | | - if ((flags & ServerEnumerationFlags.Razor) == ServerEnumerationFlags.Razor && |
62 | | - Path.GetFileName(path).StartsWith(RazorPidFile.FilePrefix)) |
| 57 | + var file = ReadRazorPidFile(new FilePath(path)); |
| 58 | + if (file != null) |
63 | 59 | { |
64 | | - var file = ReadRazorPidFile(new FilePath(path)); |
65 | | - if (file != null) |
66 | | - { |
67 | | - yield return new RazorServer(file); |
68 | | - } |
| 60 | + yield return new RazorServer(file); |
69 | 61 | } |
70 | 62 | } |
71 | 63 | } |
| 64 | + } |
72 | 65 |
|
73 | | - public DirectoryPath GetPidFileDirectory() |
| 66 | + public DirectoryPath GetPidFileDirectory() |
| 67 | + { |
| 68 | + var directory = _environmentProvider.GetEnvironmentVariable(PidFileDirectoryVariableName); |
| 69 | + if (!string.IsNullOrEmpty(directory)) |
74 | 70 | { |
75 | | - var directory = _environmentProvider.GetEnvironmentVariable(PidFileDirectoryVariableName); |
76 | | - if (!string.IsNullOrEmpty(directory)) |
77 | | - { |
78 | | - return new DirectoryPath(directory); |
79 | | - } |
80 | | - |
81 | | - return new DirectoryPath( |
82 | | - Path.Combine( |
83 | | - CliFolderPathCalculator.DotnetUserProfileFolderPath, |
84 | | - "pids", |
85 | | - "build")); |
| 71 | + return new DirectoryPath(directory); |
86 | 72 | } |
87 | 73 |
|
88 | | - private RazorPidFile ReadRazorPidFile(FilePath path) |
| 74 | + return new DirectoryPath( |
| 75 | + Path.Combine( |
| 76 | + CliFolderPathCalculator.DotnetUserProfileFolderPath, |
| 77 | + "pids", |
| 78 | + "build")); |
| 79 | + } |
| 80 | + |
| 81 | + private RazorPidFile ReadRazorPidFile(FilePath path) |
| 82 | + { |
| 83 | + try |
89 | 84 | { |
90 | | - try |
91 | | - { |
92 | | - return RazorPidFile.Read(path, _fileSystem); |
93 | | - } |
94 | | - catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException) |
95 | | - { |
96 | | - _reporter.WriteLine( |
97 | | - string.Format( |
98 | | - LocalizableStrings.FailedToReadPidFile, |
99 | | - path.Value, |
100 | | - ex.Message).Yellow()); |
101 | | - return null; |
102 | | - } |
| 85 | + return RazorPidFile.Read(path, _fileSystem); |
| 86 | + } |
| 87 | + catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException) |
| 88 | + { |
| 89 | + _reporter.WriteLine( |
| 90 | + string.Format( |
| 91 | + LocalizableStrings.FailedToReadPidFile, |
| 92 | + path.Value, |
| 93 | + ex.Message).Yellow()); |
| 94 | + return null; |
103 | 95 | } |
104 | 96 | } |
105 | 97 | } |
0 commit comments