forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginUtils.swift
More file actions
129 lines (109 loc) · 3.45 KB
/
PluginUtils.swift
File metadata and controls
129 lines (109 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import PackagePlugin
// Note: the JAVA_HOME environment variable must be set to point to where
// Java is installed, e.g.,
// Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home.
func findJavaHome() -> String {
if let home = ProcessInfo.processInfo.environment["JAVA_HOME"] {
return home
}
// This is a workaround for envs (some IDEs) which have trouble with
// picking up env variables during the build process
let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home"
if let home = try? String(contentsOfFile: path, encoding: .utf8) {
if let lastChar = home.last, lastChar.isNewline {
return String(home.dropLast())
}
return home
}
if let home = getJavaHomeFromSDKMAN() {
return home
}
if let home = getJavaHomeFromPath() {
return home
}
fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.")
}
func getSwiftJavaConfigPath(target: Target) -> String? {
let configPath = target.directoryURL.appending(component: "swift-java.config").path()
if FileManager.default.fileExists(atPath: configPath) {
return configPath
} else {
return nil
}
}
func getEnvironmentBool(_ name: String) -> Bool {
if let value = ProcessInfo.processInfo.environment[name] {
switch value.lowercased() {
case "true", "yes", "1": true
case "false", "no", "0": false
default: false
}
} else {
false
}
}
extension PluginContext {
var outputJavaDirectory: URL {
self.pluginWorkDirectoryURL
.appending(path: "src")
.appending(path: "generated")
.appending(path: "java")
}
var outputSwiftDirectory: URL {
self.pluginWorkDirectoryURL
.appending(path: "Sources")
}
func cachedClasspathFile(swiftModule: String) -> URL {
self.pluginWorkDirectoryURL
.appending(path: "\(swiftModule)", directoryHint: .notDirectory)
}
}
func getJavaHomeFromSDKMAN() -> String? {
let home = FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent(".sdkman/candidates/java/current")
let javaBin = home.appendingPathComponent("bin/java").path
if FileManager.default.isExecutableFile(atPath: javaBin) {
return home.path
}
return nil
}
func getJavaHomeFromPath() -> String? {
let task = Process()
task.executableURL = URL(fileURLWithPath: "/usr/bin/which")
task.arguments = ["java"]
let pipe = Pipe()
task.standardOutput = pipe
do {
try task.run()
task.waitUntilExit()
guard task.terminationStatus == 0 else { return nil }
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard
let javaPath = String(data: data, encoding: .utf8)?
.trimmingCharacters(in: .whitespacesAndNewlines),
!javaPath.isEmpty
else { return nil }
let resolved = URL(fileURLWithPath: javaPath).resolvingSymlinksInPath()
return
resolved
.deletingLastPathComponent()
.deletingLastPathComponent()
.path
} catch {
return nil
}
}