Subject: bug#63336: [PATCH] package-vc: Process :make and :shell-command spec args

From: Joseph Turner <[email-masked]>
Date: Mon, 08 May 2023 12:38:06 -0700

Ruijie Yu <[email-masked]> writes:

> Hello Joseph,
>
> On mobile so please excuse my brevity and top-posting.
>
> Minor remark on the defcustom type: I think you should move the current tag from "symbol" to its outer "repeat", and optionally tag "symbol" as something like "package name".  WDYT?

Thanks, Ruijie! I think the defcustom type is now correct.

Joseph

>From 8f9238b841f6deec65fd52d696aa26caf1bf123a Mon Sep 17 00:00:00 2001
From: Joseph Turner <[email-masked]>
Date: Sat, 6 May 2023 13:44:32 -0700
Subject: [PATCH] package-vc: Process :make and :shell-command spec args

---
 lisp/emacs-lisp/package-vc.el | 36 +++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/lisp/emacs-lisp/package-vc.el b/lisp/emacs-lisp/package-vc.el
index 421947b528d..95e12fc829a 100644
--- a/lisp/emacs-lisp/package-vc.el
+++ b/lisp/emacs-lisp/package-vc.el
@@ -344,6 +344,32 @@ asynchronously."
         "\n")
        nil pkg-file nil 'silent))))
 
+(defcustom package-vc-process-make nil
+  "Whether to process :make and :shell-command spec arguments.
+
+When set to a list of symbols (packages), run commands for only
+packages in the list. When `nil', never run commands. Otherwise
+when non-`nil', run commands for any package with :make or
+:shell-command specified.
+
+Package specs are loaded from trusted package archives."
+  :type '(choice (const :tag "Run for all packages" t)
+                 (repeat :tag "Run only for selected packages" (symbol :tag "Package name"))
+                 (const :tag "Never run" nil))
+  :version "30.1")
+
+(defun package-vc--make (pkg-spec)
+  "Process :make and :shell-command PKG-SPEC arguments."
+  (let ((target (plist-get pkg-spec :make))
+        (cmd (plist-get pkg-spec :shell-command)))
+    (when (or cmd target)
+      (with-current-buffer (get-buffer-create " *package-vc make*")
+        (erase-buffer)
+        (when (and cmd (/= 0 (call-process shell-file-name nil t nil shell-command-switch cmd)))
+          (warn "Failed to run %s, see buffer %S" cmd (buffer-name)))
+        (when (and target (/= 0 (apply #'call-process "make" nil t nil (if (consp target) target (list target)))))
+          (warn "Failed to make %s, see buffer %S" target (buffer-name)))))))
+
 (declare-function org-export-to-file "ox" (backend file))
 
 (defun package-vc--build-documentation (pkg-desc file)
@@ -486,6 +512,16 @@ documentation and marking the package as installed."
       ;; Generate package file
       (package-vc--generate-description-file pkg-desc pkg-file)
 
+      ;; Process :make and :shell-command arguments before building documentation
+      (pcase package-vc-process-make
+        ((pred consp) ; When non-`nil' list, check if package is on the list.
+         (when (memq (package-desc-name pkg-desc) package-vc-process-make)
+           (package-vc--make pkg-spec)))
+        ('nil         ; When `nil', do nothing.
+         nil)
+        (_            ; When otherwise non-`nil', run commands.
+         (package-vc--make pkg-spec)))
+
       ;; Detect a manual
       (when (executable-find "install-info")
         (dolist (doc-file (ensure-list (plist-get pkg-spec :doc)))
-- 
2.39.2