<commit_before>/*
 * Copyright 2011 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.provider;

import org.gradle.StartParameter;
import org.gradle.api.logging.LogLevel;
import org.gradle.initialization.GradleLauncherAction;
import org.gradle.internal.Factory;
import org.gradle.launcher.daemon.client.DaemonClient;
import org.gradle.launcher.daemon.client.DaemonClientServices;
import org.gradle.launcher.daemon.configuration.DaemonParameters;
import org.gradle.launcher.exec.BuildActionParameters;
import org.gradle.launcher.exec.GradleLauncherActionExecuter;
import org.gradle.logging.LoggingManagerInternal;
import org.gradle.logging.LoggingServiceRegistry;
import org.gradle.logging.internal.OutputEventRenderer;
import org.gradle.logging.internal.logback.SimpleLogbackLoggingConfigurer;
import org.gradle.process.internal.streams.SafeStreams;
import org.gradle.tooling.internal.build.DefaultBuildEnvironment;
import org.gradle.tooling.internal.consumer.protocoladapter.ProtocolToModelAdapter;
import org.gradle.tooling.internal.protocol.*;
import org.gradle.tooling.internal.provider.connection.AdaptedOperationParameters;
import org.gradle.tooling.internal.provider.connection.BuildLogLevelMixIn;
import org.gradle.tooling.internal.provider.connection.ProviderBuildResult;
import org.gradle.tooling.internal.provider.connection.ProviderOperationParameters;
import org.gradle.util.GUtil;
import org.gradle.util.GradleVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;

public class DefaultConnection implements InternalConnection, BuildActionRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultConnection.class);
    private final EmbeddedExecuterSupport embeddedExecuterSupport;
    private final SimpleLogbackLoggingConfigurer loggingConfigurer = new SimpleLogbackLoggingConfigurer();
    private final ProtocolToModelAdapter adapter = new ProtocolToModelAdapter();

    public DefaultConnection() {
        LOGGER.debug("Provider implementation created.");
        //embedded use of the tooling api is not supported publicly so we don't care about its thread safety
        //we can still keep this state:
        embeddedExecuterSupport = new EmbeddedExecuterSupport();
        LOGGER.debug("Embedded executer support created.");
    }

    public void configureLogging(boolean verboseLogging) {
        LogLevel providerLogLevel = verboseLogging? LogLevel.DEBUG : LogLevel.INFO;
        LOGGER.debug("Configuring logging to level: {}", providerLogLevel);
        loggingConfigurer.configure(providerLogLevel);
    }

    public ConnectionMetaDataVersion1 getMetaData() {
        return new ConnectionMetaDataVersion1() {
            public String getVersion() {
                return GradleVersion.current().getVersion();
            }

            public String getDisplayName() {
                return String.format("Gradle %s", getVersion());
            }
        };
    }

    public void stop() {
    }

    @Deprecated
    public void executeBuild(final BuildParametersVersion1 buildParameters,
                             BuildOperationParametersVersion1 operationParameters) {
        logTargetVersion();
        run(new ExecuteBuildAction(), new AdaptedOperationParameters(operationParameters, buildParameters.getTasks()));
    }

    @Deprecated
    public ProjectVersion3 getModel(Class<? extends ProjectVersion3> type, BuildOperationParametersVersion1 parameters) {
        return getTheModel(type, parameters);
    }

    @Deprecated
    public <T> T getTheModel(Class<T> type, BuildOperationParametersVersion1 parameters) {
        logTargetVersion();
        return getModel(type, new AdaptedOperationParameters(parameters));
    }

    public <T> BuildResult<T> run(Class<T> type, BuildParameters buildParameters) throws UnsupportedOperationException, IllegalStateException {
        ProviderOperationParameters providerParameters = adapter.adapt(ProviderOperationParameters.class, buildParameters, BuildLogLevelMixIn.class);
        List<String> tasks = providerParameters.getTasks();
        if (type.equals(Void.class) && tasks == null) {
            throw new IllegalArgumentException("No model type or tasks specified.");
        }

        final T result;
        if (tasks == null) {
            result = getModel(type, providerParameters);
        } else {
            if (!type.equals(Void.class)) {
                throw new UnsupportedOperationException(String.format("Don't know how to build model of type %s from the build result.", type.getSimpleName()));
            }
            run(new ExecuteBuildAction(), providerParameters);
            result = null;
        }

        return new ProviderBuildResult<T>(result);
    }

    private <T> T getModel(Class<T> type, ProviderOperationParameters parameters) {
        if (type == InternalBuildEnvironment.class) {

            //we don't really need to launch gradle to acquire information needed for BuildEnvironment
            DaemonParameters daemonParameters = init(parameters);
            DefaultBuildEnvironment out = new DefaultBuildEnvironment(
                GradleVersion.current().getVersion(),
                daemonParameters.getEffectiveJavaHome(),
                daemonParameters.getEffectiveJvmArgs());

            return type.cast(out);
        }
        DelegatingBuildModelAction<T> action = new DelegatingBuildModelAction<T>(type);
        return run(action, parameters);
    }<commit_msg>Changed tooling API provider so that when both tasks and a result type are specified, then run the build and build the result as requested. Currently, does these as two separate operations.<commit_after>/*
 * Copyright 2011 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.provider;

import org.gradle.StartParameter;
import org.gradle.api.logging.LogLevel;
import org.gradle.initialization.GradleLauncherAction;
import org.gradle.internal.Factory;
import org.gradle.launcher.daemon.client.DaemonClient;
import org.gradle.launcher.daemon.client.DaemonClientServices;
import org.gradle.launcher.daemon.configuration.DaemonParameters;
import org.gradle.launcher.exec.BuildActionParameters;
import org.gradle.launcher.exec.GradleLauncherActionExecuter;
import org.gradle.logging.LoggingManagerInternal;
import org.gradle.logging.LoggingServiceRegistry;
import org.gradle.logging.internal.OutputEventRenderer;
import org.gradle.logging.internal.logback.SimpleLogbackLoggingConfigurer;
import org.gradle.process.internal.streams.SafeStreams;
import org.gradle.tooling.internal.build.DefaultBuildEnvironment;
import org.gradle.tooling.internal.consumer.protocoladapter.ProtocolToModelAdapter;
import org.gradle.tooling.internal.protocol.*;
import org.gradle.tooling.internal.provider.connection.AdaptedOperationParameters;
import org.gradle.tooling.internal.provider.connection.BuildLogLevelMixIn;
import org.gradle.tooling.internal.provider.connection.ProviderBuildResult;
import org.gradle.tooling.internal.provider.connection.ProviderOperationParameters;
import org.gradle.util.GUtil;
import org.gradle.util.GradleVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;

public class DefaultConnection implements InternalConnection, BuildActionRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultConnection.class);
    private final EmbeddedExecuterSupport embeddedExecuterSupport;
    private final SimpleLogbackLoggingConfigurer loggingConfigurer = new SimpleLogbackLoggingConfigurer();
    private final ProtocolToModelAdapter adapter = new ProtocolToModelAdapter();

    public DefaultConnection() {
        LOGGER.debug("Provider implementation created.");
        //embedded use of the tooling api is not supported publicly so we don't care about its thread safety
        //we can still keep this state:
        embeddedExecuterSupport = new EmbeddedExecuterSupport();
        LOGGER.debug("Embedded executer support created.");
    }

    public void configureLogging(boolean verboseLogging) {
        LogLevel providerLogLevel = verboseLogging? LogLevel.DEBUG : LogLevel.INFO;
        LOGGER.debug("Configuring logging to level: {}", providerLogLevel);
        loggingConfigurer.configure(providerLogLevel);
    }

    public ConnectionMetaDataVersion1 getMetaData() {
        return new ConnectionMetaDataVersion1() {
            public String getVersion() {
                return GradleVersion.current().getVersion();
            }

            public String getDisplayName() {
                return String.format("Gradle %s", getVersion());
            }
        };
    }

    public void stop() {
    }

    @Deprecated
    public void executeBuild(final BuildParametersVersion1 buildParameters,
                             BuildOperationParametersVersion1 operationParameters) {
        logTargetVersion();
        run(new ExecuteBuildAction(), new AdaptedOperationParameters(operationParameters, buildParameters.getTasks()));
    }

    @Deprecated
    public ProjectVersion3 getModel(Class<? extends ProjectVersion3> type, BuildOperationParametersVersion1 parameters) {
        return getTheModel(type, parameters);
    }

    @Deprecated
    public <T> T getTheModel(Class<T> type, BuildOperationParametersVersion1 parameters) {
        logTargetVersion();
        return getModel(type, new AdaptedOperationParameters(parameters));
    }

    public <T> BuildResult<T> run(Class<T> type, BuildParameters buildParameters) throws UnsupportedOperationException, IllegalStateException {
        ProviderOperationParameters providerParameters = adapter.adapt(ProviderOperationParameters.class, buildParameters, BuildLogLevelMixIn.class);
        List<String> tasks = providerParameters.getTasks();
        if (type.equals(Void.class) && tasks == null) {
            throw new IllegalArgumentException("No model type or tasks specified.");
        }

        final T result;
        if (tasks != null) {
            run(new ExecuteBuildAction(), providerParameters);
        }
        if (!type.equals(Void.class)) {
            result = getModel(type, providerParameters);
        } else {
            result = null;
        }

        return new ProviderBuildResult<T>(result);
    }

    private <T> T getModel(Class<T> type, ProviderOperationParameters parameters) {
        if (type == InternalBuildEnvironment.class) {

            //we don't really need to launch gradle to acquire information needed for BuildEnvironment
            DaemonParameters daemonParameters = init(parameters);
            DefaultBuildEnvironment out = new DefaultBuildEnvironment(
                GradleVersion.current().getVersion(),
                daemonParameters.getEffectiveJavaHome(),
                daemonParameters.getEffectiveJvmArgs());

            return type.cast(out);
        }
        DelegatingBuildModelAction<T> action = new DelegatingBuildModelAction<T>(type);
        return run(action, parameters);
    }
{'fraction_non_alphanumeric': 0.08140967715435801, 'fraction_numerical': 0.002957364659492319, 'mean_word_length': 3.7948011027963764, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6966091', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2941, 'n_words': 793}