Swiftgen: xcassets ignores "Provides Namespace"

Created on 18 Jul 2018  Â·  9Comments  Â·  Source: SwiftGen/SwiftGen

According to documentation, xcassets templates "take into account any namespacing folder in your Assets Catalogs". Yet no matter what value this option is, generated code is always the same.

SwiftGen

/// with "Provides Namespaces" unchecked on "Member" folder
let image = Asset.Member.profilSupp.image

/// with "Provides Namespaces" checked  on "Member" folder
let image = Asset.Member.profilSupp.image

R.swift

/// with "Provides Namespaces" unchecked on "Member" folder
let image = R.image.profil_supp

/// with "Provides Namespaces" checked on "Member" folder
let image = R.image.member.profil_supp

Tested with SwiftGen 5.3.0.

easy enhancement

Most helpful comment

All 9 comments

Did you check the "Provides namespace" checkbox on your folders/groups you created in your at catalog?

What we mean with it is that the default templates take into account wether the key should be "profile_supp" or "Member/profile_supp" when calling the .init(named: ). The structure of the generated enums doesn't take this into account though.

You could write a template that checkes wether the key contains the group name or not, and if so create a sub-enum or not. We could add this behaviour to the built-in templates, some sort of flag to onlyProvidesNamespaces or something.

@AliSoftware Yes and it is unchecked.
@djbe So the behaviour I see is correct and there is no bug, right? In that case I've been misleaded by documentation and R.swift behaviour, sorry!

Yeah the behavior is correct/intented, we only rely on the "provides namespace" feature of xcassets to create the structure/hierarchy, so that it matches what's committed by Xcode.

We could indeed imagine changing this behavior and always consider groups — even the ones with "provides namespace" unchecked/not enabled — to provide a namespace in the generated constants anyway, but it's more logical to me to tell people to enable namespaces in their xcasset catalog if they want that feature. It's as simple as checking a box after all and gets your assets better orgamized

@AliSoftware From my understanding that's what SwiftGen does actually. I'll try to re-explain my point :)

I have a xcasset folder with namespace unchecked:
screen shot 2018-07-18 at 11 54 11

Yet SwiftGen generates a namespaced constant (Asset.Member.profilSupp.image) for it while R.Swift does not (R.image.profil_supp)

From @djbe comment it is indeed intended behaviour, but from yours... it seems not :D

Ohhh ahah yeah I might have been confused and just did a Trump and telling the opposite of what is the intended behaviour 😂 tbh I know that there has been a choice here in SwiftGen (either rely on the folder structure to create sub-structures in the generated constants, or rely on the "Provides namespace"), but now I'm not sure which of the two choices we ended up choosing (I've replied to you above on a rush without checking anything).

What's important is that it's currently definitely one or the other but not both (i.e. it can't currently be customized by an existing flag that would be provided in the bundled templates)

We should propagate the namespaced property from our parser to the context, and we could add a flag to the bundled template to toggle the behaviour.

Propagating the flag is really easy. Adjusting the template is hard, because I hate naming things 😆 (need a name for the flag to toggle the behaviour)

I'll take a look see if I can do it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AliSoftware picture AliSoftware  Â·  6Comments

jrmybrault picture jrmybrault  Â·  7Comments

gereons picture gereons  Â·  5Comments

danielsaidi picture danielsaidi  Â·  7Comments

stuffmc picture stuffmc  Â·  3Comments