Resource Rewriter for Xcode

1.1.0

Xcode 15+ plugin to migrate string-based asset literals to compile-time checked resource literals
idrougge/ResourceRewriterForXcode

What's New

1.1.0

2024-02-26T14:08:29Z

What's Changed

  • Add support for rewriting colour literals as well.

Full Changelog: 1.0.0...1.1.0

Resource Rewriter for Xcode 15+

This plugin lets you automatically rewrite UIKit/SwiftUI image and colour instantations from unreliable string-based inits such as:

UIImage(named: "some icon")
Image("some icon")
UIColor(named: "light blue green")
Color("light blue green")

into ImageResource and ColorResource literals (as introduced in Xcode 15) such as:

UIImage(resource: .someIcon)
Image(.someIcon)
UIColor(resource: .lightBlueGreen)
Color(.lightBlueGreen)

Installation

  • In Xcode, go to File → Add Package Dependencies and enter the URL for this repository.
  • In the following popup, select Add to Target: None as the package is for running only in Xcode and not part of your app itself.

Add to Target: None

  • In case your application is split into several packages, as is increasingly common, you also need to add the dependency to your package's Package.swift file to process images in that package:
dependencies: [
    .package(url: "https://github.com/idrougge/ResourceRewriterForXcode.git", branch: "main"),
]

Usage

After a rebuild, a secondary click on your project (or package) in the Project Navigator brings up a menu where you will now find the options "Rewrite image resource strings" and "Rewrite colour resource strings". Select that option and the target where you want your asset references to be fixed up.

Project menu

Cleanup

As the UIImage(named:) init returns an optional and UIImage(resource:) does not, you may now have if let, guard let or nil coalescing (??) statements that are no longer necessary. These you will have to fix up by yourself as it is beyond the capabilities of a simple plugin.

If you have turned off generated asset symbols, go into your build settings and enable Generate Asset Symbols (ASSETCATALOG_COMPILER_GENERATE_ASSET_SYMBOLS) or the resource names will not resolve.

After you are done, you are free to remove this dependency again, possibly introducing a linter rule forbidding calls to string-based asset inits.

Limitations

  • Short-hand calls such as image = .init(named: "Something") aren't handled.
  • Any image name built with string interpolation or concatenation is untouched as those must be resolved at run-time.
  • The plugin strives to follow Xcode's pattern for translating string-based asset names into ImageResource/ColorResource names but there may be cases where this does not match. Please open an issue in that case so it may added.
  • Functions or enums that return or accept string names, as well as wrapper functions or generated code must be rewritten manually if you wish to use ImageResource/ColorResource for those. You may fork and customise this plugin if such uses permeate your project.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Wed May 01 2024 07:34:52 GMT-0900 (Hawaii-Aleutian Daylight Time)