summaryrefslogtreecommitdiff
path: root/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ExtractNativesTask.kt
blob: a3c33ed9a959f51e475803c7fd94325c6bc08dd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package moe.nea.archenemy.mojang

import moe.nea.archenemy.util.readZipFs
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.io.File

abstract class ExtractNativesTask : DefaultTask() {

	@get:OutputDirectory
	abstract val nativeDirectory: DirectoryProperty

	@get:Input
	abstract val natives: ListProperty<MojangVersionMetadata.Library>

	@Internal
	fun getNativeDirectoryPath(): File {
		return nativeDirectory.get().asFile
	}

	@TaskAction
	fun extractNatives() {
		val nativeDirectory = nativeDirectory.get().asFile
		natives.get().forEach {
			val extract = it.extract ?: return@forEach
			val file = project.configurations
				.detachedConfiguration(project.dependencies.create(it.getArtifactCoordinate()))
				.files
				.single().toPath()
			file.readZipFs { path, inputStream ->
				if (extract.exclude.any { path.startsWith(it) }) return@readZipFs
				val targetFile = nativeDirectory.resolve(path)
				targetFile.parentFile.mkdirs()
				targetFile.outputStream().use { output ->
					inputStream.copyTo(output)
				}
			}
		}
	}
}