github.com/bazelbuild/rules_webtesting@v0.2.0/web/internal/web_test_archive.bzl (about)

     1  # Copyright 2016 Google Inc.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #      http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  """A rule for configuring archives that contain named files.
    15  
    16  DO NOT load this file. Use "@io_bazel_rules_web//web:web.bzl".
    17  """
    18  
    19  load(":files.bzl", "files")
    20  load(":metadata.bzl", "metadata")
    21  load(":provider.bzl", "WebTestInfo")
    22  load(":runfiles.bzl", "runfiles")
    23  
    24  def _web_test_archive_impl(ctx):
    25      if ctx.attr.extract == "run":
    26          return _web_test_archive_run_impl(ctx)
    27      if ctx.attr.extract == "build":
    28          return _web_test_archive_build_impl(ctx)
    29  
    30      fail("unknown value %s" % ctx.attr.extract, attr = "extract")
    31  
    32  def _web_test_archive_run_impl(ctx):
    33      metadata.create_file(
    34          ctx = ctx,
    35          output = ctx.outputs.web_test_metadata,
    36          web_test_files = [
    37              metadata.web_test_files(
    38                  ctx = ctx,
    39                  archive_file = ctx.file.archive,
    40                  named_files = ctx.attr.named_files,
    41                  strip_prefix = ctx.attr.strip_prefix,
    42              ),
    43              metadata.web_test_files(
    44                  ctx = ctx,
    45                  named_files = {
    46                      "EXTRACT_EXE": ctx.executable.extract_exe_target,
    47                  },
    48              ),
    49          ],
    50      )
    51  
    52      return [
    53          DefaultInfo(
    54              runfiles = runfiles.collect(
    55                  ctx = ctx,
    56                  files = [ctx.file.archive],
    57                  targets = [ctx.attr.extract_exe_target],
    58              ),
    59          ),
    60          WebTestInfo(metadata = ctx.outputs.web_test_metadata),
    61      ]
    62  
    63  def _web_test_archive_build_impl(ctx):
    64      out_dir = ctx.actions.declare_directory(ctx.label.name + ".out")
    65      ctx.actions.run(
    66          executable = ctx.executable.extract_exe_host,
    67          arguments = [ctx.file.archive.path, out_dir.path, ctx.attr.strip_prefix],
    68          mnemonic = "WebTestArchive",
    69          progress_message = "Extracting %s" % ctx.file.archive.short_path,
    70          use_default_shell_env = True,
    71          inputs = [ctx.file.archive],
    72          outputs = [out_dir],
    73      )
    74  
    75      out_dir_path = files.long_path(ctx, out_dir)
    76      named_files = {}
    77  
    78      for n, p in ctx.attr.named_files.items():
    79          named_files[n] = out_dir_path + "/" + p
    80  
    81      metadata.create_file(
    82          ctx = ctx,
    83          output = ctx.outputs.web_test_metadata,
    84          web_test_files = [
    85              metadata.web_test_files(ctx = ctx, named_files = named_files),
    86          ],
    87      )
    88  
    89      return [
    90          DefaultInfo(runfiles = runfiles.collect(ctx = ctx, files = [out_dir])),
    91          WebTestInfo(metadata = ctx.outputs.web_test_metadata),
    92      ]
    93  
    94  web_test_archive = rule(
    95      attrs = {
    96          "archive": attr.label(
    97              doc = "Archive file that contains named files.",
    98              allow_single_file = [
    99                  ".deb",
   100                  ".tar",
   101                  ".tar.bz2",
   102                  ".tbz2",
   103                  ".tar.gz",
   104                  ".tgz",
   105                  ".tar.Z",
   106                  ".zip",
   107              ],
   108              mandatory = True,
   109          ),
   110          "named_files": attr.string_dict(
   111              doc = "A map of names to paths in the archive.",
   112              mandatory = True,
   113          ),
   114          "extract": attr.string(
   115              doc = "When the archive shoud be extracted.",
   116              default = "run",
   117              values = [
   118                  "build",
   119                  "run",
   120              ],
   121          ),
   122          "strip_prefix": attr.string(doc = """Prefix to strip when archive is extracted.
   123                  BASH-style globbing is allowed."""),
   124          "extract_exe_host": attr.label(
   125              doc = """Executable to extract files if extract = build.
   126                      Should accept three positional parameters:
   127                        archive out_dir strip_prefix""",
   128              allow_files = True,
   129              cfg = "host",
   130              default = Label("//web/internal:extract.sh"),
   131              executable = True,
   132          ),
   133          "extract_exe_target": attr.label(
   134              doc = """Executable to extract files if extract = run.
   135                      Should accept three positional parameters:
   136                        archive out_dir strip_prefix""",
   137              allow_files = True,
   138              cfg = "target",
   139              default = Label("//web/internal:extract.sh"),
   140              executable = True,
   141          ),
   142      },
   143      doc = """Specifies an archive file with named files in it.
   144  
   145          If extract=="run", then the archive will only be extracted if WTL wants one
   146          of the named files in it.""",
   147      outputs = {"web_test_metadata": "%{name}.gen.json"},
   148      implementation = _web_test_archive_impl,
   149  )