Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | def _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 Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 10 | ] + ['%s --dir %s --target "$@" --type %s %s' % ( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | ctx.executable._downloader.short_path, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | d.downloader_dir, |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 13 | ctx.attr.target_type, |
| 14 | " ".join([src.short_path for src in d.downloader_srcs]), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 15 | ) for d in ctx.attr.dirs] + [ |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 16 | 'exec %s --target "$@" --type %s %s' % ( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | ctx.executable._downloader.short_path, |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 18 | ctx.attr.target_type, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | " ".join([src.short_path for src in all_files]), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | ), |
| 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 | |
| 44 | def _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 | |
| 52 | Running this with `bazel run` will actually download everything. |
| 53 | |
| 54 | This also generates a start_list.txt file with the names of binaries to start. |
| 55 | |
| 56 | Attrs: |
| 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 60 | """ |
| 61 | |
| 62 | aos_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 Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 77 | "target_type": attr.string( |
| 78 | default = "roborio", |
| 79 | ), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 80 | "dirs": attr.label_list( |
| 81 | mandatory = False, |
| 82 | providers = [ |
| 83 | "downloader_dir", |
| 84 | "downloader_srcs", |
| 85 | ], |
| 86 | ), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 87 | }, |
| 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 | |
| 97 | This rule does nothing by itself. Use it by adding to the dirs attribute of an |
| 98 | aos_downloader rule. |
| 99 | |
| 100 | Attrs: |
| 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 | |
| 106 | aos_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 | ) |