<commit_before>/*
 * Copyright 2015 DiffPlug
 *
 * 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.diffplug.gradle.eclipse;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.Files;

import com.diffplug.common.swt.os.SwtPlatform;
import com.diffplug.gradle.CmdLine;
import com.diffplug.gradle.FileMisc;
import com.diffplug.gradle.ZipUtil;

public class PdeProductBuildTask extends DefaultTask {
	private Object buildDir;

	/** The directory from which plugins will be pulled, besides the delta pack. */
	public void buildDir(Object buildDir) {
		this.buildDir = buildDir;
	}

	@OutputDirectory
	protected File getBuildDir() {
		return getProject().file(buildDir);
	}

	private Object pluginPath;

	/** The directory from which plugins will be pulled, besides the delta pack. */
	public void setPluginPath(Object pluginPath) {
		this.pluginPath = pluginPath;
	}

	protected File getPluginPath() {
		return getProject().file(pluginPath);
	}

	private Object featureFolder;

	/** The directory from which plugins will be pulled, besides the delta pack. */
	public void setFeatureFolder(Object featureFolder) {
		this.featureFolder = featureFolder;
	}

	private Map<String, String> buildProperties = Maps.newLinkedHashMap();

	/** Adds a property to the build properties file. */
	public void addBuildProperty(String key, String value) {
		buildProperties.put(key, value);
	}

	private Map<String, List<String>> resolveMap = Maps.newHashMap();

	/** Resolves conflicts at the given plugin with the given versions. */
	public void resolveWithFirst(String pluginName, String... versions) {
		resolveMap.put(pluginName, Arrays.asList(versions));
	}

	private Object copyProductDir;
	private String dstRelPath;

	/** Copies the product and imgs from the given directory to the given path within the build directory. */
	public void copyProductAndImgs(Object src, String dstRelPath) {
		this.copyProductDir = src;
		this.dstRelPath = dstRelPath;
	}

	private static final String PRODUCT_FILE = "com.diffplug.core.product";

	@TaskAction
	public void build() throws Exception {
		Preconditions.checkNotNull(buildDir, "buildDir must not be null!");

		// delete the buildDir and make a fresh directory
		File buildDir = getBuildDir();
		FileMisc.cleanDir(buildDir);

		// setup build.properties
		BuildProperties properties = new BuildProperties(getProject());
		properties.setConfigs(SwtPlatform.getAll()); // for all configs
		properties.setBuildDirectory(getBuildDir()); // tied to the appropriate build directory
		if (pluginPath != null) {
			properties.setPluginPath(getPluginPath());
		} else {
			properties.setPluginPath();
		}
		// set all the properties we'd like to set
		for (Map.Entry<String, String> entry : buildProperties.entrySet()) {
			properties.setProp(entry.getKey(), entry.getValue());
		}
		// write build.properties to the appropriate directory
		File buildDirProperties = new File(buildDir, "build.properties");
		Files.write(properties.getContent(), buildDirProperties, StandardCharsets.UTF_8);

		// copy any other files the user might like us to copy
		if (copyProductDir != null) {
			File srcDir = getProject().file(copyProductDir);
			File dstDir = buildDir.toPath().resolve(dstRelPath).toFile();

			// catalog all of the available plugins, and resolve any plugins that require resolving
			PluginCatalog catalog = new PluginCatalog(properties.getPluginLookupPath());
			resolveMap.entrySet().forEach(entry -> {
				catalog.resolveWithFirst(entry.getKey(), entry.getValue());
			});

			// get DiffPlug's version 
			String dpVersion = "1.6.3.RC1";

			// set all the version tags in the product file
			File productInput = new File(srcDir, PRODUCT_FILE);
			File productOutput = new File(dstDir, PRODUCT_FILE);

			// read the lines of the file, and set the exact versions
			List<String> lines = Files.readLines(productInput, StandardCharsets.UTF_8);
			setProductFileVersions(productInput, dpVersion, lines, catalog);
			// write it to its destination<commit_msg>dpVersion is no longer hardcoded.  None of DiffPlug should be hardcoded, but we'll have to get to that later.<commit_after>/*
 * Copyright 2015 DiffPlug
 *
 * 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.diffplug.gradle.eclipse;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.Files;

import com.diffplug.common.swt.os.SwtPlatform;
import com.diffplug.gradle.CmdLine;
import com.diffplug.gradle.FileMisc;
import com.diffplug.gradle.ZipUtil;

public class PdeProductBuildTask extends DefaultTask {
	private Object buildDir;

	/** The directory from which plugins will be pulled, besides the delta pack. */
	public void buildDir(Object buildDir) {
		this.buildDir = buildDir;
	}

	@OutputDirectory
	protected File getBuildDir() {
		return getProject().file(buildDir);
	}

	private Object pluginPath;

	/** The directory from which plugins will be pulled, besides the delta pack. */
	public void setPluginPath(Object pluginPath) {
		this.pluginPath = pluginPath;
	}

	protected File getPluginPath() {
		return getProject().file(pluginPath);
	}

	private Object featureFolder;

	/** The directory from which plugins will be pulled, besides the delta pack. */
	public void setFeatureFolder(Object featureFolder) {
		this.featureFolder = featureFolder;
	}

	private Map<String, String> buildProperties = Maps.newLinkedHashMap();

	/** Adds a property to the build properties file. */
	public void addBuildProperty(String key, String value) {
		buildProperties.put(key, value);
	}

	private Map<String, List<String>> resolveMap = Maps.newHashMap();

	/** Resolves conflicts at the given plugin with the given versions. */
	public void resolveWithFirst(String pluginName, String... versions) {
		resolveMap.put(pluginName, Arrays.asList(versions));
	}

	private Object copyProductDir;
	private String dstRelPath;

	/** Copies the product and imgs from the given directory to the given path within the build directory. */
	public void copyProductAndImgs(Object src, String dstRelPath) {
		this.copyProductDir = src;
		this.dstRelPath = dstRelPath;
	}

	private static final String PRODUCT_FILE = "com.diffplug.core.product";

	@TaskAction
	public void build() throws Exception {
		Preconditions.checkNotNull(buildDir, "buildDir must not be null!");

		// delete the buildDir and make a fresh directory
		File buildDir = getBuildDir();
		FileMisc.cleanDir(buildDir);

		// setup build.properties
		BuildProperties properties = new BuildProperties(getProject());
		properties.setConfigs(SwtPlatform.getAll()); // for all configs
		properties.setBuildDirectory(getBuildDir()); // tied to the appropriate build directory
		if (pluginPath != null) {
			properties.setPluginPath(getPluginPath());
		} else {
			properties.setPluginPath();
		}
		// set all the properties we'd like to set
		for (Map.Entry<String, String> entry : buildProperties.entrySet()) {
			properties.setProp(entry.getKey(), entry.getValue());
		}
		// write build.properties to the appropriate directory
		File buildDirProperties = new File(buildDir, "build.properties");
		Files.write(properties.getContent(), buildDirProperties, StandardCharsets.UTF_8);

		// copy any other files the user might like us to copy
		if (copyProductDir != null) {
			File srcDir = getProject().file(copyProductDir);
			File dstDir = buildDir.toPath().resolve(dstRelPath).toFile();

			// catalog all of the available plugins, and resolve any plugins that require resolving
			PluginCatalog catalog = new PluginCatalog(properties.getPluginLookupPath());
			resolveMap.entrySet().forEach(entry -> {
				catalog.resolveWithFirst(entry.getKey(), entry.getValue());
			});

			// get DiffPlug's version
			String dpVersion = (String) getProject().getProperties().get("VER_DIFFPLUG");

			// set all the version tags in the product file
			File productInput = new File(srcDir, PRODUCT_FILE);
			File productOutput = new File(dstDir, PRODUCT_FILE);

			// read the lines of the file, and set the exact versions
			List<String> lines = Files.readLines(productInput, StandardCharsets.UTF_8);
			setProductFileVersions(productInput, dpVersion, lines, catalog);
			// write it to its destination
{'fraction_non_alphanumeric': 0.09854391733208079, 'fraction_numerical': 0.002254579614842649, 'mean_word_length': 4.612018977332631, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '6647668', 'n_tokens_mistral': 3160, 'n_tokens_neox': 2947, 'n_words': 948}