help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Install packages picked up from a list automatically.


From: Hongyi Zhao
Subject: Install packages picked up from a list automatically.
Date: Thu, 24 Dec 2020 21:37:20 +0800

I want to let emacs install needed packages stored in a list
automatically. For this purpose, I find the following methods which
can be used in ~/.emacs.d/init.el:

1. The method represented at
<https://realpython.com/emacs-the-best-python-editor/#initialization-file>:

;; If there are no archived package contents, refresh them
(when (not package-archive-contents)
  (package-refresh-contents))
;; Installs packages
;;

;; myPackages contains a list of package names
(defvar myPackages
  '(better-defaults                 ;; Set up some better Emacs defaults
    material-theme                  ;; Theme
    )
  )


;; Scans the list in myPackages
;; If the package listed is not already installed, install it
(mapc #'(lambda (package)
          (unless (package-installed-p package)
            (package-install package)))
      myPackages)

2. The method given by Bozhidar Batsov in his prelude project, see
<https://github.com/bbatsov/prelude/blob/aaedc8537c04e4af7a53690b8bbb8522d5a35b9d/core/prelude-packages.el#L56>
for more information.

(defvar prelude-packages
  ;; https://github.com/magnars/s.el#installation
  '(s
    flycheck)
  "A list of packages to ensure are installed at launch.")

(defun prelude-packages-installed-p ()
  "Check if all packages in `prelude-packages' are installed."
  (cl-every #'package-installed-p prelude-packages))

(defun prelude-require-package (package)
  "Install PACKAGE unless already installed."
  (unless (memq package prelude-packages)
    (add-to-list 'prelude-packages package))
  (unless (package-installed-p package)
    (package-install package)))

(defun prelude-require-packages (packages)
  "Ensure PACKAGES are installed.
Missing packages are installed automatically."
  (mapc #'prelude-require-package packages))

(defun prelude-install-packages ()
  "Install all packages listed in `prelude-packages'."
  (unless (prelude-packages-installed-p)
    ;; check for new packages (package versions)
    (message "%s" "Emacs Prelude is now refreshing its package database...")
    (package-refresh-contents)
    (message "%s" " done.")
    ;; install the missing packages
    (prelude-require-packages prelude-packages)))

;; run package installation
(prelude-install-packages)


I'm a thorough beginner as for emacs lisp. Could you please give me
some hints/notes/comments on the pros can cons of the above two
mentioned methods? Thanks in advance.

Regards,
HY
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Polytechnic University of Science and Technology engineering
NO. 552 North Gangtie Road, Xingtai, China



reply via email to

[Prev in Thread] Current Thread [Next in Thread]