-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathPlugin.swift
More file actions
69 lines (61 loc) · 2.3 KB
/
Plugin.swift
File metadata and controls
69 lines (61 loc) · 2.3 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
import Foundation
import PackagePlugin
@main
struct CargoBuildPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
// Paths
let packageDir = context.package.directoryURL
let capiDir = packageDir.appendingPathComponent("capi")
// Use the plugin's work directory (sandboxed) to avoid macOS permission issues
let scratchTarget = context.pluginWorkDirectoryURL.appendingPathComponent("cargo-target")
// Auto-build is enabled by default. To disable automatic Cargo build, set `LIBCHEWING_AUTO_BUILD_CARGO=0`.
if ProcessInfo.processInfo.environment["LIBCHEWING_AUTO_BUILD_CARGO"] == "0" {
return []
}
// Prefer common cargo install locations; if not found, abort with a clear error
let fm = FileManager.default
let home = fm.homeDirectoryForCurrentUser.path
let candidates: [URL] = [
URL(fileURLWithPath: "/usr/bin/cargo"),
URL(fileURLWithPath: "/usr/local/bin/cargo"),
URL(fileURLWithPath: "/opt/homebrew/bin/cargo"),
URL(fileURLWithPath: "\(home)/.cargo/bin/cargo"),
]
var cargoURL: URL? = nil
for url in candidates {
if fm.fileExists(atPath: url.path) {
cargoURL = url
break
}
}
guard let cargo = cargoURL else {
struct UserError: Error, CustomStringConvertible {
let description: String
init(_ s: String) { description = s }
}
throw UserError(
"`cargo` not found on the system. Please install Rust (https://rustup.rs/) to enable automatic builds, or disable automatic Cargo build by setting `LIBCHEWING_AUTO_BUILD_CARGO=0` and run `scripts/build-cargo.sh` manually to produce the library before running `swift build`."
)
}
let manifestPath = capiDir.appendingPathComponent("Cargo.toml").path
let targetDir = scratchTarget.path
// Arguments: build the chewing_capi crate in release mode into the plugin workdir target
let args = [
"build",
"--release",
"--manifest-path",
manifestPath,
"--target-dir",
targetDir,
]
return [
.prebuildCommand(
displayName: "Building chewing_capi via cargo",
executable: cargo,
arguments: args,
environment: [:],
outputFilesDirectory: scratchTarget
)
]
}
}