/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.tooling.internal.connection;

import org.gradle.internal.composite.GradleParticipantBuild;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.BuildIdentifier;
import org.gradle.tooling.model.ProjectIdentifier;
import org.gradle.tooling.internal.consumer.DefaultGradleConnector;

import java.io.File;
import java.util.concurrent.TimeUnit;

class ParticipantConnector {
    private final GradleParticipantBuild build;
    private final File gradleUserHome;
    private final File projectDirectory;
    private final File daemonBaseDir;
    private final Integer daemonMaxIdleTimeValue;
    private final TimeUnit daemonMaxIdleTimeUnits;

    public ParticipantConnector(GradleParticipantBuild build, File gradleUserHome, File daemonBaseDir, Integer daemonMaxIdleTimeValue, TimeUnit daemonMaxIdleTimeUnits) {
        this(build, build.getProjectDir(), gradleUserHome, daemonBaseDir, daemonMaxIdleTimeValue, daemonMaxIdleTimeUnits);
    }

    private ParticipantConnector(GradleParticipantBuild build, File projectDirectory, File gradleUserHome, File daemonBaseDir, Integer daemonMaxIdleTimeValue, TimeUnit daemonMaxIdleTimeUnits) {
        this.build = build;
        this.projectDirectory = projectDirectory;
        this.gradleUserHome = gradleUserHome;
        this.daemonBaseDir = daemonBaseDir;
        this.daemonMaxIdleTimeValue = daemonMaxIdleTimeValue;
        this.daemonMaxIdleTimeUnits = daemonMaxIdleTimeUnits;
    }

    public BuildIdentifier toBuildIdentifier() {
        return new DefaultBuildIdentifier(build.getProjectDir());
    }

    public ProjectIdentifier toProjectIdentifier(String projectPath) {
        return new DefaultProjectIdentifier(build.getProjectDir(), projectPath);
    }

    public ProjectConnection connect() {
        return connector().forProjectDirectory(projectDirectory).connect();
    }

    private GradleConnector connector() {
        DefaultGradleConnector connector = (DefaultGradleConnector) GradleConnector.newConnector();
        connector.useGradleUserHomeDir(gradleUserHome);
        if (daemonBaseDir != null) {
            connector.daemonBaseDir(daemonBaseDir);
        }
        if (daemonMaxIdleTimeValue != null) {
            connector.daemonMaxIdleTime(daemonMaxIdleTimeValue, daemonMaxIdleTimeUnits);
        }
        if (isRoot()) {
            connector.searchUpwards(false);
        }
        configureDistribution(connector);
        return connector;
    }

    private boolean isRoot() {
        return build.getProjectDir().equals(projectDirectory);
    }

    private void configureDistribution(GradleConnector connector) {
        if (build.getGradleDistribution() == null) {
            if (build.getGradleHome() == null) {
                if (build.getGradleVersion() == null) {
                    connector.useBuildDistribution();
                } else {
                    connector.useGradleVersion(build.getGradleVersion());
                }
            } else {
                connector.useInstallation(build.getGradleHome());
            }
        } else {
            connector.useDistribution(build.getGradleDistribution());
        }
    }

}

@@ -1157,49 +1157,8 @@
 me;%0A
-    private final File projectDirectory;%0A
     
@@ -1479,396 +1479,22 @@
 this
-(build, build.getProjectDir(), gradleUserHome, daemonBaseDir, daemonMaxIdleTimeValue, daemonMaxIdleTimeUnits);%0A    %7D%0A%0A    private ParticipantConnector(GradleParticipantBuild build, File projectDirectory, File gradleUserHome, File daemonBaseDir, Integer daemonMaxIdleTimeValue, TimeUnit daemonMaxIdleTimeUnits) %7B%0A        this.build = build;%0A        this.projectDirectory = projectDirectory
+.build = build
 ;%0A  
@@ -2081,17 +2081,26 @@
 rectory(
-p
+build.getP
 rojectDi
@@ -2100,22 +2100,18 @@
 ojectDir
-ectory
+()
 ).connec
@@ -2568,36 +2568,8 @@
   %7D%0A
-        if (isRoot()) %7B%0A    
     
@@ -2604,26 +2604,16 @@
 false);%0A
-        %7D%0A
         
@@ -2683,109 +2683,8 @@
  %7D%0A%0A
-    private boolean isRoot() %7B%0A        return build.getProjectDir().equals(projectDirectory);%0A    %7D%0A%0A
     

Remove code for connecting to non-root projects in a composite participant