2018-03-20 16:40:26 +00:00
|
|
|
;;; use-package-ensure-system-package.el --- auto install system packages -*- lexical-binding: t; -*-
|
2017-12-04 15:57:23 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 2022-2024 Free Software Foundation, Inc.
|
2017-12-04 15:57:23 +00:00
|
|
|
|
|
|
|
;; Author: Justin Talbott <justin@waymondo.com>
|
|
|
|
;; Keywords: convenience, tools, extensions
|
|
|
|
;; URL: https://github.com/waymondo/use-package-ensure-system-package
|
2018-04-25 20:22:53 +00:00
|
|
|
;; Package-Requires: ((use-package "2.1") (system-packages "1.0.4"))
|
2022-11-16 07:37:27 +00:00
|
|
|
|
2022-12-08 22:56:24 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2022-11-16 07:37:27 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
2022-12-08 22:56:24 +00:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2022-11-16 07:37:27 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2022-12-08 22:56:24 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2017-12-04 15:57:23 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
2022-12-08 22:56:24 +00:00
|
|
|
|
2017-12-04 15:57:23 +00:00
|
|
|
;; The `:ensure-system-package` keyword allows you to ensure system
|
2022-12-08 23:24:15 +00:00
|
|
|
;; binaries exist alongside your `use-package` declarations. Using it
|
|
|
|
;; requires the `system-packages' package to be installed (available
|
|
|
|
;; on GNU ELPA).
|
|
|
|
;;
|
|
|
|
;; See the `use-package' info manual for more information.
|
2017-12-04 15:57:23 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'use-package)
|
2017-12-04 19:53:35 +00:00
|
|
|
(require 'system-packages nil t)
|
|
|
|
|
|
|
|
(eval-when-compile
|
2018-05-19 18:44:43 +00:00
|
|
|
(declare-function system-packages-get-command "system-packages"))
|
2017-12-04 15:57:23 +00:00
|
|
|
|
2021-04-17 21:08:35 +00:00
|
|
|
(defvar use-package-ensure-system-package--custom-packages '()
|
2022-12-08 05:27:22 +00:00
|
|
|
"List of commands used to install custom packages.")
|
2017-12-04 15:57:23 +00:00
|
|
|
|
|
|
|
(defun use-package-ensure-system-package-consify (arg)
|
2022-12-08 05:27:22 +00:00
|
|
|
"Turn ARG into a cons of the form (PACKAGE-NAME . INSTALL-COMMAND')."
|
2017-12-04 15:57:23 +00:00
|
|
|
(cond
|
|
|
|
((stringp arg)
|
2018-09-12 16:53:26 +00:00
|
|
|
(cons arg `(system-packages-install ,arg)))
|
2017-12-04 15:57:23 +00:00
|
|
|
((symbolp arg)
|
2018-09-12 16:53:26 +00:00
|
|
|
(cons arg `(system-packages-install ,(symbol-name arg))))
|
2018-03-16 16:29:20 +00:00
|
|
|
((consp arg)
|
2018-09-12 16:53:26 +00:00
|
|
|
(cond
|
|
|
|
((not (cdr arg))
|
|
|
|
(use-package-ensure-system-package-consify (car arg)))
|
|
|
|
((stringp (cdr arg))
|
2021-04-17 21:08:35 +00:00
|
|
|
(progn
|
|
|
|
(push (cdr arg) use-package-ensure-system-package--custom-packages)
|
|
|
|
(cons (car arg) `(async-shell-command ,(cdr arg)))))
|
2018-09-12 16:53:26 +00:00
|
|
|
(t
|
2018-03-16 16:29:20 +00:00
|
|
|
(cons (car arg)
|
2018-09-12 16:53:26 +00:00
|
|
|
`(system-packages-install ,(symbol-name (cdr arg)))))))))
|
2017-12-04 15:57:23 +00:00
|
|
|
|
2021-04-17 21:08:35 +00:00
|
|
|
(defun use-package-ensure-system-package-update-custom-packages ()
|
2022-12-08 05:27:22 +00:00
|
|
|
"Update custom packages (not installed by system package manager).
|
|
|
|
Run the same commands used for installing them."
|
2021-04-17 21:08:35 +00:00
|
|
|
(interactive)
|
|
|
|
(dolist (cmd use-package-ensure-system-package--custom-packages)
|
|
|
|
(async-shell-command cmd)))
|
|
|
|
|
2017-12-05 18:28:28 +00:00
|
|
|
;;;###autoload
|
2018-03-20 16:40:26 +00:00
|
|
|
(defun use-package-normalize/:ensure-system-package (_name-symbol keyword args)
|
2022-12-08 05:27:22 +00:00
|
|
|
"Turn ARGS into a list of conses of the form (PACKAGE-NAME . INSTALL-COMMAND)."
|
2019-02-10 01:52:50 +00:00
|
|
|
(use-package-as-one (symbol-name keyword) args
|
2018-03-20 16:40:26 +00:00
|
|
|
(lambda (_label arg)
|
2017-12-04 15:57:23 +00: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 21:47:42 +00:00
|
|
|
(defun use-package-ensure-system-package-exists? (file-or-exe)
|
2022-12-08 05:27:22 +00:00
|
|
|
"If FILE-OR-EXE is a string, ensure the file path exists.
|
2018-07-09 21:47:42 +00:00
|
|
|
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 16:53:26 +00:00
|
|
|
|
2017-12-05 18:28:28 +00:00
|
|
|
;;;###autoload
|
2018-03-20 16:40:26 +00:00
|
|
|
(defun use-package-handler/:ensure-system-package (name _keyword arg rest state)
|
2017-12-04 15:57:23 +00: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 21:47:42 +00:00
|
|
|
`(unless (use-package-ensure-system-package-exists? ',(car cons))
|
2018-09-12 16:53:26 +00:00
|
|
|
,(cdr cons))) arg)
|
2017-12-04 15:57:23 +00:00
|
|
|
body)))
|
|
|
|
|
2017-12-04 17:31:13 +00:00
|
|
|
(add-to-list 'use-package-keywords :ensure-system-package t)
|
|
|
|
|
2017-12-04 15:57:23 +00:00
|
|
|
(provide 'use-package-ensure-system-package)
|
2017-12-04 17:31:13 +00:00
|
|
|
|
2017-12-04 15:57:23 +00:00
|
|
|
;;; use-package-ensure-system-package.el ends here
|