github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/parse/rules/subrepo_rules.build_defs (about)

     1  """Contains rules for working with sub-repositories.
     2  
     3  These allow downloading targets from another repo and making them available here.
     4  Each repository has a name, which must be unique (including packages in the
     5  current repository).
     6  
     7  Rules in subrepos can be accessed in one of two ways:
     8   - by prefixing them with the name of the repo, for example //my_repo/pkg:target
     9     to access what would be //pkg:target within it.
    10   - by using Bazel-style @my_repo//pkg:target syntax.
    11  
    12  These are still fairly experimental.
    13  """
    14  
    15  def workspace(name:str):
    16      """Defines the name of the current workspace.
    17  
    18      Args:
    19        name (str): Name to bind this workspace as.
    20      """
    21      subrepo(
    22          name = name,
    23          path = '',
    24      )
    25  
    26  
    27  def http_archive(name:str, urls:list, strip_prefix:str=None, hashes:str|list=None,
    28                   sha256:str=None, visibility:list=None):
    29      """Fetches a remote file over HTTP and expands its contents.
    30  
    31      The archive should be either a zipfile or a tarball. Which one to use will be autodetected
    32      based on the file extension in the URL given.
    33  
    34      This is still experimental and known not to work in many cases.
    35  
    36      Args:
    37        name: Name of the rule.
    38        urls: List of URLs to fetch from. These are assumed to be mirrors and will be
    39              tried in sequence.
    40        strip_prefix: Prefix to strip from the expanded archive.
    41        hashes: List of hashes to verify the rule with.
    42        sha256: Used for Bazel compat but currently has no effect.
    43        visibility: Visibility declaration of the rule.
    44      """
    45      new_http_archive(
    46          name = name,
    47          urls = urls,
    48          strip_prefix = strip_prefix,
    49          hashes = hashes,
    50          sha256 = sha256,
    51          visibility = visibility,
    52      )
    53  
    54  
    55  def new_http_archive(name:str, urls:list, build_file:str=None, build_file_content:str=None,
    56                       strip_prefix:str=None, hashes:str|list=None, sha256:str=None, visibility:list=None):
    57      """Fetches a remote file over HTTP and expands its contents, combined with a BUILD file.
    58  
    59      The archive should be either a zipfile or a tarball. Which one to use will be autodetected
    60      based on the file extension in the URL given.
    61  
    62      The given build file (via build_file or build_file_contents) will replace any existing file
    63      and will therefore be used to build the contents of the subrepo.
    64  
    65      This is still experimental and known not to work in many cases.
    66  
    67      Args:
    68        name: Name of the rule.
    69        urls: List of URLs to fetch from. These are assumed to be mirrors and will be
    70                     tried in sequence.
    71        build_file: The file to use as a BUILD file for this subrepository.
    72        build_file_content: Text content to use for the BUILD file for the subrepository.
    73                            We suggest using build_file instead, but this is provided for Bazel compatibility.
    74        strip_prefix: Prefix to strip from the expanded archive.
    75        hashes: List of hashes to verify the rule with.
    76        sha256: Used for Bazel compat but currently has no effect.
    77        visibility: Visibility declaration of the rule.
    78      """
    79      remote_rule = remote_file(
    80          name = name,
    81          _tag = 'download',
    82          url = urls,
    83          out = name + '_' + basename(urls[0]),
    84          hashes = hashes,
    85      )
    86      if any([u.endswith('.jar') or u.endswith('.zip') for u in urls]):
    87          if strip_prefix:
    88              cmd = '$TOOL x $SRCS_REMOTE -s ' + strip_prefix
    89          else:
    90              cmd = '$TOOL x $SRCS_REMOTE'
    91      else:
    92          if strip_prefix:
    93              cmd = 'tar -axf $SRCS_REMOTE && mv %s $OUT' % strip_prefix
    94          else:
    95              cmd = 'mkdir $OUT && tar -axf $SRCS_REMOTE -C $OUT'
    96      if build_file:
    97          cmd += ' && mv $SRCS_BUILD $OUT/' + CONFIG.BUILD_FILE_NAMES[0]
    98      elif build_file_content:
    99          cmd += ' && cat > $OUT/%s << EOF\n%s\nEOF' % (CONFIG.BUILD_FILE_NAMES[0], build_file_content)
   100  
   101      extract_rule = build_rule(
   102          name = name,
   103          srcs = {
   104              'remote': [remote_rule],
   105              'build': [build_file],
   106          },
   107          outs = [name],
   108          cmd = cmd,
   109      )
   110      subrepo(
   111          name = name,
   112          dep = extract_rule,
   113      )