blob: 75a1a9be2bce4a8fb73e71011f5360bd848ca040 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001def _aos_downloader_impl(ctx):
2 all_files = ctx.files.srcs + ctx.files.start_srcs + [ctx.outputs._startlist]
3 ctx.file_action(
4 output = ctx.outputs.executable,
5 executable = True,
6 content = "\n".join([
7 "#!/bin/bash",
8 "set -e",
9 'cd "${BASH_SOURCE[0]}.runfiles/%s"' % ctx.workspace_name,
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080010 ] + ['%s --dir %s --target "$@" --type %s %s' % (
Alex Perrycb7da4b2019-08-28 19:35:56 -070011 ctx.executable._downloader.short_path,
Alex Perrycb7da4b2019-08-28 19:35:56 -070012 d.downloader_dir,
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080013 ctx.attr.target_type,
14 " ".join([src.short_path for src in d.downloader_srcs]),
Alex Perrycb7da4b2019-08-28 19:35:56 -070015 ) for d in ctx.attr.dirs] + [
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080016 'exec %s --target "$@" --type %s %s' % (
Alex Perrycb7da4b2019-08-28 19:35:56 -070017 ctx.executable._downloader.short_path,
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080018 ctx.attr.target_type,
Alex Perrycb7da4b2019-08-28 19:35:56 -070019 " ".join([src.short_path for src in all_files]),
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 ),
21 ]),
22 )
23
24 ctx.file_action(
25 output = ctx.outputs._startlist,
26 content = "\n".join([f.basename for f in ctx.files.start_srcs]) + "\n",
27 )
28
29 to_download = [ctx.outputs._startlist]
30 to_download += all_files
31 for d in ctx.attr.dirs:
32 to_download += d.downloader_srcs
33
34 return struct(
35 runfiles = ctx.runfiles(
36 files = to_download + ctx.files._downloader,
37 transitive_files = ctx.attr._downloader.default_runfiles.files,
38 collect_data = True,
39 collect_default = True,
40 ),
41 files = depset([ctx.outputs.executable]),
42 )
43
44def _aos_downloader_dir_impl(ctx):
45 return struct(
46 downloader_dir = ctx.attr.dir,
47 downloader_srcs = ctx.files.srcs,
48 )
49
50"""Creates a binary which downloads code to a robot.
51
52Running this with `bazel run` will actually download everything.
53
54This also generates a start_list.txt file with the names of binaries to start.
55
56Attrs:
57 srcs: The files to download. They currently all get shoved into one folder.
58 dirs: A list of aos_downloader_dirs to download too.
59 start_srcs: Like srcs, except they also get put into start_list.txt.
Alex Perrycb7da4b2019-08-28 19:35:56 -070060"""
61
62aos_downloader = rule(
63 attrs = {
64 "_downloader": attr.label(
65 executable = True,
66 cfg = "host",
67 default = Label("//frc971/downloader"),
68 ),
69 "start_srcs": attr.label_list(
70 mandatory = True,
71 allow_files = True,
72 ),
73 "srcs": attr.label_list(
74 mandatory = True,
75 allow_files = True,
76 ),
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080077 "target_type": attr.string(
78 default = "roborio",
79 ),
Alex Perrycb7da4b2019-08-28 19:35:56 -070080 "dirs": attr.label_list(
81 mandatory = False,
82 providers = [
83 "downloader_dir",
84 "downloader_srcs",
85 ],
86 ),
Alex Perrycb7da4b2019-08-28 19:35:56 -070087 },
88 executable = True,
89 outputs = {
90 "_startlist": "%{name}.start_list.dir/start_list.txt",
91 },
92 implementation = _aos_downloader_impl,
93)
94
95"""Downloads files to a specific directory.
96
97This rule does nothing by itself. Use it by adding to the dirs attribute of an
98aos_downloader rule.
99
100Attrs:
101 srcs: The files to download. They all go in the same directory.
102 dir: The directory (relative to the standard download directory) to put all
103 the files in.
104"""
105
106aos_downloader_dir = rule(
107 attrs = {
108 "srcs": attr.label_list(
109 mandatory = True,
110 allow_files = True,
111 ),
112 "dir": attr.string(
113 mandatory = True,
114 ),
115 },
116 implementation = _aos_downloader_dir_impl,
117)