|
| 1 | +load("@build_bazel_rules_nodejs//:providers.bzl", "JSNamedModuleInfo") |
| 2 | + |
| 3 | +def _get_workspace_name(ctx): |
| 4 | + if ctx.label.workspace_root: |
| 5 | + # We need the workspace_name for the target being visited. |
| 6 | + # Starlark doesn't have this - instead they have a workspace_root |
| 7 | + # which looks like "external/repo_name" - so grab the second path segment. |
| 8 | + return ctx.label.workspace_root.split("/")[1] |
| 9 | + else: |
| 10 | + return ctx.workspace_name |
| 11 | + |
| 12 | +def _http_server_rule_impl(ctx): |
| 13 | + """Implementation of the http server rule.""" |
| 14 | + |
| 15 | + workspace_name = _get_workspace_name(ctx) |
| 16 | + package_manifest_path = "%s/%s" % (workspace_name, ctx.label.package) |
| 17 | + |
| 18 | + # List of files which are required for the server to run. This includes the |
| 19 | + # bazel runfile helpers (to resolve runfiles in bash) and the server binary |
| 20 | + # with its transitive runfiles (in order to be able to run the server). |
| 21 | + required_tools = ctx.files._bash_runfile_helpers + \ |
| 22 | + ctx.files._server_bin + \ |
| 23 | + ctx.attr._server_bin[DefaultInfo].files.to_list() + \ |
| 24 | + ctx.attr._server_bin[DefaultInfo].data_runfiles.files.to_list() |
| 25 | + |
| 26 | + # Walk through all dependencies specified in the "deps" attribute. These labels need to be |
| 27 | + # unwrapped in case there are built using TypeScript-specific rules. |
| 28 | + transitive_depsets = [] |
| 29 | + for dep in ctx.attr.deps: |
| 30 | + if JSNamedModuleInfo in dep: |
| 31 | + transitive_depsets.append(dep[JSNamedModuleInfo].sources) |
| 32 | + elif DefaultInfo in dep: |
| 33 | + transitive_depsets.append(dep[DefaultInfo].files) |
| 34 | + |
| 35 | + root_paths = ["", package_manifest_path] + ctx.attr.additional_root_paths |
| 36 | + |
| 37 | + # We can't use "ctx.actions.args()" because there is no way to convert the args object |
| 38 | + # into a string representing the command line arguments. It looks like bazel has some |
| 39 | + # internal logic to compute the string representation of "ctx.actions.args()". |
| 40 | + args = "" |
| 41 | + |
| 42 | + for root in root_paths: |
| 43 | + args += "--root-paths '%s' " % root |
| 44 | + |
| 45 | + if ctx.attr.history_api_fallback: |
| 46 | + args += "--history-api-fallback=true " |
| 47 | + |
| 48 | + if ctx.attr.enable_dev_ui: |
| 49 | + args += "--enable-dev-ui=true " |
| 50 | + |
| 51 | + for variable_name in ctx.attr.environment_variables: |
| 52 | + args += "--environment-variables '%s' " % variable_name |
| 53 | + |
| 54 | + ctx.actions.expand_template( |
| 55 | + template = ctx.file._launcher_template, |
| 56 | + output = ctx.outputs.launcher, |
| 57 | + substitutions = { |
| 58 | + "TEMPLATED_args": args, |
| 59 | + }, |
| 60 | + is_executable = True, |
| 61 | + ) |
| 62 | + |
| 63 | + runfiles = ctx.runfiles( |
| 64 | + files = ctx.files.srcs + required_tools, |
| 65 | + transitive_files = depset(transitive = transitive_depsets), |
| 66 | + collect_data = True, |
| 67 | + collect_default = True, |
| 68 | + ) |
| 69 | + |
| 70 | + return [ |
| 71 | + DefaultInfo( |
| 72 | + files = depset([ctx.outputs.launcher]), |
| 73 | + runfiles = runfiles, |
| 74 | + ), |
| 75 | + ] |
| 76 | + |
| 77 | +http_server_rule = rule( |
| 78 | + implementation = _http_server_rule_impl, |
| 79 | + outputs = { |
| 80 | + "launcher": "%{name}.sh", |
| 81 | + }, |
| 82 | + attrs = { |
| 83 | + "additional_root_paths": attr.string_list( |
| 84 | + doc = """ |
| 85 | + Additional paths to serve files from. The paths should be passed |
| 86 | + as manifest paths (e.g. "my_workspace/src"). |
| 87 | + """, |
| 88 | + ), |
| 89 | + "deps": attr.label_list( |
| 90 | + allow_files = True, |
| 91 | + doc = """ |
| 92 | + Dependencies that need to be available for resolution. This attribute can be |
| 93 | + used for TypeScript targets which provide multiple flavors of output. |
| 94 | + """, |
| 95 | + ), |
| 96 | + "enable_dev_ui": attr.bool( |
| 97 | + default = False, |
| 98 | + doc = """ |
| 99 | + Whether an additional UI for development should be enabled. |
| 100 | + The development UI can be helpful for throttling network and more. This |
| 101 | + is a feature from the underlying browsersync implementation. |
| 102 | + """, |
| 103 | + ), |
| 104 | + "history_api_fallback": attr.bool( |
| 105 | + default = True, |
| 106 | + doc = """ |
| 107 | + Whether the server should fallback to "/index.html" for non-file requests. |
| 108 | + This is helpful for single page applications using the HTML history API. |
| 109 | + """, |
| 110 | + ), |
| 111 | + "environment_variables": attr.string_list( |
| 112 | + default = [], |
| 113 | + doc = """ |
| 114 | + List of environment variables that will be made available in the `index.html` |
| 115 | + file. Variables can be accessed through `window.<NAME>`. |
| 116 | +
|
| 117 | + This is useful as an example when developers want to have an API key available |
| 118 | + as part of their development workflow, but not hard-coding it into the sources. |
| 119 | + """, |
| 120 | + ), |
| 121 | + "srcs": attr.label_list( |
| 122 | + allow_files = True, |
| 123 | + doc = """ |
| 124 | + Sources that should be available to the server for resolution. This attribute can |
| 125 | + be used for explicit files. This attribute only uses the files exposed by the |
| 126 | + `DefaultInfo` provider (i.e. TypeScript targets should be added to "deps"). |
| 127 | + """, |
| 128 | + ), |
| 129 | + "_bash_runfile_helpers": attr.label(default = Label("@bazel_tools//tools/bash/runfiles")), |
| 130 | + "_server_bin": attr.label( |
| 131 | + default = Label("//bazel/http-server:server_bin"), |
| 132 | + ), |
| 133 | + "_launcher_template": attr.label( |
| 134 | + allow_single_file = True, |
| 135 | + default = Label("//bazel/http-server:launcher_template.sh"), |
| 136 | + ), |
| 137 | + }, |
| 138 | +) |
| 139 | + |
| 140 | +def http_server(name, testonly = False, port = 4200, tags = [], **kwargs): |
| 141 | + """Creates a HTTP server that can depend on individual bazel targets. The server uses |
| 142 | + bazel runfile resolution in order to work with Bazel package paths. e.g. developers can |
| 143 | + request files through their manifest path: "my_workspace/src/dev-app/my-genfile".""" |
| 144 | + |
| 145 | + http_server_rule( |
| 146 | + name = "%s_launcher" % name, |
| 147 | + visibility = ["//visibility:private"], |
| 148 | + tags = tags, |
| 149 | + testonly = testonly, |
| 150 | + **kwargs |
| 151 | + ) |
| 152 | + |
| 153 | + native.sh_binary( |
| 154 | + name = name, |
| 155 | + # The "ibazel_notify_changes" tag tells ibazel to not relaunch the executable on file |
| 156 | + # changes. Rather it will communicate with the server implementation through "stdin". |
| 157 | + tags = tags + ["ibazel_notify_changes"], |
| 158 | + srcs = ["%s_launcher.sh" % name], |
| 159 | + data = [":%s_launcher" % name], |
| 160 | + args = ["--port=%s" % port], |
| 161 | + testonly = testonly, |
| 162 | + ) |
0 commit comments