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

     1  # Copyright 2018 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  """Runfiles modules contains utility functions for working with runfiles."""
    15  
    16  def _collect(ctx, files = [], targets = []):
    17      """Builds a runfiles object with transitive files from all targets.
    18  
    19      Args:
    20          ctx: Context object for the rule where this being used.
    21          files: a list of File object to include in the runfiles.
    22          targets: a list of Target object from which runfiles will be collected.
    23  
    24      Returns:
    25          A configured runfiles object that include data and default runfiles for the
    26          rule, all transitive runfiles from targets, and all files from files.
    27      """
    28      transitive_runfiles = depset()
    29      dep_files = depset()
    30      default_runfiles = []
    31      data_runfiles = []
    32  
    33      for target in targets:
    34          if hasattr(target, "transitive_runfiles"):
    35              transitive_runfiles = depset(
    36                  transitive = [transitive_runfiles, target.transitive_runfiles],
    37              )
    38          if hasattr(target, "default_runfiles"):
    39              default_runfiles += [target.default_runfiles]
    40          if hasattr(target, "data_runfiles"):
    41              data_runfiles += [target.data_runfiles]
    42          if hasattr(target, "files"):
    43              dep_files = depset(transitive = [dep_files, target.files])
    44  
    45      result = ctx.runfiles(
    46          collect_data = True,
    47          collect_default = True,
    48          files = files + dep_files.to_list(),
    49          transitive_files = transitive_runfiles,
    50      )
    51  
    52      for default in default_runfiles:
    53          result = result.merge(default)
    54  
    55      for data in data_runfiles:
    56          result = result.merge(data)
    57  
    58      return result
    59  
    60  runfiles = struct(collect = _collect)