forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregisterJextractTask.kt
More file actions
70 lines (61 loc) · 3.36 KB
/
registerJextractTask.kt
File metadata and controls
70 lines (61 loc) · 3.36 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package utilities
import org.gradle.api.Project
import org.gradle.api.tasks.Exec
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.register
import java.io.File
import java.nio.file.Files
private fun Project.swiftProductsWithJExtractPlugin(): List<String> = swiftPMPackage().targets.filter {
it.productDependencies.contains("JExtractSwiftPlugin")
}.flatMap {
it.productMemberships
}
private fun Project.registerSwiftCheckValidTask(): TaskProvider<*> = tasks.register<Exec>("swift-check-valid") {
commandLine("swift")
args("-version")
}
fun Project.registerJextractTask(
arguments: () -> List<String> = {
listOf("build", "--disable-experimental-prebuilts")
}
): TaskProvider<*> {
val swiftCheckValid = registerSwiftCheckValidTask()
return tasks.register<Exec>("jextract") {
description = "Generate Java wrappers for swift target"
dependsOn(swiftCheckValid)
// only because we depend on "live developing" the plugin while using this project to test it
inputs.file(File(rootDir, "Package.swift"))
inputs.dir(File(rootDir, "Sources"))
// If the package description changes, we should execute jextract again, maybe we added jextract to new targets
inputs.file(File(projectDir, "Package.swift"))
// monitor all targets/products which depend on the JExtract plugin
swiftProductsWithJExtractPlugin().forEach {
logger.info("[swift-java:jextract (Gradle)] Swift input target: ${it}")
inputs.dir(File(layout.projectDirectory.asFile, "Sources/${it}"))
}
outputs.dir(layout.buildDirectory.dir("../.build/plugins/outputs/${layout.projectDirectory.asFile.getName().lowercase()}"))
val baseSwiftPluginOutputsDir = layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile
if (!baseSwiftPluginOutputsDir.exists()) {
baseSwiftPluginOutputsDir.mkdirs()
}
Files.walk(layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile.toPath()).forEach {
// Add any Java sources generated by the plugin to our sourceSet
if (it.endsWith("JExtractSwiftPlugin/src/generated/java")) {
outputs.dir(it)
}
}
workingDir = layout.projectDirectory.asFile
commandLine("swift")
// FIXME: disable prebuilts until swift-syntax isn't broken on 6.2 anymore: https://github.com/swiftlang/swift-java/issues/418
args(arguments()) // since Swift targets which need to be jextract-ed have the jextract build plugin, we just need to build
// If we wanted to execute a specific subcommand, we can like this:
// args("run",/*
// "swift-java", "jextract",
// "--swift-module", "MySwiftLibrary",
// // java.package is obtained from the swift-java.config in the swift module
// "--output-java", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/src/generated/java").get()}",
// "--output-swift", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/Sources").get()}",
// "--log-level", (logging.level <= LogLevel.INFO ? "debug" : */"info")
// )
}
}