<commit_before>package net.filebot.ant.spk;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Checksum;
import org.apache.tools.ant.taskdefs.Delete;
import org.apache.tools.ant.taskdefs.Tar;
import org.apache.tools.ant.taskdefs.Tar.TarCompressionMethod;
import org.apache.tools.ant.taskdefs.Tar.TarFileSet;
import org.apache.tools.ant.taskdefs.Tar.TarLongFileMode;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.FileSet;

public class SpkTask extends Task {

	public static class Info {

		String name;
		String value;

		public void setName(String name) {
			this.name = name;
		}

		public void setValue(String value) {
			this.value = value;
		}
	}

	public static class Icon {

		public static class Size extends EnumeratedAttribute {

			@Override
			public String[] getValues() {
				return new String[] { "72", "140", "256" };
			}

			public String getFileName() {
				switch (value) {
				case "72":
					return "PACKAGE_ICON.PNG";
				default:
					return String.format("PACKAGE_ICON_%s.PNG", value);
				}
			}
		}

		Size size;
		File file;

		public void setSize(Size size) {
			this.size = size;
		}

		public void setFile(File file) {
			this.file = file;
		}
	}

	static final String NAME = "package";
	static final String VERSION = "version";
	static final String ARCH = "arch";

	File destDir;
	Map<String, String> infoList = new LinkedHashMap<String, String>();

	List<FileSet> packageFiles = new ArrayList<FileSet>();
	List<FileSet> spkFiles = new ArrayList<FileSet>();

	public void setDestdir(File value) {
		destDir = value;
	}

	public void setName(String value) {
		infoList.put(NAME, value);
	}

	public void setVersion(String value) {
		infoList.put(VERSION, value);
	}

	public void setArch(String value) {
		infoList.put(ARCH, value);
	}

	public void addConfiguredInfo(Info info) {
		infoList.put(info.name, info.value);
	}

	public void addConfiguredPackage(TarFileSet files) {
		packageFiles.add(files);
	}

	public void addConfiguredScripts(TarFileSet files) {
		files.setPrefix("scripts");
		spkFiles.add(files);
	}

	public void addConfiguredWizard(TarFileSet files) {
		files.setPrefix("WIZARD_UIFILES");
		spkFiles.add(files);
	}

	public void addConfiguredConf(TarFileSet files) {
		files.setPrefix("conf");
		spkFiles.add(files);
	}

	public void addConfiguredIcon(Icon icon) {
		TarFileSet files = new TarFileSet();
		files.setFullpath(icon.size.getFileName());
		files.setFile(icon.file);
		spkFiles.add(files);
	}

	public void setLicense(File file) {
		TarFileSet files = new TarFileSet();
		files.setFullpath("LICENSE");
		files.setFile(file);
		spkFiles.add(files);
	}

	@Override
	public void execute() throws BuildException {
		if (destDir == null || !infoList.containsKey(NAME) || !infoList.containsKey(VERSION) || !infoList.containsKey(ARCH))
			throw new BuildException("Required attributes: destdir, name, version, arch");

		if (packageFiles.isEmpty() || spkFiles.isEmpty())
			throw new BuildException("Required elements: package, scripts");

		String spkName = String.format("%s-%s-%s", infoList.get(NAME), infoList.get(VERSION), infoList.get(ARCH));

		File spkStaging = new File(destDir, spkName);
		File spkFile = new File(destDir, spkName + ".spk");

		spkStaging.mkdirs(); // make sure staging folder exists

		// generate info and package files and add to spk fileset
		preparePackage(spkStaging);
		prepareInfo(spkStaging);

		tar(spkFile, false, spkFiles);
	}

