forked from progfolio/elpaca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelpaca.el
1773 lines (1626 loc) · 78.5 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 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.0
;; 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:
;; Would-be Developers:
;; This package has a few idiosyncracies I'm experimenting with.
;; I've defined all structs to end with the character "<", such that slots are
;; accessed via a faux "arrow" like syntax. e.g. elpaca<-repo-dir
;; The main data structures of this program are "elpacas" and "queues".
;; They are abbreviated as "e" and "q" when used as function parameters.
;;; 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)
(unless (executable-find "git") (error "Elpaca unable to find git executable"))
(defgroup elpaca nil
"An elisp package manager."
:group 'elpaca
:prefix "elpaca-")
(defface elpaca-finished
'((t (:weight bold :foreground "#00FF00")))
"Indicates an order is finished.")
(defface elpaca-blocked
'((t (:weight bold :foreground "#FFC1CC")))
"Indicates an order is blocked.")
(defface elpaca-failed
'((t (:weight bold :foreground "#FF1818")))
"Indicates an order has failed.")
(defvar elpaca--info-timer nil "Timer to debounce order info printing.")
(defvar elpaca--pre-built-steps
'(elpaca--queue-dependencies elpaca--add-info-path elpaca--activate-package)
"List of steps for packages which are already built.")
(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-cache-autoloads t
"If non-nil, cache package autoloads and load all at once.
Results in faster start-up time.
However, loading errors will prevent later package autoloads from loading."
: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.")
(defcustom elpaca-makeinfo-executable (executable-find "makeinfo")
"Path of the makeinfo executable."
:type 'string)
(defcustom elpaca-install-info-executable
(executable-find "install-info")
"Path of the install-info executable."
:type 'string)
(defcustom elpaca-info-timer-interval 0.02
"Number of idle seconds to wait before printing order statuses.
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--process-busy-interval 5
"Seconds to wait between subprocess outputs before declaring process blocked."
:type 'number)
(defcustom elpaca-use-build-step-short-names t
"When non-nil, recipe :build functions are auto-prefixed with `elpaca--`."
:type 'boolean)
(defcustom elpaca-build-steps '(elpaca--clone
elpaca--add-remotes
elpaca--fetch
elpaca--checkout-ref
elpaca--run-pre-build-commands
elpaca--clone-dependencies
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 'list)
(defvar elpaca--debug-init debug-on-error "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"
(: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 :remotes "origin" :inherit t :depth 1)
"Default order modifications.")
(defun elpaca-order-defaults (_order)
"Matches any order."
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)
(defun elpaca-recipe-defaults (recipe)
"Default RECIPE modifications. Matches any RECIPE."
(let ((plist))
(unless (plist-get recipe :files)
(push (list :defaults) plist)
(push :files plist))
(when-let ((url (plist-get recipe :url))
((string-match-p "depp.brause.cc" url)))
(push nil plist)
(push :depth plist))
plist))
(defcustom elpaca-recipe-functions '(elpaca-recipe-defaults)
"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)
(defcustom elpaca-menu-functions
'(elpaca-menu-org elpaca-menu-melpa elpaca-menu-gnu-elpa-mirror elpaca-menu-non-gnu-elpa)
"Abnormal hook to lookup packages in menus.
Each function is passed a request, which may be any of the follwoing 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."
:type 'hook)
(defcustom elpaca-hide-status-during-build nil
"When non-nil, don't display `elpaca-status' when a package requires a build."
:type 'boolean)
(defvar elpaca--show-status nil
"When non-nil, show `elpaca-status' during build.")
(defvar elpaca-ignored-dependencies
'(emacs cl-lib cl-generic nadvice org org-mode map seq json)
"Ignore these unless the user explicitly requests they be installed.")
(defvar elpaca-overriding-prompt nil "Overriding prompt for interactive functions.")
(defun elpaca--read-file (path)
"Read file at PATH into memory."
(when (file-exists-p path)
(condition-case err
(with-temp-buffer
(insert-file-contents path)
(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 nil)
(print-level nil)
(print-length nil))
,@body
nil))))
(defvar elpaca--menu-items-cache
(elpaca--read-file (expand-file-name "menu-items.eld" elpaca-cache-directory))
"Cache for menu candidates.")
(defvar elpaca--package-requires-regexp
"\\(?:[[:space:]]*;+[[:space:]]*Package-Requires:[[:space:]]*\\(([^z-a]*?))\\)\\)"
"Regexp matching the Package-Requires metadata in an elisp source file.")
(cl-defstruct (elpaca-q< (:constructor elpaca-q<-create)
(:type list)
(:copier nil)
(:named))
"Queue to hold elpacas."
(type (unless after-init-time 'init))
(id (if (boundp 'elpaca--queues) (length elpaca--queues) 0))
(processed 0)
(status 'incomplete)
(time (current-time))
pre post 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))
(defun elpaca--write-menu-cache ()
"Write menu item cache to disk."
(unless (file-exists-p elpaca-cache-directory)
(make-directory elpaca-cache-directory))
(elpaca--write-file (expand-file-name "menu-items.eld" elpaca-cache-directory)
(prin1 elpaca--menu-items-cache)))
(defun elpaca--menu-items (&optional cache menus)
"Return alist of `elpaca-menu-functions' candidates from MENUS.
If CACHE may be any of the following symbols:
`t` Return cache or recompute if nil. Ignore MENUS.
`nil` Recompute items, ignoring cache altogether.
`recache` Invalidate and recompute cache considering MENUS.
See `elpaca-menu-functions' for valid values of MENUS."
(or (and (eq cache t) elpaca--menu-items-cache)
(let ((items (sort (copy-tree
(cl-loop for fn in (or menus elpaca-menu-functions)
append (and (functionp fn) (funcall fn 'index))))
(lambda (a b) (string-lessp (car a) (car b))))))
(when cache
(setq elpaca--menu-items-cache items)
(elpaca--write-menu-cache))
items)))
(defsubst elpaca-alist-get (key alist)
"Return KEY's value in ALIST.
Simplified version of `alist-get'."
(cdr (assq key alist)))
(defsubst elpaca--first (obj)
"Return `car' of OBJ if it is a list, else OBJ."
(if (listp obj) (car obj) obj))
;;@TODO: clean up interface.
;;;###autoload
(defun elpaca-menu-item (&optional interactive symbol menus filter no-descriptions)
"Return menu item matching SYMBOL in MENUS or `elpaca-menu-functions'.
If SYMBOL is nil, prompt for it.
If INTERACTIVE is equivalent to \\[universal-argument] prompt for MENUS.
FILTER must be a function which accepts a candidate.
If it returns nil, the candidate is not considered for selection.
If NO-DESCRIPTIONS is non-nil, candidate descriptions are not included.
This is faster (what you want with non-interactive calls)."
(interactive "P")
(let* ((omenus menus)
(menus (if (equal interactive '(4))
(mapcar #'intern-soft
(cl-remove-duplicates
(completing-read-multiple "Menus: " elpaca-menu-functions
nil t)
:test #'equal))
(or menus elpaca-menu-functions (user-error "No menus found"))))
(candidates
(let ((c (if filter
(cl-remove-if-not filter (elpaca--menu-items (not omenus) menus))
(elpaca--menu-items (not omenus) menus))))
(if no-descriptions
c
(mapcar (lambda (candidate)
(propertize
(format "%-30s %s" (car candidate)
(or (plist-get (cdr candidate) :description) ""))
'candidate candidate))
c))))
(symbol (or symbol
(intern
(let ((choice
(completing-read (or elpaca-overriding-prompt "Package: ")
candidates nil t)))
(if no-descriptions
choice
(car (split-string choice "\\(?:[[:space:]]+\\)")))))))
(candidate (elpaca-alist-get symbol
(if no-descriptions
candidates
(mapcar (lambda (c) (get-text-property 0 'candidate c))
candidates))))
(recipe (plist-get candidate :recipe)))
(if (called-interactively-p 'interactive)
(progn
(unless recipe (user-error "No menu recipe for %s" symbol))
(kill-new (format "%S" recipe))
(message "%S menu recipe for %s copied to kill ring:\n%S"
(plist-get candidate :source) symbol recipe))
recipe)))
;;;###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))
(elpaca--menu-items 'recache))
(defsubst elpaca--inheritance-disabled-p (obj)
"Return t if OBJ explicitly has :inherit nil key val, nil otherwise."
(when-let (((listp obj))
(member (plist-member obj :inherit)))
(not (cadr member))))
;;;###autoload
(defun elpaca-recipe (order &optional interactive)
"Return recipe computed from ORDER.
ORDER is any of the following values:
- nil. The order is prompted for.
- an item symbol which will be looked up via `elpaca-menu-functions'
- an order list of the form: //='(ITEM . PROPS).
When INTERACTIVE is non-nil, `yank' the recipe to the clipboard."
(interactive (list (if-let ((elpaca-overriding-prompt "Recipe: ")
(recipe (elpaca-menu-item)))
(push (intern (plist-get recipe :package)) recipe)
(user-error "No recipe selected"))
t))
(let* ((props (cdr-safe order))
(item (elpaca--first order))
(nonheritablep (elpaca--inheritance-disabled-p props))
(mods (unless nonheritablep (run-hook-with-args-until-success
'elpaca-order-functions order)))
(menu-item (unless (or interactive ;; we already queried for this.
(elpaca--inheritance-disabled-p
(elpaca-merge-plists
mods (plist-member props :inherit))))
(elpaca-menu-item nil item nil nil 'no-descriptions)))
(recipe (elpaca-merge-plists menu-item mods props)))
(unless (plist-get recipe :package)
(setq recipe (plist-put recipe :package (symbol-name item))))
(when-let ((recipe-mods (run-hook-with-args-until-success
'elpaca-recipe-functions recipe)))
(setq recipe (elpaca-merge-plists recipe recipe-mods)))
(if (not interactive)
recipe
(kill-new (format "%S" recipe))
(message "%S recipe copied to kill-ring:\n%S"
(plist-get recipe :package) recipe))))
(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."
(substring string (1+ (string-match-p "/" string))))
(defsubst elpaca--repo-user (string)
"Return user name portion of STRING."
(substring string 0 (string-match-p "/" string)))
(defun elpaca--full-repo-protocol-p (string)
"Return t if STRING specifies a protocol."
;;@TODO: this needs to be more robust.
(and (string-match-p ":" string) t))
(defvar elpaca--repo-dirs nil "List of registered repository directories.")
(defun elpaca-repo-dir (recipe)
"Return path to repo given RECIPE."
(let* ((local-repo (plist-get recipe :local-repo))
(url (plist-get recipe :url))
(repo (plist-get recipe :repo))
(host (or (plist-get recipe :host) (plist-get recipe :fetcher)))
(user nil)
(info (intern (concat url repo (symbol-name host))))
(mono-repo (alist-get info elpaca--repo-dirs))
(name (cond
(local-repo
(if mono-repo (error "Duplicate :local-repo %S" local-repo) local-repo))
(mono-repo mono-repo)
(url
(unless (featurep 'url-parse) (require 'url-parse))
(file-name-base (directory-file-name (url-filename
(url-generic-parse-url url)))))
(repo
(setq user (elpaca--repo-user repo))
(elpaca--repo-name repo))
(t (error "Unable to determine repo name"))))
(dir (if (and (not mono-repo)
(member name (mapcar #'cdr elpaca--repo-dirs)))
(string-join (list name (format "%s" host) user) ".")
(and name (file-name-sans-extension name)))))
(push (cons info dir) elpaca--repo-dirs)
(expand-file-name (concat "repos/" dir) elpaca-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
(if (elpaca--full-repo-protocol-p repo)
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 stringp) ,host ,recipe))))))
(concat (car p) h (cdr p) (when (eq host 'sourcehut) "~") repo
(unless (eq host 'sourcehut) ".git"))))))
(defun elpaca--build-steps1 (recipe)
"Return a list of build functions from RECIPE."
(let* ((build (plist-member recipe :build))
(steps (cadr build))
(removep (and (eq (car-safe steps) :not) (pop steps))))
(when (and elpaca-use-build-step-short-names (listp steps))
(setq steps (cl-loop for step in steps
collect (intern (concat "elpaca--" (symbol-name step))))))
(cond
((or (not build) (eq steps t)) elpaca-build-steps)
(removep (cl-set-difference elpaca-build-steps steps))
((listp steps) steps))))
(cl-defstruct (elpaca< (:constructor elpaca<--create) (:type list) (:named))
"Order for queued processing."
id package item statuses
repo-dir build-dir mono-repo
files build-steps recipe
dependencies dependents includes
(queue-id (1- (length elpaca--queues)))
(queue-time (current-time))
(init (not after-init-time))
process log)
(defmacro elpaca--required-arg (try info)
"TRY to set arg. If error, fail P with INFO."
(declare (indent 1) (debug t))
`(condition-case err ,try
((error) (setq status 'failed info (format ,info err)) nil)))
(defsubst elpaca--mono-repo (repo-dir)
"Return previously queued P with REPO-DIR."
(cl-some (lambda (queued)
(and-let* ((e (cdr queued))
((equal repo-dir (elpaca<-repo-dir e)))
e)))
(elpaca--queued)))
(defsubst elpaca--build-steps (recipe builtp clonedp mono-repo)
"Return list of build functions for RECIPE.
BUILTP, CLONEDP, and MONO-REPO control which steps are excluded."
(when-let ((steps (elpaca--build-steps1 recipe)))
(if builtp
;;@FIX: should this be hardcoded?
(cons 'elpaca--queue-dependencies (cl-intersection elpaca--pre-built-steps steps))
(unless elpaca-hide-status-during-build (setq elpaca--show-status t))
(when (and mono-repo (memq 'ref-checked-out (elpaca<-statuses mono-repo)))
(setq steps
(cl-set-difference steps '(elpaca--clone elpaca--add-remotes elpaca--checkout-ref))))
(when clonedp (setq steps (delq 'elpaca--clone steps)))
steps)))
(cl-defun elpaca<-create
(item &key recipe repo-dir build-dir files mono-repo)
"Create a new elpaca struct from ITEM.
Keys are as follows:
:RECIPE metadata for building package
:REPO-DIR package's build-dir
:BUILD-DIR package's repo-dir
:CACHED whether or not the package was read from the cache
:FILES list of package's linked files
:MONO-REPO E which is responsible for cloning repo current E is in."
(let* ((status 'queued)
(info "Package queued")
(id (elpaca--first item))
(recipe (or recipe (elpaca--required-arg (elpaca-recipe item) "No recipe: %S")))
(repo-dir
(or repo-dir (and recipe (elpaca--required-arg (elpaca-repo-dir recipe)
"Unable to determine repo dir: %S"))))
(build-dir (or build-dir (and recipe (elpaca-build-dir recipe))))
(clonedp (and repo-dir (file-exists-p repo-dir)))
(builtp (and clonedp (and build-dir (file-exists-p build-dir))))
(mono-repo (or mono-repo
(when-let (((not builtp))
(e (elpaca--mono-repo repo-dir)))
(setq status 'blocked info (format "Waiting on monorepo %S" repo-dir))
e)))
(build-steps (elpaca--build-steps recipe builtp clonedp mono-repo))
(elpaca (elpaca<--create
:id id :package (format "%S" id) :item item :statuses (list status)
:repo-dir repo-dir :build-dir build-dir :mono-repo mono-repo
:files files :build-steps build-steps :recipe recipe
:includes (and mono-repo (list mono-repo))
:log (list (list status nil info)))))
(when mono-repo (cl-pushnew elpaca (elpaca<-includes mono-repo)))
elpaca))
(defun elpaca--fail (e &optional reason)
"Fail E for REASON."
(let ((item (elpaca<-item e))
(queue (car (last elpaca--queues (1+ (elpaca<-queue-id e))))))
(setf (elpaca-q<-forms queue)
(assq-delete-all (elpaca--first item) (elpaca-q<-forms queue))))
(elpaca--update-info e reason 'failed)
(elpaca--finalize e))
(defsubst elpaca--status (e)
"Return `car' of E's statuses."
(car (elpaca<-statuses e)))
(defun elpaca--log-event (e text &optional replace)
"Store TEXT in E's log.
Each event is of the form: (STATUS TIME TEXT)
If REPLACE is non-nil, the most recent log entry is replaced."
(let ((event (list (elpaca--status e) (current-time) text)))
(if replace
(setf (car (elpaca<-log e)) event)
(push event (elpaca<-log e)))))
(defun elpaca--queued (&optional n)
"Return list of elpacas from Nth queue.
If N is nil return a list of all queued elpacas."
(nreverse
(if n
(copy-sequence (elpaca-q<-elpacas (nth n (reverse elpaca--queues))))
(cl-loop for queue in elpaca--queues append (elpaca-q<-elpacas queue)))))
(defsubst elpaca--status-face (status &optional default)
"Return face for STATUS or DEFAULT if not found."
(cond
((eq status 'blocked) 'elpaca-blocked)
((eq status 'finished) 'elpaca-finished)
((eq status 'failed) 'elpaca-failed)
(t (or default 'default))))
(defun elpaca--run-build-commands (commands)
"Run build COMMANDS."
(dolist (command (if (listp (car-safe commands)) commands (list commands)))
(if (cl-every #'stringp command)
(elpaca-with-process (apply #'elpaca-process-call command)
(if success
(message stdout)
(message "Build command error: %S" result)
(error "Build command failed: %S" stderr)))
(eval command t))))
(defsubst elpaca--info (e)
"Return E's most recent log event info."
(nth 2 (car (elpaca<-log e))))
(defsubst elpaca--continue-build (e)
"Run E's next build step."
(funcall (or (pop (elpaca<-build-steps e)) #'elpaca--finalize) e))
(defun elpaca--continue-mono-repo-dependency (e)
"Continue processing E after its mono-repo is in the proper state."
(elpaca--remove-build-steps e '(elpaca--clone elpaca--add-remotes elpaca--checkout-ref))
(elpaca--continue-build e))
(declare-function elpaca-log "elpaca-log")
(declare-function elpaca-status "elpaca-status")
(declare-function elpaca-ui--update-search-filter "elpaca-ui")
(defun elpaca--update-info-buffers ()
"Update views in `elpaca-log-buffer'."
(when-let ((log (bound-and-true-p elpaca-log-buffer))
((get-buffer-window log t))) ;; log buffer visible
(elpaca-ui--update-search-filter log)))
(defun elpaca--update-info (e info &optional status replace)
"Update E's INFO.
Print the elpaca status line in `elpaca-log-buffer'.
If STATUS is non-nil and differs from E's current STATUS,
signal E's depedentents to check (and possibly change) their statuses.
If REPLACE is non-nil, E's log is updated instead of appended."
(when (and status (not (equal status (elpaca--status e))))
(push status (elpaca<-statuses e))
(when (memq status '(finished failed blocked))
(mapc #'elpaca--check-status (elpaca<-dependents e)))
(when (eq status 'ref-checked-out)
(mapc #'elpaca--continue-mono-repo-dependency (elpaca<-includes e)))
(when (eq status 'failed) (elpaca-status)))
(when info (elpaca--log-event e info replace))
(when elpaca--info-timer (cancel-timer elpaca--info-timer))
(setq elpaca--info-timer
(run-at-time elpaca-info-timer-interval nil #'elpaca--update-info-buffers)))
(defun elpaca--log-duration (e)
"Return E's log duration."
(let* ((log (elpaca<-log e))
(end (nth 1 (car log))))
(time-subtract end (elpaca<-queue-time e))))
;;;###autoload
(defun elpaca-split-queue (&rest args)
"Split remaining elpacas into new queue with ARGS."
(let ((current (car elpaca--queues)))
(unless (or (elpaca-q<-elpacas current) (elpaca-q<-forms current))
(pop elpaca--queues)))
(push (apply #'elpaca-q<-create args) elpaca--queues)
nil)
;;;###autoload
(defmacro elpaca-queue (&rest body)
"Execute BODY in new queue with [KEY VAL...] args.
Accepted KEYS are :pre and :post which are hooks run around queue processing."
(declare (debug t))
(let* ((pre (when-let ((found (memq :pre body)))
(butlast found (- (length found) 2))))
(post (when-let ((found (memq :post body)))
(butlast found (- (length found) 2)))))
(while (keywordp (car body)) (pop body) (pop body))
`(progn
(apply #'elpaca-split-queue ',(append pre post))
,@body
(elpaca-split-queue))))
(defun elpaca--finalize-queue (q)
"Run Q's post isntallation functions:
- load cached autoloads
- evaluate deferred package configuration forms
- possibly run `elpaca-after-init-hook'."
(when-let ((autoloads (elpaca-q<-autoloads q)))
(eval `(progn ,@autoloads) t))
(when-let ((forms (elpaca-q<-forms q)))
(eval `(progn ,@(apply #'append (mapcar #'cdr (reverse forms)))) t))
(setf (elpaca-q<-status q) 'complete)
(let ((next (nth (1+ (elpaca-q<-id q)) (reverse elpaca--queues))))
(if (and (eq (elpaca-q<-type q) 'init)
(or (null next)
(not (eq (elpaca-q<-type next) 'init))))
(progn
(run-hooks 'elpaca-after-init-hook)
(elpaca-split-queue))
(when-let ((post (elpaca-q<-post q))) (funcall post))
(run-hooks 'elpaca-post-queue-hook)
(when next (elpaca--process-queue next)))))
(defun elpaca--finalize (e)
"Declare E finished or failed."
(let ((status (elpaca--status e)))
(if (eq status 'finished)
(cl-loop for dependent in (elpaca<-dependents e)
unless (eq (elpaca--status dependent) 'finished)
do (elpaca--check-status dependent))
(unless (eq (elpaca--status e) 'failed)
(elpaca--update-info e
(concat "✓ " (format-time-string "%s.%3N" (elpaca--log-duration e)) " secs")
'finished))
(when-let ((q (car (last elpaca--queues (1+ (elpaca<-queue-id e)))))
((= (cl-incf (elpaca-q<-processed q))
(length (elpaca-q<-elpacas q)))))
(elpaca--finalize-queue q)))))
(defun elpaca--queue (item)
"Queue (ITEM . e) in `elpaca--queued'. Return e."
(if (and (not after-init-time) (elpaca-alist-get item (elpaca--queued)))
(warn "Duplicate item declaration: %S" item)
(let* ((e (elpaca<-create item))
(log (pop (elpaca<-log e)))
(status (car log))
(info (nth 2 log)))
(push (cons (elpaca<-id e) e) (elpaca-q<-elpacas (car elpaca--queues)))
(if (eq status 'failed)
(elpaca--fail e info)
(elpaca--update-info e info status))
e)))
(defun elpaca--add-remotes (e &optional recurse)
"Add E's repo remotes.
RECURSE is used to keep track of recursive calls."
(let ((default-directory (elpaca<-repo-dir e))
(recipe (elpaca<-recipe e)))
(cl-destructuring-bind
( &key remotes
((:host recipe-host))
((:protocol recipe-protocol))
((:repo recipe-repo))
&allow-other-keys)
recipe
(unless recurse (elpaca--update-info e "Adding Remotes"))
(pcase remotes
("origin" nil)
((and (pred stringp) remote)
(elpaca-process-call "git" "remote" "rename" "origin" remote))
((pred listp)
(dolist (spec remotes)
(if (stringp spec)
(elpaca--add-remotes (let ((copy (copy-elpaca< e)))
(setf (elpaca<-recipe copy)
(plist-put (copy-tree recipe) :remotes spec))
copy)
'recurse)
(pcase-let ((`(,remote . ,props) spec))
(when props
(cl-destructuring-bind
(&key (host recipe-host)
(protocol recipe-protocol)
(repo recipe-repo)
&allow-other-keys
&aux
(recipe (list :host host :protocol protocol :repo repo)))
props
(elpaca-process-call
"git" "remote" "add" remote (elpaca--repo-uri recipe))
(unless (equal remote "origin")
(elpaca-process-call "git" "remote" "rename" "origin" remote))))))))
(_ (elpaca--fail e (format "(wrong-type-argument ((stringp listp)) %S" remotes))))))
(unless recurse (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."
(let* ((repo-dir (elpaca<-repo-dir e))
(default-directory repo-dir)
(build-dir (elpaca<-build-dir e))
(recipe (elpaca<-recipe e))
(files (or files (plist-get recipe :files)))
(exclusions nil)
(targets nil)
(with-subdirs nil))
(dolist (el files)
(pcase el
((pred stringp) (push (or (file-expand-wildcards el) el) targets))
(`(:exclude . ,excluded)
(push (elpaca--files e excluded 'nocons) exclusions)
nil)
(: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))))))
(if nocons
targets
(append
with-subdirs
(cl-loop for target in (flatten-tree targets)
unless (or (not (file-exists-p (expand-file-name target)))
(member target (flatten-tree exclusions)))
collect
(cons (expand-file-name target)
(expand-file-name (file-name-nondirectory target) build-dir)))))))
(defun elpaca--link-build-files (e)
"Link E's :files into its builds subdirectory."
(elpaca--update-info e "Linking build files")
(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)
(make-symbolic-link file link 'overwrite))))
(elpaca--update-info e "Build files linked" 'build-linked)
(elpaca--continue-build e))
(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--update-info e "Adding Info path" 'info)
(with-eval-after-load 'info
(info-initialize)
(cl-pushnew build-dir Info-directory-list)))
(elpaca--update-info e "No Info dir file found" 'info))
(elpaca--continue-build e)))
(defun elpaca--process-busy (process)
"Update E's status when PROCESS has stopped producing output."
(when-let (((eq (process-status process) 'run))
(e (process-get process :elpaca)))
(elpaca--update-info e (process-get process :result) 'blocked)))
(defun elpaca--process-filter (process output &optional pattern status)
"Filter PROCESS OUTPUT.
PATTERN is a string which is checked against the entire process output.
If it matches, the E associated with process has its STATUS updated."
(process-put process :raw-output (concat (process-get process :raw-output) output))
(let* ((e (process-get process :elpaca))
(result (process-get process :result))
(timer (process-get process :timer))
(chunk (concat result output))
(lines (split-string chunk "\n"))
(returnp (string-match-p "" chunk))
(linep (string-empty-p (car (last lines)))))
(when timer (cancel-timer timer))
(process-put process :timer (run-at-time elpaca--process-busy-interval nil
(lambda () (elpaca--process-busy process))))
(unless linep
(process-put process :result (car (last lines)))
(setq lines (butlast lines)))
(dolist (line lines)
(unless (string-empty-p line)
(elpaca--update-info
e (car (last (split-string line "" t)))
(and pattern (string-match-p pattern line) status) returnp)))
(when (and pattern (string-match-p pattern output))
(process-put process :result nil)
(if (eq status 'failed)
(elpaca--fail e output)
(elpaca--update-info e output status)))))
(defun elpaca--process-sentinel (info &optional status process event)
"Update E's INFO and STATUS when PROCESS EVENT is finished."
(when-let (((equal event "finished\n"))
(e (process-get process :elpaca))
((not (eq (elpaca--status e) 'failed))))
(elpaca--update-info e info status)
(elpaca--continue-build e)))
(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")))
(elpaca--update-info e (if finished
"Info compiled"
(format "Failed to compile Info: %S" (string-trim event))))
(unless finished
(setf (elpaca<-build-steps e)
(cl-set-difference (elpaca<-build-steps e)
'(elpaca--install-info elpaca--add-info-path))))
(elpaca--continue-build e)))
(defun elpaca--compile-info (e)
"Compile E's .texi files."
(elpaca--update-info e "Compiling Info files" 'info)
(if-let ((elpaca-makeinfo-executable)
(files
(cl-loop for (repo-file . build-file) in
(or (elpaca<-files e)
(setf (elpaca<-files e) (elpaca--files e)))
for f = (when-let (((string-match-p "\\.texi\\(nfo\\)?$" repo-file))
(info (concat (file-name-sans-extension build-file) ".info"))
((not (file-exists-p info))))
(list repo-file "-o" info))
when f collect f))
(command `(,elpaca-makeinfo-executable ,@(apply #'append files)))
(process (make-process
:name (format "elpaca-compile-info-%s" (elpaca<-package e))
:command command
:filter #'elpaca--process-filter
:sentinel #'elpaca--compile-info-process-sentinel)))
(process-put process :elpaca e)
(elpaca--update-info
e (if elpaca-makeinfo-executable "No .info files found" "makeinfo executable not found"))
(elpaca--remove-build-steps e '(elpaca--install-info elpaca--add-info-path))
(elpaca--continue-build e)))
;;@TODO: DRY. Identical to compile-info sentinel
(defun elpaca--install-info-process-sentinel (process event)
"Sentinel for info installation PROCESS EVENT."
(let ((e (process-get process :elpaca)))
(elpaca--update-info e (if (equal event "finished\n")
"Info installed"
(format "Failed to install Info: %S" (string-trim event))))
(elpaca--continue-build e)))
(defun elpaca--install-info-async (file dir e)
"Asynchronously Install E's .info FILE in Info DIR."
(let ((process (make-process
:name (format "elpaca-install-info-%s" (elpaca<-package e))
:command (list elpaca-install-info-executable file dir)
:filter #'elpaca--process-filter
:sentinel #'elpaca--install-info-process-sentinel)))
(process-put process :elpaca e)))
(defun elpaca--install-info (e)
"Install E's .info files."
(elpaca--update-info e "Installing Info files")
(when-let ((dir (expand-file-name "dir" (elpaca<-build-dir e)))
((not (file-exists-p dir)))
(specs (or (elpaca<-files e) (setf (elpaca<-files e) (elpaca--files e)))))
(cl-loop for (target . link) in specs
for file = (cond
((string-match-p "\\.info$" link) link)
((string-match-p "\\.texi\\(nfo\\)?$" target)
(concat (file-name-sans-extension link) ".info")))
when (and file (file-exists-p file))
do (setf (elpaca<-build-steps e)
(push (apply-partially #'elpaca--install-info-async file dir)
(elpaca<-build-steps e)))
finally (elpaca--continue-build e))))
(defun elpaca--dispatch-build-commands-process-sentinel (process event)
"PROCESS EVENT."
(let ((e (process-get process :elpaca))
(type (process-get process :build-type)))
(cond
((equal event "finished\n")
(elpaca--update-info
e (format "%s steps finished" type) (intern (substring (symbol-name type) 1)))
(elpaca--continue-build e))
((string-match-p "abnormally" event)
;; We want the event prior to the last "exited abnormally" event.
(elpaca--fail e (nth 2 (car (last (elpaca<-log e) 2))))))))
(defun elpaca--dispatch-build-commands (e type)
"Run E's TYPE commands for.
TYPE is either the keyword :pre-build, or :post-build.
Each command is either an elisp form to be evaluated or a list of
strings to be executed in a shell context of the form:
(\"executable\" \"arg\"...)
Commands are exectued in the E's repository directory.
The keyword's value is expected to be one of the following:
- A single command
- A list of commands
- nil, in which case no commands are executed.
Note if :build is nil, :pre/post-build commands are not executed."
(if-let ((recipe (elpaca<-recipe e))
(commands (plist-get recipe type)))
(progn
(elpaca--update-info e (format "Running %S commands" type))
(let* ((default-directory (elpaca<-repo-dir e))
(emacs (elpaca--emacs-path))
(program `(progn
(require 'elpaca)