<commit_before>/*
 * 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.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.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> {
  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;
  }

  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;
  }

  public MakeBeforeRunTask createTask(RunConfiguration runConfiguration) {
    return /*!(runConfiguration instanceof RemoteConfiguration) && */runConfiguration instanceof RunProfileWithCompileBeforeLaunchOption
           ? new MakeBeforeRunTask()
           : null;
  }

  public boolean configureTask(RunConfiguration runConfiguration, MakeBeforeRunTask task) {
    return false;
  }

  @Override
  public boolean canExecuteTask(RunConfiguration configuration, MakeBeforeRunTask task) {
    return true;
  }

  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() {
        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();
        }
      };

      SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
          CompileScope scope;
          final CompilerManager compilerManager = CompilerManager.getInstance(myProject);
          if (Boolean.valueOf(System.getProperty(MAKE_PROJECT_ON_RUN_KEY, Boolean.FALSE.toString())).booleanValue()) {
            // user explicitly requested whole-project make
            scope = compilerManager.createProjectCompileScope(myProject);
          }
          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(myProject);
            }
          }

          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();
  }

  public boolean isConfigurable() {
    return false;
  }<commit_msg>suppressor for adding Make task<commit_after>/*
 * 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.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 runConfiguration) {
    return !(runConfiguration instanceof Suppressor) && runConfiguration instanceof RunProfileWithCompileBeforeLaunchOption
           ? new MakeBeforeRunTask()
           : null;
  }

  @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();
        }
      };

      SwingUtilities.invokeAndWait(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(myProject);
          }
          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(myProject);
            }
          }

          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;
  }
{'fraction_non_alphanumeric': 0.08883211678832116, 'fraction_numerical': 0.0020437956204379564, 'mean_word_length': 3.611578593066308, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '5110042', 'n_tokens_mistral': 3690, 'n_tokens_neox': 3558, 'n_words': 862}