-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrules.bzl
676 lines (591 loc) · 21.8 KB
/
rules.bzl
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# Copyright (C) 2020 Google Inc.
#
# This file has been licensed under Apache 2.0 license. Please see the LICENSE
# file at the root of the repository.
load("@bazel_skylib//lib:paths.bzl", "paths")
load(":script.bzl", _script_cmd = "script_cmd")
load(":pandoc.bzl",
_pandoc_standalone_html = "pandoc_standalone_html",
_pandoc_chunked_html = "pandoc_chunked_html",
)
load(":providers.bzl", "EbookInfo")
load(":attrs.bzl", _ADDITIONAL_INPUTS = "ADDITIONAL_INPUTS")
pandoc_standalone_html = _pandoc_standalone_html
pandoc_chunked_html = _pandoc_chunked_html
def _plantuml_png_impl(ctx):
cmd = "plantuml"
docker_run = ctx.executable._script
figures = []
for target in ctx.attr.srcs:
for src in target.files.to_list():
in_file = src
out_file = ctx.actions.declare_file(
paths.replace_extension(in_file.basename, ".png"))
figures += [out_file]
script_cmd = _script_cmd(docker_run.path, in_file.path)
ctx.actions.run_shell(
progress_message = "plantuml diagram to PNG with {1}: {0}".format(
in_file.short_path, cmd),
inputs = [in_file],
outputs = [out_file],
tools = [docker_run],
command = """\
{script} \
{cmd} -Djava.awt.headless=true -o "$(realpath {out_dir})" "{in_file}"
""".format(
cmd=cmd,
out_dir=out_file.dirname,
in_file=in_file.path,
script=script_cmd),
)
deps = []
for target in ctx.attr.deps:
ebook_provider = target[EbookInfo]
if not ebook_provider:
continue
deps += ebook_provider.figures
runfiles = ctx.runfiles(files = figures)
return [
EbookInfo(figures=figures+deps, markdowns=[]),
DefaultInfo(files=depset(figures+deps), runfiles=runfiles),
]
plantuml_png = rule(implementation = _plantuml_png_impl,
attrs = {
"srcs": attr.label_list(
allow_files = [".txt"],
doc = "The file to compile",
),
"deps": attr.label_list(
doc = "The dependencies, any targets should be allowed",
),
"output": attr.output(doc="The generated file"),
"_script": attr.label(
default="@bazel_rules_bid//build:docker_run",
executable=True,
cfg="host"),
},
doc = "Transform a timing diagram file into png using plantuml",
)
def _drawtiming_png_impl(ctx):
cmd = "drawtiming"
docker_run = ctx.executable._script
figures = []
for target in ctx.attr.srcs:
for src in target.files.to_list():
in_file = src
out_file = ctx.actions.declare_file(in_file.basename + ".png")
figures += [out_file]
script_cmd = _script_cmd(docker_run.path, in_file.path)
ctx.actions.run_shell(
progress_message = "timing diagram to PNG with {1}: {0}".format(in_file.short_path, cmd),
inputs = [in_file],
outputs = [out_file],
tools = [docker_run],
command = """\
{script} \
{cmd} --output "{out_file}" "{in_file}"
""".format(
cmd=cmd,
out_file=out_file.path,
in_file=in_file.path,
args=" ".join(ctx.attr.args),
script=script_cmd),
)
deps = []
for target in ctx.attr.deps:
ebook_provider = target[EbookInfo]
if not ebook_provider:
continue
deps += ebook_provider.figures
runfiles = ctx.runfiles(files = figures)
return [
EbookInfo(figures=figures+deps, markdowns=[]),
DefaultInfo(files=depset(figures+deps), runfiles=runfiles),
]
drawtiming_png = rule(implementation = _drawtiming_png_impl,
attrs = {
"srcs": attr.label_list(
allow_files = [".t"],
doc = "The file to compile",
),
"deps": attr.label_list(
doc = "The dependencies, any targets should be allowed",
),
"output": attr.output(doc="The generated file"),
"args": attr.string_list(
doc = "A list of arguments prepended verbatim to the invocation of drawtiming",
),
"_script": attr.label(
default="@bazel_rules_bid//build:docker_run",
executable=True,
cfg="host"),
},
doc = "Transform a timing diagram file into png using drawtiming",
)
def _generalized_graphviz_rule_impl(ctx, cmd):
docker_run = ctx.executable._script
figures = []
for target in ctx.attr.srcs:
for src in target.files.to_list():
in_file = src
out_file = ctx.actions.declare_file(in_file.basename + ".png")
figures += [out_file]
script_cmd = _script_cmd(docker_run.path, in_file.path)
ctx.actions.run_shell(
progress_message = "graphviz to PNG with {1}: {0}".format(in_file.short_path, cmd),
inputs = [in_file],
outputs = [out_file],
tools = [docker_run],
command = """\
{script} \
{cmd} -Tpng -o "{out_file}" "{in_file}"
""".format(
cmd=cmd,
out_file=out_file.path,
in_file=in_file.path,
script=script_cmd),
)
deps = []
for target in ctx.attr.deps:
ebook_provider = target[EbookInfo]
if not ebook_provider:
continue
deps += ebook_provider.figures
runfiles = ctx.runfiles(files = figures)
return [
EbookInfo(figures=figures+deps, markdowns=[]),
DefaultInfo(files=depset(figures+deps), runfiles=runfiles),
]
def _neato_png_impl(ctx):
return _generalized_graphviz_rule_impl(ctx, "neato")
neato_png = rule(implementation = _neato_png_impl,
attrs = {
"srcs": attr.label_list(
allow_files = [".dot"],
doc = "The file to compile",
),
"deps": attr.label_list(
doc = "The dependencies, any targets should be allowed",
),
"output": attr.output(doc="The generated file"),
"_script": attr.label(
default="@bazel_rules_bid//build:docker_run",
executable=True,
cfg="host"),
},
doc = "Transform a graphviz dot file into png using neato",
)
def _dot_png_impl(ctx):
return _generalized_graphviz_rule_impl(ctx, "dot")
dot_png = rule(implementation = _dot_png_impl,
attrs = {
"srcs": attr.label_list(
allow_files = [".dot"],
doc = "The file to compile",
),
"deps": attr.label_list(
doc = "The dependencies, any targets should be allowed",
),
"output": attr.output(doc="The generated file"),
"_script": attr.label(
default="@bazel_rules_bid//build:docker_run",
executable=True,
cfg="host"),
},
doc = "Transform a graphviz dot file into png using dot",
)
def _asymptote_impl(ctx):
asycc = ctx.executable._script
figures = []
for target in ctx.attr.srcs:
for src in target.files.to_list():
in_file = src
out_file = ctx.actions.declare_file(in_file.basename + ".png")
figures += [out_file]
script_cmd = _script_cmd(asycc.path, in_file.path)
ctx.actions.run_shell(
progress_message = "ASY to PNG: {0}".format(in_file.short_path),
inputs = [in_file],
outputs = [out_file],
tools = [asycc],
command = """\
{script} \
asy -render 5 -f png -o "{out_file}" "{in_file}"
""".format(
out_file=out_file.path[:-4], in_file=in_file.path, script=script_cmd),
)
deps = []
for target in ctx.attr.deps:
ebook_provider = target[EbookInfo]
if not ebook_provider:
continue
deps += ebook_provider.figures or []
runfiles = ctx.runfiles(files=figures+deps)
for dep in ctx.attr.deps:
runfiles = runfiles.merge(dep[DefaultInfo].data_runfiles)
return [
EbookInfo(figures=figures+deps, markdowns=[]),
DefaultInfo(files=depset(figures+deps), runfiles=runfiles),
]
asymptote = rule(implementation = _asymptote_impl,
attrs = {
"srcs": attr.label_list(
doc = "The file to compile",
allow_files = True,
),
"deps": attr.label_list(
doc = "The dependencies, any targets should be allowed",
),
"output": attr.output(doc="The generated file"),
"_script": attr.label(
default="@bazel_rules_bid//build:docker_run",
executable=True,
cfg="host"),
},
doc = "Transform an asymptote file into png",
)
def _copy_file_to_workdir_renamed(ctx, src):
src_copy = ctx.actions.declare_file("{}_{}".format(ctx.label.name, src.short_path))
ctx.actions.run_shell(
progress_message = "Copying {} to {}".format(src.short_path, src_copy.short_path),
outputs = [src_copy],
inputs = [src],
command="cp {} {}".format(src.path, src_copy.path),
)
return src_copy
def _copy_file_to_workdir(ctx, src):
src_copy = ctx.actions.declare_file(src.basename)
ctx.actions.run_shell(
progress_message = "Copying {}".format(src.short_path),
outputs = [src_copy],
inputs = [src],
command="cp {} {}".format(src.path, src_copy.path),
)
return src_copy
def _markdown_lib_impl(ctx):
markdowns = []
figures = []
additional_inputs = []
for target in ctx.attr.additional_inputs:
for src in target.files.to_list():
additional_inputs += [src]
for target in ctx.attr.srcs:
if EbookInfo in target:
provider = target[EbookInfo]
figures += (provider.figures or [])
markdowns += (provider.markdowns or [])
else:
for src in target.files.to_list():
markdowns += [_copy_file_to_workdir(ctx, src)]
for target in ctx.attr.deps:
provider = target[EbookInfo]
figures += (provider.figures or [])
markdowns += (provider.markdowns or [])
additional_inputs += (provider.additional_inputs or [])
runfiles = ctx.runfiles(files=figures+markdowns+additional_inputs)
for dep in ctx.attr.deps:
runfiles = runfiles.merge(dep[DefaultInfo].data_runfiles)
return [
EbookInfo(
figures=figures,
markdowns=markdowns,
additional_inputs=additional_inputs),
DefaultInfo(
files=depset(figures+markdowns+additional_inputs),
runfiles=runfiles,
),
]
markdown_lib = rule(
implementation = _markdown_lib_impl,
doc = "Declares a set of markdown files",
attrs = {
"srcs": attr.label_list(
allow_files = [".md"],
providers = [EbookInfo],
doc = "The markdown source files",
),
"deps": attr.label_list(
doc = "The file to compile",
providers = [EbookInfo],
),
} | _ADDITIONAL_INPUTS,
)
def _ebook_epub_impl(ctx):
name = ctx.label.name
# This is duplicated in _ebook_pdf_impl.
# steps
# run htex on all *md, gives book.htex
markdowns = []
figures = []
additional_inputs = []
for dep in ctx.attr.deps:
provider = dep[EbookInfo]
markdowns += provider.markdowns or []
figures += provider.figures or []
additional_inputs += provider.additional_inputs or []
dir_reference = markdowns[0]
htex_file = ctx.actions.declare_file("{}.htex".format(name))
markdowns_paths = [file.path for file in markdowns]
markdowns_paths_stripped = _strip_reference_dir_from_files(dir_reference, markdowns)
script = ctx.executable._script
script_cmd = _script_cmd(script.path, markdowns_paths[0])
ctx.actions.run_shell(
progress_message = "Building equation environments for: {}".format(name),
inputs = markdowns + additional_inputs,
outputs = [htex_file],
tools = [script],
command = """\
{script} \
pandoc -s --gladtex {args} -o {target} {sources} \
""".format(
script=script_cmd,
target=htex_file.path,
args=" ".join(ctx.attr.args),
sources=" ".join(markdowns_paths))
)
# run gladtex on the resulting htex to obtain html and output directory with figures.
outdir = ctx.actions.declare_directory("{}.eqn".format(name))
html_file = ctx.actions.declare_file("{}.html".format(name))
ctx.actions.run_shell(
progress_message = "Extracting equations for: {}".format(name),
inputs = [htex_file] + additional_inputs,
outputs = [outdir, html_file],
tools = [script],
command = """\
{script} --cd-to-dir-reference \
env LC_ALL=en_US gladtex -f 12 -d {outdir} {htex_file} \
""".format(
script=script_cmd,
outdir=_strip_reference_dir(dir_reference, outdir.path),
htex_file=_strip_reference_dir(dir_reference, htex_file.path),
)
)
outdir_tar = ctx.actions.declare_file("{}.tar".format(outdir.basename))
tar_command = "(cd {base} ; tar cf {archive} {dir})".format(
base=outdir_tar.dirname,
archive=outdir_tar.basename,
dir=outdir.basename)
ctx.actions.run_shell(
progress_message = "Archiving equations: {}".format(outdir_tar.short_path),
inputs = [outdir],
outputs = [outdir_tar],
command = tar_command,
)
# run htexepub to obtain book.epub.
# This is gonna be fun!
epub_metadata = ctx.attr.metadata_xml.files.to_list()[0]
epub_metadata = _copy_file_to_workdir_renamed(ctx, epub_metadata)
title_yaml = ctx.attr.title_yaml.files.to_list()[0]
title_yaml = _copy_file_to_workdir_renamed(ctx, epub_metadata)
ebook_epub = ctx.actions.declare_file("{}.epub".format(name))
inputs = [epub_metadata, title_yaml, html_file, outdir, outdir_tar] + markdowns + figures
ctx.actions.run_shell(
progress_message = "Building EPUB for: {}".format(name),
inputs = inputs + additional_inputs,
tools = [script],
outputs = [ebook_epub],
command = """\
{script} --cd-to-dir-reference \
pandoc --epub-metadata={epub_metadata} {args} \
-f html -t epub3 -o {ebook_epub} {html_file} \
""".format(
script=script_cmd,
epub_metadata=_strip_reference_dir(dir_reference, epub_metadata.path),
ebook_epub=_strip_reference_dir(dir_reference, ebook_epub.path),
args=" ".join(ctx.attr.args),
html_file=_strip_reference_dir(dir_reference, html_file.path),
))
runfiles = ctx.runfiles(files=[ebook_epub])
for dep in ctx.attr.deps:
runfiles = runfiles.merge(dep[DefaultInfo].data_runfiles)
return [
dep[EbookInfo],
DefaultInfo(
files=depset([ebook_epub, outdir, outdir_tar]),
runfiles=runfiles,
)
]
ebook_epub = rule(
implementation = _ebook_epub_impl,
attrs = {
"deps": attr.label_list(
doc = "All the targets you need to make this book work.",
providers = [EbookInfo],
),
"title_yaml": attr.label(
allow_files = True,
doc = "The title.yaml file to use for this book",
),
"metadata_xml": attr.label(
allow_files = True,
doc = "The epub-metadata.xml file to use for this book",
),
'args': attr.string_list(
doc = 'Any additional args to insert',
allow_empty = True,
),
"_script": attr.label(
default="@bazel_rules_bid//build:docker_run",
executable=True,
cfg="host"),
} | _ADDITIONAL_INPUTS,
doc = "Generate an ebook in EPUB format"
)
def _strip_reference_dir(reference_dir, path):
return path.replace(reference_dir.dirname+"/", "")
def _strip_reference_dir_from_files(reference_dir, files):
return [ _strip_reference_dir(reference_dir, file.path) for file in files]
def _ebook_pdf_impl(ctx):
name = ctx.label.name
# steps
# run htex on all *md, gives book.htex
markdowns = []
figures = []
additional_inputs = []
for dep in ctx.attr.deps:
provider = dep[EbookInfo]
markdowns += provider.markdowns
figures += provider.figures
additional_inputs += provider.additional_inputs
dir_reference = markdowns[0]
# Fixed up paths -- relative to the directory dir_reference, not the
# directory where the build happens! This is needed because we can not control
# figure inclusion.
markdowns_paths = _strip_reference_dir_from_files(dir_reference, markdowns)
script = ctx.executable._script
script_cmd = _script_cmd(script.path, dir_reference.path)
# run htexepub to obtain book.epub.
# This is gonna be fun!
epub_metadata = ctx.attr.metadata_xml.files.to_list()[0]
epub_metadata = _copy_file_to_workdir(ctx, epub_metadata)
title_yaml = ctx.attr.title_yaml.files.to_list()[0]
title_yaml = _copy_file_to_workdir(ctx, title_yaml)
ebook_pdf = ctx.actions.declare_file("{}.pdf".format(name))
inputs = [epub_metadata, title_yaml] + markdowns + figures
ctx.actions.run_shell(
progress_message = "Building PDF for: {}".format(name),
inputs = inputs + additional_inputs,
tools = [script],
outputs = [ebook_pdf],
command = """\
{script} --cd-to-dir-reference \
pandoc --epub-metadata={epub_metadata} \
--mathml -o {ebook_pdf} {args} {markdowns} \
""".format(
script=script_cmd,
epub_metadata=_strip_reference_dir(dir_reference, epub_metadata.path),
ebook_pdf=_strip_reference_dir(dir_reference, ebook_pdf.path),
args=" ".join(ctx.attr.args),
markdowns=" ".join(markdowns_paths),
))
runfiles = ctx.runfiles(files=[ebook_pdf])
for dep in ctx.attr.deps:
runfiles = runfiles.merge(dep[DefaultInfo].data_runfiles)
return [
DefaultInfo(
files=depset([ebook_pdf]),
runfiles=runfiles,
)
]
ebook_pdf = rule(
implementation = _ebook_pdf_impl,
attrs = {
"deps": attr.label_list(
doc = "All the targets you need to make this book work.",
providers = [EbookInfo],
),
"title_yaml": attr.label(
allow_files = True,
doc = "The title.yaml file to use for this book",
),
"metadata_xml": attr.label(
allow_files = True,
doc = "The epub-metadata.xml file to use for this book",
),
'args': attr.string_list(
doc = 'Any additional args to insert',
allow_empty = True,
),
"_script": attr.label(
default="@bazel_rules_bid//build:docker_run",
executable=True,
cfg="host"),
} | _ADDITIONAL_INPUTS,
doc = "Generate an ebook in PDF format"
)
def _ebook_kindle_impl(ctx):
mobi_file = ctx.actions.declare_file("{}.mobi".format(ctx.label.name))
# First provider is EbookInfo, second is DefaultInfo.
(ebook_info, default_info) = _ebook_epub_impl(ctx)
# There can be only one such file
outputs = default_info.files.to_list()
epub_file = outputs[0]
equation_outdir = outputs[1]
equation_outdir_tar = outputs[2]
captured_output = ctx.actions.declare_file(
"{}.untar-out".format(ctx.label.name))
# untar the equation dir
# Maybe this is not needed.
tar_command = "(cd {base} ; tar xvf {archive}) > {output}".format(
base=equation_outdir_tar.dirname,
archive=equation_outdir_tar.basename,
output=captured_output.path)
ctx.actions.run_shell(
progress_message = "Unarchiving equations: {}".format(equation_outdir_tar.short_path),
inputs = [equation_outdir_tar],
outputs = [captured_output],
command = tar_command,
)
dir_reference = epub_file
script = ctx.executable._script
name = ctx.label.name
script_cmd = _script_cmd(script.path, epub_file.path)
ctx.actions.run_shell(
progress_message = "Building MOBI for: {}".format(name),
inputs = [epub_file, equation_outdir],
tools = [script],
outputs = [mobi_file],
command = """\
{script} --cd-to-dir-reference \
ebook-convert {args} {epub_file} {mobi_file} \
""".format(
script=script_cmd,
epub_file=_strip_reference_dir(dir_reference, epub_file.path),
mobi_file=_strip_reference_dir(dir_reference, mobi_file.path),
args=" ".join(ctx.attr.args),
))
runfiles = ctx.runfiles(files=[mobi_file])
for dep in ctx.attr.deps:
runfiles = runfiles.merge(dep[DefaultInfo].data_runfiles)
return [
DefaultInfo(
files=depset([mobi_file, captured_output]),
runfiles=runfiles,
)
]
ebook_kindle = rule(
implementation = _ebook_kindle_impl,
attrs = {
"deps": attr.label_list(
doc = "All the targets you need to make this book work.",
providers = [EbookInfo],
),
"title_yaml": attr.label(
allow_files = True,
doc = "The title.yaml file to use for this book",
),
"metadata_xml": attr.label(
allow_files = True,
doc = "The epub-metadata.xml file to use for this book",
),
'args': attr.string_list(
doc = 'Any additional args to insert',
allow_empty = True,
),
"_script": attr.label(
default="@bazel_rules_bid//build:docker_run",
executable=True,
cfg="host"),
},
doc = "Generate an ebook in the Kindle's MOBI format"
)