Skip to content

Commit cf57b46

Browse files
committed
wip
1 parent 89277e1 commit cf57b46

File tree

5 files changed

+72
-16
lines changed

5 files changed

+72
-16
lines changed

Plugins/JExtractSwiftCommandPlugin/JExtractSwiftCommandPlugin.swift

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ final class JExtractSwiftCommandPlugin: SwiftJavaPluginProtocol, BuildToolPlugin
7979
}
8080

8181
var arguments: [String] = [
82+
"jextract",
8283
"--swift-module", sourceModule.name,
8384
"--package-name", javaPackage,
8485
"--output-directory-java", context.outputDirectoryJava.path(percentEncoded: false),

Plugins/JExtractSwiftPlugin/JExtractSwiftPlugin.swift

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct JExtractSwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
5454
let outputDirectorySwift = context.outputDirectorySwift
5555

5656
var arguments: [String] = [
57+
"jextract",
5758
"--swift-module", sourceModule.name,
5859
"--package-name", javaPackage,
5960
"--output-directory-java", outputDirectoryJava.path(percentEncoded: false),

Sources/JExtractSwift/Swift2Java.swift

+17-7
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import JavaKitShared
2020

2121
/// Command-line utility, similar to `jextract` to export Swift types to Java.
2222
public struct SwiftToJava: ParsableCommand {
23-
public init() {}
23+
public static var _commandName: String { "jextract" }
24+
25+
public static var configuration = CommandConfiguration(
26+
commandName: Self._commandName,
27+
abstract: "Generate Java wrappers for Swift code.")
2428

25-
public static var _commandName: String {
26-
"jextract-swift"
27-
}
29+
30+
public init() {}
2831

2932
@Option(help: "The package the generated Java code should be emitted into.")
3033
var packageName: String
@@ -49,18 +52,23 @@ public struct SwiftToJava: ParsableCommand {
4952
var input: [String]
5053

5154
public func run() throws {
52-
let inputPaths = self.input.dropFirst().map { URL(string: $0)! }
55+
let inputPaths = self.input.map { URL(string: $0)! }
5356

5457
let translator = Swift2JavaTranslator(
5558
javaPackage: packageName,
5659
swiftModuleName: swiftModule
5760
)
5861
translator.log.logLevel = logLevel
59-
62+
6063
var allFiles: [URL] = []
6164
let fileManager = FileManager.default
6265
let log = translator.log
63-
66+
67+
guard !inputPaths.isEmpty else {
68+
log.warning("Input paths are empty!")
69+
return
70+
}
71+
6472
for path in inputPaths {
6573
log.debug("Input path: \(path)")
6674
if isDirectory(url: path) {
@@ -74,6 +82,8 @@ public struct SwiftToJava: ParsableCommand {
7482
}
7583
}
7684

85+
log.trace("Input file: \(allFiles)")
86+
7787
for file in allFiles where canExtract(from: file) {
7888
translator.log.debug("Importing module '\(swiftModule)', file: \(file)")
7989

Sources/SwiftJavaTool/JavaToSwift.swift

+12-9
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ import SwiftSyntaxBuilder
2424
import JavaKitConfigurationShared
2525
import JavaKitShared
2626

27-
@main
2827
struct JavaToSwift: AsyncParsableCommand {
29-
static var _commandName: String { "swift-java" }
28+
static var _commandName: String { "java2swift" }
29+
30+
static var configuration = CommandConfiguration(
31+
commandName: Self._commandName,
32+
abstract: "Generate Swift wrappers for Java code.")
3033

3134
@Option(help: "The name of the Swift module into which the resulting Swift types will be generated.")
3235
var moduleName: String?
@@ -61,10 +64,10 @@ struct JavaToSwift: AsyncParsableCommand {
6164
@Option(name: .shortAndLong, help: "The directory in which to output the generated Swift files or the swift-java configuration file.")
6265
var outputDirectory: String? = nil
6366

64-
67+
6568
@Option(name: .shortAndLong, help: "Directory where to write cached values (e.g. swift-java.classpath files)")
6669
var cacheDirectory: String? = nil
67-
70+
6871
var effectiveCacheDirectory: String? {
6972
if let cacheDirectory {
7073
return cacheDirectory
@@ -74,7 +77,7 @@ struct JavaToSwift: AsyncParsableCommand {
7477
return nil
7578
}
7679
}
77-
80+
7881
@Option(name: .shortAndLong, help: "How to handle an existing swift-java.config; by default 'overwrite' by can be changed to amending a configuration")
7982
var existingConfig: ExistingConfigFileMode = .overwrite
8083
public enum ExistingConfigFileMode: String, ExpressibleByArgument, Codable {
@@ -168,7 +171,7 @@ struct JavaToSwift: AsyncParsableCommand {
168171
print("[info][swift-java] Run: \(CommandLine.arguments.joined(separator: " "))")
169172
do {
170173
let config: Configuration
171-
174+
172175
// Determine the mode in which we'll execute.
173176
let toolMode: ToolMode
174177
if jar {
@@ -217,14 +220,14 @@ struct JavaToSwift: AsyncParsableCommand {
217220
let classpathFromEnv = ProcessInfo.processInfo.environment["CLASSPATH"]?.split(separator: ":").map(String.init) ?? []
218221
let classpathFromConfig: [String] = config.classpath?.split(separator: ":").map(String.init) ?? []
219222
print("[debug][swift-java] Base classpath from config: \(classpathFromConfig)")
220-
223+
221224
var classpathEntries: [String] = classpathFromConfig
222-
225+
223226
let swiftJavaCachedModuleClasspath = findSwiftJavaClasspaths(
224227
in: self.effectiveCacheDirectory ?? FileManager.default.currentDirectoryPath)
225228
print("[debug][swift-java] Classpath from *.swift-java.classpath files: \(swiftJavaCachedModuleClasspath)")
226229
classpathEntries += swiftJavaCachedModuleClasspath
227-
230+
228231
if !classpathOptionEntries.isEmpty {
229232
print("[debug][swift-java] Classpath from options: \(classpathOptionEntries)")
230233
classpathEntries += classpathOptionEntries
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import ArgumentParser
16+
import Foundation
17+
import Java2SwiftLib
18+
import JExtractSwift
19+
import JavaKit
20+
import JavaKitJar
21+
import JavaKitNetwork
22+
import JavaKitReflection
23+
import SwiftSyntax
24+
import SwiftSyntaxBuilder
25+
import JavaKitConfigurationShared
26+
import JavaKitShared
27+
28+
@main
29+
struct SwiftJavaMain: AsyncParsableCommand {
30+
static var _commandName: String { "swift-java" }
31+
32+
static var configuration = CommandConfiguration(
33+
abstract: "A utility for Swift and Java interoperability.",
34+
subcommands: [
35+
JavaToSwift.self,
36+
SwiftToJava.self,
37+
],
38+
defaultSubcommand: JavaToSwift.self
39+
)
40+
}
41+

0 commit comments

Comments
 (0)