Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Xcode 16 plugin to use versioned release build #109

Merged
merged 13 commits into from
Sep 3, 2024
18 changes: 18 additions & 0 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ let package = Package(
name: "SafeDIGenerator",
targets: ["SafeDIGenerator"]
),
.plugin(
name: "InstallSafeDITool",
targets: ["InstallSafeDITool"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.0"),
Expand Down Expand Up @@ -110,6 +114,20 @@ let package = Package(
.swiftLanguageMode(.v6),
]
),
.plugin(
name: "InstallSafeDITool",
capability: .command(
intent: .custom(
verb: "safedi-release-install",
description: "Installs a release version of the SafeDITool build plugin executable."
),
permissions: [
.writeToPackageDirectory(reason: "Downloads the SafeDI release build plugin executable into your project directory."),
.allowNetworkConnections(scope: .all(ports: []), reason: "Downloads the SafeDI release build plugin executable from GitHub."),
]
),
dependencies: []
),

// Core
.target(
Expand Down
91 changes: 91 additions & 0 deletions Plugins/InstallSafeDITool/InstallCLIPluginCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Distributed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation
import PackagePlugin

@main
struct InstallSafeDITool: CommandPlugin {
func performCommand(
context: PackagePlugin.PluginContext,
arguments _: [String]
) async throws {
guard let safeDIOrigin = context.package.dependencies.first(where: { $0.package.displayName == "SafeDI" })?.package.origin else {
print("No package origin found for SafeDI package")
return
}
switch safeDIOrigin {
case let .repository(url, displayVersion, _):
let expectedToolFolder = context.package.directoryURL.appending(
components: ".safedi",
displayVersion
)
let expectedToolLocation = expectedToolFolder.appending(component: "safeditool")

guard let url = URL(string: url)?.deletingPathExtension() else {
print("No package origin found for SafeDI package")
return
}
#if arch(arm64)
let toolName = "SafeDITool-arm64"
#elseif arch(x86_64)
let toolName = "SafeDITool-x86_64"
#else
print("Unexpected architecture type")
return
#endif

let downloadURL = url.appending(
components: "releases",
"download",
displayVersion,
toolName
)
let (downloadedURL, _) = try await URLSession.shared.download(
for: URLRequest(url: downloadURL)
)

try FileManager.default.createDirectory(
at: expectedToolFolder,
withIntermediateDirectories: true
)
try FileManager.default.moveItem(
at: downloadedURL,
to: expectedToolLocation
)
let gitIgnoreLocation = expectedToolFolder.appending(component: ".gitignore")
if !FileManager.default.fileExists(atPath: gitIgnoreLocation.path()) {
try """
safeditool
""".write(
to: gitIgnoreLocation,
atomically: true,
encoding: .utf8
)
}

case .registry, .root, .local:
fallthrough

@unknown default:
print("Cannot download SafeDITool from \(safeDIOrigin)")
}
}
}
68 changes: 56 additions & 12 deletions Plugins/SafeDIGenerator/SafeDIGenerateDependencyTree.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Distributed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
Comment on lines +1 to +19
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. this header should always have been here


import Foundation
import PackagePlugin

Expand Down Expand Up @@ -39,16 +59,12 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin {
outputSwiftFile.path(),
]

let toolPath: URL = if FileManager.default.fileExists(atPath: Self.armMacBrewInstallLocation) {
// SafeDITool has been installed via homebrew on an ARM Mac.
URL(filePath: Self.armMacBrewInstallLocation)
} else if FileManager.default.fileExists(atPath: Self.intelMacBrewInstallLocation) {
// SafeDITool has been installed via homebrew on an Intel Mac.
URL(filePath: Self.intelMacBrewInstallLocation)
let toolLocation: URL = if let toolLocation = context.downloadedToolLocation {
toolLocation
} else {
// Fall back to the just-in-time built tool.
try context.tool(named: "SafeDITool").url
}

#else
let outputSwiftFile = context.pluginWorkDirectory.appending(subpath: "SafeDI.swift")
// Swift Package Plugins do not (as of Swift 5.9) allow for
Expand Down Expand Up @@ -79,7 +95,9 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin {
outputSwiftFile.string,
]

let toolPath: PackagePlugin.Path = if FileManager.default.fileExists(atPath: Self.armMacBrewInstallLocation) {
let armMacBrewInstallLocation = "/opt/homebrew/bin/safeditool"
let intelMacBrewInstallLocation = "/usr/local/bin/safeditool"
let toolLocation: PackagePlugin.Path = if FileManager.default.fileExists(atPath: Self.armMacBrewInstallLocation) {
// SafeDITool has been installed via homebrew on an ARM Mac.
PackagePlugin.Path(Self.armMacBrewInstallLocation)
} else if FileManager.default.fileExists(atPath: Self.intelMacBrewInstallLocation) {
Expand All @@ -94,17 +112,14 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin {
return [
.buildCommand(
displayName: "SafeDIGenerateDependencyTree",
executable: toolPath,
executable: toolLocation,
arguments: arguments,
environment: [:],
inputFiles: targetSwiftFiles + dependenciesSourceFiles,
outputFiles: [outputSwiftFile]
),
]
}

private static let armMacBrewInstallLocation = "/opt/homebrew/bin/safeditool"
private static let intelMacBrewInstallLocation = "/usr/local/bin/safeditool"
}

extension Target {
Expand Down Expand Up @@ -257,3 +272,32 @@ extension Data {
#endif
}
}

#if compiler(>=6.0)
extension PackagePlugin.PluginContext {
var safeDIVersion: String? {
guard let safeDIOrigin = package.dependencies.first(where: { $0.package.displayName == "SafeDI" })?.package.origin else {
return nil
}
switch safeDIOrigin {
case let .repository(_, displayVersion, _):
return displayVersion
case .registry, .root, .local:
fallthrough
@unknown default:
return nil
}
}

var downloadedToolLocation: URL? {
guard let safeDIVersion else { return nil }
let location = package.directoryURL.appending(
components: ".safedi",
safeDIVersion,
"safeditool"
)
guard FileManager.default.fileExists(atPath: location.path()) else { return nil }
return location
}
}
#endif
Loading