2018-03-20 11:40:26 -05:00
|
|
|
;;; use-package-ensure-system-package.el --- auto install system packages -*- lexical-binding: t; -*-
|
2017-12-04 10:57:23 -05:00
|
|
|
|
|
|
|
;; Copyright (C) 2017 Justin Talbott
|
|
|
|
|
|
|
|
;; Author: Justin Talbott <justin@waymondo.com>
|
|
|
|
;; Keywords: convenience, tools, extensions
|
|
|
|
;; URL: https://github.com/waymondo/use-package-ensure-system-package
|
2018-09-12 17:53:26 +01:00
|
|
|
;; Version: 0.2
|
2018-04-26 02:22:53 +06:00
|
|
|
;; Package-Requires: ((use-package "2.1") (system-packages "1.0.4"))
|
2017-12-04 10:57:23 -05:00
|
|
|
;; Filename: use-package-ensure-system-package.el
|
|
|
|
;; License: GNU General Public License version 3, or (at your option) any later version
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
;; The `:ensure-system-package` keyword allows you to ensure system
|
|
|
|
;; binaries exist alongside your `use-package` declarations.
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'use-package)
|
2017-12-04 11:53:35 -08:00
|
|
|
(require 'system-packages nil t)
|
|
|
|
|
|
|
|
(eval-when-compile
|
2018-05-20 00:44:43 +06:00
|
|
|
(declare-function system-packages-get-command "system-packages"))
|
2017-12-04 10:57:23 -05:00
|
|
|
|
2021-04-17 17:08:35 -04:00
|
|
|
(defvar use-package-ensure-system-package--custom-packages '()
|
|
|
|
"List of custom packages installed.")
|
2017-12-04 10:57:23 -05:00
|
|
|
|
|
|
|
(defun use-package-ensure-system-package-consify (arg)
|
|
|
|
"Turn `arg' into a cons of (`package-name' . `install-command')."
|
|
|
|
(cond
|
|
|
|
((stringp arg)
|
2018-09-12 17:53:26 +01:00
|
|
|
(cons arg `(system-packages-install ,arg)))
|
2017-12-04 10:57:23 -05:00
|
|
|
((symbolp arg)
|
2018-09-12 17:53:26 +01:00
|
|
|
(cons arg `(system-packages-install ,(symbol-name arg))))
|
2018-03-16 12:29:20 -04:00
|
|
|
((consp arg)
|
2018-09-12 17:53:26 +01:00
|
|
|
(cond
|
|
|
|
((not (cdr arg))
|
|
|
|
(use-package-ensure-system-package-consify (car arg)))
|
|
|
|
((stringp (cdr arg))
|
2021-04-17 17:08:35 -04:00
|
|
|
(progn
|
|
|
|
(push (cdr arg) use-package-ensure-system-package--custom-packages)
|
|
|
|
(cons (car arg) `(async-shell-command ,(cdr arg)))))
|
2018-09-12 17:53:26 +01:00
|
|
|
(t
|
2018-03-16 12:29:20 -04:00
|
|
|
(cons (car arg)
|
2018-09-12 17:53:26 +01:00
|
|
|
`(system-packages-install ,(symbol-name (cdr arg)))))))))
|
2017-12-04 10:57:23 -05:00
|
|
|
|
2021-04-17 17:08:35 -04:00
|
|
|
(defun use-package-ensure-system-package-update-custom-packages ()
|
|
|
|
(interactive)
|
|
|
|
(dolist (cmd use-package-ensure-system-package--custom-packages)
|
|
|
|
(async-shell-command cmd)))
|
|
|
|
|
2017-12-05 10:28:28 -08:00
|
|
|
;;;###autoload
|
2018-03-20 11:40:26 -05:00
|
|
|
(defun use-package-normalize/:ensure-system-package (_name-symbol keyword args)
|
2017-12-04 10:57:23 -05:00
|
|
|
"Turn `arg' into a list of cons-es of (`package-name' . `install-command')."
|
2019-02-09 20:52:50 -05:00
|
|
|
(use-package-as-one (symbol-name keyword) args
|
2018-03-20 11:40:26 -05:00
|
|
|
(lambda (_label arg)
|
2017-12-04 10:57:23 -05:00
|
|
|
(cond
|
|
|
|
((and (listp arg) (listp (cdr arg)))
|
|
|
|
(mapcar #'use-package-ensure-system-package-consify arg))
|
|
|
|
(t
|
|
|
|
(list (use-package-ensure-system-package-consify arg)))))))
|
|
|
|
|
2018-07-09 17:47:42 -04:00
|
|
|
(defun use-package-ensure-system-package-exists? (file-or-exe)
|
|
|
|
"If variable is a string, ensure the file path exists.
|
|
|
|
If it is a symbol, ensure the binary exist."
|
|
|
|
(if (stringp file-or-exe)
|
|
|
|
(file-exists-p file-or-exe)
|
|
|
|
(executable-find (symbol-name file-or-exe))))
|
|
|
|
|
2018-09-12 17:53:26 +01:00
|
|
|
|
2017-12-05 10:28:28 -08:00
|
|
|
;;;###autoload
|
2018-03-20 11:40:26 -05:00
|
|
|
(defun use-package-handler/:ensure-system-package (name _keyword arg rest state)
|
2017-12-04 10:57:23 -05:00
|
|
|
"Execute the handler for `:ensure-system-package' keyword in `use-package'."
|
|
|
|
(let ((body (use-package-process-keywords name rest state)))
|
|
|
|
(use-package-concat
|
|
|
|
(mapcar #'(lambda (cons)
|
2018-07-09 17:47:42 -04:00
|
|
|
`(unless (use-package-ensure-system-package-exists? ',(car cons))
|
2018-09-12 17:53:26 +01:00
|
|
|
,(cdr cons))) arg)
|
2017-12-04 10:57:23 -05:00
|
|
|
body)))
|
|
|
|
|
2017-12-04 09:31:13 -08:00
|
|
|
(add-to-list 'use-package-keywords :ensure-system-package t)
|
|
|
|
|
2017-12-04 10:57:23 -05:00
|
|
|
(provide 'use-package-ensure-system-package)
|
2017-12-04 09:31:13 -08:00
|
|
|
|
2017-12-04 10:57:23 -05:00
|
|
|
;;; use-package-ensure-system-package.el ends here
|