Search in sources :

Example 1 with DistributionFactoryExt

use of org.jetbrains.plugins.gradle.service.project.DistributionFactoryExt 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;
}
Also used : DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) IOException(java.io.IOException) ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) IOException(java.io.IOException) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) Distribution(org.gradle.tooling.internal.consumer.Distribution) DefaultExecutorServiceFactory(org.gradle.tooling.internal.consumer.DefaultExecutorServiceFactory) File(java.io.File) DistributionFactoryExt(org.jetbrains.plugins.gradle.service.project.DistributionFactoryExt) Application(com.intellij.openapi.application.Application) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Application (com.intellij.openapi.application.Application)1 ExternalSystemException (com.intellij.openapi.externalSystem.model.ExternalSystemException)1 File (java.io.File)1 IOException (java.io.IOException)1 DefaultExecutorServiceFactory (org.gradle.tooling.internal.consumer.DefaultExecutorServiceFactory)1 DefaultGradleConnector (org.gradle.tooling.internal.consumer.DefaultGradleConnector)1 Distribution (org.gradle.tooling.internal.consumer.Distribution)1 NotNull (org.jetbrains.annotations.NotNull)1 DistributionFactoryExt (org.jetbrains.plugins.gradle.service.project.DistributionFactoryExt)1