Swiftgen: Support `CaseIterable` or iteration over generated assets

Created on 7 Jun 2019  路  10Comments  路  Source: SwiftGen/SwiftGen

I saw this closed issue: https://github.com/SwiftGen/SwiftGen/issues/571

I'd like to reopen this discussion. I am currently converting a bunch of icons to image assets, and use SwiftGen to generate an enum for all available icons.

However, I want to be able to list all available images in the icon set. The (stripped) generated code currently looks like this:

internal enum Asset {
  internal enum Colors {
    internal static let skyBlue = ColorAsset(name: "skyBlue")
  }
  internal enum Images {
    internal enum MapIcons {
      internal static let accessDenied = ImageAsset(name: "Map Icons/AccessDenied")
    }
  }
}

I'd love for the generated enums to implement CaseIterable, since that would allow people to list all available values. Is this supported in some way?

I now manually add it manually after each generation.

internal enum Asset {
  internal enum Colors {
    internal static let skyBlue = ColorAsset(name: "skyBlue")
  }
  internal enum Images {
    internal enum MapIcons: CaseIterable {
      internal static let accessDenied = ImageAsset(name: "Map Icons/AccessDenied")
    }
  }
}

Is this something that SwiftGen could support?

Most helpful comment

All 10 comments

CaseIterable won't do anything though, as there are no enum cases, see my 1st comment in that issue:

The thing with CaseIterable is that there need to be actual cases in the enums. In our (SwiftGen) case, the enums are empty and only used for namespacing purposes, note that we only have static constants.

Yeah, CaseIterable probably isn't the tool then, but you could generate an allImages property that just includes all generated images within a namespace. This would be immensely helpful.

There we go, thank you!

@ikesyo How do you provide the template with allValues? I've tried:

xcassets:
  allValues:
  inputs:
    - Pinny/Assets/Colors.xcassets
    - Pinny/Assets/Images.xcassets
  outputs:
    - templateName: swift4
      output: Pinny/Assets/Assets.swift

and

xcassets:
  inputs:
    - Pinny/Assets/Colors.xcassets
    - Pinny/Assets/Images.xcassets
  outputs:
    - templateName: swift4
      allValues:
      output: Pinny/Assets/Assets.swift

but none of this works.

It should be:

xcassets:
  inputs:
    - Pinny/Assets/Colors.xcassets
    - Pinny/Assets/Images.xcassets
  outputs:
    - templateName: swift4
      output: Pinny/Assets/Assets.swift
      params:
        allValues: true

Thanks! I checked the documentation, but didn't see an example of how to provide custom params. I tried your suggestion and it worked perfectly!

hey @danielsaidi ,

I tried to use allValues: true in my yml file. But it still doesn't create allValues with list of all values. I am missing any thing?

I am using swiftgen v6.4.0 thanks.

outputs:
        templateName: structured-swift5
        output: MyCopy.swift
        params:
            allValues: true

cc: @djbe

@mysticvalley Sorry, but I don鈥檛 use allValues in my current apps atm 馃槙

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stuffmc picture stuffmc  路  3Comments

filip-zielinski picture filip-zielinski  路  3Comments

gorbat-o picture gorbat-o  路  5Comments

danielsaidi picture danielsaidi  路  7Comments

djbe picture djbe  路  6Comments