<commit_before>/*******************************************************************************
 * Copyright (c) 2014 Made in Switzerland, <NAME>
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of The MIT License
 * which accompanies this distribution, and is available at
 * http://opensource.org/licenses/MIT
 *
 * Contributors:
 *     <NAME> - job for adding a project to the index
 ******************************************************************************/
package ch.mlutz.plugins.t4e.index.jobs;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import ch.mlutz.plugins.t4e.index.TapestryIndex;
import ch.mlutz.plugins.t4e.index.TapestryIndexer;
import ch.mlutz.plugins.t4e.log.EclipseLogFactory;
import ch.mlutz.plugins.t4e.log.IEclipseLog;
import ch.mlutz.plugins.t4e.tapestry.TapestryException;
import ch.mlutz.plugins.t4e.tapestry.TapestryModule;

/**
 * An Eclipse API job for adding a project to the TapestryIndex.
 *
 * @author <NAME>
 */
public class AddProjectToIndexJob extends Job {

	/**
	 * the Log
	 */
	public static final IEclipseLog log= EclipseLogFactory.create(
			AddProjectToIndexJob.class);

	protected static final String TASK_NAME_ADD_PROJECT=
			"Adding project ";

	protected static final String TASK_NAME_CREATING_MODULES=
			"Creating modules for project ";

	protected static final String TASK_NAME_UPDATING_MODULES=
			"Updating modules for project ";

	protected static final String TASK_NAME_UPDATE_TAPESTRY_INDEX=
			"Update Tapestry index";

	private final IProject projectToAdd;

	private final TapestryIndexer tapestryIndexer;

	private final TapestryIndex tapestryIndex;

	/**
	 * @param projectToAdd the project to be added to the TapestryIndex
	 * @param tapestryIndexer the TapestryIndexer instance to be used to add the
	 * 		project
	 */
	public AddProjectToIndexJob(IProject projectToAdd,
			TapestryIndexer tapestryIndexer) {
		super(TASK_NAME_UPDATE_TAPESTRY_INDEX);
		this.projectToAdd= projectToAdd;
		this.tapestryIndexer= tapestryIndexer;
		this.tapestryIndex= tapestryIndexer.getTapestryIndex();

		setRule(new TapestrySchedulingRule(projectToAdd));
		setUser(false);
		setPriority(Job.LONG);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public IStatus run(IProgressMonitor monitor) {
		// use a NullProgressMonitor as stub if needed
		if (monitor == null) {
			monitor= new NullProgressMonitor();
		}

		monitor.beginTask(TASK_NAME_ADD_PROJECT + projectToAdd.getName(), 1000);

		if (tapestryIndex.contains(projectToAdd)) {
			// early out
			monitor.worked(1000);
			monitor.done();
			return Status.OK_STATUS;
		}

		try {
			tapestryIndex.add(projectToAdd);

			monitor.worked(100);
			monitor.setTaskName(TASK_NAME_CREATING_MODULES + projectToAdd.getName());

			List<TapestryModule> modulesToAdd= tapestryIndexer
					.createModulesForProject(projectToAdd);
				log.info("Found " + modulesToAdd.size() + " modules to add to " + projectToAdd.getName());

			if (monitor.isCanceled()) {
				 return Status.CANCEL_STATUS;
			}

			monitor.worked(300);
			monitor.setTaskName(TASK_NAME_UPDATING_MODULES + projectToAdd.getName());

			for (TapestryModule module: modulesToAdd) {
				// totalWork+= module.getScanAndUpdateWork();
				tapestryIndex.add(module);
				log.info("Scanning and updating module " + module.getAppSpecification().getAppSpecificationFile().getName());
				module.scanAndUpdateIndex(monitor);
			}

			monitor.worked(600);
		} catch(TapestryException e) {
			log.error("Could not add project " + projectToAdd.getName(), e);
		} finally {
			monitor.done();
		}
		return Status.OK_STATUS;
	}
}
<commit_msg>Added waiting for auto refreshes and builds before adding project to index.<commit_after>/*******************************************************************************
 * Copyright (c) 2014 Made in Switzerland, <NAME>
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of The MIT License
 * which accompanies this distribution, and is available at
 * http://opensource.org/licenses/MIT
 *
 * Contributors:
 *     <NAME> - job for adding a project to the index
 ******************************************************************************/
package ch.mlutz.plugins.t4e.index.jobs;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;

import ch.mlutz.plugins.t4e.index.TapestryIndex;
import ch.mlutz.plugins.t4e.index.TapestryIndexer;
import ch.mlutz.plugins.t4e.log.EclipseLogFactory;
import ch.mlutz.plugins.t4e.log.IEclipseLog;
import ch.mlutz.plugins.t4e.tapestry.TapestryException;
import ch.mlutz.plugins.t4e.tapestry.TapestryModule;

/**
 * An Eclipse API job for adding a project to the TapestryIndex.
 *
 * @author <NAME>
 */
public class AddProjectToIndexJob extends Job {

