10
10
import java .io .IOException ;
11
11
import java .net .URI ;
12
12
import java .util .HashMap ;
13
+ import java .util .HashSet ;
13
14
import java .util .List ;
14
15
import java .util .Map ;
15
16
import java .util .Set ;
22
23
import org .gradle .tooling .BuildLauncher ;
23
24
import org .gradle .tooling .GradleConnectionException ;
24
25
import org .gradle .tooling .GradleConnector ;
26
+ import org .gradle .tooling .ModelBuilder ;
25
27
import org .gradle .tooling .ProjectConnection ;
26
28
import org .gradle .tooling .TestLauncher ;
27
29
import org .gradle .tooling .events .OperationType ;
28
30
import org .gradle .tooling .model .build .BuildEnvironment ;
31
+ import org .gradle .tooling .model .gradle .GradleBuild ;
29
32
import org .gradle .util .GradleVersion ;
30
33
31
34
import com .microsoft .java .bs .core .internal .managers .PreferenceManager ;
@@ -47,28 +50,103 @@ public class GradleApiConnector {
47
50
private final Map <File , GradleConnector > connectors ;
48
51
private final PreferenceManager preferenceManager ;
49
52
53
+ private static final String UNSUPPORTED_BUILD_ENVIRONMENT_MESSAGE =
54
+ "Could not create an instance of Tooling API implementation "
55
+ + "using the specified Gradle distribution" ;
56
+
50
57
public GradleApiConnector (PreferenceManager preferenceManager ) {
51
58
this .preferenceManager = preferenceManager ;
52
59
connectors = new HashMap <>();
53
60
}
54
61
55
62
/**
56
- * Get the Gradle version of the project.
63
+ * Extracts the GradleVersion for the given project.
64
+ *
65
+ * @param projectUri URI of the project used to fetch the gradle version.
66
+ * @return Gradle version of the project or empty string upon failure.
57
67
*/
58
68
public String getGradleVersion (URI projectUri ) {
59
69
try (ProjectConnection connection = getGradleConnector (projectUri ).connect ()) {
60
- return getGradleVersion (connection );
70
+ return getBuildEnvironment (connection ).getGradle ().getGradleVersion ();
71
+ } catch (BuildException e ) {
72
+ LOGGER .severe ("Failed to get Gradle version: " + e .getMessage ());
73
+ return "" ;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Extracts the GradleVersion for the given project connection.
79
+ *
80
+ * @param connection ProjectConnection used to fetch the gradle version.
81
+ * @return Gradle version of the project or empty string upon failure.
82
+ */
83
+ public String getGradleVersion (ProjectConnection connection ) {
84
+ try {
85
+ return getBuildEnvironment (connection ).getGradle ().getGradleVersion ();
61
86
} catch (BuildException e ) {
62
87
LOGGER .severe ("Failed to get Gradle version: " + e .getMessage ());
63
88
return "" ;
64
89
}
65
90
}
66
91
67
- private String getGradleVersion (ProjectConnection connection ) {
68
- BuildEnvironment model = connection
92
+ /**
93
+ * Extracts the BuildEnvironment model for the given project.
94
+ *
95
+ * @param projectUri URI of the project used to fetch the gradle java home.
96
+ * @return BuildEnvironment of the project or {@code null} upon failure.
97
+ */
98
+ public BuildEnvironment getBuildEnvironment (URI projectUri ) {
99
+ try (ProjectConnection connection = getGradleConnector (projectUri ).connect ()) {
100
+ return getBuildEnvironment (connection );
101
+ } catch (BuildException e ) {
102
+ LOGGER .severe ("Failed to get Build Environment: " + e .getMessage ());
103
+ return null ;
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Extracts the BuildEnvironment model for the given project.
109
+ *
110
+ * @param connection ProjectConnection used to fetch the gradle version.
111
+ * @return BuildEnvironment of the project.
112
+ */
113
+ private BuildEnvironment getBuildEnvironment (ProjectConnection connection ) {
114
+ return connection
69
115
.model (BuildEnvironment .class )
70
116
.get ();
71
- return model .getGradle ().getGradleVersion ();
117
+ }
118
+
119
+ /**
120
+ * Runs a probe build to check if the build fails due to java home incompatibility.
121
+ *
122
+ * @param projectUri URI of the project for which the check needs to be performed.
123
+ * @return true if the given project has compatible java home, false otherwise.
124
+ */
125
+ public boolean checkCompatibilityWithProbeBuild (URI projectUri ) {
126
+ try (ProjectConnection connection = getGradleConnector (projectUri ).connect ()) {
127
+ ModelBuilder <GradleBuild > modelBuilder = Utils .setLauncherProperties (
128
+ connection .model (GradleBuild .class ), preferenceManager .getPreferences ()
129
+ );
130
+ modelBuilder .get ();
131
+ return true ;
132
+ } catch (BuildException e ) {
133
+ return hasUnsupportedBuildEnvironmentMessage (e );
134
+ }
135
+ }
136
+
137
+ private boolean hasUnsupportedBuildEnvironmentMessage (BuildException e ) {
138
+ Set <Throwable > seen = new HashSet <>();
139
+ Throwable current = e ;
140
+ while (current != null ) {
141
+ if (current .getMessage ().contains (UNSUPPORTED_BUILD_ENVIRONMENT_MESSAGE )) {
142
+ return true ;
143
+ }
144
+ if (!seen .add (current )) {
145
+ break ;
146
+ }
147
+ current = current .getCause ();
148
+ }
149
+ return false ;
72
150
}
73
151
74
152
/**
@@ -152,13 +230,15 @@ public StatusCode runTasks(URI projectUri, ProgressReporter reporter, String...
152
230
/**
153
231
* request Gradle to run tests.
154
232
*/
155
- public StatusCode runTests (URI projectUri ,
233
+ public StatusCode runTests (
234
+ URI projectUri ,
156
235
Map <BuildTargetIdentifier , Map <String , Set <String >>> testClassesMethodsMap ,
157
236
List <String > jvmOptions ,
158
237
List <String > args ,
159
238
Map <String , String > envVars ,
160
239
BuildClient client , String originId ,
161
- CompileProgressReporter compileProgressReporter ) {
240
+ CompileProgressReporter compileProgressReporter
241
+ ) {
162
242
163
243
StatusCode statusCode = StatusCode .OK ;
164
244
ProgressReporter reporter = new DefaultProgressReporter (client );
0 commit comments