codegen: I have a script that extracts info from p...
# plugins
p
codegen: I have a script that extracts info from python sources to generate a config file and an api spec. It seems like this is the doc I need: https://www.pantsbuild.org/docs/plugins-codegen But it seems to be very protobuf centric, talking about protocols and whatnot. I'm doing py -> ini and py -> json, not whatever protobuf does. Does anyone have a simple example like this?
a
its quite simple actually. I made one just the other day.
I cant share the code tho
c
In the docker backend is a completely different use of codegen. It conditionally generates Dockerfile from an instructions field.
a
sorry, was pulled away to cook dinner.
🍛 1
c
and I to breakfast 😛
a
Following https://www.pantsbuild.org/docs/plugins-codegen I did: 1. Created a
SingleSourceField
subclass for my input on my
GenerateSourcesRequest
(I guess you'd use
PythonSourceField
) (Note: This file doesn't need to exist) 2. Used
PythonSourceField
as my output. (At a guess you'll need to create a
FrobulatorIniSourceField
) 3. Did Step #3 for the Union rule 4. For step #4 you can stick whatever you want in your
@rule
you just need to generate a
Snapshot
containing the files as you'd expect to see them on disk. (i.e.
src/python/foo.py
which I calculated with:
f"{target.address.spec_path}/{target.address.target_name}.py"
Because my 'codegen' code was pure-python, I just did it directly in the
@rule
. Less than 100 lines to: • get the target and it's files deps • strip the files source-roots • retrieve the contents of the files • parse the python • spit out a
Digest
&
Snapshot
https://gist.github.com/xlevus/f1ff1a20fe7b32ed5e244dcee8bb1867 Roughly (sorry if this doesn't 100% work, I'm hand-transcribing it)
❤️ 2
honestly it's kinda stupid how easy it is.
makes me go "I could codegen EVERYTHING"
💯 1
c
I think it’s a learning curve, once you get over a certain threshold it’s a joy to work with, but it takes a while to get there. (applies to more than just codegen)
a
yeah.
holding a session on friday with my team to try and explain to them the basics
👍 1
p
😆 "Frobulator"
c
@proud-dentist-22844 I’ll take a stab at that explanation. Codegen only works in source_X => source_Y (X and Y may be the same, as used in the docker backend for example). In other words, you define a subclass of
GenerateSourcesRequest
for a setup of target/destination source field types. So whenever the sources for a target is hydrated, the engine looks for any such subclasses that can translate what you have into the target source type (provided that codegen is enabled on the source hydrate request).
p
ooh. The Dockerfile example was helpful. I'm slowly piecing it together.
👍 1
So I'm using some bogus content 😛
asdf\n
to test outputing a "generated" file. But
./pants export-codegen
puts it under
dist/codegen
- is there a way to get that written (or moved/copied) to the actual workspace? edit: that's a generic question. I'll post it as a new top-level question