IDEADEV-41380: Artifacts: results of manual editing of "Build artifacts" setting...
[fedora-idea.git] / java / execution / impl / src / com / intellij / compiler / options / CompileStepBeforeRun.java
blobf4204cdeb0ea37bd98c8aa79b46ea64e9025006c
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.compiler.options;
18 import com.intellij.execution.BeforeRunTask;
19 import com.intellij.execution.BeforeRunTaskProvider;
20 import com.intellij.execution.ExecutionBundle;
21 import com.intellij.execution.configurations.RunConfiguration;
22 import com.intellij.execution.configurations.RunProfileWithCompileBeforeLaunchOption;
23 import com.intellij.execution.remote.RemoteConfiguration;
24 import com.intellij.openapi.actionSystem.DataContext;
25 import com.intellij.openapi.application.ApplicationManager;
26 import com.intellij.openapi.application.ModalityState;
27 import com.intellij.openapi.compiler.CompileContext;
28 import com.intellij.openapi.compiler.CompileScope;
29 import com.intellij.openapi.compiler.CompileStatusNotification;
30 import com.intellij.openapi.compiler.CompilerManager;
31 import com.intellij.openapi.module.Module;
32 import com.intellij.openapi.project.Project;
33 import com.intellij.openapi.util.Key;
34 import com.intellij.util.concurrency.Semaphore;
35 import org.jetbrains.annotations.NonNls;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
39 /**
40 * @author spleaner
42 public class CompileStepBeforeRun extends BeforeRunTaskProvider<CompileStepBeforeRun.MakeBeforeRunTask> {
43 public static final Key<MakeBeforeRunTask> ID = Key.create("Make");
44 private static final Key<RunConfiguration> RUN_CONFIGURATION = Key.create("RUN_CONFIGURATION");
46 @NonNls protected static final String MAKE_PROJECT_ON_RUN_KEY = "makeProjectOnRun";
47 private final Project myProject;
49 public CompileStepBeforeRun(@NotNull final Project project) {
50 myProject = project;
53 public Key<MakeBeforeRunTask> getId() {
54 return ID;
57 public String getDescription(final RunConfiguration runConfiguration, MakeBeforeRunTask task) {
58 return ExecutionBundle.message("before.launch.compile.step");
61 public MakeBeforeRunTask createTask(RunConfiguration runConfiguration) {
62 return !(runConfiguration instanceof RemoteConfiguration) && runConfiguration instanceof RunProfileWithCompileBeforeLaunchOption
63 ? new MakeBeforeRunTask()
64 : null;
67 public boolean configureTask(RunConfiguration runConfiguration, MakeBeforeRunTask task) {
68 return false;
71 public boolean executeTask(DataContext context, final RunConfiguration configuration, MakeBeforeRunTask task) {
72 if (!(configuration instanceof RunProfileWithCompileBeforeLaunchOption)) {
73 return true;
76 final RunProfileWithCompileBeforeLaunchOption runConfiguration = (RunProfileWithCompileBeforeLaunchOption)configuration;
77 final Semaphore done = new Semaphore();
78 final boolean[] result = new boolean[1];
79 try {
80 final CompileStatusNotification callback = new CompileStatusNotification() {
81 public void finished(final boolean aborted, final int errors, final int warnings, CompileContext compileContext) {
82 if (errors == 0 && !aborted) {
83 result[0] = true;
86 done.up();
90 ApplicationManager.getApplication().invokeAndWait(new Runnable() {
91 public void run() {
92 CompileScope scope;
93 final CompilerManager compilerManager = CompilerManager.getInstance(myProject);
94 if (Boolean.valueOf(System.getProperty(MAKE_PROJECT_ON_RUN_KEY, Boolean.FALSE.toString())).booleanValue()) {
95 // user explicitly requested whole-project make
96 scope = compilerManager.createProjectCompileScope(myProject);
98 else {
99 final Module[] modules = runConfiguration.getModules();
100 if (modules.length > 0) {
101 scope = compilerManager.createModulesCompileScope(modules, true);
103 else {
104 scope = compilerManager.createProjectCompileScope(myProject);
108 done.down();
109 scope.putUserData(RUN_CONFIGURATION, configuration);
110 compilerManager.make(scope, callback);
112 }, ModalityState.NON_MODAL);
114 catch (Exception e) {
115 return false;
118 done.waitFor();
119 return result[0];
122 public boolean hasConfigurationButton() {
123 return false;
126 @Nullable
127 public static RunConfiguration getRunConfiguration(final CompileContext context) {
128 return context.getCompileScope().getUserData(RUN_CONFIGURATION);
131 @Nullable
132 public static RunConfiguration getRunConfiguration(final CompileScope compileScope) {
133 return compileScope.getUserData(RUN_CONFIGURATION);
136 public static class MakeBeforeRunTask extends BeforeRunTask {
137 private MakeBeforeRunTask() {
138 setEnabled(true);