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

Steps to create and release artifactbundle. #838

Merged
merged 2 commits into from
Sep 10, 2023

Conversation

roman-aliyev
Copy link
Contributor

@roman-aliyev roman-aliyev commented Jul 15, 2023

PR adds ability to create custom Swift Package Plugins. The idea is described here.

Example of plugin implementation:

Package.swift

// swift-tools-version: 5.8
import PackageDescription

let package = Package(
    name: "RswiftPlugin",
    platforms: [
        .macOS(.v13),
        .iOS(.v14),
    ],
    products: [
        .library(name: "RswiftPluginDependencies", targets: [ "RswiftPluginDependencies" ]),
        .plugin(name: "RswiftPlugin", targets: [ "RswiftPlugin" ])
    ],
    dependencies: [
        .package(url: "https://github.com/mac-cain13/R.swift.git", exact: "7.3.2")
    ],
    targets: [
        .target(
            name: "RswiftPluginDependencies",
            dependencies: [
                .product(name: "RswiftLibrary", package: "R.swift")
            ]
        ),
        .plugin(
            name: "RswiftPlugin",
            capability: .buildTool(),
            dependencies: [ "RswiftBinary" ]
        ),
        .binaryTarget(
            name: "RswiftBinary",
            url: "https://github.com/mac-cain13/R.swift/releases/download/7.3.2/rswift-7.3.2.artifactbundle.zip",
            checksum: "ce20486625ff25bfb6119f5eea4147483b6c882cc0ccfc697c6e7edaaf4b9eb0"
        ),
    ]
)

RswiftPlugin.swift

import PackagePlugin
import Foundation

@main
struct RswiftPlugin: BuildToolPlugin {
    func createBuildCommands(
        context: PluginContext,
        target: Target
    ) throws -> [Command] {
        fatalError("not implemented")
    }
}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

extension RswiftPlugin: XcodeBuildToolPlugin {
    func createBuildCommands(
        context: XcodePluginContext,
        target: XcodeTarget
    ) throws -> [Command] {
        let sourceFiles = target.inputFiles
                        .filter { $0.type == .resource || $0.type == .unknown }
                        .map(\.path.string)
        
        return [
            .prebuildCommand(
                displayName: "Rswift",
                executable: try context.tool(named: "rswift").path,
                arguments: [
                    "generate", context.pluginWorkDirectory.appending(subpath: "R.generated.swift").string,
                    "--input-type", "input-files",
                ] + sourceFiles.flatMap { ["--input-files", $0 ] },
                outputFilesDirectory: context.pluginWorkDirectory
            )
        ]
    }
}

#endif

@abramchuk-v
Copy link

@RomanAliyev Great work, our team faced with the same issue(
@mac-cain13 @tomlokhorst Sorry guys for bothering you, but can you take a look on this PR, for my current project it'll simplify process a lot :)

@tomlokhorst tomlokhorst merged commit d8ef96f into mac-cain13:main Sep 10, 2023
@tomlokhorst
Copy link
Collaborator

Thanks for this PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants