<commit_before>package me.nallar;

import lombok.Data;
import lombok.Getter;
import lombok.val;
import me.nallar.javatransformer.api.JavaTransformer;
import me.nallar.mixin.internal.MixinApplicator;
import me.nallar.modpatcher.tasks.TaskProcessBinary;
import me.nallar.modpatcher.tasks.TaskProcessSource;
import org.apache.log4j.Logger;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;

import java.io.*;
import java.nio.file.*;
import java.util.*;

public class ModPatcherPlugin implements Plugin<Project> {
	public static Logger logger = Logger.getLogger("ModPatcher");
	public static String BINARY_PROCESSING_TASK = "processMcBin";
	public static String SRC_PROCESSING_TASK = "processMcSrc";

	public ModPatcherGradleExtension extension = new ModPatcherGradleExtension();
	private Project project;
	@Getter(lazy = true)
	private final JavaTransformer mixinTransformer = makeMixinTransformer();

	public static ModPatcherPlugin get(Project project) {
		return (ModPatcherPlugin) project.getPlugins().findPlugin("me.nallar.ModPatcherGradle");
	}

	@Override
	public void apply(Project project) {
		this.project = project;
		project.getPlugins().apply("net.minecraftforge.gradle.forge");

		project.getExtensions().add("modpatcher", extension);

		val tasks = project.getTasks();
		tasks.create(BINARY_PROCESSING_TASK, TaskProcessBinary.class);
		tasks.create(SRC_PROCESSING_TASK, TaskProcessSource.class);

		project.afterEvaluate(this::afterEvaluate);
	}

	public JavaTransformer makeMixinTransformer() {
		val applicator = new MixinApplicator();
		applicator.setNoMixinIsError(extension.noMixinIsError);
		return applicator.getMixinTransformer(getSourceSet().getJava().getSrcDirs().iterator().next().toPath(), extension.getMixinPackageToUse());
	}

	public void mixinTransform(Path toTransform) {
		if (extension.shouldMixin()) {
			Path old = toTransform.resolveSibling(toTransform.getFileName().toString() + ".bak");
			try {
				Files.move(toTransform, old);
				getMixinTransformer().transform(old, toTransform);
				Files.delete(old);
			} catch (IOException e) {
				throw new IOError(e);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private SourceSet getSourceSet() {
		for (SourceSet s : (Iterable<SourceSet>) project.getProperties().get("sourceSets")) {
			if (s.getName().equals("main")) {<commit_msg>Remove backup file if it already exists<commit_after>package me.nallar;

import lombok.Data;
import lombok.Getter;
import lombok.val;
import me.nallar.javatransformer.api.JavaTransformer;
import me.nallar.mixin.internal.MixinApplicator;
import me.nallar.modpatcher.tasks.TaskProcessBinary;
import me.nallar.modpatcher.tasks.TaskProcessSource;
import org.apache.log4j.Logger;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;

import java.io.*;
import java.nio.file.*;
import java.util.*;

public class ModPatcherPlugin implements Plugin<Project> {
	public static Logger logger = Logger.getLogger("ModPatcher");
	public static String BINARY_PROCESSING_TASK = "processMcBin";
	public static String SRC_PROCESSING_TASK = "processMcSrc";

	public ModPatcherGradleExtension extension = new ModPatcherGradleExtension();
	private Project project;
	@Getter(lazy = true)
	private final JavaTransformer mixinTransformer = makeMixinTransformer();

	public static ModPatcherPlugin get(Project project) {
		return (ModPatcherPlugin) project.getPlugins().findPlugin("me.nallar.ModPatcherGradle");
	}

	@Override
	public void apply(Project project) {
		this.project = project;
		project.getPlugins().apply("net.minecraftforge.gradle.forge");

		project.getExtensions().add("modpatcher", extension);

		val tasks = project.getTasks();
		tasks.create(BINARY_PROCESSING_TASK, TaskProcessBinary.class);
		tasks.create(SRC_PROCESSING_TASK, TaskProcessSource.class);

		project.afterEvaluate(this::afterEvaluate);
	}

	public JavaTransformer makeMixinTransformer() {
		val applicator = new MixinApplicator();
		applicator.setNoMixinIsError(extension.noMixinIsError);
		return applicator.getMixinTransformer(getSourceSet().getJava().getSrcDirs().iterator().next().toPath(), extension.getMixinPackageToUse());
	}

	public void mixinTransform(Path toTransform) {
		if (extension.shouldMixin()) {
			Path old = toTransform.resolveSibling("bak_" + toTransform.getFileName().toString());
			try {
				Files.deleteIfExists(old);
				Files.move(toTransform, old);
				getMixinTransformer().transform(old, toTransform);
				Files.delete(old);
			} catch (IOException e) {
				throw new IOError(e);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private SourceSet getSourceSet() {
		for (SourceSet s : (Iterable<SourceSet>) project.getProperties().get("sourceSets")) {
			if (s.getName().equals("main")) {
{'fraction_non_alphanumeric': 0.12006681979536438, 'fraction_numerical': 0.00041762372102735435, 'mean_word_length': 6.064896755162242, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3945581', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1493, 'n_words': 277}