I am very new to pants. I would like to programmat...
# plugins
p
I am very new to pants. I would like to programmatically run the equivalent of these two commands: 1.
pants generate-lockfiles --resolve=<resolve>
2.
pants export --resolve=<resolve>
with a single
pants init --resolve=<resolve>
goal. I have gone through the plugins docs/ tutorial and the source, but I can't figure out how to manipulate the inputs to a goal like
Get(GenerateLockfilesGoal, <inputT>, <input>)
or
Get(Export, <inputT>, <input>)
. Simply calling
Get(Export)
without any inputs works because it uses
--export-resolve=
argument. Am I going in the wrong direction?
e
You could probably just do this with a CLI alias Your 2 commands should be equivalent to
pants generate-lockfiles export --resolve=<resolve>
(not 100% sure, but I think this should work. So you could create an alias like
init = generate-lockfiles export
and then
pants init --resolve=<resolve>
should work
c
n.b. that providing a goal option on the command line as:
pants <goal> --option-name
is a shortcut for the
<goal>-option-name
option, i.e. equiv. to
pants <goal> --<goal>-option-name
in other words, you need to provide resolve to both goals,
Copy code
pants generate-lockfiles export --generate-lockfiles-resolve=... --export-resolve=...
using cli alias here is clever, but I don't see a way to avoid having to provide the resolve option twice unfortunately. (unless using a hack involving
.pants.bootstrap
involving env vars or parsing cli args manually)
p
I had tried @elegant-florist-94385's suggestion to use CLI alias with two commands, but I have to provide the resolve argument twice like @curved-television-6568 said. More to the point, I cannot provide the resolve as a single optional argument, which is what I am looking for, for ergonomics.
Is there an existing example for invoking a goal with constructed arguments, from another
goal_rule
? My thought was that I would create a new
init
goal that takes a
--resolve
argument and then use that argument to invoke
generate-lockfiles
and
export
goals programmatically. So, something like:
Copy code
class InitSubsystem(GoalSubsystem):
    name = "init"
    help = "Initialize the project environment"
    resolve = StrOption(default="root", help="Default resolve to export")


class Init(Goal):
    subsystem_cls = InitSubsystem
    environment_behavior = Goal.EnvironmentBehavior.LOCAL_ONLY

@goal_rule
async def init(
    init_subsystem: InitSubsystem, build_root: BuildRoot, dist_dir: DistDir, export_subsys: ExportSubsystem
) -> Init:
    await Get(GenerateLockfilesGoal, ...)
    await Get(Export, ...)
    return Init(exit_code=1)


def rules():
    return collect_rules()
I just can't figure out how to trigger the two required goals.
c
trigger goals by await'ing on their "Effect":
Copy code
await Effect(Export)
also, this must be invoked from your
goal_rule
not just any regular
rule
.
I'm not sure of a proper way to mutate the configuration from any (goal_)rule tho...
p
Thanks. Yes, need a way to mutate the configuration before
await Effect(Export)
, couldn't figure out how. I feel like I am missing something obvious, it can't be that hard to mutate/ create configuration, can it? After all, some form of this would be needed for testing, right?