Gradle.cs
4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor.Android;
using UnityEngine;
namespace UnityEditor.XR.ARCore
{
static class Gradle
{
public const string gradleLauncherPrefix = "gradle-launcher-";
public static bool TryGetFullPathToGradleLauncher(out string path, out string diagnosticMessage)
{
#if UNITY_ANDROID
path = AndroidExternalToolsSettings.gradlePath;
if (string.IsNullOrEmpty(path))
{
diagnosticMessage = "No Gradle path set.";
return false;
}
path = Directory.EnumerateFiles(Path.Combine(path, "lib"), $"{gradleLauncherPrefix}*.jar").FirstOrDefault();
if (string.IsNullOrEmpty(path))
{
diagnosticMessage = $"Could not find '{gradleLauncherPrefix}*.jar'";
return false;
}
diagnosticMessage = null;
return true;
#else
(path, diagnosticMessage) = (null, "Cannot get path to the Gradle launcher unless the active build platform is Android.");
return false;
#endif
}
public static bool TryParseVersion(string input, out Version gradleVersion, out string diagnosticMessage)
{
var matches = Regex.Matches(input, @"\d+(\.\d+)+");
if (matches.Count == 0)
{
gradleVersion = null;
diagnosticMessage = $"Could not determine Gradle version from '{input}'.";
return false;
}
var versionString = matches[0].Value;
if (Version.TryParse(versionString, out gradleVersion))
{
diagnosticMessage = null;
return true;
}
(gradleVersion, diagnosticMessage) = (null, $"Could not parse Gradle version from '{versionString}'.");
return false;
}
/// <summary>
/// Attempts to determine the Gradle version.
/// </summary>
/// <remarks>
/// This method tries to get the Gradle version method with a fallback. First, it tries to get the path to the
/// "gradle launcher", which is expected to be in the form `gradle-launcher-*.jar`. If such a file does not
/// exist, then it cannot retrieve the version. If it does exist, then the `*` should be the version number,
/// e.g., `gradle-launcher-5.6.4.jar`. If it cannot parse the version number from the filename, then it will
/// attempt to execute it with the argument `--version` and parse the output.
/// </remarks>
/// <param name="gradleVersion">The gradle version, or `null` if it could not be determined.</param>
/// <param name="diagnosticMessage">If <paramref name="gradleVersion"/> is `null`, this will contain a
/// diagnostic message explaining why.</param>
/// <returns>Returns `true` if the Gradle version could be parsed. Returns `false` otherwise.</returns>
public static bool TryGetVersion(out Version gradleVersion, out string diagnosticMessage)
{
// Get the path to the gradle-launcher-*.jar file
if (!TryGetFullPathToGradleLauncher(out var pathToGradleLauncher, out diagnosticMessage))
{
gradleVersion = null;
return false;
}
// Try to determine the version from the filename alone
if (TryParseVersion(Path.GetFileNameWithoutExtension(pathToGradleLauncher), out gradleVersion, out diagnosticMessage))
{
return true;
}
// If parsing the filename fails, execute it with the --version CLI option
if (!Java.canExecute)
{
(gradleVersion, diagnosticMessage) = (null, "No Java executable found.");
return false;
}
var (stdout, stderr, exitCode) = Java.Execute(pathToGradleLauncher, "--version");
if (exitCode != 0)
{
(gradleVersion, diagnosticMessage) = (null, $"Execution of \"{pathToGradleLauncher} --version\" failed with exit code {exitCode}:\n{stderr}");
return false;
}
// Make sure stdout contains something
if (string.IsNullOrEmpty(stdout))
{
(gradleVersion, diagnosticMessage) = (null, $"Execution of Execution of \"{pathToGradleLauncher} --version\" resulted in no output. stderr:\n{stderr}");
return false;
}
// We were able to run it successfully; parse the output. Expected output looks like this:
// ------------------------
// Gradle 5.6.4
// ------------------------
// ...
return TryParseVersion(stdout, out gradleVersion, out diagnosticMessage);
}
}
}