Function: bazel--download-http-archive

bazel--download-http-archive is a natively compiled function defined in bazel.el.

Signature

(bazel--download-http-archive URL)

Documentation

Download and interpret HTTP archive at URL.

Return a list (NAME INTEGRITY PREFIX TIME) for
‘bazel-insert-http-archive’.

Source Code

;; Defined in /nix/store/ws8pkszjvcad1rgzqw6wja7qxacylfhn-emacs-packages-deps/share/emacs/site-lisp/elpa/bazel-20261002.1510/bazel.el
(defun bazel--download-http-archive (url)
  "Download and interpret HTTP archive at URL.
Return a list (NAME INTEGRITY PREFIX TIME) for
‘bazel-insert-http-archive’."
  (declare (ftype (function (string) cons)))
  (cl-check-type url string)
  (let* ((temp-dir (make-temp-file "bazel-http-archive-" :directory))
         (name (url-file-nondirectory url))
         (archive-file (expand-file-name
                        (if (string-empty-p name) "archive" name) temp-dir))
         (reporter (make-progress-reporter
                    (format-message "Downloading %s into %s..." url temp-dir)))
         (coding-system-for-read 'no-conversion)
         (coding-system-for-write 'no-conversion))
    (url-copy-file url archive-file)
    (progress-reporter-update reporter)
    ;; The ‘bound-and-true-p’ check below won’t load ‘tramp-archive’, so load it
    ;; manually here.
    (require 'tramp-archive nil :noerror)
    (let* ((archive-dir
            ;; Prefer TRAMP’s archive support if available.
            (if (bound-and-true-p tramp-archive-enabled)
                (file-name-as-directory (file-name-unquote archive-file))
              ;; Fall back to extracting the archive locally.
              (let ((dir (expand-file-name "extract/" temp-dir)))
                (make-directory dir)
                (bazel--extract-archive archive-file dir)
                (progress-reporter-update reporter)
                dir)))
           (archive-entries
            (or (prog1 (directory-files-and-attributes
                        archive-dir nil directory-files-no-dot-files-regexp)
                  (progress-reporter-update reporter))
                (user-error "Empty archive")))
           (candidates
            ;; Find directories in the archive.  If there’s more than one,
            ;; prompt the user which prefix to strip.
            (cl-loop for (name . attrs) in archive-entries
                     when (eq (file-attribute-type attrs) t)
                     ;; Put attribute list into text property so that
                     ;; ‘completing-read’ won’t lose it.
                     collect (propertize (file-name-as-directory name)
                                         'attributes attrs)))
           (prefix (cl-case (length candidates)
                     (0 "")               ; no directories, prefix must be empty
                     (1 (car candidates)) ; no need to prompt user
                     (t (completing-read "Prefix to strip: " candidates
                                         nil nil nil nil ""))))
           (time (file-attribute-modification-time
                  (if (string-empty-p prefix)
                      ;; No prefix directory selected (or archive doesn’t
                      ;; contain any directory), pick an arbitrary file for the
                      ;; modification time.
                      (cdar archive-entries)
                    (get-text-property 0 'attributes prefix))))
           (root-dir (expand-file-name prefix archive-dir))
           (name (when-let* ((workspace (bazel--locate-workspace-file root-dir)))
                   (with-temp-buffer
                     (insert-file-contents workspace)
                     (bazel-workspace-mode)
                     (progress-reporter-update reporter)
                     (bazel--workspace-name))))
           (sha256 (with-temp-buffer
                     (insert-file-contents-literally archive-file)
                     (progress-reporter-update reporter)
                     (secure-hash 'sha256 (current-buffer) nil nil :binary)))
           (integrity
            (concat "sha256-" (base64-encode-string sha256 :no-line-break))))
      ;; We delete the temporary directory only when successful to make
      ;; debugging easier.
      (delete-directory temp-dir :recursive)
      (progress-reporter-done reporter)
      (list name integrity prefix time))))