@@ -49,21 +49,34 @@ dependencies {
4949 testImplementation(" org.junit.jupiter:junit-jupiter" )
5050}
5151
52- // // This is for development, when we edit the Swift swift-java project, the outputs of the generated sources may change.
53- // // Thus, we also need to watch and re-build the top level project.
54- // def compileSwiftJExtractPlugin = tasks.register("compileSwiftJava", Exec) {
55- // description = "Rebuild the swift-java root project (" + swiftBuildConfiguration() + ")"
56- //
57- // inputs.file(new File(rootDir, "Package.swift"))
58- // inputs.dir(new File(rootDir, "Sources"))
59- // outputs.dir(new File(rootDir, ".build"))
60- //
61- // workingDir = rootDir
62- // commandLine "swift"
63- // args("build",
64- // "-c", swiftBuildConfiguration(),
65- // "--product", "swift-java")
66- // }
52+ def swiftProductsWithJExtractPlugin () {
53+ def stdout = new ByteArrayOutputStream ()
54+ def stderr = new ByteArrayOutputStream ()
55+
56+ def result = exec {
57+ commandLine ' swift' , ' package' , ' describe' , ' --type' , ' json'
58+ standardOutput = stdout
59+ errorOutput = stderr
60+ ignoreExitValue = true
61+ }
62+
63+ def jsonOutput = stdout. toString()
64+
65+ if (result. exitValue == 0 ) {
66+ def json = new JsonSlurper (). parseText(jsonOutput)
67+ def products = json. targets
68+ .findAll { target ->
69+ target. product_dependencies?. contains(" JExtractSwiftPlugin" )
70+ }
71+ .collectMany { target ->
72+ target. product_memberships ?: []
73+ }
74+ return products
75+ } else {
76+ logger. warn(" Command failed: ${ stderr.toString()} " )
77+ return []
78+ }
79+ }
6780
6881def jextract = tasks. register(" jextract" , Exec ) {
6982 description = " Generate Java wrappers for swift target"
@@ -73,12 +86,14 @@ def jextract = tasks.register("jextract", Exec) {
7386 inputs. file(new File (rootDir, " Package.swift" ))
7487 inputs. dir(new File (rootDir, " Sources" ))
7588
89+ // If the package description changes, we should execute jextract again, maybe we added jextract to new targets
7690 inputs. file(new File (projectDir, " Package.swift" ))
77- inputs. dir(new File (projectDir, " Sources" ))
7891
79- // TODO: we can use package describe --type json to figure out which targets depend on JExtractSwiftPlugin and will produce outputs
80- // Avoid adding this directory, but create the expected one specifically for all targets
81- // which WILL produce sources because they have the plugin
92+ // monitor all targets/products which depend on the JExtract plugin
93+ swiftProductsWithJExtractPlugin(). each {
94+ logger. info(" [swift-java:jextract (Gradle)] Swift input target: ${ it} " )
95+ inputs. dir(new File (layout. projectDirectory. asFile, " Sources/${ it} " . toString()))
96+ }
8297 outputs. dir(layout. buildDirectory. dir(" ../.build/plugins/outputs/${ layout.projectDirectory.asFile.getName().toLowerCase()} " ))
8398
8499 File baseSwiftPluginOutputsDir = layout. buildDirectory. dir(" ../.build/plugins/outputs/" ). get(). asFile
@@ -94,16 +109,16 @@ def jextract = tasks.register("jextract", Exec) {
94109
95110 workingDir = layout. projectDirectory
96111 commandLine " swift"
97- args(" run " ,
98- " swift-java " , " jextract " ,
99- " --swift-module " , " MySwiftLibrary " ,
100- " -- java-module " , " MyJavaLibrary " ,
101- // "--java-package ", "com.example.myjava ",
102- " --java-output " , " ${ layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/src/generated/ java") } " ,
103- " --swift- output" , " ${ layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/Sources" )} " ,
104- // "-v ",
105- // "--log-level", "info" // TODO: pass log level from Gradle build
106- )
112+ args(" build " ) // since Swift targets which need to be jextract-ed have the jextract build plugin, we just need to build
113+ // If we wanted to execute a specific subcommand, we can like this:
114+ // args("run",/*
115+ // "swift- java", "jextract ",
116+ // "--swift-module ", "MySwiftLibrary ",
117+ // // java.package is obtained from the swift- java.config in the swift module
118+ // "--output-java ", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/src/generated/java").get( )}",
119+ // "--output-swift", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/Sources").get()} ",
120+ // "--log-level", (logging.level <= LogLevel.INFO ? "debug" : */"info")
121+ // )
107122}
108123
109124
0 commit comments