Skip to content

Commit fc9e94c

Browse files
committed
Deduplicate shared configuration between swiftpm plugins
1 parent 82b5e60 commit fc9e94c

File tree

22 files changed

+165
-230
lines changed

22 files changed

+165
-230
lines changed

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ let package = Package(
128128

129129
// ==== Plugin for wrapping Java classes in Swift
130130
.plugin(
131-
name: "JExtractSwiftPlugin",
131+
name: "SwiftJavaPlugin",
132132
targets: [
133-
"JExtractSwiftPlugin"
133+
"SwiftJavaPlugin"
134134
]
135135
),
136136
.plugin(
@@ -347,7 +347,7 @@ let package = Package(
347347
),
348348

349349
.plugin(
350-
name: "JExtractSwiftPlugin",
350+
name: "SwiftJavaPlugin",
351351
capability: .buildTool(),
352352
dependencies: [
353353
"JExtractSwiftTool"

Plugins/JExtractSwiftCommandPlugin/JExtractSwiftCommandPlugin.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
4848
}
4949

5050
for target in context.package.targets {
51-
guard let configPath = getSwiftJavaConfig(target: target) else {
51+
guard getSwiftJavaConfigPath(target: target) != nil else {
5252
log("Skipping target '\(target.name)', has no 'swift-java.config' file")
5353
continue
5454
}
@@ -73,21 +73,15 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
7373
let sourceDir = target.directory.string
7474

7575
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")
76+
guard let javaPackage = configuration.javaPackage else {
77+
throw SwiftJavaPluginError.missingConfiguration(sourceDir: "\(sourceDir)", key: "javaPackage")
78+
}
8579

8680
var arguments: [String] = [
8781
"--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),
82+
"--package-name", javaPackage,
83+
"--output-directory-java", context.outputDirectoryJava.path(percentEncoded: false),
84+
"--output-directory-swift", context.outputDirectorySwift.path(percentEncoded: false),
9185
// TODO: "--build-cache-directory", ...
9286
// Since plugins cannot depend on libraries we cannot detect what the output files will be,
9387
// as it depends on the contents of the input files. Therefore we have to implement this as a prebuild plugin.
@@ -124,7 +118,13 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
124118
log("Post-extract building products with target '\(target.name)'...")
125119
for product in context.package.products where product.targets.contains(where: { $0.id == target.id }) {
126120
log("Post-extract building product '\(product.name)'...")
127-
try self.packageManager.build(.product(product.name), parameters: .init())
121+
let buildResult = try self.packageManager.build(.product(product.name), parameters: .init())
122+
123+
if buildResult.succeeded {
124+
log("Post-extract build: " + "done".green + ".")
125+
} else {
126+
log("Post-extract build: " + "done".red + "!")
127+
}
128128
}
129129
}
130130
}
@@ -155,7 +155,11 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
155155

156156
// Mini coloring helper, since we cannot have dependencies we keep it minimal here
157157
extension String {
158+
var red: String {
159+
"\u{001B}[0;31m" + "\(self)" + "\u{001B}[0;0m"
160+
}
158161
var green: String {
159162
"\u{001B}[0;32m" + "\(self)" + "\u{001B}[0;0m"
160163
}
161-
}
164+
}
165+

Plugins/JExtractSwiftCommandPlugin/PluginsShared/Configuration.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../PluginsShared

Plugins/JExtractSwiftPlugin/PluginsShared/Configuration.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

Plugins/JExtractSwiftPlugin/PluginsShared/PluginUtils.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.

Plugins/Java2SwiftPlugin/Configuration.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

Plugins/Java2SwiftPlugin/Java2SwiftPlugin.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,3 @@ struct Java2SwiftBuildToolPlugin: BuildToolPlugin {
137137
]
138138
}
139139
}
140-
141-
// Note: the JAVA_HOME environment variable must be set to point to where
142-
// Java is installed, e.g.,
143-
// Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home.
144-
func findJavaHome() -> String {
145-
if let home = ProcessInfo.processInfo.environment["JAVA_HOME"] {
146-
return home
147-
}
148-
149-
// This is a workaround for envs (some IDEs) which have trouble with
150-
// picking up env variables during the build process
151-
let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home"
152-
if let home = try? String(contentsOfFile: path, encoding: .utf8) {
153-
if let lastChar = home.last, lastChar.isNewline {
154-
return String(home.dropLast())
155-
}
156-
157-
return home
158-
}
159-
160-
fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.")
161-
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../PluginsShared

Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,3 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
7878
]
7979
}
8080
}
81-
82-
// Note: the JAVA_HOME environment variable must be set to point to where
83-
// Java is installed, e.g.,
84-
// Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home.
85-
func findJavaHome() -> String {
86-
if let home = ProcessInfo.processInfo.environment["JAVA_HOME"] {
87-
return home
88-
}
89-
90-
// This is a workaround for envs (some IDEs) which have trouble with
91-
// picking up env variables during the build process
92-
let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home"
93-
if let home = try? String(contentsOfFile: path, encoding: .utf8) {
94-
if let lastChar = home.last, lastChar.isNewline {
95-
return String(home.dropLast())
96-
}
97-
98-
return home
99-
}
100-
101-
fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.")
102-
}

0 commit comments

Comments
 (0)