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

Align resource bundle accessor generation with SPM #6146

Merged

Conversation

danibachar
Copy link
Collaborator

@danibachar danibachar commented Apr 2, 2024

Resolves #6149

Short description πŸ“

Comply to SPM bundle accessors, both for internal and external module.
Following up on this PR with a more novel solution.

Currently Tuist have a mix of how it generates bundle accessors for both ObjC and Swift run time. This PR tries to unify them and comply to SPM logic.

How to test the changes locally 🧐

Include a set of steps for the reviewer to test the changes locally (see the documentation for reference).

Use the disableBundleAccessors: true option.

In a target with Swift code and Resources, code will be generated and linked to the target to support the Bundle.module accessor - similar to SPM.

In a target with Objective-C code and Resources, code will be generated and linked to the target to support the SWIFTPM_MODULE_BUNDLE accessor macro - similar to SPM.

Contributor checklist βœ…

  • The code has been linted using run mise run lint:fix
  • The change is tested via unit testing or acceptance testing, or both
  • The title of the PR is formulated in a way that is usable as a changelog entry
  • In case the PR introduces changes that affect users, the documentation has been updated

Reviewer checklist βœ…

  • The code architecture and patterns are consistent with the rest of the codebase
  • Reviewer has checked that, if needed, the PR includes the label changelog:added, changelog:fixed, or changelog:changed, and the title is usable as a changelog entry

@danibachar danibachar mentioned this pull request Apr 2, 2024
6 tasks
@danibachar danibachar force-pushed the danibachar/spm-complaince-to-bundler-accessor branch from adcbe0d to 10a7f25 Compare April 2, 2024 20:56
@danibachar danibachar closed this Apr 2, 2024
@fortmarek fortmarek reopened this Apr 4, 2024
@fortmarek
Copy link
Member

@danibachar I think this is the right way to go about it. We should preserve the \(targetName)Resources accessor for internal targets, but there's no need to have it for external resources – for SPM dependencies, we should really closely follow whatever Apple is doing.

With this PR, we can also remove this line and it will resolve this issue.

Let me know if you have time to work on this, otherwise, I can take it over πŸ™

@danibachar
Copy link
Collaborator Author

Hi, yes, I opened this PR which lead me to the understanding people are probably using. the objc accessor to access the target resources from the outside.

I thought about it and I came to the conclusion that to comply with SPM we need to distinguish internal from public accessors.
With SPM if one wishes to allow accessing resources from a certain target they should write explicit code for that, its not given by SPM out of the box.

I'm going to add these changes + some others into my original PR if that is ok with you...

I think in this way we can comply with SPM - i.e generate internal accessors for both objc and swift runtime. In addition to allow public accessor for both (with the same objc class) like today.

struct BundleAccessorOptions {
 let internal: Bool // if true will create the internal accessors
 let public: Bool // if true will create the public accessor class, I do wonder if we should leave those in the same file as the internal ones?
}

Then the backward compatibility will be simple.
we keep the disableBundleAccessors: Bool and add a deprecation warning.

let bundleAccessorOptions = BundleAccessorOptions(internal: !disableBundleAccessors, public: !disableBundleAccessors)

WDYT?

@fortmarek
Copy link
Member

I'd do two PRs:

  • One that for SPM creates only internal accessors
  • Possibly second one that allows users to control that. However, I'm not sure we need that level of control – from your use-case, making SPM accessors internal only should be good enough?

@danibachar
Copy link
Collaborator Author

danibachar commented Apr 4, 2024

I'd do two PRs:

  • One that for SPM creates only internal accessors
  • Possibly second one that allows users to control that. However, I'm not sure we need that level of control – from your use-case, making SPM accessors internal only should be good enough?

It will introduce breaking changes though - wouldn't it? I mean we don't know if users are using the TargetNameResource.swift class for external swift packages.
If I'd add the isExternal check like we do for Objc it might cause disruption for those who are using it (even though it sounds a bit abusing...

I have one example Fixture that is doing that - the app_spm_dependencies - it defines a Swift Package, and the app is using the codegen accessors to use resources (like colors, fonts) from that Package.

But to your question - yes it will solve my issue

I do think that changes here are quite simple and comprehensive, keeping backwards compatibility, and are not that big?

@fortmarek
Copy link
Member

It will introduce breaking changes though - wouldn't it? I mean we don't know if users are using the TargetNameResource.swift class for external swift packages.

We have never marketed that SPM resources can be available externally and I don't think our users should expect us to support that option when it's not available via vanilla integration. While technically a breaking change, I think it's something we can live with.

I do think that changes #6124 are quite simple and comprehensive, keeping backwards compatibility, and are not that big?

I'd still redo the PR and remove the ProjectDescription changes. If we don't have a use-case for the added granularity, I'd skip it. We can still consider it but on its own and as a follow-up.

@fortmarek
Copy link
Member

Closing in favor of: #6124

@fortmarek fortmarek closed this Apr 5, 2024
@danibachar danibachar reopened this Apr 18, 2024
Daniel Bachar and others added 5 commits April 17, 2024 23:52
# Conflicts:
#	Sources/TuistGenerator/Mappers/ResourcesProjectMapper.swift
#	Tests/TuistGeneratorTests/ProjectMappers/ResourcesProjectMapperTests.swift
@fortmarek fortmarek changed the title Fix Bundle Accessor - to comply with SPM bundle accessors codegen Align resource bundle accessor generation with SPM Apr 22, 2024
@fortmarek fortmarek added the changelog:fixed PR will be listed in the Fixed section of CHANGELOG label Apr 22, 2024
Copy link
Member

@fortmarek fortmarek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add the dependency from this issue into the app_with_spm_dependencies fixture to confirm the bug is resolved (and that we won't introduce a regression for this in the future?)

Otherwise, just a couple of nits after which we can merge this.

Comment on lines +249 to +252
/// Since \(target.name) is a \(
target
.product
), the bundle containing the resources is copied into the final product.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting is slightly odd. Is it actually treated as one comment? πŸ€”

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't touch this formatting, mise lint didn't change it, but indeed its considered as one comment - i.e one line

Example from the app_with_spm_dependencies, the derived file Derived/Sources/TuistBundle+Styles

...
/// Since Styles is a staticFramework, the bundle containing the resources is copied into the final product.
...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change it if you wish

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it's fine as long as it's treated as a single line of comment πŸ‘

@danibachar
Copy link
Collaborator Author

Note this following PR and related issue I have found with formatting the bundleName

Copy link
Member

@fortmarek fortmarek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work πŸ‘

@fortmarek fortmarek merged commit 3e2d777 into tuist:main Apr 24, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:fixed PR will be listed in the Fixed section of CHANGELOG
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Caching BrazeUI fails with compiler errors
2 participants