1
- load ("@bazel_skylib//lib:paths.bzl" , "paths" )
2
-
3
- OpaInfo = provider ("opa" , fields = ["bundle" , "target" , "file_deps" , "strip_prefix" ])
1
+ OpaInfo = provider ("opa" , fields = {
2
+ "bundle" : "The bundle file" ,
3
+ "target" : "The bundle target (wasm, rego or plan)" ,
4
+ "file_deps" : "A depset containing all the source files" ,
5
+ "file_aliases" : "A flat list of all the files with their alias in the bundle" ,
6
+ })
4
7
5
8
def _opa_toolchain (ctx ):
6
9
return ctx .toolchains ["//tools:toolchain_type" ].opacinfo
@@ -40,53 +43,56 @@ def _run_opa_signer(ctx, bundle_file, signed_bundle_file):
40
43
executable = ctx .executable ._opa_signer ,
41
44
)
42
45
46
+ def _file_alias (file , strip_prefix ):
47
+ return "%s:%s" % (file .path , file .short_path .removeprefix (strip_prefix ).removeprefix ("/" ))
48
+
43
49
def _run_opa_build (ctx , bundle_file ):
44
50
toolchain = _opa_toolchain (ctx )
51
+ wd = ctx .actions .declare_directory ("%s_work" % (ctx .label .name ))
45
52
args = ctx .actions .args ()
46
53
47
- strip_prefix = paths .normalize (ctx .attr .strip_prefix )
48
- opa_relative_path = toolchain .opa .path
49
- bundle_relative_path = bundle_file .path
54
+ srcs = ctx .files .srcs or []
55
+ data = ctx .files .data or []
56
+
57
+ file_aliases = [_file_alias (file , ctx .attr .strip_prefix or "" ) for file in srcs + data ]
50
58
51
- if strip_prefix != "." :
52
- rel = [".." for _ in strip_prefix .split ("/" )]
53
- opa_relative_path = paths .normalize ("/" .join (rel + [toolchain .opa .path ]))
54
- bundle_relative_path = paths .normalize ("/" .join (rel + [bundle_file .path ]))
59
+ for dep in ctx .attr .deps :
60
+ file_aliases .extend (dep [OpaInfo ].file_aliases )
55
61
56
- args .add ("build" )
57
- args .add ("-t" )
58
- args .add (ctx .attr .target )
59
- args .add ("-o" )
60
- args .add ("%s" % (bundle_relative_path ))
62
+ args .add ("-d" ).add (wd .path )
63
+ args .add ("-o" ).add (bundle_file , format = "%s:bundle.tar.gz" )
64
+ args .add ("-i" ).add_all (file_aliases ).add ("--" )
65
+ args .add (toolchain .opa ).add ("build" )
66
+ args .add ("-t" ).add (ctx .attr .target )
67
+ args .add ("-o" ).add ("bundle.tar.gz" )
61
68
62
69
if ctx .attr .optimize :
63
70
args .add (ctx .attr .optimize , format = "--optimize=%s" )
64
71
65
72
for entrypoint in ctx .attr .entrypoints :
66
- args .add (entrypoint , format = "-e %s" )
73
+ args .add ("-e" ). add ( entrypoint )
67
74
68
75
args .add ("." )
69
76
70
- srcs = ctx .files .srcs if ctx .files .srcs else []
71
- data = ctx .files .data if ctx .files .data else []
72
-
73
- ctx .actions .run_shell (
77
+ ctx .actions .run (
78
+ executable = ctx .executable ._opa_ctx ,
74
79
inputs = depset (srcs + data , transitive = [d [OpaInfo ].file_deps for d in ctx .attr .deps ]),
75
- outputs = [bundle_file ],
80
+ outputs = [bundle_file , wd ],
76
81
arguments = [args ],
77
82
tools = [toolchain .opa ],
78
83
progress_message = "Bundling policies" ,
79
84
mnemonic = "OpaBuild" ,
80
- command = "cd %s && %s $@" % (strip_prefix , opa_relative_path ),
81
85
)
82
86
87
+ return file_aliases
88
+
83
89
def _opa_library_impl (ctx ):
84
90
if ctx .attr .target == "wasm" and len (ctx .attr .entrypoints ) == 0 :
85
91
fail ("At least one entrypoint is required for a wasm target" )
86
92
87
93
bundle_file = ctx .actions .declare_file ("%s.tar.gz" % (ctx .attr .name ))
88
94
89
- _run_opa_build (ctx , bundle_file )
95
+ file_aliases = _run_opa_build (ctx , bundle_file )
90
96
91
97
output_files = [bundle_file ]
92
98
@@ -97,13 +103,13 @@ def _opa_library_impl(ctx):
97
103
_run_opa_signer (ctx , bundle_file , signed_bundle_file )
98
104
output_files = [signed_bundle_file ] + output_files
99
105
100
- srcs = ctx .files .srcs if ctx . files . srcs else []
101
- data = ctx .files .data if ctx . files . data else []
106
+ srcs = ctx .files .srcs or []
107
+ data = ctx .files .data or []
102
108
103
109
return [
104
110
OpaInfo (
105
111
bundle = bundle_file ,
106
- strip_prefix = paths . normalize ( ctx . attr . strip_prefix ) ,
112
+ file_aliases = file_aliases ,
107
113
target = ctx .attr .target ,
108
114
file_deps = depset (srcs + data , transitive = [d [OpaInfo ].file_deps for d in ctx .attr .deps ]),
109
115
),
@@ -172,6 +178,11 @@ wasm The wasm target emits a bundle containing a WebAssembly module compiled
172
178
executable = True ,
173
179
cfg = "exec" ,
174
180
),
181
+ "_opa_ctx" : attr .label (
182
+ default = "//tools:opa_ctx" ,
183
+ executable = True ,
184
+ cfg = "exec" ,
185
+ ),
175
186
},
176
187
toolchains = ["//tools:toolchain_type" ],
177
188
)
0 commit comments