-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathelpaca.el
2101 lines (1929 loc) · 100 KB
/
elpaca.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; elpaca.el --- An elisp package manager -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2025 Nicholas Vollmer
;; Author: Nicholas Vollmer
;; URL: https://github.com/progfolio/elpaca
;; Created: Jan 1, 2022
;; Keywords: tools, convenience, lisp
;; Package-Requires: ((emacs "27.1"))
;; Version: 0.0.2
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; 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.
;; This program is distributed in the hope that it will be useful,
;; 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
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(eval-and-compile (require 'cl-lib))
(eval-when-compile (require 'subr-x))
(cl-declaim (optimize (safety 0) (speed 3)))
(require 'elpaca-process)
(declare-function autoload-rubric "autoload")
(declare-function info-initialize "info")
(declare-function url-filename "url-parse")
(defvar autoload-timestamps)
(defvar generated-autoload-file)
(defvar Info-directory-list)
(defconst elpaca--inactive-states '(blocked finished failed))
(defvar elpaca-installer-version -1)
(or noninteractive (= elpaca-installer-version 0.9)
(lwarn '(elpaca installer) :warning "%s installer version does not match %s."
(or (symbol-file 'elpaca-installer-version 'defvar) "Init")
(expand-file-name "./doc/installer.el" (file-name-directory (or load-file-name (buffer-file-name))))))
(or (executable-find "git") (error "Elpaca unable to find git executable"))
(and (not after-init-time) load-file-name (featurep 'package) (warn "Package.el loaded before Elpaca"))
(defgroup elpaca nil "An elisp package manager." :group 'applications :prefix "elpaca-")
(defface elpaca-finished '((t (:inherit success))) "Indicates an order is finished.")
(defface elpaca-busy '((t (:inherit warning :inverse-video t)))
"Indicates order's subprocess has not produced output in `elpaca-busy-interval'.")
(defface elpaca-blocked '((t (:inherit warning))) "Indicates an order is blocked.")
(defface elpaca-failed '((t (:inherit error))) "Indicates an order has failed.")
(defcustom elpaca-status-faces '((blocked . elpaca-blocked)
(finished . elpaca-finished)
(failed . elpaca-failed)
(busy . elpaca-busy))
"Alist mapping order statuses to faces."
:type '(alist :key-type symbol :options (blocked finished failed busy) :value-type face))
(defvar elpaca--pre-built-steps
'(elpaca--queue-dependencies elpaca--add-info-path elpaca--activate-package)
"List of steps for packages which are already built.")
(defvar elpaca-after-init-time nil "`current-time' after processing all queues.")
(defcustom elpaca-after-init-hook nil
"Elpaca's analogue to `after-init-hook'.
This is run after all orders queued during init have finished processing.
It is only run once after init.
Note a blocked process will prevent this hook from being run."
:type 'hook)
(defcustom elpaca-post-queue-hook nil
"Hook run after a queue is finished processing.
Note blocked or failed orders will prevent this hook from being run."
:type 'hook)
(defcustom elpaca-queue-limit nil
"When non-nil, limit the number of orders which can be processed at once."
:type 'integer)
(defcustom elpaca-cache-autoloads t
"If non-nil, cache package autoloads and load all at once.
Results in faster start-up time." :type 'boolean)
(defcustom elpaca-directory (expand-file-name "elpaca" user-emacs-directory)
"Location of the elpaca package store." :type 'directory)
(defvar elpaca-cache-directory (expand-file-name "cache" elpaca-directory)
"Location of the cache directory.")
(defvar elpaca-builds-directory (expand-file-name "builds" elpaca-directory)
"Location of the builds directory.")
(defvar elpaca-repos-directory (expand-file-name "repos" elpaca-directory)
"Location of the repos directory.")
(defcustom elpaca-makeinfo-executable (executable-find "makeinfo")
"Path of the makeinfo executable." :type '(file :must-match t))
(defcustom elpaca-install-info-executable (executable-find "install-info")
"Path of the install-info executable." :type '(file :must-match t))
(defvar elpaca--log-timer nil "Timer to debounce order info printing.")
(defcustom elpaca-log-interval 0.02
"Number of idle seconds to wait before updating log buffer.
Setting this to too low may cause the status buffer to block more.
Setting it too high causes prints fewer status updates."
:type 'number)
(defcustom elpaca-busy-interval 60
"Seconds to wait between subprocess outputs before declaring process blocked."
:type 'number)
(defcustom elpaca-build-steps '(elpaca--clone
elpaca--configure-remotes
elpaca--checkout-ref
elpaca--run-pre-build-commands
elpaca--queue-dependencies
elpaca--check-version
elpaca--link-build-files
elpaca--generate-autoloads-async
elpaca--byte-compile
elpaca--compile-info
elpaca--install-info
elpaca--add-info-path
elpaca--run-post-build-commands
elpaca--activate-package)
"List of steps which are run when installing/building a package."
:type '(repeat function))
(defvar elpaca--debug-init init-file-debug "Preserves --debug-init option.")
(defvar elpaca-default-files-directive
'("*.el" "*.el.in" "dir" "*.info" "*.texi" "*.texinfo"
"doc/dir" "doc/*.info" "doc/*.texi" "doc/*.texinfo" "lisp/*.el"
"docs/dir" "docs/*.info" "docs/*.texi" "docs/*.texinfo"
(:exclude ".dir-locals.el" "test.el" "tests.el" "*-test.el" "*-tests.el" "LICENSE"
"README*" "*-pkg.el"))
"Default value for the `:files' directive in recipes.
It is also spliced in at any point where the `:defaults' keyword
is used in a `:files' directive.")
(defvar elpaca-order-defaults (list :protocol 'https :inherit t :depth 'treeless)
"Default order modifications.")
(defun elpaca-order-defaults (_order) "Return order defaults." elpaca-order-defaults)
(defcustom elpaca-order-functions '(elpaca-order-defaults)
"Abnormal hook run to alter orders.
Each element must be a unary function which accepts an order.
An order may be nil, a symbol naming a package, or a plist.
The function may return nil or a plist to be merged with the order.
This hook is run via `run-hook-with-args-until-success'."
:type 'hook)
(defcustom elpaca-recipe-functions nil
"Abnormal hook run to alter recipes.
Each element must be a unary function which accepts an recipe plist.
The function may return nil or a plist to be merged with the recipe.
This hook is run via `run-hook-with-args-until-success'."
:type 'hook)
(defsubst elpaca-alist-get (key alist &optional default)
"Return KEY's value in ALIST or DEFAULT.
Simplified, faster version of `alist-get'."
(or (cdr (assq key alist)) default))
(defvar elpaca-menu-extensions--cache nil "Cache for `elpaca-menu-extenions' items.")
(defun elpaca-menu-extensions (request &optional item)
"Return menu ITEM REQUEST."
(if-let* (((eq request 'index))
(cache (or elpaca-menu-extensions--cache (elpaca-menu-extensions 'update))))
(if item (elpaca-alist-get item cache) cache)
(setq elpaca-menu-extensions--cache
(list (cons 'elpaca-use-package
(list :source "Elpaca extensions"
:description "Elpaca use-package support."
:recipe (list :package "elpaca-use-package" :wait t
:repo "https://github.com/progfolio/elpaca.git"
:files '("extensions/elpaca-use-package.el")
:main "extensions/elpaca-use-package.el"
:build '(:not elpaca--compile-info))))))))
(defcustom elpaca-menu-functions
'( elpaca-menu-lock-file elpaca-menu-extensions elpaca-menu-org elpaca-menu-melpa
elpaca-menu-non-gnu-elpa elpaca-menu-gnu-elpa elpaca-menu-declarations)
"Abnormal hook to lookup packages in menus.
Each function is passed a request, which may be any of the following symbols:
- `index`
Must return a alist of the menu's package candidates.
Each candidate is a cell of form:
(PACKAGE-NAME . (:source SOURCE-NAME :recipe RECIPE-PLIST))
- `update`
Updates the menu's package candidate list.
Each function must also accept an optional ITEM as a second argument."
;;@TODO: document ITEM spec requirements
:type 'hook)
(defcustom elpaca-verbosity 0 "Maximum event verbosity level shown in logs."
:type 'integer)
(defcustom elpaca-default-remote-name "origin" "Default remote name." :type 'string)
;;@COMPAT @HACK:
;;Transient not in `package--builtin-versions' from addition in 28.1 until Emacs 30.
(when-let* (((< 27 emacs-major-version 30))
(transient-versions '(("28.1" . (0 3 7)) ("28.2" . (0 3 7))
("29.1" . (0 4 1)) ("29.2" . (0 4 3))
("29.3" . (0 4 3)) ("29.4" . (0 4 3)))))
(add-to-list 'package--builtin-versions
(cons 'transient (alist-get emacs-version transient-versions
nil nil #'string-prefix-p))))
(defcustom elpaca-ignored-dependencies (mapcar #'car package--builtin-versions)
"List of IDs which are not installed unless the user explicitly requests them."
:type '(repeat symbol))
(defvar elpaca-overriding-prompt nil "Overriding prompt for interactive functions.")
(defun elpaca--read-file (path)
"Read file at PATH into memory."
(when-let* (((file-exists-p path))
(dir default-directory))
(condition-case-unless-debug err
(with-current-buffer (get-buffer-create " elpaca--read-file")
(setq default-directory dir)
(insert-file-contents path nil nil nil 'replace)
(goto-char (point-min))
(read (current-buffer)))
((error) (warn "Error reading %S into memory: %S" path err) nil))))
(defmacro elpaca--write-file (file &rest body)
"Write FILE using BODY.
`standard-output' and print variables are lexically bound for convenience.
e.g. elisp forms may be printed via `prin1'."
(declare (indent 1) (debug t))
`(let ((coding-system-for-write 'utf-8))
(with-temp-file ,file
(let* ((standard-output (current-buffer))
print-circle print-level print-length)
,@body
nil))))
(cl-defstruct (elpaca-q (:constructor elpaca-q<-create)
(:type list) (:copier nil)
(:named) (:conc-name elpaca-q<-))
"Queue to hold elpacas."
(type (when (or (not after-init-time)
(let ((init (expand-file-name "init.el" user-emacs-directory)))
(member init (list load-file-name (buffer-file-name)))))
'init))
(id (if (boundp 'elpaca--queues) (length elpaca--queues) 0))
(processed 0)
(status 'incomplete)
(time (current-time))
autoloads forms elpacas)
(defvar elpaca--queues (list (elpaca-q<-create)) "List of elpaca queue objects.")
(defun elpaca-merge-plists (&rest plists)
"Return plist with set of unique keys from PLISTS.
Values for each key are that of the right-most plist containing that key."
(let ((plists (delq nil plists))
current plist)
(while (setq current (pop plists))
(while current (setq plist (plist-put plist (pop current) (pop current)))))
plist))
(defsubst elpaca--first (obj)
"Return `car' of OBJ if it is a list, else OBJ."
(if (listp obj) (car obj) obj))
(defsubst elpaca--q (e)
"Return E's Q."
(and e (car (last elpaca--queues (1+ (elpaca<-queue-id e))))))
;;;###autoload
(defun elpaca-menu-item (&optional id interactive)
"Return menu item matching ID in `elpaca-menu-functions'.
If ID is nil, prompt for item. If INTERACTIVE is non-nil, copy to `kill-ring'."
(interactive (list nil t))
(let ((item
(if id (run-hook-with-args-until-success 'elpaca-menu-functions 'index id) ;; Depth first search
(cl-loop
for menu in elpaca-menu-functions
append (cl-loop for i in (funcall menu 'index) collect
(cons (concat (symbol-name (car i)) " " (plist-get (cdr i) :source)) i))
into candidates
finally return
(let* ((completion-extra-properties
(list :annotation-function
(lambda (s)
(concat " " (plist-get (cdr (alist-get s candidates nil nil #'equal))
:description)))))
(choice (completing-read (or elpaca-overriding-prompt "Menu Item: ") candidates nil t)))
(alist-get choice candidates nil nil #'equal))))))
(when interactive
(message "menu-item copied to kill-ring:\n%S" item)
(kill-new (format "%S" item)))
item))
;;;###autoload
(defun elpaca-update-menus (&rest menus)
"Update all menus in MENUS or `elpaca-menu-functions'.
When called interactively with \\[universal-argument] update all menus."
(interactive (if (equal current-prefix-arg '(4))
elpaca-menu-functions
(mapcar #'intern (completing-read-multiple "Update Menus: "
elpaca-menu-functions))))
(let ((elpaca-menu-functions (or menus elpaca-menu-functions)))
(run-hook-with-args 'elpaca-menu-functions 'update)))
(defsubst elpaca--nonheritable-p (obj)
"Return t if OBJ has explicitly nil :inherit key, nil otherwise."
(when-let* (((listp obj))
(member (plist-member obj :inherit)))
(not (cadr member))))
(defun elpaca--order (&optional err)
"Prompt for order. User ERR is messaged when no order selected."
(if-let* ((elpaca-overriding-prompt (or elpaca-overriding-prompt "Order: "))
(item (elpaca-menu-item)))
(cons (car item) (plist-get (cdr item) :recipe))
(user-error (or err "No order selected"))))
;;;###autoload
(defun elpaca-recipe (&optional order interactive)
"Return recipe computed from ORDER.
ORDER is any of the following values:
- nil. The order is prompted for.
- an ID symbol, looked up in ITEMS or `elpaca-menu-functions' cache.
- an order list of the form: \\='(ID . PROPS).
When INTERACTIVE is non-nil, `yank' the recipe to the clipboard."
(interactive (list (elpaca--order) t))
(let* ((order (or order (elpaca--order)))
(id (elpaca--first order))
(props (let ((it (cdr-safe order)))
(unless (zerop (logand (length it) 1))
(user-error "Uneven property list for order %S %s" id it))
it))
(nonheritable-p (elpaca--nonheritable-p props))
(mods (unless nonheritable-p (run-hook-with-args-until-success
'elpaca-order-functions order)))
(inherit (plist-member props :inherit))
(item (let ((elpaca-menu-functions
(unless (or interactive ;; we already queried for this.
(elpaca--nonheritable-p (elpaca-merge-plists mods inherit)))
(if-let* ((menus (cadr inherit))
((not (eq menus t))))
(if (listp menus) menus (list menus))
elpaca-menu-functions))))
(elpaca-menu-item id)))
(item-recipe (plist-put (plist-get item :recipe) :source (plist-get item :source)))
(r (elpaca-merge-plists item-recipe mods props)))
(unless (plist-get r :package) (setq r (plist-put r :package (symbol-name id))))
(when-let* ((recipe-mods (run-hook-with-args-until-success 'elpaca-recipe-functions r)))
(setq r (elpaca-merge-plists r recipe-mods)))
(when-let* ((remotes (plist-get r :remotes)) ;; Normalize :remotes to list of specs
((not (ignore-errors (mapc #'length remotes)))))
(setq r (plist-put r :remotes (list remotes))))
(when interactive
(setq r (elpaca-merge-plists (append (list :package (symbol-name (car-safe id)))
(cdr-safe id))
r))
(kill-new (format "%S" r))
(message "%S recipe copied to kill-ring:\n%S" (plist-get r :package) r))
r))
(defsubst elpaca--emacs-path ()
"Return path to running Emacs."
(concat invocation-directory invocation-name))
(defsubst elpaca--repo-name (string)
"Return repo name portion of STRING."
(setq string (directory-file-name string)) ;; remove external :repo trailing slash
(file-name-base (substring string (- (or (string-match-p "/" (reverse string))
(error "Invalid repo name %S" string))))))
(defsubst elpaca--repo-user (string)
"Return user name portion of STRING."
(substring string 0 (string-match-p "/" string)))
(defun elpaca--repo-type (string)
"Return type of :repo STRING.
Type is `local' for a local filesystem path, `remote' for a remote URL, or nil."
(cond ((string-match-p "^[/~]" string) 'local)
((string-match-p ":" string) 'remote)))
(defvar elpaca--repo-dirs nil "List of registered repository directories.")
(defun elpaca-repo-dir (recipe)
"Return path to repo given RECIPE."
(let* ((url (plist-get recipe :url))
(repo (plist-get recipe :repo))
(remote (car-safe repo))
(local (cdr-safe repo))
(pkg (plist-get recipe :package))
(host (or (plist-get recipe :host) (plist-get recipe :fetcher)))
(hostname (and host (prin1-to-string host 'noescape)))
(user nil)
(info (concat url (or remote repo) hostname))
(key (or (and info (> (length info) 0) (intern info))
(error "Cannot determine URL from recipe: %S" recipe)))
(mono-repo (elpaca-alist-get key elpaca--repo-dirs))
(dirs (and (not mono-repo) (mapcar #'cdr elpaca--repo-dirs)))
(name (cond
(local
(if-let* ((owner (assoc local dirs)))
(error "Local repo %S owned by %s" local (cdr owner))
local))
(mono-repo (car mono-repo))
(url
(unless (featurep 'url-parse) (require 'url-parse))
(file-name-base (directory-file-name (url-filename
(url-generic-parse-url url)))))
(repo (if-let* ((r (or remote repo))
((eq (elpaca--repo-type r) 'local)))
(file-name-base (directory-file-name (or local r)))
(when host (setq user (elpaca--repo-user r)))
(elpaca--repo-name (or local r))))
(pkg pkg)
(t (error "Unable to determine repo name"))))
(dir (if (assoc name dirs)
(string-join (list name hostname user) ".")
(and name (replace-regexp-in-string "\\.el$" "" name)))))
(unless mono-repo (push (cons key (cons dir pkg)) elpaca--repo-dirs))
(file-name-as-directory (expand-file-name dir elpaca-repos-directory))))
(defun elpaca-build-dir (recipe)
"Return RECIPE's build dir."
(expand-file-name (plist-get recipe :package) elpaca-builds-directory))
(defun elpaca--repo-uri (recipe)
"Return repo URI from RECIPE."
(cl-destructuring-bind (&key (protocol 'https)
url
fetcher
(host fetcher)
(repo url) &allow-other-keys)
recipe
(when (consp repo) (setq repo (car repo))) ; Handle :repo rename
(pcase (elpaca--repo-type (or repo (error "Unable to determine recipe URL")))
('remote repo)
('local (expand-file-name repo))
(_ (let ((p (pcase protocol
('https '("https://" . "/"))
('ssh '("git@" . ":"))
(_ (signal 'wrong-type-argument `((https ssh) ,protocol)))))
(h (pcase host
('github "github.com")
('gitlab "gitlab.com")
('codeberg "codeberg.org")
('sourcehut "git.sr.ht")
((pred stringp) host)
(_ (signal 'wrong-type-argument
`(:host (github gitlab codeberg sourcehut stringp)
,host ,recipe))))))
(concat (car p) h (cdr p) (when (eq host 'sourcehut) "~") repo
(unless (eq host 'sourcehut) ".git")))))))
(cl-defstruct (elpaca (:constructor elpaca<--create) (:type list) (:named)
(:conc-name elpaca<-))
"Order for queued processing."
id package order statuses
repo-dir build-dir mono-repo main
files build-steps recipe blocking blockers
dependencies dependents
(queue-id (1- (length elpaca--queues)))
(queue-time (current-time))
(init (not after-init-time))
process log builtp)
(defun elpaca--queued (&optional n)
"Return list of elpacas from Nth queue.
If N is nil return a list of all queued elpacas."
(if n (elpaca-q<-elpacas (nth n elpaca--queues))
(cl-loop for queue in elpaca--queues append (elpaca-q<-elpacas queue))))
(defsubst elpaca--mono-repo (id repo)
"Return previously queued E with REPO other than ID."
(cl-loop for (_ . e) in (reverse (elpaca--queued)) thereis
(and (not (eq (elpaca<-id e) id)) (equal repo (elpaca<-repo-dir e)) e)))
(defun elpaca--build-steps (recipe &optional builtp clonedp mono-repo)
"Return list of build functions for RECIPE.
BUILTP, CLONEDP, and MONO-REPO control which steps are excluded."
(when-let* ((defaults (if builtp elpaca--pre-built-steps elpaca-build-steps))
(steps (let* ((build (plist-member recipe :build))
(steps (cadr build))
(removep (and (eq (car-safe steps) :not) (pop steps))))
(cond
((or (not build) (eq steps t)) defaults)
(removep (cl-set-difference defaults steps))
((listp steps) steps)))))
(if builtp
steps
(when mono-repo
(setq steps
(cl-set-difference steps '(elpaca--clone elpaca--configure-remotes elpaca--checkout-ref))))
(when clonedp (setq steps (remq 'elpaca--clone steps)))
steps)))
(declare-function elpaca-log-defaults "elpaca-log")
(declare-function elpaca-log-initial-queues "elpaca-log")
(defcustom elpaca-log-functions '(elpaca-log-initial-queues elpaca-log-command-query)
"Hook run prior to logging.
It's functions should return either:
- t to log with the buffer's default filter.
- a string which is used as the search query.
- `silent' to prevent logging altogether.
- nil to skip the function.
The first function, if any, which returns non-nil is used." :type 'hook)
(defvar elpaca--log-request-time nil "Time of most recent log event.")
(declare-function elpaca-log "elpaca-log")
(defun elpaca--maybe-log ()
"Log if `elpaca-log-functions' return non-nil."
(when-let* ((query (run-hook-with-args-until-success 'elpaca-log-functions)))
(require 'elpaca-log)
(unless (eq query 'silent)
(setq elpaca--log-request-time (current-time)) ;;@MAYBE: move into elpaca-log?
(elpaca-log (cond ((eq query t) nil)
((stringp query) query)
(t (signal 'wrong-type-error `((stringp t) ,query))))
t))))
(defun elpaca<-create (order)
"Create a new elpaca struct from ORDER."
(let* ((status 'queued)
(info "Package queued")
(id (elpaca--first order))
(recipe (condition-case-unless-debug err (elpaca-recipe order)
((error) (setq status 'struct-failed
info (format "No recipe: %S" err))
nil)))
(repo-dir (and recipe (condition-case-unless-debug err (elpaca-repo-dir recipe)
((error) (setq status 'struct-failed
info (format "Unable to determine repo dir: %S" err))
nil))))
(build-dir (and recipe (elpaca-build-dir recipe)))
(clonedp (and repo-dir (file-exists-p repo-dir)))
(builtp (and clonedp build-dir (file-exists-p build-dir)))
(blockers nil)
(mono-repo (when-let* (((not builtp))
(e (elpaca--mono-repo id repo-dir)))
(when (and (eq (elpaca<-queue-id e) (elpaca-q<-id (car elpaca--queues)))
(not (memq 'ref-checked-out (elpaca<-statuses e))))
(setq status 'blocked info (concat "Waiting on monorepo " repo-dir))
(push (elpaca<-id e) blockers)
(cl-pushnew id (elpaca<-blocking e)))
e))
(build-steps (elpaca--build-steps recipe builtp clonedp mono-repo)))
(when (memq id elpaca-ignored-dependencies)
(setq elpaca-ignored-dependencies (delq id elpaca-ignored-dependencies)))
(elpaca<--create
:id id :package (symbol-name id) :order order :statuses (list status)
:repo-dir repo-dir :build-dir build-dir :mono-repo mono-repo
:build-steps build-steps :recipe recipe :builtp builtp :blockers blockers
:log (list (list status nil info 0)))))
(defsubst elpaca--status (e) "Return E's status." (car (elpaca<-statuses e)))
(declare-function elpaca-ui--update-search-query "elpaca-ui")
(defun elpaca--update-log-buffer ()
"Update views in `elpaca-log-buffer'."
(when-let* ((log (bound-and-true-p elpaca-log-buffer))
((get-buffer-window log t))) ;; log buffer visible
(with-current-buffer log (elpaca-ui--update-search-query log))))
(defun elpaca--log (e text &optional verbosity replace)
"Store TEXT in E's log.
Each event is of the form: (STATUS TIME TEXT (or VERBOSITY 0))
If REPLACE is non-nil, the most recent log entry is replaced."
(let ((event (list (elpaca--status e) (current-time) text (or verbosity 0))))
(if replace
(setf (car (elpaca<-log e)) event)
(push event (elpaca<-log e)))))
(defvar elpaca--waiting nil "Non-nil when `elpaca-wait' is polling.")
(defvar elpaca--status-counts nil "Status counts for UI progress bar.")
(defun elpaca--count-statuses ()
"Update `elpaca--status-counts'."
(cl-loop with statuses for q in elpaca--queues
do (cl-loop for (_ . e) in (elpaca-q<-elpacas q)
for status = (elpaca--status e)
for state = (if (memq status elpaca--inactive-states) status 'other)
do (cl-incf (alist-get state statuses 0)))
finally return statuses))
(defun elpaca--signal (e &optional info status replace verbosity)
"Signal a change to E. Return nil.
If INFO is non-nil, log and possibly print it in `elpaca-log-buffer'.
If REPLACE is non-nil, E's log is updated instead of appended.
If VERBOSITY is non-nil, log event is given that verbosity number.
If STATUS is non-nil and is not E's current STATUS, signal E's dependents to
check (and possibly change) their statuses."
(let* ((new-status-p (and status (not (eq status (elpaca--status e)))))
(queued (and new-status-p (elpaca--queued))))
(when-let* ((new-status-p)
((push status (elpaca<-statuses e)))
((memq status elpaca--inactive-states)))
(setq elpaca--status-counts (elpaca--count-statuses))
(dolist (blocked (elpaca<-blocking e))
(elpaca--check-status e (elpaca-alist-get blocked queued)))
(and (not elpaca-after-init-time) (eq status 'failed) (elpaca--maybe-log)))
(when info (elpaca--log e info verbosity replace))
(when (<= (or verbosity 0) elpaca-verbosity)
(when elpaca--log-timer (cancel-timer elpaca--log-timer))
(if elpaca--waiting ; Don't set timer. We're already polling.
(elpaca--update-log-buffer)
(setq elpaca--log-timer
(and info (run-at-time elpaca-log-interval nil #'elpaca--update-log-buffer))))))
nil)
;;@TODO: Should this cause a non-local exit? Signal custom error type or just `throw'?
;;@MAYBE: UI command to manually fail an order?
(defun elpaca--fail (e &optional reason)
"Fail E for REASON."
(unless (eq (elpaca--status e) 'failed)
(let ((q (elpaca--q e)))
(setf (elpaca-q<-forms q) (assq-delete-all (elpaca<-id e) (elpaca-q<-forms q))))
(when-let* ((p (elpaca<-process e)))
(when-let* ((entry (car (last (elpaca<-log e) (process-get p :loglen)))))
(setf (nth 2 entry) (propertize (nth 2 entry) 'face 'elpaca-failed)))
(when (process-live-p p) (kill-process p)))
(push 'reclone (elpaca<-statuses e)) ;;@HACK: Prevent reclone when legitimate failure during clone.
(elpaca--signal e reason 'failed)
(elpaca--finalize e)))
(defun elpaca-get (id)
"Return queued E associated with ID."
(elpaca-alist-get id (elpaca--queued)))
(defun elpaca--run-build-commands (commands)
"Run build COMMANDS."
(dolist (command (if (listp (car-safe commands)) commands (list commands)))
(message "Running command: %S" command)
(if (cl-every #'stringp command)
(apply #'elpaca-process-poll command)
(eval command t))))
(defun elpaca--caller-name (n &rest skip)
"Return Nth calling function's name, skipping symbols in SKIP."
(cl-loop with current = (backtrace-frame (1+ n)) ;; Skip this function call.
with previous = nil
while (and (or (not (eq (car current) t))
(memq (cadr current) skip))
(setq previous current current (backtrace-frame (cl-incf n))))
finally return (cadr (or current previous))))
(defun elpaca--continue-build (e &rest args)
"Run E's next build step.
Optional ARGS are passed to `elpaca--signal', which see."
(elpaca--signal e (format "Continued by: %S"
(elpaca--caller-name 2 'elpaca--process-sentinel 'apply))
nil nil 2)
(when args (apply #'elpaca--signal e args))
(unless (memq (elpaca--status e) elpaca--inactive-states)
(if-let* ((elpaca-queue-limit)
((not (elpaca<-builtp e)))
;; Don't double count current E
(active (1- (cl-loop for (_ . e) in (elpaca-q<-elpacas (elpaca--q e))
count (not (memq (elpaca--status e) elpaca--inactive-states)))))
((>= active elpaca-queue-limit)))
(unless (eq (elpaca--status e) 'blocked) ;;@MAYBE: check for queue-throttled, too?
(push 'queue-throttled (elpaca<-statuses e))
(elpaca--signal e "elpaca-queue-limit exceeded" 'blocked nil 1))
(let ((step (or (pop (elpaca<-build-steps e)) #'elpaca--finalize)))
(condition-case-unless-debug err ;;@TODO: signal/catch custom error types
(if-let* ((vars (plist-get (elpaca<-recipe e) :vars))
(fn `(lambda (elpaca elpaca-build-step)
(let* (,@vars) (funcall elpaca-build-step elpaca)))))
(funcall fn e step)
(funcall step e))
((error) (elpaca--fail e (format "%s: %S" step err))))))))
(defun elpaca--log-duration (e)
"Return E's log duration."
(time-subtract (nth 1 (car (elpaca<-log e))) (elpaca<-queue-time e)))
(defun elpaca--queue (order &optional queue)
"ADD ORDER to QUEUE or current queue. Return E."
(if-let* ((id (elpaca--first (or order (signal 'wrong-type-argument
'((or symbolp listp) nil)))))
((not after-init-time))
(e (elpaca-get id)))
(if-let* ((dependents (elpaca<-dependents e)))
(warn "%S previously queued as dependency of: %S" id dependents)
(warn "Duplicate item ID queued: %S" id))
(let* ((e (elpaca<-create order))
(log (pop (elpaca<-log e)))
(status (car log))
(info (nth 2 log))
(q (or queue (car elpaca--queues))))
(when queue (setf (elpaca<-queue-id e) (elpaca-q<-id q)
(elpaca<-init e) (elpaca-q<-type q)))
(setf (alist-get id (elpaca-q<-elpacas q)) e)
(if (eq status 'struct-failed)
(elpaca--fail e info)
(elpaca--signal e info status nil 1))
e)))
;;;###autoload
(defun elpaca-split-queue (&rest args)
"Split remaining elpacas into new queue with ARGS."
(unless (elpaca-q<-elpacas (car elpaca--queues)) (pop elpaca--queues))
(push (apply #'elpaca-q<-create args) elpaca--queues)
nil)
;;;###autoload
(defmacro elpaca-queue (&rest body)
"Execute BODY in new queue."
(declare (debug t))
`(progn (elpaca-split-queue) ,@body (elpaca-split-queue)))
(defvar elpaca--post-queues-hook nil)
(defun elpaca--finalize-queue (q)
"Run Q's post installation functions:
- load cached autoloads
- evaluate deferred package configuration forms
- possibly run `elpaca-after-init-hook'."
(when-let* ((autoloads (elpaca-q<-autoloads q)))
(condition-case-unless-debug err
(eval `(progn ,@(reverse autoloads)) t)
((error) (warn "Autoload Error: %S" err))))
(setf (elpaca-q<-status q) 'complete) ; Avoid loop when forms call elpaca-process-queue.
(when-let* ((forms (nreverse (elpaca-q<-forms q))))
(with-current-buffer (get-buffer-create " *elpaca--finalize-queue*")
(setq-local lexical-binding t)
(cl-loop for (id . body) in forms
do (condition-case-unless-debug err
(eval `(progn ,@body) t)
((error) (warn "Config Error %s: %S" id err)))))
(setf (elpaca-q<-forms q) nil))
(run-hooks 'elpaca-post-queue-hook)
(let ((next (nth (1+ (elpaca-q<-id q)) (reverse elpaca--queues))))
(unless (or elpaca-after-init-time ; Already run.
elpaca--waiting ; Won't know if final queue until after waiting.
(not (eq (elpaca-q<-type q) 'init)) ; Already run.
(and next (eq (elpaca-q<-type next) 'init))) ; More init queues.
(elpaca-split-queue)
(setq elpaca-after-init-time (current-time))
(run-hooks 'elpaca-after-init-hook))
(if (and next (elpaca-q<-elpacas next))
(elpaca--process-queue next)
(run-hooks 'elpaca--post-queues-hook))))
(defun elpaca--throttled-p (e)
"Return t if E is blocked due to `elpaca-queue-limit'."
(let ((statuses (elpaca<-statuses e)))
(and (eq (car statuses) 'blocked) (eq (cadr statuses) 'queue-throttled))))
(defun elpaca--finalize (e)
"Declare E finished or failed."
(unless (eq (elpaca--status e) 'failed)
(elpaca--signal
e (concat "✓ " (format-time-string "%s.%3N" (elpaca--log-duration e)) " secs")
'finished))
(let* ((q (elpaca--q e))
(es (elpaca-q<-elpacas q)))
(when-let* ((elpaca-queue-limit)
(next (cl-loop for (_ . e) in es thereis (and (elpaca--throttled-p e) e))))
(elpaca--signal next nil 'unthrottled)
(elpaca--continue-build next))
(when (= (cl-incf (elpaca-q<-processed q)) (length es)) (elpaca--finalize-queue q))))
(defun elpaca--propertize-subprocess (process)
"Propertize PROCESS according to exit status in associated E."
(when-let* ((e (process-get process :elpaca))
(entry (car (last (elpaca<-log e) (process-get process :loglen)))))
(setf (nth 2 entry) (propertize (nth 2 entry) 'face
(if (zerop (process-exit-status process))
'elpaca-finished 'elpaca-failed)))))
(defun elpaca--command-string (strings &optional prefix)
"Return string of form PREFIX STRINGS."
(concat (or prefix "$") (string-join strings " ")))
(defun elpaca--call-with-log (e verbosity &rest command)
"Call and Log E's COMMAND and output with VERBOSITY."
(elpaca-with-process (apply #'elpaca-process-call command)
(elpaca--signal e (propertize (elpaca--command-string command)
'face (if success 'elpaca-finished 'elpaca-failed))
nil nil verbosity)
(when-let* ((output (string-trim (concat stdout stderr)))
((not (string-empty-p output))))
(elpaca--signal e output nil nil verbosity))
result))
(defun elpaca--initial-fetch (e)
"Perform initial fetch for E, respecting :remotes recipe inheritance."
(let* ((recipe (copy-tree (elpaca<-recipe e)))
(remotes (plist-get recipe :remotes)))
(setf recipe (elpaca-merge-plists recipe '(:remotes nil)))
(cl-loop for remote in remotes
for opts = (elpaca-merge-plists recipe (cdr-safe remote))
for command = `("git" "fetch" ,@(when-let* ((depth (plist-get opts :depth))
((numberp depth)))
(list "--depth" (format "%s" depth)))
,(or (car-safe remote) remote))
for fn = (apply-partially (lambda (command e) (apply #'elpaca--fetch e command))
command)
do (push fn (elpaca<-build-steps e))
finally (elpaca--continue-build e))))
(defun elpaca--configure-remotes (e)
"Add and/or rename E's repo remotes."
(let ((fetchp nil))
(when-let* ((default-directory (elpaca<-repo-dir e))
(recipe (elpaca<-recipe e))
(remotes (plist-get recipe :remotes)))
(elpaca--signal e "Configuring Remotes" 'adding-remotes)
(cl-loop with renamed for spec in remotes do
(if (stringp spec)
(if renamed
(elpaca--signal e (format "ignoring :remotes rename %S" spec))
(unless (equal spec elpaca-default-remote-name)
(elpaca--call-with-log
e 1 "git" "remote" "rename" elpaca-default-remote-name spec))
(setq renamed spec))
(when-let* ((remote (car spec))
(props (cdr spec))
(inherited (elpaca-merge-plists recipe props))
(URI (elpaca--repo-uri inherited)))
(setq fetchp t)
(elpaca-with-process
(elpaca--call-with-log e 1 "git" "remote" "add" remote URI)
(unless success (elpaca--fail e stderr)))))))
(when fetchp (push #'elpaca--initial-fetch (elpaca<-build-steps e))))
(elpaca--continue-build e))
(defun elpaca--remove-build-steps (e spec)
"Remove each step in SPEC from E."
(setf (elpaca<-build-steps e) (cl-set-difference (elpaca<-build-steps e) spec)))
(defun elpaca--files (e &optional files nocons)
"Return alist of E :files to be symlinked: (PATH . TARGET PATH).
FILES and NOCONS are used recursively."
(cl-loop
with repo-dir = (elpaca<-repo-dir e)
with default-directory = repo-dir
with build-dir = (elpaca<-build-dir e)
with recipe = (elpaca<-recipe e)
with files = (or files (if-let* ((member (plist-member recipe :files)))
(cadr member) elpaca-default-files-directive))
with (exclusions targets with-subdirs)
for el in files do
(pcase el
((pred stringp) (push (or (file-expand-wildcards el) el) targets))
(`(:exclude . ,excluded) (push (elpaca--files e excluded 'nocons) exclusions))
(:defaults (push (elpaca--files e elpaca-default-files-directive 'nocons) targets))
;;@FIX: subdir needn't be same name as globbed path...
(`(,_subdir . ,paths)
(cl-loop for path in paths
for expanded = (file-expand-wildcards path)
do (cl-loop for path in expanded
do (push (cons (expand-file-name path repo-dir)
(expand-file-name path build-dir))
with-subdirs)))))
finally return
(let ((targets (cl-nset-difference (flatten-tree targets) (flatten-tree exclusions)
:test #'equal)))
(if nocons
targets
(append with-subdirs
(cl-loop for target in targets when (file-exists-p target)
collect (cons (expand-file-name target)
(expand-file-name (file-name-nondirectory target)
build-dir))))))))
(defun elpaca--find-source-file-maybe ()
"Find corresponding source file for `current-buffer'."
(when-let* ((file (buffer-file-name))
((string-prefix-p elpaca-builds-directory file))
(e (cdr (cl-find-if (lambda (bdir) (string-match-p bdir file))
(elpaca--queued)
:key (lambda (qd) (elpaca<-build-dir (cdr qd))))))
(source (car (rassoc file (elpaca--files e))))
((file-exists-p source)))
(find-alternate-file source)
(message "Found Elpaca build file source")))
;;;###autoload
(define-minor-mode elpaca-no-symlink-mode
"Global minor mode which installs build files by copying."
:global t :group 'elpaca
(if elpaca-no-symlink-mode
(add-hook 'find-file-hook #'elpaca--find-source-file-maybe)
(remove-hook 'find-file-hook #'elpaca--find-source-file-maybe)))
(defun elpaca--link-build-files (e)
"Link E's :files into its builds subdirectory."
(elpaca--signal e "Linking build files" 'linking)
(let* ((build-dir (elpaca<-build-dir e))
(files (or (elpaca<-files e)
(setf (elpaca<-files e) (elpaca--files e)))))
(when (file-exists-p build-dir) (delete-directory build-dir 'recusrive))
(make-directory build-dir 'parents)
(dolist (spec files)
(when-let* ((file (car spec))
((file-exists-p file))
(link (cdr spec)))
(make-directory (file-name-directory link) 'parents)
(if elpaca-no-symlink-mode
(if (file-directory-p file)
(copy-directory file link nil 'parents 'recursive)
(copy-file file link 'overwrite))
(make-symbolic-link file link 'overwrite)))))
(setf (elpaca<-builtp e) t)
(elpaca--continue-build e "Build files linked"))
(defun elpaca--add-info-path (e)
"Add the E's info to `Info-directory-list'."
(let ((build-dir (elpaca<-build-dir e)))
(if (file-exists-p (expand-file-name "dir" build-dir))
(progn
(elpaca--signal e "Adding Info path" 'info)
(with-eval-after-load 'info
(info-initialize)
(cl-pushnew build-dir Info-directory-list :test #'equal)))
(elpaca--signal e "No Info dir file found" 'info))
(elpaca--continue-build e)))
(defvar elpaca--eol (if (memq system-type '(ms-dos windows-nt cygwin)) "\r\n" "\n"))
(defun elpaca--process-filter (process output)
"Filter PROCESS OUTPUT."
(process-put process :raw-output (concat (process-get process :raw-output) output))
(let* ((e (process-get process :elpaca))
(parsed (process-get process :parsed))
(timer (process-get process :timer))
(chunk (concat parsed output))
(lines (split-string chunk elpaca--eol))
(linep (string-empty-p (car (last lines)))))
(when timer (cancel-timer timer))
(unless (eq (elpaca--status e) 'failed)
(process-put
process :timer (run-at-time elpaca-busy-interval nil #'elpaca--update-log-buffer)))
(unless linep
(process-put process :parsed (car (last lines)))
(setq lines (butlast lines)))
(dolist (line lines)
(unless (string-empty-p line)
(elpaca--signal e (concat " " (car (last (split-string line "\r" t)))))))))
(defun elpaca--process-sentinel (&optional info status process event)
"Update E's INFO and STATUS when PROCESS EVENT is finished."
(if-let* ((e (process-get process :elpaca))
((and (equal event "finished\n") (not (eq (elpaca--status e) 'failed)))))
(progn (elpaca--propertize-subprocess process)
(elpaca--continue-build e info status))
(elpaca--fail e (format "Subprocess error (see previous log entries)"))))
(defun elpaca--compile-info-process-sentinel (process event)
"Sentinel for info compilation PROCESS EVENT."
(let* ((e (process-get process :elpaca))
(finished (equal event "finished\n")))
(unless finished
(setf (elpaca<-build-steps e)
(cl-set-difference (elpaca<-build-steps e) '(elpaca--install-info elpaca--add-info-path))))
(elpaca--propertize-subprocess process)
(elpaca--continue-build
e (if finished "Info compiled" (concat "Compilation failure: " (string-trim event))))))
(defun elpaca--make-process (e &rest spec)
"Attach process to E from `make-process' SPEC plist."
(declare (indent 1))
(let* ((command (plist-get spec :command))
(_ (elpaca--signal ;;@HACK Signal before process started. SIGSTP/SIGSTOP failed orders.
e (propertize (elpaca--command-string command) 'face 'elpaca-blocked)))
(process (make-process ;;@FIX: Find way to stop process and continue after bookkeeping
:name (concat "elpaca-" (plist-get spec :name) "-" (elpaca<-package e))
:connection-type (or (plist-get spec :connection-type) 'pipe)
:command command
:filter (or (plist-get spec :filter) #'elpaca--process-filter)
:sentinel (plist-get spec :sentinel))))
(process-put process :elpaca e)
(process-put process :loglen (length (elpaca<-log e)))
(setf (elpaca<-process e) process)))
(defun elpaca--compile-info (e)
"Compile E's .texi files."
(elpaca--signal e "Compiling Info files" 'info)
(if-let* ((default-directory (elpaca<-build-dir e))
(elpaca-makeinfo-executable)
(no-info t)
(files
(cl-loop for (repo-file . build-file) in
(or (elpaca<-files e)
(setf (elpaca<-files e) (elpaca--files e)))
when (and no-info (string-match-p "\\.info$" repo-file))
do (setq no-info nil)
for f = (when (string-match-p "\\.texi\\(nfo\\)?$" repo-file)
(list repo-file "-o"
(concat (file-name-sans-extension build-file) ".info")))
when f collect f)))
(elpaca--make-process e
:name "compile-info"
:command `(,elpaca-makeinfo-executable ,@(apply #'append files))