Skip to content

Commit 3a0f3af

Browse files
Create MonoAndroidHelper.Nullable.cs
1 parent 2f264e5 commit 3a0f3af

File tree

2 files changed

+88
-71
lines changed

2 files changed

+88
-71
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#nullable enable
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq;
7+
using Xamarin.Android.Tools;
8+
9+
#if MSBUILD
10+
using Microsoft.Android.Build.Tasks;
11+
using Microsoft.Build.Framework;
12+
using Microsoft.Build.Utilities;
13+
#endif
14+
15+
namespace Xamarin.Android.Tasks;
16+
17+
public partial class MonoAndroidHelper
18+
{
19+
public static string GetExecutablePath (string? dir, string exe)
20+
{
21+
if (dir is not { Length: > 0 })
22+
return exe;
23+
foreach (var e in Executables (exe))
24+
if (File.Exists (Path.Combine (dir, e)))
25+
return e;
26+
return exe;
27+
}
28+
29+
public static IEnumerable<string> Executables (string executable)
30+
{
31+
var pathExt = Environment.GetEnvironmentVariable ("PATHEXT");
32+
var pathExts = pathExt?.Split (new char [] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
33+
34+
if (pathExts is not null) {
35+
foreach (var ext in pathExts)
36+
yield return Path.ChangeExtension (executable, ext);
37+
}
38+
yield return executable;
39+
}
40+
41+
public static JdkInfo? GetJdkInfo (Action<TraceLevel, string> logger, string? javaSdkPath, Version minSupportedVersion, Version maxSupportedVersion)
42+
{
43+
JdkInfo? info = null;
44+
try {
45+
info = new JdkInfo (javaSdkPath ?? "", logger:logger);
46+
} catch {
47+
info = JdkInfo.GetKnownSystemJdkInfos (logger)
48+
.Where (jdk => jdk.Version >= minSupportedVersion && jdk.Version <= maxSupportedVersion)
49+
.FirstOrDefault ();
50+
}
51+
return info;
52+
}
53+
54+
#if MSBUILD
55+
public static void RefreshAndroidSdk (string? sdkPath, string? ndkPath, string? javaPath, TaskLoggingHelper? logHelper = null)
56+
{
57+
Action<TraceLevel, string> logger = (level, value) => {
58+
var log = logHelper;
59+
switch (level) {
60+
case TraceLevel.Error:
61+
if (log == null)
62+
Console.Error.Write (value);
63+
else
64+
log.LogCodedError ("XA5300", "{0}", value);
65+
break;
66+
case TraceLevel.Warning:
67+
if (log == null)
68+
Console.WriteLine (value);
69+
else
70+
log.LogCodedWarning ("XA5300", "{0}", value);
71+
break;
72+
default:
73+
if (log == null)
74+
Console.WriteLine (value);
75+
else
76+
log.LogDebugMessage ("{0}", value);
77+
break;
78+
}
79+
};
80+
AndroidSdk = new AndroidSdkInfo (logger, sdkPath, ndkPath, javaPath);
81+
}
82+
83+
public static void RefreshSupportedVersions (string[]? referenceAssemblyPaths)
84+
{
85+
SupportedVersions = new AndroidVersions (referenceAssemblyPaths ?? []);
86+
}
87+
#endif // MSBUILD
88+
}

src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -179,54 +179,6 @@ internal static string GetOSLibPath ()
179179
return Path.Combine (toolsDir, "lib", $"host-{uname.Value}");
180180
}
181181

182-
#if MSBUILD
183-
public static void RefreshAndroidSdk (string sdkPath, string ndkPath, string javaPath, TaskLoggingHelper logHelper = null)
184-
{
185-
Action<TraceLevel, string> logger = (level, value) => {
186-
var log = logHelper;
187-
switch (level) {
188-
case TraceLevel.Error:
189-
if (log == null)
190-
Console.Error.Write (value);
191-
else
192-
log.LogCodedError ("XA5300", "{0}", value);
193-
break;
194-
case TraceLevel.Warning:
195-
if (log == null)
196-
Console.WriteLine (value);
197-
else
198-
log.LogCodedWarning ("XA5300", "{0}", value);
199-
break;
200-
default:
201-
if (log == null)
202-
Console.WriteLine (value);
203-
else
204-
log.LogDebugMessage ("{0}", value);
205-
break;
206-
}
207-
};
208-
AndroidSdk = new AndroidSdkInfo (logger, sdkPath, ndkPath, javaPath);
209-
}
210-
211-
public static void RefreshSupportedVersions (string[] referenceAssemblyPaths)
212-
{
213-
SupportedVersions = new AndroidVersions (referenceAssemblyPaths);
214-
}
215-
#endif // MSBUILD
216-
217-
public static JdkInfo GetJdkInfo (Action<TraceLevel, string> logger, string javaSdkPath, Version minSupportedVersion, Version maxSupportedVersion)
218-
{
219-
JdkInfo info = null;
220-
try {
221-
info = new JdkInfo (javaSdkPath, logger:logger);
222-
} catch {
223-
info = JdkInfo.GetKnownSystemJdkInfos (logger)
224-
.Where (jdk => jdk.Version >= minSupportedVersion && jdk.Version <= maxSupportedVersion)
225-
.FirstOrDefault ();
226-
}
227-
return info;
228-
}
229-
230182
class SizeAndContentFileComparer : IEqualityComparer<FileInfo>
231183
#if MSBUILD
232184
, IEqualityComparer<ITaskItem>
@@ -512,29 +464,6 @@ public static string [] GetProguardEnvironmentVaribles (string proguardHome)
512464
new string [] { proguardHomeVariable, "JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8" };
513465
}
514466

515-
public static string GetExecutablePath (string dir, string exe)
516-
{
517-
if (string.IsNullOrEmpty (dir))
518-
return exe;
519-
foreach (var e in Executables (exe))
520-
if (File.Exists (Path.Combine (dir, e)))
521-
return e;
522-
return exe;
523-
}
524-
525-
public static IEnumerable<string> Executables (string executable)
526-
{
527-
var pathExt = Environment.GetEnvironmentVariable ("PATHEXT");
528-
var pathExts = pathExt?.Split (new char [] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
529-
530-
if (pathExts != null) {
531-
foreach (var ext in pathExts)
532-
yield return Path.ChangeExtension (executable, ext);
533-
}
534-
yield return executable;
535-
}
536-
537-
538467
#if MSBUILD
539468
public static string TryGetAndroidJarPath (TaskLoggingHelper log, string platform, bool designTimeBuild = false, bool buildingInsideVisualStudio = false, string targetFramework = "", string androidSdkDirectory = "")
540469
{

0 commit comments

Comments
 (0)