gradle: reuse io files for init scripts
[idea/community.git] / plugins / gradle / src / org / jetbrains / plugins / gradle / service / task / GradleTaskManager.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.jetbrains.plugins.gradle.service.task;
17
18 import com.intellij.openapi.externalSystem.model.ExternalSystemException;
19 import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId;
20 import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener;
21 import com.intellij.openapi.externalSystem.task.AbstractExternalSystemTaskManager;
22 import com.intellij.openapi.externalSystem.task.ExternalSystemTaskManager;
23 import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil;
24 import com.intellij.openapi.util.io.FileUtil;
25 import com.intellij.openapi.util.text.StringUtil;
26 import com.intellij.util.ArrayUtil;
27 import com.intellij.util.Consumer;
28 import com.intellij.util.Function;
29 import com.intellij.util.SystemProperties;
30 import com.intellij.util.containers.ContainerUtil;
31 import org.gradle.tooling.BuildLauncher;
32 import org.gradle.tooling.CancellationTokenSource;
33 import org.gradle.tooling.GradleConnector;
34 import org.gradle.tooling.ProjectConnection;
35 import org.gradle.util.GradleVersion;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 import org.jetbrains.plugins.gradle.service.project.GradleExecutionHelper;
39 import org.jetbrains.plugins.gradle.service.project.GradleProjectResolver;
40 import org.jetbrains.plugins.gradle.service.project.GradleProjectResolverExtension;
41 import org.jetbrains.plugins.gradle.service.execution.UnsupportedCancellationToken;
42 import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings;
43 import org.jetbrains.plugins.gradle.util.GradleConstants;
44
45 import java.io.File;
46 import java.io.IOException;
47 import java.util.List;
48 import java.util.Map;
49
50 /**
51  * @author Denis Zhdanov
52  * @since 3/14/13 5:09 PM
53  */
54 public class GradleTaskManager extends AbstractExternalSystemTaskManager<GradleExecutionSettings>
55   implements ExternalSystemTaskManager<GradleExecutionSettings> {
56
57   private final GradleExecutionHelper myHelper = new GradleExecutionHelper();
58
59   private final Map<ExternalSystemTaskId, CancellationTokenSource> myCancellationMap = ContainerUtil.newConcurrentMap();
60
61   public GradleTaskManager() {
62   }
63
64   @Override
65   public void executeTasks(@NotNull final ExternalSystemTaskId id,
66                            @NotNull final List<String> taskNames,
67                            @NotNull String projectPath,
68                            @Nullable final GradleExecutionSettings settings,
69                            @NotNull final List<String> vmOptions,
70                            @NotNull final List<String> scriptParameters,
71                            @Nullable final String debuggerSetup,
72                            @NotNull final ExternalSystemTaskNotificationListener listener) throws ExternalSystemException {
73
74     // TODO add support for external process mode
75     if (ExternalSystemApiUtil.isInProcessMode(GradleConstants.SYSTEM_ID)) {
76       for (GradleTaskManagerExtension gradleTaskManagerExtension : GradleTaskManagerExtension.EP_NAME.getExtensions()) {
77         if (gradleTaskManagerExtension.executeTasks(
78           id, taskNames, projectPath, settings, vmOptions, scriptParameters, debuggerSetup, listener)) {
79           return;
80         }
81       }
82     }
83
84     Function<ProjectConnection, Void> f = new Function<ProjectConnection, Void>() {
85       @Override
86       public Void fun(ProjectConnection connection) {
87
88         final List<String> initScripts = ContainerUtil.newArrayList();
89         final GradleProjectResolverExtension projectResolverChain = GradleProjectResolver.createProjectResolverChain(settings);
90         for (GradleProjectResolverExtension resolverExtension = projectResolverChain;
91              resolverExtension != null;
92              resolverExtension = resolverExtension.getNext()) {
93           final String resolverClassName = resolverExtension.getClass().getName();
94           resolverExtension.enhanceTaskProcessing(taskNames, debuggerSetup, new Consumer<String>() {
95             @Override
96             public void consume(String script) {
97               if (StringUtil.isNotEmpty(script)) {
98                 ContainerUtil.addAllNotNull(
99                   initScripts,
100                   "//-- Generated by " + resolverClassName,
101                   script,
102                   "//");
103               }
104             }
105           });
106         }
107
108         if (!initScripts.isEmpty()) {
109           try {
110             File tempFile = GradleExecutionHelper.writeToFileGradleInitScript(StringUtil.join(initScripts, SystemProperties.getLineSeparator()));
111             ContainerUtil.addAll(scriptParameters, GradleConstants.INIT_SCRIPT_CMD_OPTION, tempFile.getAbsolutePath());
112           }
113           catch (IOException e) {
114             throw new ExternalSystemException(e);
115           }
116         }
117
118         GradleVersion gradleVersion = GradleExecutionHelper.getGradleVersion(connection);
119         BuildLauncher launcher = myHelper.getBuildLauncher(id, connection, settings, listener, vmOptions, scriptParameters);
120         launcher.forTasks(ArrayUtil.toStringArray(taskNames));
121
122         if (gradleVersion != null && gradleVersion.compareTo(GradleVersion.version("2.1")) < 0) {
123           myCancellationMap.put(id, new UnsupportedCancellationToken());
124         } else {
125           final CancellationTokenSource cancellationTokenSource = GradleConnector.newCancellationTokenSource();
126           launcher.withCancellationToken(cancellationTokenSource.token());
127           myCancellationMap.put(id, cancellationTokenSource);
128         }
129         try {
130           launcher.run();
131         }
132         finally {
133           myCancellationMap.remove(id);
134         }
135         return null;
136       }
137     };
138     myHelper.execute(projectPath, settings, f);
139   }
140
141   @Override
142   public boolean cancelTask(@NotNull ExternalSystemTaskId id, @NotNull ExternalSystemTaskNotificationListener listener)
143     throws ExternalSystemException {
144
145     // extension points are available only in IDE process
146     if (ExternalSystemApiUtil.isInProcessMode(GradleConstants.SYSTEM_ID)) {
147       for (GradleTaskManagerExtension gradleTaskManagerExtension : GradleTaskManagerExtension.EP_NAME.getExtensions()) {
148         if (gradleTaskManagerExtension.cancelTask(id, listener)) return true;
149       }
150     }
151
152     final CancellationTokenSource cancellationTokenSource = myCancellationMap.get(id);
153     if (cancellationTokenSource != null) {
154       cancellationTokenSource.cancel();
155     }
156     return true;
157   }
158 }