16

I want my emacs to automatically upgrade all packages under certain conditions.

What's the best way of doing this?

PythonNut
  • 10,243
  • 2
  • 29
  • 75
  • Here is a command fo upgrade all outdated packages: https://github.com/Malabarba/paradox/blob/2.3.5/paradox.el#L165-L181 (replace all `paradox` with `package`). – xuchunyang Sep 06 '15 at 23:17
  • @xuchunyang Ah. I see that doesn't look too bad. If you make that an answer, I'd accept it. – PythonNut Sep 07 '15 at 02:31

3 Answers3

12

I'm not sure this is what you want (I don't know what you mean by “under certain conditions”), but here is a function I use to upgrade all packages without showing *Packages* buffer, which I find annoying when I just want to upgrade packages.

(defun package-upgrade-all ()
  "Upgrade all packages automatically without showing *Packages* buffer."
  (interactive)
  (package-refresh-contents)
  (let (upgrades)
    (cl-flet ((get-version (name where)
                (let ((pkg (cadr (assq name where))))
                  (when pkg
                    (package-desc-version pkg)))))
      (dolist (package (mapcar #'car package-alist))
        (let ((in-archive (get-version package package-archive-contents)))
          (when (and in-archive
                     (version-list-< (get-version package package-alist)
                                     in-archive))
            (push (cadr (assq package package-archive-contents))
                  upgrades)))))
    (if upgrades
        (when (yes-or-no-p
               (message "Upgrade %d package%s (%s)? "
                        (length upgrades)
                        (if (= (length upgrades) 1) "" "s")
                        (mapconcat #'package-desc-full-name upgrades ", ")))
          (save-window-excursion
            (dolist (package-desc upgrades)
              (let ((old-package (cadr (assq (package-desc-name package-desc)
                                             package-alist))))
                (package-install package-desc)
                (package-delete  old-package)))))
      (message "All packages are up to date"))))

This is well-tried. It also prevents compilation buffers from popping up.

Mark Karpov
  • 4,893
  • 1
  • 24
  • 53
  • Nice. I'm going to use this with a slight variation that doesn't prompt before upgrading the packages. – PythonNut Sep 07 '15 at 15:03
  • Is there any particular reason why you do `(y-or-n-p (message ..))` instead of just `(y-or-n-p ...)`? – PythonNut Sep 08 '15 at 21:19
  • @PythonNut, Well I need to construct prompt message, I don't remember now why I use `message`, it seems it can be done with `format` just as well. – Mark Karpov Sep 08 '15 at 21:55
  • Ah yes. I meant using `format`. Obviously you can't do it without any sort of string interpolation. – PythonNut Sep 08 '15 at 21:58
9

You can use auto-package-update.

Install it by M-x package-install auto-update-package. It provides a function called auto-package-update-now. You can write a simple if condition and add that to your .emacs.

(if your-condition
   (auto-package-update-now))
Chillar Anand
  • 4,042
  • 1
  • 23
  • 52
  • Hm... I'll keep this in mind, but the idea is to try automatically fixing packaged if they are broken. Depending on a package to do so is kinda nonsensical. :-) – PythonNut Sep 07 '15 at 04:05
2

The package is now called auto-package update. (Too low reputation to write it as a comment to the Chillar post above).

(Copied from his post and merged with this answer):

Install it by M-x package-install auto-package-update. It provides a function called auto-package-update-now. You can write a simple if condition and add that to your ~/.emacs.

(if your-condition
   (auto-package-update-now))
Stefan
  • 26,154
  • 3
  • 46
  • 84
Richard
  • 121
  • 2