Skip to content

Commit df0daa4

Browse files
committed
WIP: add article documenting bundle and lookupFunction
1 parent fbcd225 commit df0daa4

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Customise loading of resources
2+
3+
By default, the built-in templates provided by SwiftGen will load resources from the same bundle as where the generated code is located at:
4+
- If your generated code is in the main app, it will load resources from the `main` bundle.
5+
- If your generated code is in a Framework, it will load resources from that Framework's bundle.
6+
7+
This works for most users, but there may be sitations where you'll need a more advanced solution.
8+
9+
## Override the default Bundle
10+
11+
Let's say you are developing a CocoaPods Pod for example. CocoaPods recommends the use of the `resource_bundles` option ([more info](https://guides.cocoapods.org/syntax/podspec.html#resource_bundles)) for adding resources to your Pod. Using this option, your resources will be grouped into a separate bundle.
12+
13+
The advantage of this is that, no matter how an end-user adds your Pod to their project, your resources won't ever clash with their resources. This is especially important with static linking, where CocoaPods won't generate a Framework for each Pod and add everything to the main App bundle of a user.
14+
15+
The disadvantage is that your resources are no longer located in the same bundle as your generated code, but in (for example) a sub-bundle `MyAwesomePodResources.bundle`. The generated code by SwiftGen will no longer work.
16+
17+
### Solution
18+
19+
To fix this, you can set the `bundle` template parameter to point to something in your code that represents your resources bundle.
20+
21+
Let's say you have the following Swift code somewhere in your app:
22+
23+
```swift
24+
final class MyAwesomePod {
25+
// This is the bundle where your code resides in
26+
static let bundle: Bundle = {
27+
Bundle(for: MyAwesomePod.self)
28+
}()
29+
30+
// Your resources bundle is inside that bundle
31+
static let resourcesBundle: Bundle = {
32+
guard let url = bundle.url(forResource: "MyAwesomePodResources", withExtension: "bundle"),
33+
let bundle = Bundle(url: url) else {
34+
fatalError("Can't find 'MyAwesomePodResources' bundle")
35+
}
36+
return bundle
37+
}()
38+
}
39+
```
40+
41+
You want SwiftGen to generate code to load resources from `MyAwesomePod.resourcesBundle`. Update your configuration file and add the `bundle` parameter, like so:
42+
43+
```yaml
44+
input_dir: Resources
45+
output_dir: Sources
46+
strings:
47+
inputs: en.lproj/Localizable.strings
48+
outputs:
49+
templateName: structured-swift5
50+
output: Generated/Strings.swift
51+
params:
52+
bundle: MyAwesomePod.resourcesBundle
53+
```
54+
55+
Run SwiftGen again to update the generated coide, and voila! Your generated code now works with a resources bundle.
56+
57+
## Override the lookup function
58+
59+
TODO

0 commit comments

Comments
 (0)