	/**
	 * the Log
	 */
	public static final IEclipseLog log= EclipseLogFactory.create(
			AddProjectToIndexJob.class);

	protected static final String TASK_NAME_ADD_PROJECT=
			"Adding project ";

	protected static final String TASK_NAME_CREATING_MODULES=
			"Creating modules for project ";

	protected static final String TASK_NAME_UPDATING_MODULES=
			"Updating modules for project ";

	protected static final String TASK_NAME_UPDATE_TAPESTRY_INDEX=
			"Update Tapestry index";

	private final IProject projectToAdd;

	private final TapestryIndexer tapestryIndexer;

	private final TapestryIndex tapestryIndex;

	/**
	 * @param projectToAdd the project to be added to the TapestryIndex
	 * @param tapestryIndexer the TapestryIndexer instance to be used to add the
	 * 		project
	 */
	public AddProjectToIndexJob(IProject projectToAdd,
			TapestryIndexer tapestryIndexer) {
		super(TASK_NAME_UPDATE_TAPESTRY_INDEX);
		this.projectToAdd= projectToAdd;
		this.tapestryIndexer= tapestryIndexer;
		this.tapestryIndex= tapestryIndexer.getTapestryIndex();

		ISchedulingRule tapestrySchedulingRule= new TapestrySchedulingRule(
				projectToAdd);

		setRule(tapestrySchedulingRule);
		setUser(false);
		setPriority(Job.LONG);

	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public IStatus run(IProgressMonitor monitor) {
		// use a NullProgressMonitor as stub if needed
		if (monitor == null) {
			monitor= new NullProgressMonitor();
		}

		monitor.beginTask(TASK_NAME_ADD_PROJECT + projectToAdd.getName(), 1000);

		if (tapestryIndex.contains(projectToAdd)) {
			// early out
			monitor.worked(1000);
			monitor.done();
			return Status.OK_STATUS;
		}

		try {
			tapestryIndex.add(projectToAdd);

			monitor.worked(100);
			monitor.setTaskName(TASK_NAME_CREATING_MODULES + projectToAdd.getName());

			waitForFamily(ResourcesPlugin.FAMILY_AUTO_REFRESH);
			waitForFamily(ResourcesPlugin.FAMILY_AUTO_BUILD);

			List<TapestryModule> modulesToAdd= tapestryIndexer
					.createModulesForProject(projectToAdd);
				log.info("Found " + modulesToAdd.size() + " modules to add to " + projectToAdd.getName());

			if (monitor.isCanceled()) {
				 return Status.CANCEL_STATUS;
			}

			monitor.worked(300);
			monitor.setTaskName(TASK_NAME_UPDATING_MODULES + projectToAdd.getName());

			for (TapestryModule module: modulesToAdd) {
				// totalWork+= module.getScanAndUpdateWork();
				tapestryIndex.add(module);
				log.info("Scanning and updating module " + module.getAppSpecification().getAppSpecificationFile().getName());
				module.scanAndUpdateIndex(monitor);
			}

			monitor.worked(600);
		} catch(TapestryException e) {
			log.error("Could not add project " + projectToAdd.getName(), e);
		} finally {
			monitor.done();
		}
		return Status.OK_STATUS;
	}

	protected static void waitForFamily(Object family) {
	    boolean wasInterrupted = false;
	    do {
	      try {
	        Job.getJobManager().join(family, null);
	        wasInterrupted = false;
	      }
	      catch (OperationCanceledException e) {
	        e.printStackTrace();
	      }
	      catch (InterruptedException e) {
	        wasInterrupted = true;
	      }
	    }
	    while (wasInterrupted);
	  }
}

{'fraction_non_alphanumeric': 0.1310690677000228, 'fraction_numerical': 0.0063824937314793705, 'mean_word_length': 4.643086816720257, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '676766', 'n_tokens_mistral': 2998, 'n_tokens_neox': 2626, 'n_words': 578}