/*
 * Copyright 2013 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.api.execution.internal.TaskInputsListener;
import org.gradle.initialization.GradleLauncherFactory;
import org.gradle.internal.classpath.CachedClasspathTransformer;
import org.gradle.internal.concurrent.ExecutorFactory;
import org.gradle.internal.concurrent.ParallelismConfigurationManager;
import org.gradle.internal.filewatch.DefaultFileSystemChangeWaiterFactory;
import org.gradle.internal.filewatch.FileWatcherEventListenerFactory;
import org.gradle.internal.filewatch.FileWatcherFactory;
import org.gradle.internal.invocation.BuildActionRunner;
import org.gradle.internal.logging.LoggingManagerInternal;
import org.gradle.internal.logging.text.StyledTextOutputFactory;
import org.gradle.internal.progress.BuildOperationListenerManager;
import org.gradle.internal.service.ServiceRegistration;
import org.gradle.internal.service.scopes.AbstractPluginServiceRegistry;
import org.gradle.internal.service.scopes.GradleUserHomeScopeServiceRegistry;
import org.gradle.launcher.exec.BuildExecuter;
import org.gradle.launcher.exec.BuildTreeScopeBuildActionExecuter;
import org.gradle.launcher.exec.ChainingBuildActionRunner;
import org.gradle.launcher.exec.InProcessBuildActionExecuter;
import org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner;
import org.gradle.tooling.internal.provider.serialization.ClassLoaderCache;
import org.gradle.tooling.internal.provider.serialization.DaemonSidePayloadClassLoaderFactory;
import org.gradle.tooling.internal.provider.serialization.DefaultPayloadClassLoaderRegistry;
import org.gradle.tooling.internal.provider.serialization.ModelClassLoaderFactory;
import org.gradle.tooling.internal.provider.serialization.PayloadClassLoaderFactory;
import org.gradle.tooling.internal.provider.serialization.PayloadSerializer;
import org.gradle.tooling.internal.provider.serialization.WellKnownClassLoaderRegistry;

import java.util.List;

public class LauncherServices extends AbstractPluginServiceRegistry {
    @Override
    public void registerGlobalServices(ServiceRegistration registration) {
        registration.addProvider(new ToolingGlobalScopeServices());
    }

    @Override
    public void registerGradleUserHomeServices(ServiceRegistration registration) {
        registration.addProvider(new ToolingBuildSessionScopeServices());
    }

    static class ToolingGlobalScopeServices {
        BuildExecuter createBuildExecuter(List<BuildActionRunner> buildActionRunners,
                                          List<SubscribableBuildActionRunnerRegistration> registrations,
                                          GradleLauncherFactory gradleLauncherFactory,
                                          BuildOperationListenerManager buildOperationListenerManager,
                                          FileWatcherFactory fileWatcherFactory,
                                          TaskInputsListener inputsListener,
                                          StyledTextOutputFactory styledTextOutputFactory,
                                          ExecutorFactory executorFactory,
                                          LoggingManagerInternal loggingManager,
                                          GradleUserHomeScopeServiceRegistry userHomeServiceRegistry,
                                          ParallelismConfigurationManager parallelismConfigurationManager) {
            return new SetupLoggingActionExecuter(
                new SessionFailureReportingActionExecuter(
                    new StartParamsValidatingActionExecuter(
                        new ParallelismConfigurationBuildActionExecuter(
                            new GradleThreadBuildActionExecuter(
                                new ServicesSetupBuildActionExecuter(
                                    new ContinuousBuildActionExecuter(
                                        new BuildTreeScopeBuildActionExecuter(
                                            new InProcessBuildActionExecuter(
                                                new SubscribableBuildActionRunner(
                                                    new RunAsBuildOperationBuildActionRunner(
                                                        new ValidatingBuildActionRunner(
                                                            new ChainingBuildActionRunner(buildActionRunners))),
                                                    buildOperationListenerManager,
                                                    registrations),
                                                gradleLauncherFactory)),
                                        new DefaultFileSystemChangeWaiterFactory(fileWatcherFactory),
                                        new FileWatcherEventListenerFactory(),
                                        inputsListener,
                                        styledTextOutputFactory,
                                        executorFactory),
                                    userHomeServiceRegistry)),
                            parallelismConfigurationManager)),
                    styledTextOutputFactory),
                loggingManager);
        }

        ExecuteBuildActionRunner createExecuteBuildActionRunner() {
            return new ExecuteBuildActionRunner();
        }

        ClassLoaderCache createClassLoaderCache() {
            return new ClassLoaderCache();
        }
    }

    static class ToolingBuildSessionScopeServices {
        PayloadClassLoaderFactory createClassLoaderFactory(CachedClasspathTransformer cachedClasspathTransformer) {
            return new DaemonSidePayloadClassLoaderFactory(
                new ModelClassLoaderFactory(),
                cachedClasspathTransformer);
        }

        PayloadSerializer createPayloadSerializer(ClassLoaderCache classLoaderCache, PayloadClassLoaderFactory classLoaderFactory) {
            return new PayloadSerializer(
                new WellKnownClassLoaderRegistry(
                    new DefaultPayloadClassLoaderRegistry(
                        classLoaderCache,
                        classLoaderFactory))
            );
        }
    }
}

@@ -1032,32 +1032,100 @@
 eWaiterFactory;%0A
+import org.gradle.internal.filewatch.FileSystemChangeWaiterFactory;%0A
 import org.gradl
@@ -1170,32 +1170,32 @@
 istenerFactory;%0A
-
 import org.gradl
@@ -3460,89 +3460,8 @@
 er,%0A
-                                          FileWatcherFactory fileWatcherFactory,%0A
     
@@ -3878,24 +3878,234 @@
 ceRegistry,%0A
+                                          FileSystemChangeWaiterFactory fileSystemChangeWaiterFactory,%0A                                          FileWatcherEventListenerFactory fileWatcherEventListenerFactory,%0A
             
@@ -5455,20 +5455,9 @@
     
-new DefaultF
+f
 ileS
@@ -5484,28 +5484,8 @@
 tory
-(fileWatcherFactory)
 ,%0A  
@@ -5526,13 +5526,9 @@
     
-new F
+f
 ileW
@@ -5545,34 +5545,32 @@
 tListenerFactory
-()
 ,%0A              
@@ -5898,32 +5898,32 @@
 OutputFactory),%0A
-
                 
@@ -5942,32 +5942,390 @@
 er);%0A        %7D%0A%0A
+        FileWatcherEventListenerFactory createFileWatcherEventListenerFactory() %7B%0A            return new FileWatcherEventListenerFactory();%0A        %7D%0A%0A        FileSystemChangeWaiterFactory createFileSystemChangeWaiterFactory(FileWatcherFactory fileWatcherFactory) %7B%0A            return new DefaultFileSystemChangeWaiterFactory(fileWatcherFactory);%0A        %7D%0A%0A
         ExecuteB

Create services for file watcher factory types