Function: package-unpack

package-unpack is a byte-compiled function defined in package.el.gz.

Signature

(package-unpack PKG-DESC)

Documentation

Install the contents of the current buffer as a package.

The argument PKG-DESC contains metadata of the yet to be installed package. The function returns a package-desc object of the actually installed package.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/package.el.gz
(defun package-unpack (pkg-desc)
  "Install the contents of the current buffer as a package.
The argument PKG-DESC contains metadata of the yet to be installed
package.  The function returns a `package-desc' object of the actually
installed package."
  (let* ((name (package-desc-name pkg-desc))
         (full-name (package-desc-full-name pkg-desc))
         (pkg-dir (expand-file-name full-name package-user-dir))
         (review-p (package--review-p pkg-desc))
         (unpack-dir (if review-p
                         (let ((temporary-file-directory package-review-directory))
                           (make-directory temporary-file-directory t) ;ensure existence
                           (expand-file-name
                            full-name
                            (make-temp-file "emacs-package-review-" t)))
                       pkg-dir))
         (old-desc (package--get-activatable-pkg name)))
    (make-directory unpack-dir t)
    (save-window-excursion
      (pcase (package-desc-kind pkg-desc)
        ('dir
         (let ((file-list
                (or (and (derived-mode-p 'dired-mode)
                         (dired-get-marked-files nil 'marked))
                    (directory-files-recursively default-directory "" nil))))
           (dolist (source-file file-list)
             (let ((target (expand-file-name
                            (file-relative-name source-file default-directory)
                            unpack-dir)))
               (make-directory (file-name-directory target) t)
               (copy-file source-file target t)))
           ;; Now that the files have been installed, this package is
           ;; indistinguishable from a `tar' or a `single'. Let's make
           ;; things simple by ensuring we're one of them.
           (setf (package-desc-kind pkg-desc)
                 (if (length> file-list 1) 'tar 'single))))
        ('tar
         (let ((default-directory (file-name-directory unpack-dir)))
           (package-untar-buffer (file-name-nondirectory unpack-dir))))
        ('single
         (let ((el-file (expand-file-name (format "%s.el" name) unpack-dir)))
           (package--write-file-no-coding el-file)))
        (kind (error "Unknown package kind: %S" kind))))

    ;; check if the user wants to review this package
    (when review-p
      (unwind-protect
          (progn
            (save-window-excursion
              (package-review pkg-desc unpack-dir old-desc))
            (make-directory package-user-dir t)
            (rename-file unpack-dir pkg-dir))
        (let ((temp-dir (file-name-directory unpack-dir)))
          (when (file-directory-p temp-dir)
            (delete-directory temp-dir t)))))
    (cl-assert (file-directory-p pkg-dir))

    (package--make-autoloads-and-stuff pkg-desc pkg-dir)
    ;; Update package-alist.
    (let ((new-desc (package-load-descriptor pkg-dir)))
      (unless (equal (package-desc-full-name new-desc)
                     (package-desc-full-name pkg-desc))
        (error "The retrieved package (`%s') doesn't match what the archive offered (`%s')"
               (package-desc-full-name new-desc) (package-desc-full-name pkg-desc)))
      ;; Activation has to be done before compilation, so that if we're
      ;; upgrading and macros have changed we load the new definitions
      ;; before compiling.
      (when (package-activate-1 new-desc :reload :deps)
        ;; FIXME: Compilation should be done as a separate, optional, step.
        ;; E.g. for multi-package installs, we should first install all packages
        ;; and then compile them.
        (package--compile new-desc)
        (when package-native-compile
          (package--native-compile-async new-desc))
        ;; After compilation, load again any files loaded by
        ;; `activate-1', so that we use the byte-compiled definitions.
        (package--reload-previously-loaded new-desc))

      new-desc)))