google / proto-lens

API for protocol buffers using modern Haskell language and library patterns.
https://google.github.io/proto-lens
BSD 3-Clause "New" or "Revised" License
465 stars 110 forks source link

auto-expose generated modules #373

Open tim2CF opened 4 years ago

tim2CF commented 4 years ago

Hi guys! Is there any way to export all modules from Haskell library? I just have really a lot (hundreds) modules generated by proto-lens tool from Google Protobuf files. It's very inconvenient to write them all manually in exposed-modules section.

Here is example of how it's working now. Just imagine that protobuf schema contains not 2 but ~200 modules which you want to export from your library. https://github.com/google/proto-lens/blob/7bb31dc868161a756b2744676425ac8f7e5860fa/proto-lens-tutorial/person/package.yaml#L18-L20

judah commented 4 years ago

I believe there isn't a way to do this right now, but you bring up a good point. When we originally set this up we had the opposite problem, namely, the set of .protos was large and we only wanted to use a small subset. But for your situtation, a blanket "export all" seems sensible.

Maybe we should change defaultMainGeneratingSpecificProtos to do what you want?

http://hackage.haskell.org/package/proto-lens-setup-0.4.0.3/docs/Data-ProtoLens-Setup.html#v:defaultMainGeneratingSpecificProtos

Currently it filters on exposed-modules/other-modules like defaultMainGeneratingProtos does, but that seems like a mistake given the API (it already takes an explicit list of files). In your Setup.hs you could walk the proto directory (e.g., with callProcess "find") and pass the result into that function.

Alternately, another idea I can think of is to make Data.ProtoLens.Setup change its behavior based on a custom Cabal flag. For example, with hpack:

extra-source-files: proto/**/*.proto
verbatim:
  x-proto-lens-export-all: true

It has the benefit of not being a "breaking" change to defaultMainGeneratingSpecificProtos. It's also simpler for your case since you don't have to walk the tree yourself. The downside is that it adds more complexity to our UX and wouldn't be as flexible (e.g., if you want all protos except one or two).

tim2CF commented 4 years ago

I like option with custom Cabal (I guess will work for Stack as well?) flag for its simplicity of usage. Maybe we can make it more flexible with transition from boolean type to regex type? Then we can have features similar to filtering and negation which will be enough for most cases. Something like this (just dummy example)

extra-source-files: proto/**/*.proto
verbatim:
  x-proto-lens-export: ^proto\/(?!private).+\.proto$