<commit_before>/*
 * #%L
 * Maven Integration for Eclipse CDT
 * %%
 * Copyright (C) 2014 <NAME>
 * %%
 * 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.
 * #L%
 */
package com.github.sdedwards.m2e_nar.internal;

import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.MojoExecution;
import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager;
import org.eclipse.cdt.core.settings.model.WriteAccessException;
import org.eclipse.cdt.core.settings.model.extension.CConfigurationData;
import org.eclipse.cdt.managedbuilder.buildproperties.IBuildProperty;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IBuilder;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
import org.eclipse.cdt.managedbuilder.internal.core.Configuration;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedProject;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant;
import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator;
import org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant;
import org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest;
import org.eclipse.m2e.jdt.internal.JavaProjectConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sdedwards.m2e_nar.MavenNarPlugin;
import com.github.sdedwards.m2e_nar.internal.cdt.AbstractSettingsSynchroniser;
import com.github.sdedwards.m2e_nar.internal.cdt.CdtUtils;
import com.github.sdedwards.m2e_nar.internal.cdt.SynchroniserFactory;
import com.github.sdedwards.m2e_nar.internal.model.NarBuildArtifact;
import com.github.sdedwards.m2e_nar.internal.model.NarExecution;

@SuppressWarnings("restriction")
public class CProjectConfigurator extends AbstractProjectConfigurator {

	private static final Logger logger = LoggerFactory.getLogger(CProjectConfigurator.class);
	public static final String CONFIGURATOR_ID = "com.github.sdedwards.m2e_nar.cConfigurator";

	@Override
	public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException {

		final ConfiguratorContext context = new ConfiguratorContext(maven, projectManager);

		IProject project = request.getProject();

		monitor.setTaskName(Messages.CProjectConfigurator_task_name + project.getName());

		logger.info("configure");

		ICProjectDescriptionManager mngr = CoreModel.getDefault().getProjectDescriptionManager();

		// Set the first created configuration as active.
		boolean setActive = true;
		final IMavenProjectFacade facade = request.getMavenProjectFacade();
		List<NarExecution> narExecutions = MavenUtils.buildCompileNarExecutions(context, facade, monitor);
		narExecutions.addAll(MavenUtils.buildTestCompileNarExecutions(context, facade, monitor));
		for (NarExecution narSettings : narExecutions) {
			if (!narSettings.isSkip()) {
				final String os = narSettings.getOS();
				final String linkerName = narSettings.getLinkerName();
				final AbstractSettingsSynchroniser synchro = SynchroniserFactory.getSettingsSynchroniser(os, linkerName);
				final String toolchain = synchro.getToolchain();
				for (NarBuildArtifact artifactSettings : narSettings.getArtifactSettings()) {
					final String configName = artifactSettings.getConfigName();
					final String cdtArtefactType = CdtUtils.convertArtefactType(artifactSettings.getType());
					IToolChain tc = getToolChain(toolchain, cdtArtefactType);
					ICProjectDescription desc = getCdtProject(project, tc, cdtArtefactType, monitor);
					ICConfigurationDescription cfg = getCdtMavenConfig(project, desc, tc, cdtArtefactType, configName, setActive, monitor);
					setActive = false;
					synchro.fullSync(cfg, artifactSettings);
					mngr.setProjectDescription(project, desc);
				}
			}
		}

		JavaProjectConfigurator jConfig = new JavaProjectConfigurator();
		jConfig.configure(request, monitor);

		// ensure CDT builder is after the Maven one
		boolean changed = false;
		IProjectDescription description = project.getDescription();
		ICommand cdtBuilder = null;
		ICommand mavenBuilder = null;
		ArrayList<ICommand> newSpec = new ArrayList<ICommand>();
		for (ICommand command : description.getBuildSpec()) {
			if (ManagedCProjectNature.getBuilderID().equals(command.getBuilderName()) && mavenBuilder == null) {
				cdtBuilder = command;
			} else {
				newSpec.add(command);
			}
			if (IMavenConstants.BUILDER_ID.equals(command.getBuilderName())) {
				mavenBuilder = command;
				if (cdtBuilder != null) {
					newSpec.add(cdtBuilder);
					changed = true;
				}
			}
		}
		if (changed) {
			description.setBuildSpec(newSpec.toArray(new ICommand[newSpec.size()]));
			project.setDescription(description, monitor);
		}
	}<commit_msg>Fix NullPointerException when importing some projects.<commit_after>/*
 * #%L
 * Maven Integration for Eclipse CDT
 * %%
 * Copyright (C) 2014 <NAME>
 * %%
 * 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.
 * #L%
 */
package com.github.sdedwards.m2e_nar.internal;

import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.MojoExecution;
import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager;
import org.eclipse.cdt.core.settings.model.WriteAccessException;
import org.eclipse.cdt.core.settings.model.extension.CConfigurationData;
import org.eclipse.cdt.managedbuilder.buildproperties.IBuildProperty;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IBuilder;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
import org.eclipse.cdt.managedbuilder.internal.core.Configuration;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedProject;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.lifecyclemapping.LifecycleMappingFactory;
import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant;
import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator;
import org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant;
import org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sdedwards.m2e_nar.MavenNarPlugin;
import com.github.sdedwards.m2e_nar.internal.cdt.AbstractSettingsSynchroniser;
import com.github.sdedwards.m2e_nar.internal.cdt.CdtUtils;
import com.github.sdedwards.m2e_nar.internal.cdt.SynchroniserFactory;
import com.github.sdedwards.m2e_nar.internal.model.NarBuildArtifact;
import com.github.sdedwards.m2e_nar.internal.model.NarExecution;

@SuppressWarnings("restriction")
public class CProjectConfigurator extends AbstractProjectConfigurator {

