<commit_before>package kaizen.plugins

import org.gradle.api.*;
import org.gradle.api.artifacts.*
import org.gradle.api.tasks.Exec
import org.gradle.api.tasks.bundling.Zip
import org.gradle.util.ConfigureUtil
import kaizen.foundation.FileName

class AssemblyPlugin implements Plugin<Project> {

	@Override
	void apply(Project project) {

		project.extensions.add('assembly', new AssemblyExtension(project))

		if (project.configurations.isEmpty()) {
			addDefaultAssemblyConfigurationTo(project)
		}

		configureCompilationOf(project)

		if (ProjectClassifier.isTest(project)) {
			configureTest(project)
			return
		}
	}

	private void addDefaultAssemblyConfigurationTo(Project project) {
		Configurations.addDefaultAssemblyConfigurationTo(project)
	}

	private void configureCompilationOf(Project project) {
		configure(project) {

			apply plugin: 'base'

			task('compile', type: Exec, dependsOn: 'copyDependencies') {
				description "Compiles all sources in the project directory."
			}

			task('copyDependencies', dependsOn: 'outputDir') {
				description "Copies all incoming dependencies into the build directory."
			}

			task('zip', type: Zip, dependsOn: 'compile') {
				description "Packs the assembly for distribution."

				baseName = assembly.name
				from project.buildDir
				include assembly.file.name
				include "${assembly.name}.xml"
			}

			afterEvaluate {
				adjustCompilationDependenciesOf project
			}

			task('outputDir') << {
				assembly.file.parentFile.mkdirs()
			}
		}
	}

	private void configureTest(Project project) {
		if (AssemblyConventions.isTest(project))
			makeTestProjectDependOnTestee(project)
	}

	private void makeTestProjectDependOnTestee(Project testProject) {
		def testeeName = testProject.name[0..(testProject.name.lastIndexOf('.') - 1)]
		def testee = testProject.rootProject.findProject(testeeName)
		if (testee) {
			testProject.dependencies.add(
				compileConfigurationNameFor(testProject),
				testProject.dependencies.project(
					path: testee.path,
					configuration: compileConfigurationNameFor(testee)))
		}
	}

	def compileConfigurationNameFor(Project project) {
		compileConfigurationFor(project).name
	}

	def configure(Object object, Closure closure) {
		ConfigureUtil.configure(closure, object)
	}

	void adjustCompilationDependenciesOf(Project project) {

		def buildDir = project.buildDir
		def assemblyInBuildDir = { name -> new File(buildDir, "${name}.dll") }
		def config = compileConfigurationFor(project)
		def assemblyReferences = config.allDependencies.collect { assemblyInBuildDir(it.name) }

		configure(project.tasks.copyDependencies) {
			inputs.source config.incoming.files
			outputs.files config.incoming.collect { assemblyInBuildDir(it.name) }
			doFirst {
				config.incoming.files.each { file ->
					project.copy {
						from project.zipTree(file)
						into buildDir
						include '*.dll'
					}
				}
			}
		}

		def mono = project.rootProject.unity.mono
		def assemblyFile = project.assembly.file
		configure(project.tasks.compile) {
			outputs.file assemblyFile
			inputs.source assemblyReferences
			executable mono.cli

			if (isBooProject(project)) {
				inputs.source project.fileTree(dir: project.projectDir, include: "**/*.boo")
				args mono.booc.executable
				args "-srcdir:${project.projectDir}"
			} else {
				inputs.source project.fileTree(dir: project.projectDir, include: "**/*.cs")
				args mono.gmcs.executable
				args "-recurse:*.cs"
				args "-doc:${xmlDocFileFor(assemblyFile)}"
				args "-nowarn:1591"
			}

			args assemblyReferences.collect { "-r:$it" }
			args "-out:$assemblyFile"
			args "-target:library"
		}

		project.artifacts.add(config.name, project.tasks.zip)
	}

	boolean isBooProject(Project project) {
		project.projectDir.listFiles({ dir, name -> name.endsWith('.boo') } as FilenameFilter).any()
	}

	private Configuration compileConfigurationFor(Project p) {
		Configurations.defaultConfigurationFor(p)
	}

