/* * Copyright 2000-2009 JetBrains s.r.o. * * 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 com.intellij.compiler.options; import com.intellij.execution.BeforeRunTask; import com.intellij.execution.BeforeRunTaskProvider; import com.intellij.execution.ExecutionBundle; import com.intellij.execution.Executor; import com.intellij.execution.configurations.RunConfiguration; import com.intellij.execution.configurations.RunConfigurationBase; import com.intellij.execution.configurations.RunProfileWithCompileBeforeLaunchOption; import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.icons.AllIcons; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.application.TransactionGuard; import com.intellij.openapi.compiler.CompileContext; import com.intellij.openapi.compiler.CompileScope; import com.intellij.openapi.compiler.CompileStatusNotification; import com.intellij.openapi.compiler.CompilerManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Ref; import com.intellij.util.concurrency.Semaphore; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; /** * @author spleaner */ public class CompileStepBeforeRun extends BeforeRunTaskProvider<CompileStepBeforeRun.MakeBeforeRunTask> { /** * Marked for disable adding CompileStepBeforeRun */ public static interface Suppressor { } private static final Logger LOG = Logger.getInstance("#com.intellij.compiler.options.CompileStepBeforeRun"); public static final Key<MakeBeforeRunTask> ID = Key.create("Make"); public static final Key<RunConfiguration> RUN_CONFIGURATION = Key.create("RUN_CONFIGURATION"); public static final Key<String> RUN_CONFIGURATION_TYPE_ID = Key.create("RUN_CONFIGURATION_TYPE_ID"); public static final Key<String> RUNNER_ID = Key.create("RUNNER_ID"); public static final Key<Executor> EXECUTOR = Key.create("EXECUTOR"); @NonNls protected static final String MAKE_PROJECT_ON_RUN_KEY = "makeProjectOnRun"; private final Project myProject; public CompileStepBeforeRun(@NotNull final Project project) { myProject = project; } @Override public Key<MakeBeforeRunTask> getId() { return ID; } @Override public String getName() { return ExecutionBundle.message("before.launch.compile.step"); } @Override public String getDescription(MakeBeforeRunTask task) { return ExecutionBundle.message("before.launch.compile.step"); } @Override public Icon getIcon() { return AllIcons.Actions.Compile; } @Override public Icon getTaskIcon(MakeBeforeRunTask task) { return AllIcons.Actions.Compile; } @Override public MakeBeforeRunTask createTask(RunConfiguration configuration) { MakeBeforeRunTask task = null; if (!(configuration instanceof Suppressor) && configuration instanceof RunProfileWithCompileBeforeLaunchOption) { task = new MakeBeforeRunTask(); if (configuration instanceof RunConfigurationBase) { task.setEnabled(((RunConfigurationBase)configuration).isCompileBeforeLaunchAddedByDefault()); } } return task; } @Override public boolean configureTask(RunConfiguration runConfiguration, MakeBeforeRunTask task) { return false; } @Override public boolean canExecuteTask(RunConfiguration configuration, MakeBeforeRunTask task) { return true; } @Override public boolean executeTask(DataContext context, final RunConfiguration configuration, final ExecutionEnvironment env, MakeBeforeRunTask task) { return doMake(myProject, configuration, env, false); } static boolean doMake(final Project myProject, final RunConfiguration configuration, final ExecutionEnvironment env, final boolean ignoreErrors) { if (!(configuration instanceof RunProfileWithCompileBeforeLaunchOption)) { return true; } if (configuration instanceof RunConfigurationBase && ((RunConfigurationBase)configuration).excludeCompileBeforeLaunchOption()) { return true; } final RunProfileWithCompileBeforeLaunchOption runConfiguration = (RunProfileWithCompileBeforeLaunchOption)configuration; final Ref<Boolean> result = new Ref<Boolean>(Boolean.FALSE); try { final Semaphore done = new Semaphore(); done.down(); final CompileStatusNotification callback = new CompileStatusNotification() { @Override public void finished(final boolean aborted, final int errors, final int warnings, CompileContext compileContext) { if ((errors == 0 || ignoreErrors) && !aborted) { result.set(Boolean.TRUE); } done.up(); } }; TransactionGuard.submitTransaction(myProject, new Runnable() { @Override public void run() { CompileScope scope; final CompilerManager compilerManager = CompilerManager.getInstance(myProject); if (Comparing.equal(Boolean.TRUE.toString(), System.getProperty(MAKE_PROJECT_ON_RUN_KEY))) { // user explicitly requested whole-project make scope = compilerManager.createProjectCompileScope(); } else { final Module[] modules = runConfiguration.getModules(); if (modules.length > 0) { for (Module module : modules) { if (module == null) { LOG.error("RunConfiguration should not return null modules. Configuration=" + runConfiguration.getName() + "; class=" + runConfiguration.getClass().getName()); } } scope = compilerManager.createModulesCompileScope(modules, true); } else { scope = compilerManager.createProjectCompileScope(); } } if (!myProject.isDisposed()) { scope.putUserData(RUN_CONFIGURATION, configuration); scope.putUserData(RUN_CONFIGURATION_TYPE_ID, configuration.getType().getId()); scope.putUserData(RUNNER_ID, env.getRunnerId()); scope.putUserData(EXECUTOR, env.getExecutor()); compilerManager.make(scope, callback); } else { done.up(); } } }); done.waitFor(); } catch (Exception e) { return false; } return result.get(); } @Override public boolean isConfigurable() { return false; } @Nullable public static RunConfiguration getRunConfiguration(final CompileContext context) { return getRunConfiguration(context.getCompileScope()); } @Nullable public static RunConfiguration getRunConfiguration(final CompileScope compileScope) { return compileScope.getUserData(RUN_CONFIGURATION); } public static class MakeBeforeRunTask extends BeforeRunTask<MakeBeforeRunTask> { private MakeBeforeRunTask() { super(ID); setEnabled(true); } } }