	private static final Logger logger = LoggerFactory.getLogger(CProjectConfigurator.class);
	public static final String CONFIGURATOR_ID = "com.github.sdedwards.m2e_nar.cConfigurator";
	public static final String JAVA_CONFIGURATOR_ID = "org.eclipse.m2e.jdt.javaConfigurator";

	@Override
	public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException {

		final ConfiguratorContext context = new ConfiguratorContext(maven, projectManager);

		IProject project = request.getProject();

		monitor.setTaskName(Messages.CProjectConfigurator_task_name + project.getName());

		logger.info("configure");

		ICProjectDescriptionManager mngr = CoreModel.getDefault().getProjectDescriptionManager();

		// Set the first created configuration as active.
		boolean setActive = true;
		final IMavenProjectFacade facade = request.getMavenProjectFacade();
		List<NarExecution> narExecutions = MavenUtils.buildCompileNarExecutions(context, facade, monitor);
		narExecutions.addAll(MavenUtils.buildTestCompileNarExecutions(context, facade, monitor));
		for (NarExecution narSettings : narExecutions) {
			if (!narSettings.isSkip()) {
				final String os = narSettings.getOS();
				final String linkerName = narSettings.getLinkerName();
				final AbstractSettingsSynchroniser synchro = SynchroniserFactory.getSettingsSynchroniser(os, linkerName);
				final String toolchain = synchro.getToolchain();
				for (NarBuildArtifact artifactSettings : narSettings.getArtifactSettings()) {
					final String configName = artifactSettings.getConfigName();
					final String cdtArtefactType = CdtUtils.convertArtefactType(artifactSettings.getType());
					IToolChain tc = getToolChain(toolchain, cdtArtefactType);
					ICProjectDescription desc = getCdtProject(project, tc, cdtArtefactType, monitor);
					ICConfigurationDescription cfg = getCdtMavenConfig(project, desc, tc, cdtArtefactType, configName, setActive, monitor);
					setActive = false;
					synchro.fullSync(cfg, artifactSettings);
					mngr.setProjectDescription(project, desc);
				}
			}
		}

		AbstractProjectConfigurator jConfig = LifecycleMappingFactory.createProjectConfigurator(JAVA_CONFIGURATOR_ID);
		jConfig.configure(request, monitor);

		// ensure CDT builder is after the Maven one
		boolean changed = false;
		IProjectDescription description = project.getDescription();
		ICommand cdtBuilder = null;
		ICommand mavenBuilder = null;
		ArrayList<ICommand> newSpec = new ArrayList<ICommand>();
		for (ICommand command : description.getBuildSpec()) {
			if (ManagedCProjectNature.getBuilderID().equals(command.getBuilderName()) && mavenBuilder == null) {
				cdtBuilder = command;
			} else {
				newSpec.add(command);
			}
			if (IMavenConstants.BUILDER_ID.equals(command.getBuilderName())) {
				mavenBuilder = command;
				if (cdtBuilder != null) {
					newSpec.add(cdtBuilder);
					changed = true;
				}
			}
		}
		if (changed) {
			description.setBuildSpec(newSpec.toArray(new ICommand[newSpec.size()]));
			project.setDescription(description, monitor);
		}
	}
{'fraction_non_alphanumeric': 0.09636419657987984, 'fraction_numerical': 0.004082575874287475, 'mean_word_length': 5.79738219895288, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6652752', 'n_tokens_mistral': 4103, 'n_tokens_neox': 3817, 'n_words': 742}