|
10 | 10 |
|
11 | 11 | namespace Xamarin.Messaging.Build {
|
12 | 12 | internal class TaskRunner : ITaskRunner {
|
13 |
| - ITaskSerializer serializer; |
14 |
| - List<Type> tasks = new List<Type> (); |
| 13 | + static readonly ITracer tracer = Tracer.Get<TaskRunner> (); |
| 14 | + |
| 15 | + readonly ITaskSerializer serializer; |
| 16 | + readonly List<Type> tasks = new List<Type> (); |
15 | 17 |
|
16 | 18 | internal TaskRunner (ITaskSerializer serializer)
|
17 | 19 | {
|
18 | 20 | this.serializer = serializer;
|
19 |
| - |
20 |
| - var sdkRootPath = Path.Combine (MessagingContext.GetXmaPath (), "SDKs"); |
21 |
| - var dotnetPath = Path.Combine (sdkRootPath, "dotnet", "dotnet"); |
22 |
| - |
23 |
| - if (File.Exists (dotnetPath)) { |
24 |
| - Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_HOME", Path.Combine (sdkRootPath, ".home")); |
25 |
| - } else { |
26 |
| - //In case the XMA dotnet has not been installed yet |
27 |
| - dotnetPath = "/usr/local/share/dotnet/dotnet"; |
28 |
| - } |
29 |
| - |
30 |
| - Environment.SetEnvironmentVariable ("DOTNET_HOST_PATH", dotnetPath); |
| 21 | + SetDotNetVariables(); |
31 | 22 | }
|
32 | 23 |
|
33 | 24 | internal IEnumerable<Type> Tasks => tasks.AsReadOnly ();
|
@@ -57,5 +48,68 @@ public ExecuteTaskResult Execute (string taskName, string inputs)
|
57 | 48 |
|
58 | 49 | return result;
|
59 | 50 | }
|
| 51 | + |
| 52 | + void SetDotNetVariables() |
| 53 | + { |
| 54 | + var xmaSdkRootPath = Path.Combine (MessagingContext.GetXmaPath (), "SDKs"); |
| 55 | + var xmaDotNetRootPath = Path.Combine (xmaSdkRootPath, "dotnet"); |
| 56 | + var xmaDotNetPath = default(string); |
| 57 | + |
| 58 | + if (IsValidDotNetInstallation(xmaDotNetRootPath)) { |
| 59 | + //If the XMA dotnet is already installed, we use it and also declare a custom home for it (for NuGet restore and caches) |
| 60 | + Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_HOME", Path.Combine (xmaSdkRootPath, ".home")); |
| 61 | + xmaDotNetPath = GetDotNetPath(xmaDotNetRootPath); |
| 62 | + } else { |
| 63 | + //In case the XMA dotnet has not been installed yet, we use the default dotnet installation |
| 64 | + xmaDotNetPath = GetDefaultDotNetPath(); |
| 65 | + xmaDotNetRootPath = Path.GetDirectoryName (xmaDotNetPath); |
| 66 | + } |
| 67 | + |
| 68 | + var pathContent = GetPathContent(); |
| 69 | + //We add the XMA dotnet path first so it has priority over the default dotnet installation |
| 70 | + var newPathContent = $"{xmaDotNetRootPath}:{pathContent}"; |
| 71 | + |
| 72 | + //Override the PATH with the XMA dotnet in it, just in case it's used internally by dotnet |
| 73 | + Environment.SetEnvironmentVariable ("PATH", newPathContent); |
| 74 | + //Deprecated dotnet environment variable. We still preserve ir for backwards compatibility with other components that haven't deprecated it yet (like dotnet ILLink) |
| 75 | + Environment.SetEnvironmentVariable ("DOTNET_HOST_PATH", xmaDotNetPath); |
| 76 | + //Custom environment variable for internal iOS SDK usage |
| 77 | + Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_PATH", xmaDotNetPath); |
| 78 | + |
| 79 | + tracer.Info ($"Using dotnet: {xmaDotNetPath}"); |
| 80 | + } |
| 81 | + |
| 82 | + string GetDefaultDotNetPath() |
| 83 | + { |
| 84 | + var dotnetRootPath = "/usr/local/share/dotnet"; |
| 85 | + |
| 86 | + if (IsValidDotNetInstallation(dotnetRootPath)) { |
| 87 | + return GetDotNetPath(dotnetRootPath); |
| 88 | + } |
| 89 | + |
| 90 | + var pathContent = GetPathContent(); |
| 91 | + var pathElements = pathContent.Split(':', StringSplitOptions.RemoveEmptyEntries); |
| 92 | + |
| 93 | + foreach (var pathElement in pathElements) |
| 94 | + { |
| 95 | + try |
| 96 | + { |
| 97 | + if (IsValidDotNetInstallation(pathElement)) |
| 98 | + { |
| 99 | + return GetDotNetPath(pathElement); |
| 100 | + } |
| 101 | + } |
| 102 | + catch |
| 103 | + { |
| 104 | + //If we can't read a directory for any reason just skip it |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + string GetPathContent() => Environment.GetEnvironmentVariable("PATH") ?? ""; |
| 110 | + |
| 111 | + bool IsValidDotNetInstallation(string rootPath) => File.Exists (GetDotNetPath(rootPath)); |
| 112 | + |
| 113 | + string GetDotNetPath(string rootPath) => Path.Combine (rootPath, "dotnet"); |
60 | 114 | }
|
61 | 115 | }
|
0 commit comments