12
12
//
13
13
// ===----------------------------------------------------------------------===//
14
14
15
+ import groovy.json.JsonSlurper
15
16
import org.swift.swiftkit.gradle.BuildUtils
16
17
17
18
import java.nio.file.*
19
+ import kotlinx.serialization.json.*
18
20
19
21
plugins {
20
22
id(" build-logic.java-application-conventions" )
@@ -34,26 +36,57 @@ java {
34
36
}
35
37
}
36
38
39
+ def swiftProductsWithJExtractPlugin () {
40
+ def stdout = new ByteArrayOutputStream ()
41
+ def stderr = new ByteArrayOutputStream ()
42
+
43
+ def result = exec {
44
+ commandLine ' swift' , ' package' , ' describe' , ' --type' , ' json'
45
+ standardOutput = stdout
46
+ errorOutput = stderr
47
+ ignoreExitValue = true
48
+ }
49
+
50
+ def jsonOutput = stdout. toString()
51
+
52
+ if (result. exitValue == 0 ) {
53
+ def json = new JsonSlurper (). parseText(jsonOutput)
54
+ def products = json. targets
55
+ .findAll { target ->
56
+ target. product_dependencies?. contains(" JExtractSwiftPlugin" )
57
+ }
58
+ .collectMany { target ->
59
+ target. product_memberships ?: []
60
+ }
61
+ return products
62
+ } else {
63
+ logger. warn(" Command failed: ${ stderr.toString()} " )
64
+ return []
65
+ }
66
+ }
67
+
68
+
37
69
def swiftCheckValid = tasks. register(" swift-check-valid" , Exec ) {
38
70
commandLine " swift"
39
71
args(" -version" )
40
72
}
41
73
42
74
def jextract = tasks. register(" jextract" , Exec ) {
43
- description = " Builds swift sources, including swift-java source generation "
75
+ description = " Generate Java wrappers for swift target "
44
76
dependsOn swiftCheckValid
45
- // dependsOn compileSwiftJExtractPlugin
46
77
47
78
// only because we depend on "live developing" the plugin while using this project to test it
48
79
inputs. file(new File (rootDir, " Package.swift" ))
49
80
inputs. dir(new File (rootDir, " Sources" ))
50
81
82
+ // If the package description changes, we should execute jextract again, maybe we added jextract to new targets
51
83
inputs. file(new File (projectDir, " Package.swift" ))
52
- inputs. dir(new File (projectDir, " Sources" ))
53
84
54
- // TODO: we can use package describe --type json to figure out which targets depend on JExtractSwiftPlugin and will produce outputs
55
- // Avoid adding this directory, but create the expected one specifically for all targets
56
- // which WILL produce sources because they have the plugin
85
+ // monitor all targets/products which depend on the JExtract plugin
86
+ swiftProductsWithJExtractPlugin(). each {
87
+ logger. info(" [swift-java:jextract (Gradle)] Swift input target: ${ it} " )
88
+ inputs. dir(new File (layout. projectDirectory. asFile, " Sources/${ it} " . toString()))
89
+ }
57
90
outputs. dir(layout. buildDirectory. dir(" ../.build/plugins/outputs/${ layout.projectDirectory.asFile.getName().toLowerCase()} " ))
58
91
59
92
File baseSwiftPluginOutputsDir = layout. buildDirectory. dir(" ../.build/plugins/outputs/" ). get(). asFile
@@ -69,7 +102,16 @@ def jextract = tasks.register("jextract", Exec) {
69
102
70
103
workingDir = layout. projectDirectory
71
104
commandLine " swift"
72
- args(" run" , " swift-java" , " jextract" , " -v" , " --log-level" , " info" ) // TODO: pass log level from Gradle build
105
+ args(" build" ) // since Swift targets which need to be jextract-ed have the jextract build plugin, we just need to build
106
+ // If we wanted to execute a specific subcommand, we can like this:
107
+ // args("run",/*
108
+ // "swift-java", "jextract",
109
+ // "--swift-module", "MySwiftLibrary",
110
+ // // java.package is obtained from the swift-java.config in the swift module
111
+ // "--output-java", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/src/generated/java").get()}",
112
+ // "--output-swift", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/Sources").get()}",
113
+ // "--log-level", (logging.level <= LogLevel.INFO ? "debug" : */"info")
114
+ // )
73
115
}
74
116
75
117
// Add the java-swift generated Java sources
0 commit comments