-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrules.py
More file actions
94 lines (91 loc) · 2.99 KB
/
rules.py
File metadata and controls
94 lines (91 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from pants.engine.internals.native_engine import (
Digest,
MergeDigests,
RemovePrefix,
AddPrefix,
Snapshot,
)
from pants.core.util_rules.stripped_source_files import StrippedSourceFiles
from pants.core.util_rules.source_files import SourceFilesRequest
from pants.engine.target import (
GeneratedSources,
TransitiveTargets,
TransitiveTargetsRequest,
)
from pants.engine.rules import rule, Get, MultiGet
from pants.engine.process import ProcessResult
from pants.source.source_root import SourceRoot, SourceRootRequest
from pants.backend.python.util_rules.interpreter_constraints import (
InterpreterConstraints,
)
from pants.backend.python.util_rules.pex import (
Pex,
PexProcess,
PexRequest,
PexRequirements,
)
from .targets import *
@rule
async def generate_python_from_asyncapi(
request: GeneratePythonFromAsyncapiRequest,
) -> GeneratedSources:
pex = await Get(
Pex,
PexRequest(
output_filename="asyncapi-python-codegen.pex",
internal_only=True,
requirements=PexRequirements([]),
interpreter_constraints=InterpreterConstraints([">=3.9"]),
# No main parameter - creates a REPL-style PEX
),
)
transitive_targets = await Get(
TransitiveTargets,
TransitiveTargetsRequest([request.protocol_target.address]),
)
all_sources_stripped = await Get(
StrippedSourceFiles,
SourceFilesRequest(
(tgt.get(AsyncapiSourcesField) for tgt in transitive_targets.closure),
for_sources_types=(AsyncapiSourcesField,),
),
)
input_digest = await Get(
Digest,
MergeDigests(
(
all_sources_stripped.snapshot.digest,
pex.digest,
)
),
)
output_dir = "_generated_files"
module_name = request.protocol_target.address.target_name
# Use PexProcess to execute the PEX
result = await Get(
ProcessResult,
PexProcess(
pex,
argv=[
"-m",
"asyncapi_python_codegen", # Execute as module
"generate", # Your CLI command
request.protocol_target[AsyncapiServiceField].value or "",
f"{output_dir}/{module_name}",
],
description=f"Generating Python sources from {request.protocol_target.address}.",
output_directories=(output_dir,),
input_digest=input_digest,
),
)
source_root_request = SourceRootRequest.for_target(request.protocol_target)
normalized_digest, source_root = await MultiGet(
Get(Digest, RemovePrefix(result.output_digest, output_dir)),
Get(SourceRoot, SourceRootRequest, source_root_request),
)
source_root_restored = (
await Get(Snapshot, AddPrefix(normalized_digest, source_root.path))
if source_root.path != "."
else await Get(Snapshot, Digest, normalized_digest)
)
return GeneratedSources(source_root_restored)