	private void preparePackage(File tempDirectory) {
		File packageFile = new File(tempDirectory, "package.tgz");
		tar(packageFile, true, packageFiles);
<commit_msg>* make sure staging folder is clean<commit_after>package net.filebot.ant.spk;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Checksum;
import org.apache.tools.ant.taskdefs.Delete;
import org.apache.tools.ant.taskdefs.Tar;
import org.apache.tools.ant.taskdefs.Tar.TarCompressionMethod;
import org.apache.tools.ant.taskdefs.Tar.TarFileSet;
import org.apache.tools.ant.taskdefs.Tar.TarLongFileMode;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.FileSet;

public class SpkTask extends Task {

	public static class Info {

		String name;
		String value;

		public void setName(String name) {
			this.name = name;
		}

		public void setValue(String value) {
			this.value = value;
		}
	}

	public static class Icon {

		public static class Size extends EnumeratedAttribute {

			@Override
			public String[] getValues() {
				return new String[] { "72", "140", "256" };
			}

			public String getFileName() {
				switch (value) {
				case "72":
					return "PACKAGE_ICON.PNG";
				default:
					return String.format("PACKAGE_ICON_%s.PNG", value);
				}
			}
		}

		Size size;
		File file;

		public void setSize(Size size) {
			this.size = size;
		}

		public void setFile(File file) {
			this.file = file;
		}
	}

	static final String NAME = "package";
	static final String VERSION = "version";
	static final String ARCH = "arch";

	File destDir;
	Map<String, String> infoList = new LinkedHashMap<String, String>();

	List<FileSet> packageFiles = new ArrayList<FileSet>();
	List<FileSet> spkFiles = new ArrayList<FileSet>();

	public void setDestdir(File value) {
		destDir = value;
	}

	public void setName(String value) {
		infoList.put(NAME, value);
	}

	public void setVersion(String value) {
		infoList.put(VERSION, value);
	}

	public void setArch(String value) {
		infoList.put(ARCH, value);
	}

	public void addConfiguredInfo(Info info) {
		infoList.put(info.name, info.value);
	}

	public void addConfiguredPackage(TarFileSet files) {
		packageFiles.add(files);
	}

	public void addConfiguredScripts(TarFileSet files) {
		files.setPrefix("scripts");
		spkFiles.add(files);
	}

	public void addConfiguredWizard(TarFileSet files) {
		files.setPrefix("WIZARD_UIFILES");
		spkFiles.add(files);
	}

	public void addConfiguredConf(TarFileSet files) {
		files.setPrefix("conf");
		spkFiles.add(files);
	}

	public void addConfiguredIcon(Icon icon) {
		TarFileSet files = new TarFileSet();
		files.setFullpath(icon.size.getFileName());
		files.setFile(icon.file);
		spkFiles.add(files);
	}

	public void setLicense(File file) {
		TarFileSet files = new TarFileSet();
		files.setFullpath("LICENSE");
		files.setFile(file);
		spkFiles.add(files);
	}

	@Override
	public void execute() throws BuildException {
		if (destDir == null || !infoList.containsKey(NAME) || !infoList.containsKey(VERSION) || !infoList.containsKey(ARCH))
			throw new BuildException("Required attributes: destdir, name, version, arch");

		if (packageFiles.isEmpty() || spkFiles.isEmpty())
			throw new BuildException("Required elements: package, scripts");

		String spkName = String.format("%s-%s-%s", infoList.get(NAME), infoList.get(VERSION), infoList.get(ARCH));

		File spkStaging = new File(destDir, spkName);
		File spkFile = new File(destDir, spkName + ".spk");
		
		// make sure staging folder is clean
		clean(spkStaging)

		// make sure staging folder exists
		spkStaging.mkdirs();

		// generate info and package files and add to spk fileset
		preparePackage(spkStaging);
		prepareInfo(spkStaging);

		tar(spkFile, false, spkFiles);
	}

	private void preparePackage(File tempDirectory) {
		File packageFile = new File(tempDirectory, "package.tgz");
		tar(packageFile, true, packageFiles);

{'fraction_non_alphanumeric': 0.12319574575841985, 'fraction_numerical': 0.0025322866548493288, 'mean_word_length': 4.41027397260274, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, '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': '2121285', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2622, 'n_words': 607}