Skip to content

Instantly share code, notes, and snippets.

@saevarb
Created June 8, 2015 13:49
Show Gist options
  • Save saevarb/181c3646c7594cf3f747 to your computer and use it in GitHub Desktop.
Save saevarb/181c3646c7594cf3f747 to your computer and use it in GitHub Desktop.
;; Given that I have a list of my packages as follows:
;; '((package package-require) (package))
;; Where each element in the list is a list containing
;; either 1 element e, which means I just need to do
;; (package-install e) and (require e)
;; or two elements, e and r, which means I need to do
;; (package-install e) but (require r)
;; Much like I want to (package-install helm) but
;; (require helm-config)
;; Is there something that does something similar to this already, or
;; is this solution alright? Any tips? Better solutions?
;; See below for maybe-require-package
(while my-packages
(setq-local curp (car my-packages))
(if (> (length curp) 1)
(progn (message "Package %s requires %s" (car curp) (car (last curp)))
(maybe-require-package (car curp))
(require (car (last curp)))
)
(progn (maybe-require-package (car curp))
(require (car curp)))
)
(setq my-packages (cdr my-packages))
)
(defun maybe-require-package (package &optional min-version no-refresh)
"Try to install PACKAGE, and return non-nil if successful.
In the event of failure, return nil and print a warning message.
Optionally require MIN-VERSION. If NO-REFRESH is non-nil, the
available package lists will not be re-downloaded in order to
locate PACKAGE."
(condition-case err
(require-package package min-version no-refresh)
(error
(message "Couldn't install package `%s': %S" package err)
nil)))
(defun require-package (package &optional min-version no-refresh)
"Install given PACKAGE, optionally requiring MIN-VERSION.
If NO-REFRESH is non-nil, the available package lists will not be
re-downloaded in order to locate PACKAGE."
(if (package-installed-p package min-version)
t
(if (or (assoc package package-archive-contents) no-refresh)
(package-install package)
(progn
(package-refresh-contents)
(require-package package min-version t)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment