R.swift icon indicating copy to clipboard operation
R.swift copied to clipboard

Document how to use R.swift together with SPM

Open SagarPrajapati98 opened this issue 5 years ago • 11 comments

have used swift package manager for R.swift but i can't see any document for generating R.generated.swift using Swift package manager

SagarPrajapati98 avatar Feb 14 '20 04:02 SagarPrajapati98

We'll have to write a proper documentation in the Readme. In short:

  • Use SPM to add https://github.com/mac-cain13/R.swift.Library to your project
  • Use rswift executable in some other way. It's currently not possible to use development dependencies in SPM (like devDependencies in NPM)

tomlokhorst avatar Apr 22 '20 18:04 tomlokhorst

have used swift package manager for R.swift but i can't see any document for generating R.generated.swift using Swift package manager

There's a walkthrough setting up R.swift with SPM here :) https://blog.apptekstudios.com/2019/12/spm-xcode-build-tools/

apptekstudios avatar Jul 15 '20 10:07 apptekstudios

Is it possible to use R.swift with resources inside SPM packages? This was added in Xcode 12.

szotp avatar Sep 26 '20 03:09 szotp

@szotp yes, you can embed resources in your Swift Package and the R.generated.swift will work fine when adding the dependency R.swift.Library, that means someone, or yourself can use you packaged containing R.swift-referenced resources. (You might need a "ResourceBundle hack" so that it uses Bundle.module

However as @tomlokhorst said, Use swift executable in some other way. It means you'll have to run the "generate" command manually when adding new resources to your Swift package. Where in the past, your framework likely was maintained by an Xcodeproj with a Script Build phase.

Ideally what I would love to see for a Swift package opened in Xcode, is an Xcode extension running R.swift generate on your current target sources...

..and ideally, the same for SwiftLint ;)

quentinfasquel avatar Nov 27 '20 16:11 quentinfasquel

Any update on this ? I would love to stop maintaining xcodeproj and only focus on Package.swift, however R.swift cannot generate R.generated.swift without an Xcodeproj file... !

quentinfasquel avatar Feb 16 '21 09:02 quentinfasquel

@tomlokhorst I've worked on a branch that allows usage of R.swift to work on a given Swift Package & target name, without using Xcodeproj. It may not be perfect but it seems to do the job. I'm happy to work a bit more on it but would gladly accept your advice on what to Unit Test & which command-line arguments would be preferred.

See here https://github.com/quentinfasquel/R.swift/tree/feature/generate-swift-package-resources

quentinfasquel avatar Mar 08 '21 14:03 quentinfasquel

@quentinfasquel Does your version allow to generate R.swift for separate package without xcproj? It would be cool to see this feature in master, did you try to create a PR?

mmbabaev avatar May 09 '21 02:05 mmbabaev

@mmbabaev yes indeed, the version I suggested does work to generate R.swift.generated for a given Swift package, which doesn't rely on an xcodeproj.

The typical example would be a Swift package library that internally uses R.swift for its resources. Currently there's no way around that without using an xcodeproj. It's restricting developers to move forward with SPM, or could have them give up R.swift if they decide to move forward with it fully. I find it quite annoying to have to maintain an xcodeproj when you've discovered the ease of a Package.swift.

However I did not create a PR for a few reasons. The approach I took forces swift tools 5.3 and actually depends on SPM itself to retrieve the package's resources. I would like to have a followup with the maintainers of R.swift and possibly have it reach master like you suggest.

quentinfasquel avatar May 09 '21 09:05 quentinfasquel

I don't know if this can work like SwiftFormat. For example: https://github.com/nicklockwood/SwiftFormat#using-swift-package-manager It does this by creating a BuildTools folder and working in it

SpectatorNan avatar Dec 08 '21 15:12 SpectatorNan

have used swift package manager for R.swift but i can't see any document for generating R.generated.swift using Swift package manager

There's a walkthrough setting up R.swift with SPM here :) https://blog.apptekstudios.com/2019/12/spm-xcode-build-tools/

I refer to SwiftFormat and report an error when running build phases. it report: An error occurred: Missing value for `SDKROOT image

my Package.swift :

// swift-tools-version:5.4
import PackageDescription

let package = Package(
    name: "BuildTools",
    platforms: [
        .macOS(.v10_11)
    ],
    dependencies: [
        .package(url: "https://github.com/mac-cain13/R.swift", from: "5.4.0"),
        .package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.48.10")
    ],
    targets: [
        .target(name: "BuildTools", path: "")
    ])

and my script :

# Type a script or drag a script file from your workspace to insert its path.
cd BuildTools
SDKROOT=(xcrun --sdk macosx --show-sdk-path)

#Temporarily uncomment the following line to update your packages (based on the versions defined in BuildTools/Package.swift)
#swift package update

#Run your build tool - using swiftformat as an example
swift run -c release rswift generate "$SRCROOT/generated/R.generated.swift"

SpectatorNan avatar Dec 08 '21 17:12 SpectatorNan

I guess R.swift not yet support SPM with resources bundle? Seems R.swift will scan resources bundle in Xcode Target only.

keithcml avatar Dec 24 '21 05:12 keithcml