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