R.swift

repository·main·Indexed 27 days ago

https://github.com/mac-cain13/r.swift

A code generation tool for Swift projects that provides strongly typed, autocompleted access to project resources such as images, fonts, colors, localized strings, nibs, storyboards, and Info.plist values. It replaces string-based lookups with compile-time checked properties to prevent runtime crashes. Supports installation via Swift Package Manager (SPM) build tool plugins, CocoaPods, and manual CLI.

Tokens
5K
Snippets
14
Records
40
Agent score
94%

What's inside R.swift

  1. Upgrade to R.swift 7.0 (Mint or Manual CLI)

    main

    If you use Mint or call the rswift executable manually instead of SPM:

    1. Remove old dependencies: Remove the separate R.swift.Library dependency.
    2. Add SPM dependency: Add github.com/mac-cain13/R.swift.git as an SPM dependency.
    3. Configure Targets: Add RswiftLibrary to your targets.
  2. Upgrade to R.swift 7.0 (SPM)

    main

    When upgrading to version 7.0 using Swift Package Manager (SPM), follow these steps to transition from the old manual/CocoaPods setup to the new Build Tool Plugin system:

    1. Remove old dependencies: Delete R.swift.Library and the R.generated.swift file from your project.
    2. Remove old Build Phase: Delete the custom Run Script Build Phase that invokes the rswift binary.
    3. Add SPM dependency: Add github.com/mac-cain13/R.swift.git as a dependency.
    4. Configure Targets: Add RswiftLibrary to your targets.
    5. Add Build Tool Plugin: Under the "Run Build Tool Plug-ins" Build Phase, add either RswiftGenerateInternalResources or RswiftGeneratePublicResources.
    6. Fix Xcode Plugin Execution: Right-click on your project and run RswiftXcodeModifyPackages to ensure the build tool plugin runs correctly during builds.
  3. Install R.swift in an Xcode project using SPM (Recommended)

    main

    To use R.swift in a standard Xcode project, use Swift Package Manager (SPM) and the built-in Build Tool Plugins. This is the recommended installation method for R.swift 7+.

    1. In Project Settings, go to the Package Dependencies tab, click +, search for https://github.com/mac-cain13/R.swift, and click Add Package.
    2. Select the target that will use R.swift next to RswiftLibrary and click Add Package.
    3. Select your target, go to the Build Phases tab, find the Run Build Tool Plug-ins section, click +, and add RswiftGenerateInternalResources.
    4. Build your project. You may need to click the build error warning to approve the new plugin on the first run.

    Once installed, the R struct will be available for autocompletion in your code.

  4. Upgrade to R.swift 4.0 (Swift 4 / Xcode 9)

    main

    Version 4.0 requires Swift 4 and Xcode 9. Key changes include:

    • Command Change: You must now use the generate command when running R.swift.
    • Color Assets: Support for CLR-files is deprecated. Use named Color assets instead. CLR-based colors have moved from R.color.* to R.clr.*.
  5. Coding guidelines for generated code

    main

    When writing logic that generates code for users, adhere to these principles:

    • Safety: Never crash. Avoid using force-unwraps (!), for example.
    • Reliability: Always ensure the generated code compiles. It is better to skip an item than to generate corrupt or uncompilable code.
    • Clarity: Prioritize clarity over brevity. Avoid using R.swift.Library methods in generated code.
    • Documentation: Generate inline comments where relevant to help the end-user.
  6. Install R.swift in a Package.swift based SPM project

    main

    For projects managed via Package.swift, add R.swift as a dependency and attach the appropriate plugin to your targets.

    1. Add the package dependency to your Package.swift.
    2. For each target that needs resource generation, add RswiftLibrary to dependencies and RswiftGeneratePublicResources to plugins.
    dependencies: [
        .package(url: "https://github.com/mac-cain13/R.swift.git", from: "7.0.0")
    ]
    
    // In your target definition:
    .target(
        name: "Example",
        dependencies: [.product(name: "RswiftLibrary", package: "R.swift")],
        plugins: [.plugin(name: "RswiftGeneratePublicResources", package: "R.swift")]
    )
  7. Submit pull requests for new features

    main

    When implementing a new feature via a pull request, follow these best practices to increase the likelihood of your changes being merged:

    • Verify the feature isn't already being built by checking existing issues and pull requests.
    • Discuss the proposed change in an issue before implementation.
    • Keep pull requests small to facilitate easier review.
    • Adhere to the project's coding guidelines.
  8. Register and dequeue reusable cells with R.reuseIdentifier and R.nib

    main

    To use R.swift with reusable cells (Table or Collection views):

    1. In Interface Builder, set the cell's Identifier field to match the name you intend to use.
    2. Use R.nib.[cellName] to register the cell.
    3. Use R.reuseIdentifier.[cellName] to dequeue the cell.
  9. Upgrade to R.swift 1.0 (Breaking Changes)

    main

    Version 1.0 introduced several breaking changes:

    • Dependency: Generated code now depends on R.swift.Library. CocoaPods users are unaffected, but manual users must include this library.
    • Property to Function: Properties that instantiate new objects are now functions.
      • R.image.settingsIcon $\rightarrow$ R.image.settingsIcon()
      • R.file.someJson $\rightarrow$ R.file.someJson()
    • Initialization: Shorter functions replace .initialize() or .initiate().
      • R.storyboard.main.initialize() $\rightarrow$ R.storyboard.main()
      • R.nib.someView.initiate() $\rightarrow$ R.nib.someView()
    • Nib Loading: R.nib.someView.firstView(nil, options: nil) $\rightarrow$ R.nib.someView.firstView(owner: nil)
    • Validation: R.validate() now throws errors. Use R.assertValid() for debug/non-optimized builds.
  10. Coding guidelines for R.swift contributors

    main

    When contributing to the R.swift source code, follow these principles:

    • Consistency: Follow existing patterns and code styles. Improvements to patterns or styles should be submitted in a separate issue or PR.
    • User Awareness: Warn the user if you are skipping items during processing.
    • Defensive Programming: Code defensively, as many formats being parsed are undocumented and subject to change without notice.
  11. Upgrade to R.swift 6.0 (Build Phase changes)

    main

    When upgrading to 6.0, update your Build Phase settings as follows:

    • Uncheck "Based on dependency analysis" to ensure R.swift runs on every build.
    • Remove $TEMP_DIR/rswift-lastrun from the "Input Files" list.
    • Keep $SRCROOT/[YOUR_PATH]/R.generated.swift in the "Output Files" list.