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

Respect excluded/included source file name environment variable

Open bazscott opened this issue 4 years ago • 11 comments

When I run rswift I get a warning warning: [R.swift] Skipping 2 images because symbol 'launchScreenLogo' would be generated for all of these images: LaunchScreenLogo, LaunchScreenLogo, which makes sense because there are two Asset Catalogues in the project with (different) images named the same, that's intentional.

I use xcconfig's to only compile in one of the Asset Catalogues at build time, depending on the configuration being built - App A style or App B style Asset Catalogue will be in the app, but never both. So I can say for sure that there will be only one of these images present when run.

Is there any way to make R.swift ignore that warning and generate for these images anyway?

bazscott avatar Feb 26 '20 00:02 bazscott

Can you provide an example project that demonstrates this?

Given your description, I'm unsure if there's a way for R.swift to see the difference between the two asset catalogs.

tomlokhorst avatar Apr 22 '20 14:04 tomlokhorst

Hi Tom,

Thanks for getting back on this. Best example I can give is in a current project I have the following setup:

image

Shared.xcassets has images shared between the two app targets. Target1 and Target2 asset catalogues have images that are named the same, but are a different image. e.g. LoginScreenLogo.png in both. So I can refer to that in code by a single name and a different looking image appears depending on the target that is built. I have xcconfig that will compile in Shared + Target1 OR Shared + Target2 but never both targets.

I hope that makes sense. It works well for "white label" type apps.

I realise this breaks a fundamental thing that R.swift does - checks for duplicates, and we want to preserve that behaviour. But I wonder if it would be possible to have a .R.swift.config type file that would allow exclusion of specifically names assets from that duplicate check (so the first one is picked up, but duplicates ignored). Or maybe I could tell R.swift to ignore Target2.xcassets completely? I'm thinking along the lines of how SwiftLint ignores files and tweaks settings.

And, yes, I realise this is not a small ask :) Just something to think about.

bazscott avatar Apr 22 '20 23:04 bazscott

Thanks for sharing your use case & info!

I'm wondering how your xcconfig looks like and how it picks between the xcassets, knowing a bit more about that setup might help understanding how R.swift can know what asset catalog is used or not during the build.

Could you share how the relevant part of the xconfig looks?

mac-cain13 avatar Apr 22 '20 23:04 mac-cain13

Sure.

Shared.xcconfig has:

EXCLUDED_SOURCE_FILE_NAMES = *.xcassets
INCLUDED_SOURCE_FILE_NAMES = Shared.xcassets

Target1.xcconfig has:

#include "Shared.xcconfig"
...
INCLUDED_SOURCE_FILE_NAMES = $(INCLUDED_SOURCE_FILE_NAMES) Target1.xcassets

And then the same for Target2, with a different asset catalogue name of course.

bazscott avatar Apr 22 '20 23:04 bazscott

Good to know, basically the feature request would be for R.swift to respect the excluded/included source file names. Turns out there was another issue about this that was closed by the reporter: https://github.com/mac-cain13/R.swift/issues/486

It's not super easy to build, but also not impossible. Especially we already have (a bit slow) globbing implementation for the ignore file.


Note to self: EXCLUDED_SOURCE_FILE_NAMES is available in the environment during build time.

mac-cain13 avatar Apr 22 '20 23:04 mac-cain13

I don't think that would work because both targets are included at different times, depending on which scheme is run, and of course R.swift doesn't know about schemes. You want to include one of the asset catalogues, but only one. So I think that would have to be put on to the dev (me) to decide which one. That's why I'm thinking a .r_swift_config.yml would be needed, or some other way of passing excluded files. Or maybe even just a flag to turn off the duplicate check 🤔

bazscott avatar Apr 23 '20 00:04 bazscott

Correct that R.swift doesn't know about schemes, it just gets info about the currently running build. I think that's fine for this use case.

Your Target1 and Target2 asset catalogs should contain the same images (or should contain a version of every images used when building that schema/target to be more exact). If this is the case the R.generated.swift file might change depending on the schema currently building, but that's fine. It just reflects the actual resources available in this exact build.

For example: It gives you more validation that you didn't forget to add a version of that image to Target2. Because if it is missing and you build, R.swift will remove it from the generated file and you will get a compile time error. But Target1 will build just fine if it has all images.

As long as we respect the exclude/include build setting I think this would work as it should. Or am I missing something?

mac-cain13 avatar Apr 23 '20 00:04 mac-cain13

Ahh, cool. That makes sense. I didn't realise R.swift was "build aware", I thought it just looked through the whole directory on disk. Sounds good, that should work then. Thanks 👍

bazscott avatar Apr 23 '20 00:04 bazscott

We're really looking in the Xcodeproj file and the environment so we pick up only the files that are really available. That makes R.swift work with quite some complex setups, think you're use case is a valid one to also support!

mac-cain13 avatar Apr 23 '20 09:04 mac-cain13

Thanks for looking in to it, I appreciate it 👍

bazscott avatar Apr 23 '20 10:04 bazscott

HI! I am facing the same issue. Could someone fix this?

Thanks in advance!

franciscorosso avatar May 13 '21 14:05 franciscorosso