@@ -16,9 +16,10 @@ import Foundation
1616import PackagePlugin
1717
1818@main
19- final class JExtractSwiftCommandPlugin : BuildToolPlugin , CommandPlugin {
19+ final class JExtractSwiftCommandPlugin : SwiftJavaPluginProtocol , BuildToolPlugin , CommandPlugin {
2020
21- var verbose : Bool = false
21+ var pluginName : String = " swift-java-command "
22+ var verbose : Bool = getEnvironmentBool ( " SWIFT_JAVA_VERBOSE " )
2223
2324 /// Build the target before attempting to extract from it.
2425 /// This avoids trying to extract from broken sources.
@@ -48,8 +49,8 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
4849 }
4950
5051 for target in context. package . targets {
51- guard let configPath = getSwiftJavaConfig ( target: target) else {
52- log ( " Skipping target ' \( target. name) ', has no 'swift-java.config' file " )
52+ guard getSwiftJavaConfigPath ( target: target) != nil else {
53+ log ( " [swift-java-command] Skipping jextract step: Missing swift-java.config for target '\( target. name) ' " )
5354 continue
5455 }
5556
@@ -73,21 +74,15 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
7374 let sourceDir = target. directory. string
7475
7576 let configuration = try readConfiguration ( sourceDir: " \( sourceDir) " )
76-
77- // We use the the usual maven-style structure of "src/[generated|main|test]/java/..."
78- // that is common in JVM ecosystem
79- let outputDirectoryJava = context. pluginWorkDirectoryURL
80- . appending ( path: " src " )
81- . appending ( path: " generated " )
82- . appending ( path: " java " )
83- let outputDirectorySwift = context. pluginWorkDirectoryURL
84- . appending ( path: " Sources " )
77+ guard let javaPackage = configuration. javaPackage else {
78+ throw SwiftJavaPluginError . missingConfiguration ( sourceDir: " \( sourceDir) " , key: " javaPackage " )
79+ }
8580
8681 var arguments : [ String ] = [
8782 " --swift-module " , sourceModule. name,
88- " --package-name " , configuration . javaPackage,
89- " --output-directory-java " , outputDirectoryJava. path ( percentEncoded: false ) ,
90- " --output-directory-swift " , outputDirectorySwift. path ( percentEncoded: false ) ,
83+ " --package-name " , javaPackage,
84+ " --output-directory-java " , context . outputDirectoryJava. path ( percentEncoded: false ) ,
85+ " --output-directory-swift " , context . outputDirectorySwift. path ( percentEncoded: false ) ,
9186 // TODO: "--build-cache-directory", ...
9287 // Since plugins cannot depend on libraries we cannot detect what the output files will be,
9388 // as it depends on the contents of the input files. Therefore we have to implement this as a prebuild plugin.
@@ -100,14 +95,17 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
10095
10196 /// Perform the command on a specific target.
10297 func performCommand( context: PluginContext , target: Target , extraArguments _: [ String ] ) throws {
103- // Make sure the target can builds properly
104- try self . packageManager. build ( . target( target. name) , parameters: . init( ) )
105-
10698 guard let sourceModule = target. sourceModule else { return }
10799
108100 if self . buildInputs {
101+ // Make sure the target can builds properly
109102 log ( " Pre-building target ' \( target. name) ' before extracting sources... " )
110- try self . packageManager. build ( . target( target. name) , parameters: . init( ) )
103+ let targetBuildResult = try self . packageManager. build ( . target( target. name) , parameters: . init( ) )
104+
105+ guard targetBuildResult. succeeded else {
106+ print ( " [swift-java-command] Build of ' \( target. name) ' failed: \( targetBuildResult. logText) " )
107+ return
108+ }
111109 }
112110
113111 let arguments = try prepareJExtractArguments ( context: context, target: target)
@@ -124,7 +122,13 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
124122 log ( " Post-extract building products with target ' \( target. name) '... " )
125123 for product in context. package . products where product. targets. contains ( where: { $0. id == target. id } ) {
126124 log ( " Post-extract building product ' \( product. name) '... " )
127- try self . packageManager. build ( . product( product. name) , parameters: . init( ) )
125+ let buildResult = try self . packageManager. build ( . product( product. name) , parameters: . init( ) )
126+
127+ if buildResult. succeeded {
128+ log ( " Post-extract build: " + " done " . green + " . " )
129+ } else {
130+ log ( " Post-extract build: " + " done " . red + " ! " )
131+ }
128132 }
129133 }
130134 }
@@ -146,16 +150,15 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
146150 }
147151 }
148152
149- func log( _ message: @autoclosure ( ) -> String , terminator: String = " \n " ) {
150- if self . verbose {
151- print ( " [swift-java-command] \( message ( ) ) " , terminator: terminator)
152- }
153- }
154153}
155154
156155// Mini coloring helper, since we cannot have dependencies we keep it minimal here
157156extension String {
157+ var red : String {
158+ " \u{001B} [0;31m " + " \( self ) " + " \u{001B} [0;0m "
159+ }
158160 var green : String {
159161 " \u{001B} [0;32m " + " \( self ) " + " \u{001B} [0;0m "
160162 }
161- }
163+ }
164+
0 commit comments