BarredEwe / Prefire

🔥 A library based on SwiftUI Preview, for easy generation: Playbook view, Snapshot and Accessibility tests
Apache License 2.0
290 stars 18 forks source link

Wrap `PreviewModels` in `DEBUG` directives. #22

Closed markst closed 5 months ago

markst commented 1 year ago

In our case we have our PreviewProvider's wrapped in compiler directives. This is due to MediaMock being also wrapped in compiler directives and so forth.

#if DEBUG
import SwiftUI
import Common
import Prefire

internal class BannerModuleViewPreview: PreviewProvider, PrefireProvider {
    static var previews: some View {
        UIElementPreview {
            ViewRepresentable(
                view: BannerModuleView(
                    media: MediaMock.bannerMock()
                )
            )
            .snapshot(delay: 0.1, precision: 1)
            .frame(width: 375, height: 400)
            .padding(16)
            .previewLayout(.sizeThatFits)
        }
    }
}
#endif
markst commented 1 year ago

I wonder if there is a better solution? It would be much better to be able to only perform plugin .when(configuration: .debug)

BarredEwe commented 1 year ago

I think we can wrap only the part with the call. This will save work in places where #if DEBUG is not used.

public static var models: [PreviewModel] = {
    var views: [PreviewModel] = []
    #if DEBUG
    {% for type in types.types where type.implements.PrefireProvider or type.based.PrefireProvider or type|annotated:"PrefireProvider" %}
    views.append(contentsOf: createModel(for: {{ type.name }}.self, name: "{{ type.name | replace:"_Previews","" | replace:"_Preview","" }}"))
    {% endfor %}
    #endif
    return views.sorted(by: { $0.name > $1.name || $0.story ?? "" > $1.story ?? "" })
}()
markst commented 5 months ago

Not sure whether we can configure Sourcery to identify if a PrefireProvider is within a DEBUG compiler directive.

I suppose we may not want to wrap in DEBUG since some people may wish to create a distribution build with Playbook?

markst commented 5 months ago

We could introduce a new compiler flag such as NO_PLAYBOOK which the consumer target could pass?

swiftSettings: [
    .define("NO_PLAYBOOK", .when(configuration: .release)),
]
BarredEwe commented 5 months ago

@markst Very cool and simple solution!

BarredEwe commented 5 months ago

I suppose we may not want to wrap in DEBUG since some people may wish to create a distribution build with Playbook?

I think so, it is better to leave this feature configurable.

BarredEwe commented 5 months ago

@markst Can you please add information about this flag in Readme.md?