use of org.gradle.tooling.internal.consumer.DefaultExecutorServiceFactory in project intellij-community by JetBrains.
the class GradleExecutionHelper method getConnection.
/**
* Allows to retrieve gradle api connection to use for the given project.
*
* @param projectPath target project path
* @param settings execution settings to use
* @return connection to use
* @throws IllegalStateException if it's not possible to create the connection
*/
@NotNull
private static ProjectConnection getConnection(@NotNull String projectPath, @Nullable GradleExecutionSettings settings) throws IllegalStateException {
File projectDir = new File(projectPath);
GradleConnector connector = GradleConnector.newConnector();
int ttl = -1;
if (settings != null) {
//noinspection EnumSwitchStatementWhichMissesCases
switch(settings.getDistributionType()) {
case LOCAL:
String gradleHome = settings.getGradleHome();
if (gradleHome != null) {
try {
// There were problems with symbolic links processing at the gradle side.
connector.useInstallation(new File(gradleHome).getCanonicalFile());
} catch (IOException e) {
connector.useInstallation(new File(settings.getGradleHome()));
}
}
break;
case WRAPPED:
if (settings.getWrapperPropertyFile() != null) {
File propertiesFile = new File(settings.getWrapperPropertyFile());
if (propertiesFile.exists()) {
Distribution distribution = new DistributionFactoryExt(new DefaultExecutorServiceFactory()).getWrappedDistribution(propertiesFile);
try {
setField(connector, "distribution", distribution);
} catch (Exception e) {
throw new ExternalSystemException(e);
}
}
}
break;
}
// Setup service directory if necessary.
String serviceDirectory = settings.getServiceDirectory();
if (serviceDirectory != null) {
connector.useGradleUserHomeDir(new File(serviceDirectory));
}
// Setup logging if necessary.
if (settings.isVerboseProcessing() && connector instanceof DefaultGradleConnector) {
((DefaultGradleConnector) connector).setVerboseLogging(true);
}
ttl = (int) settings.getRemoteProcessIdleTtlInMs();
}
if (ttl > 0 && connector instanceof DefaultGradleConnector) {
// do not spawn gradle daemons during test execution
final Application app = ApplicationManager.getApplication();
ttl = (app != null && app.isUnitTestMode()) ? 10000 : ttl;
((DefaultGradleConnector) connector).daemonMaxIdleTime(ttl, TimeUnit.MILLISECONDS);
}
connector.forProjectDirectory(projectDir);
ProjectConnection connection = connector.connect();
if (connection == null) {
throw new IllegalStateException(String.format("Can't create connection to the target project via gradle tooling api. Project path: '%s'", projectPath));
}
return connection;
}
Aggregations