	String xmlDocFileFor(File assemblyFile) {<commit_msg>always reference Boo.Lang.PatternMatching.dll and Boo.Lang.Useful.dll<commit_after>package kaizen.plugins

import org.gradle.api.*;
import org.gradle.api.artifacts.*
import org.gradle.api.tasks.Exec
import org.gradle.api.tasks.bundling.Zip
import org.gradle.util.ConfigureUtil
import kaizen.foundation.FileName

class AssemblyPlugin implements Plugin<Project> {

	@Override
	void apply(Project project) {

		project.extensions.add('assembly', new AssemblyExtension(project))

		if (project.configurations.isEmpty()) {
			addDefaultAssemblyConfigurationTo(project)
		}

		configureCompilationOf(project)

		if (ProjectClassifier.isTest(project)) {
			configureTest(project)
			return
		}
	}

	private void addDefaultAssemblyConfigurationTo(Project project) {
		Configurations.addDefaultAssemblyConfigurationTo(project)
	}

	private void configureCompilationOf(Project project) {
		configure(project) {

			apply plugin: 'base'

			task('compile', type: Exec, dependsOn: 'copyDependencies') {
				description "Compiles all sources in the project directory."
			}

			task('copyDependencies', dependsOn: 'outputDir') {
				description "Copies all incoming dependencies into the build directory."
			}

			task('zip', type: Zip, dependsOn: 'compile') {
				description "Packs the assembly for distribution."

				baseName = assembly.name
				from project.buildDir
				include assembly.file.name
				include "${assembly.name}.xml"
			}

			afterEvaluate {
				adjustCompilationDependenciesOf project
			}

			task('outputDir') << {
				assembly.file.parentFile.mkdirs()
			}
		}
	}

	private void configureTest(Project project) {
		if (AssemblyConventions.isTest(project))
			makeTestProjectDependOnTestee(project)
	}

	private void makeTestProjectDependOnTestee(Project testProject) {
		def testeeName = testProject.name[0..(testProject.name.lastIndexOf('.') - 1)]
		def testee = testProject.rootProject.findProject(testeeName)
		if (testee) {
			testProject.dependencies.add(
				compileConfigurationNameFor(testProject),
				testProject.dependencies.project(
					path: testee.path,
					configuration: compileConfigurationNameFor(testee)))
		}
	}

	def compileConfigurationNameFor(Project project) {
		compileConfigurationFor(project).name
	}

	def configure(Object object, Closure closure) {
		ConfigureUtil.configure(closure, object)
	}

	void adjustCompilationDependenciesOf(Project project) {

		def buildDir = project.buildDir
		def assemblyInBuildDir = { name -> new File(buildDir, "${name}.dll") }
		def config = compileConfigurationFor(project)
		def assemblyReferences = config.allDependencies.collect { assemblyInBuildDir(it.name) }

		configure(project.tasks.copyDependencies) {
			inputs.source config.incoming.files
			outputs.files config.incoming.collect { assemblyInBuildDir(it.name) }
			doFirst {
				config.incoming.files.each { file ->
					project.copy {
						from project.zipTree(file)
						into buildDir
						include '*.dll'
					}
				}
			}
		}

		def mono = project.rootProject.unity.mono
		def assemblyFile = project.assembly.file
		configure(project.tasks.compile) {
			outputs.file assemblyFile
			inputs.source assemblyReferences
			executable mono.cli

			if (isBooProject(project)) {
				inputs.source project.fileTree(dir: project.projectDir, include: "**/*.boo")
				args mono.booc.executable
				args "-srcdir:${project.projectDir}"
				args '-r:Boo.Lang.PatternMatching.dll'
				args '-r:Boo.Lang.Useful.dll'
			} else {
				inputs.source project.fileTree(dir: project.projectDir, include: "**/*.cs")
				args mono.gmcs.executable
				args "-recurse:*.cs"
				args "-doc:${xmlDocFileFor(assemblyFile)}"
				args "-nowarn:1591"
			}

			args assemblyReferences.collect { "-r:$it" }
			args "-out:$assemblyFile"
			args "-target:library"
		}

		project.artifacts.add(config.name, project.tasks.zip)
	}

	boolean isBooProject(Project project) {
		project.projectDir.listFiles({ dir, name -> name.endsWith('.boo') } as FilenameFilter).any()
	}

	private Configuration compileConfigurationFor(Project p) {
		Configurations.defaultConfigurationFor(p)
	}

	String xmlDocFileFor(File assemblyFile) {
{'fraction_non_alphanumeric': 0.10978461538461538, 'fraction_numerical': 0.0014769230769230768, 'mean_word_length': 4.293811074918567, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '5039537', 'n_tokens_mistral': 2863, 'n_tokens_neox': 2368, 'n_words': 472}