-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathquelpa.el
2029 lines (1810 loc) · 84.4 KB
/
quelpa.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
;;; quelpa.el --- Emacs Lisp packages built directly from source
;; Copyright 2014-2021, Steckerhalter
;; Copyright 2014-2015, Vasilij Schneidermann <v.schneidermann@gmail.com>
;; Author: steckerhalter
;; URL: https://github.com/quelpa/quelpa
;; Version: 1.0
;; Package-Requires: ((emacs "25.1"))
;; Keywords: tools package management build source elpa
;; This file is not part of GNU Emacs.
;; This file 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Your personal local Emacs Lisp Package Archive (ELPA) with packages
;; built on-the-fly directly from source.
;; See the README for more info:
;; https://github.com/quelpa/quelpa/blob/master/README.org
;;; Requirements:
;; Emacs 25.1
;;; Code:
(require 'cl-lib)
(require 'help-fns)
(require 'url-parse)
(require 'package)
(require 'lisp-mnt)
(eval-when-compile (require 'subr-x))
;; --- customs / variables ---------------------------------------------------
(defgroup quelpa nil
"Build and install packages from source code"
:group 'package)
(defcustom quelpa-upgrade-p nil
"When non-nil, `quelpa' will try to upgrade packages.
The global value can be overridden for each package by supplying
the `:upgrade' argument."
:group 'quelpa
:type 'boolean)
(defcustom quelpa-stable-p nil
"When non-nil, try to build stable packages like MELPA does."
:group 'quelpa
:type 'boolean)
(defcustom quelpa-autoremove-p t
"When non-nil, automatically remove old packages after upgrading.
The global value can be overridden for each package by supplying the
`:autoremove' argument."
:group 'quelpa
:type 'boolean)
(defcustom quelpa-verbose t
"When non-nil, `quelpa' prints log messages."
:group 'quelpa
:type 'boolean)
(defcustom quelpa-before-hook nil
"List of functions to be called before quelpa."
:group 'quelpa
:type 'hook)
(defcustom quelpa-after-hook nil
"List of functions to be called after quelpa."
:group 'quelpa
:type 'hook)
(defcustom quelpa-dir (expand-file-name "quelpa" user-emacs-directory)
"Where quelpa builds and stores packages."
:group 'quelpa
:type 'string)
(defcustom quelpa-melpa-dir (expand-file-name "melpa" quelpa-dir)
"Where the melpa repo cloned to."
:group 'quelpa
:type 'string)
(defcustom quelpa-build-dir (expand-file-name "build" quelpa-dir)
"Where quelpa builds packages."
:group 'quelpa
:type 'string)
(defcustom quelpa-packages-dir (expand-file-name "packages" quelpa-dir)
"Where quelpa puts built packages."
:group 'quelpa
:type 'string)
(defcustom quelpa-melpa-recipe-stores (list (expand-file-name
"recipes"
quelpa-melpa-dir))
"Recipe stores where quelpa finds default recipes for packages.
A store can either be a string pointing to a directory with
recipe files or a list with recipes."
:group 'quelpa
:type '(repeat
(choice directory
(repeat
:tag "List of recipes"
(restricted-sexp :tag "Recipe"
:match-alternatives (listp))))))
(defcustom quelpa-persistent-cache-file (expand-file-name "cache" quelpa-dir)
"Location of the persistent cache file."
:group 'quelpa
:type 'string)
(defcustom quelpa-persistent-cache-p t
"Non-nil when quelpa's cache is saved on and read from disk."
:group 'quelpa
:type 'boolean)
(defcustom quelpa-checkout-melpa-p t
"If non-nil the MELPA git repo is cloned when quelpa is initialized."
:group 'quelpa
:type 'boolean)
(defcustom quelpa-update-melpa-p t
"If non-nil the MELPA git repo is updated when quelpa is initialized.
If nil the update is disabled and the repo is only updated on
`quelpa-upgrade' or `quelpa-self-upgrade'."
:group 'quelpa
:type 'boolean)
(defcustom quelpa-melpa-repo-url "https://github.com/melpa/melpa.git"
"The melpa git repository url."
:group 'quelpa
:type 'string)
(defcustom quelpa-self-upgrade-p t
"If non-nil upgrade quelpa itself when doing a
`quelpa-upgrade-all', otherwise only upgrade the packages in the
quelpa cache."
:group 'quelpa
:type 'boolean)
(defcustom quelpa-git-clone-depth nil
"If non-nil shallow clone quelpa git recipes."
:group 'quelpa
:type '(choice (const :tag "Don't shallow clone" nil)
(integer :tag "Depth")))
(defcustom quelpa-git-clone-partial :blobless
"If non-nil partially clone quelpa git recipes."
:group 'quelpa
:type '(choice (const :tag "Don't partially clone" nil)
(const :tag "Blobless clone" :blobless)
(const :tag "Treeless clone" :treeless)))
(defcustom quelpa-upgrade-interval nil
"Interval in days for `quelpa-upgrade-all-maybe'."
:group 'quelpa
:type '(choice (const :tag "Don't upgrade" nil)
(integer :tag "Days")))
(defvar quelpa-initialized-p nil
"Non-nil when quelpa has been initialized.")
(defvar quelpa-cache nil
"The `quelpa' command stores processed pkgs/recipes in the cache.")
(defvar quelpa-recipe '(quelpa :repo "quelpa/quelpa" :fetcher github)
"The recipe for quelpa.")
;; --- package building ------------------------------------------------------
(defun quelpa-package-type (file)
"Determine the package type of FILE.
Return `tar' for tarball packages, `single' for single file
packages, or nil, if FILE is not a package."
(let ((ext (file-name-extension file)))
(cond
((string= ext "tar") 'tar)
((string= ext "el") 'single)
(:else nil))))
(defun quelpa-get-package-desc (file)
"Extract and return the PACKAGE-DESC struct from FILE.
On error return nil."
(let* ((kind (quelpa-package-type file))
(desc (with-demoted-errors "Error getting PACKAGE-DESC: %s"
(with-temp-buffer
(pcase kind
(`single (insert-file-contents file)
(package-buffer-info))
(`tar (insert-file-contents-literally file)
(tar-mode)
(with-no-warnings
(package-tar-file-info))))))))
(when (package-desc-p desc)
desc)))
(defun quelpa-archive-file-name (archive-entry)
"Return the path of the file in which the package for ARCHIVE-ENTRY is stored."
(let* ((name (car archive-entry))
(pkg-info (cdr archive-entry))
(version (package-version-join (aref pkg-info 0)))
(flavour (aref pkg-info 3)))
(expand-file-name
(format "%s-%s.%s" name version (if (eq flavour 'single) "el" "tar"))
quelpa-packages-dir)))
(defconst quelpa--min-ver '(0 -10) "Smallest possible version.")
(defun quelpa-version-cmp (name version op)
"Return non-nil if version of pkg with NAME and VERSION satisfies OP.
OP is taking two version list and comparing."
(let ((ver (if version (version-to-list version) quelpa--min-ver))
(pkg-ver
(or (when-let ((pkg-desc (cdr (assq name package-alist)))
(pkg-ver (package-desc-version (car pkg-desc))))
pkg-ver)
(alist-get name package--builtin-versions)
quelpa--min-ver)))
(funcall op ver pkg-ver)))
(defmacro quelpa-version>-p (name version)
"Return non-nil if VERSION of pkg with NAME is newer than what is currently installed."
`(quelpa-version-cmp ,name ,version (lambda (o1 o2) (not (version-list-<= o1 o2)))))
(defmacro quelpa-version<-p (name version)
"Return non-nil if VERSION of pkg with NAME is older than what is currently installed."
`(quelpa-version-cmp ,name ,version 'version-list-<))
(defmacro quelpa-version=-p (name version)
"Return non-nil if VERSION of pkg with NAME is same which what is currently installed."
`(quelpa-version-cmp ,name ,version 'version-list-=))
(defun quelpa--package-installed-p (package &optional min-version)
"Return non-nil if PACKAGE, of MIN-VERSION or newer, is installed.
Like `package-installed-p' but properly check for built-in package even when all
packages are not initialized."
(or (package-installed-p package (or min-version quelpa--min-ver))
(package-built-in-p package (or min-version quelpa--min-ver))))
(defvar quelpa--override-version-check nil)
(defun quelpa-checkout (rcp dir)
"Return the version of the new package given a RCP and DIR.
Return nil if the package is already installed and should not be upgraded."
(pcase-let ((`(,name . ,config) rcp)
(quelpa-build-stable quelpa-stable-p)
(quelpa--override-version-check quelpa--override-version-check))
(unless (or (and (quelpa--package-installed-p name) (not quelpa-upgrade-p))
(and (not config)
(quelpa-message t "no recipe found for package `%s'" name)))
(let ((version (condition-case-unless-debug err
(quelpa-build-checkout name config dir)
(error
(error "Failed to checkout `%s': `%s'"
name (error-message-string err))))))
(cond
((and quelpa--override-version-check
(quelpa-version=-p name version))
(setq version (concat version ".1"))
version)
((or quelpa--override-version-check
(quelpa-version>-p name version))
version))))))
(defun quelpa-build (rcp)
"Build a package from the given recipe RCP.
Uses the `package-build' library to get the source code and build
an elpa compatible package in `quelpa-build-dir' storing it in
`quelpa-packages-dir'. Return the path to the created file or nil
if no action is necessary (like when the package is installed
already and should not be upgraded etc)."
(let* ((name (car rcp))
(build-dir (expand-file-name (symbol-name name) quelpa-build-dir))
(ver-type (plist-get (cdr rcp) :version-type))
(files (quelpa-build--config-file-list (cdr rcp)))
(melpa-ver (quelpa-checkout rcp build-dir))
(version
(cond
((or (not (equal ver-type 'elpa)) quelpa-stable-p) melpa-ver)
(melpa-ver
(let ((base-ver
(if-let ((info (quelpa-build--pkg-info (symbol-name name)
files build-dir)))
(aref info 3)
'(0 0 0))))
(while (< (length base-ver) 3) (setq base-ver (append base-ver '(0))))
(package-version-join
(nconc base-ver (version-to-list melpa-ver))))))))
(prog1
(if version
(quelpa-archive-file-name
(quelpa-build-package (symbol-name name)
version
files
build-dir
quelpa-packages-dir))
(quelpa-build--message "Newer package has been installed. Not upgrading.")
nil)
(when (fboundp 'package--quickstart-maybe-refresh)
(package--quickstart-maybe-refresh)))))
;; --- package-build.el integration ------------------------------------------
(defun quelpa-file-version (file-path type version time-stamp)
"Return version of file at FILE-PATH."
(if (eq type 'directory)
time-stamp
(cl-letf* ((package-strip-rcs-id-orig (symbol-function 'package-strip-rcs-id))
((symbol-function 'package-strip-rcs-id)
(lambda (str)
(or (funcall package-strip-rcs-id-orig (lm-header "package-version"))
(funcall package-strip-rcs-id-orig (lm-header "version"))
"0"))))
(concat (if-let ((desc (quelpa-get-package-desc file-path)))
(mapconcat #'number-to-string (package-desc-version desc) ".")
"0")
(pcase version
(`original "")
(_ (concat "pre0." time-stamp)))))))
(defun quelpa-directory-files (path)
"Return list of directory files from PATH recursively."
(let ((result '()))
(mapc
(lambda (file)
(if (file-directory-p file)
(progn
;; When directory is not empty.
(when (cddr (directory-files file))
(dolist (subfile (quelpa-directory-files file))
(add-to-list 'result subfile))))
(add-to-list 'result file)))
(mapcar
(lambda (file) (expand-file-name file path))
;; Without first two entries because they are always "." and "..".
(remove ".." (remove "." (directory-files path)))))
result))
(defun quelpa-expand-source-file-list (file-path config)
"Return list of source files from FILE-PATH corresponding to
CONFIG."
(let ((source-files
(mapcar
(lambda (file) (expand-file-name file file-path))
(quelpa-build--expand-source-file-list file-path config))))
;; Replace any directories in the source file list with the filenames of the
;; files they contain (so that these files can subsequently be hashed).
(dolist (file source-files source-files)
(when (file-directory-p file)
(setq source-files (remove file source-files))
(setq source-files (append source-files
(quelpa-directory-files file)))))))
(defun quelpa-slurp-file (file)
"Return the contents of FILE as a string, or nil if no such
file exists."
(when (file-exists-p file)
(with-temp-buffer
(set-buffer-multibyte nil)
(setq-local buffer-file-coding-system 'binary)
(insert-file-contents-literally file)
(buffer-substring-no-properties (point-min) (point-max)))))
(defun quelpa-check-hash (name config file-path dir &optional fetcher)
"Check if hash of FILE-PATH is different as in STAMP-FILE.
If it is different save the new hash and timestamp to STAMP-FILE
and return TIME-STAMP, otherwise return OLD-TIME-STAMP."
(unless (file-directory-p dir)
(make-directory dir))
(let* (files
hashes
new-stamp-info
new-content-hash
(time-stamp
(replace-regexp-in-string "\\.0+" "." (format-time-string "%Y%m%d.%H%M%S")))
(stamp-file (concat (expand-file-name (symbol-name name) dir) ".stamp"))
(old-stamp-info (quelpa-build--read-from-file stamp-file))
(old-content-hash (cdr old-stamp-info))
(old-time-stamp (car old-stamp-info))
(type (if (file-directory-p file-path) 'directory 'file))
(version (plist-get config :version)))
(if (not (file-exists-p file-path))
(error "`%s' does not exist" file-path)
(if (eq type 'directory)
(setq files (quelpa-expand-source-file-list file-path config)
hashes (mapcar
(lambda (file)
(secure-hash
'sha1 (concat file (quelpa-slurp-file file)))) files)
new-content-hash (secure-hash 'sha1 (mapconcat 'identity hashes "")))
(setq new-content-hash (secure-hash 'sha1 (quelpa-slurp-file file-path)))))
(setq new-stamp-info (cons time-stamp new-content-hash))
(if (and old-content-hash
(string= new-content-hash old-content-hash))
(quelpa-file-version file-path type version old-time-stamp)
(unless (eq fetcher 'url)
(delete-directory dir t)
(make-directory dir)
(if (eq type 'file)
(copy-file file-path dir t t t t)
(copy-directory file-path dir t t t)))
(quelpa-build--dump new-stamp-info stamp-file)
(quelpa-file-version file-path type version time-stamp))))
;; --- package-build fork ------------------------------------------
(defcustom quelpa-build-verbose t
"When non-nil, then print additional progress information."
:type 'boolean)
(defvar quelpa-build-stable nil
"When non-nil, then try to build packages from versions-tagged code.")
(defcustom quelpa-build-timeout-executable
(let ((prog (or (executable-find "timeout")
(executable-find "gtimeout"))))
(when (and prog
(string-match-p "^ *-k"
(shell-command-to-string (concat prog " --help"))))
prog))
"Path to a GNU coreutils \"timeout\" command if available.
This must be a version which supports the \"-k\" option."
:type '(choice (const nil)
(file :must-match t)))
(defcustom quelpa-build-timeout-secs 600
"Wait this many seconds for external processes to complete.
If an external process takes longer than specified here to
complete, then it is terminated. This only has an effect
if `quelpa-build-timeout-executable' is non-nil."
:type 'number)
(defcustom quelpa-build-tar-executable
(or (executable-find "gtar")
(executable-find "tar"))
"Path to a (preferably GNU) tar command.
Certain package names (e.g. \"@\") may not work properly with a BSD tar."
:type '(choice (const nil)
(file :must-match t)))
(defvar quelpa--tar-type nil
"Type of `quelpa-build-tar-executable'. Can be `gnu' or `bsd'.
nil means the type is not decided yet.")
(defcustom quelpa-build-explicit-tar-format-p nil
"If non-nil pass \"--format=gnu\" option to tar command.
Passing the option is necessary on the systems where the default
tar format isn't gnu."
:type 'boolean)
(defcustom quelpa-build-version-regexp "^[rRvV]?\\(.*\\)$"
"Default pattern for matching valid version-strings within repository tags.
The string in the capture group should be parsed as valid by `version-to-list'."
:type 'string)
;;; Internal Variables
(defconst quelpa-build-default-files-spec
'("*.el" "lisp/*.el"
"dir" "*.info" "*.texi" "*.texinfo"
"doc/dir" "doc/*.info" "doc/*.texi" "doc/*.texinfo"
"docs/dir" "docs/*.info" "docs/*.texi" "docs/*.texinfo"
(:exclude
".dir-locals.el" "lisp/.dir-locals.el"
"test.el" "tests.el" "*-test.el" "*-tests.el"
"lisp/test.el" "lisp/tests.el" "lisp/*-test.el" "lisp/*-tests.el"))
"Default value for :files attribute in recipes.")
;;; Utilities
(defun quelpa-build--message (format-string &rest args)
"Behave like `message' if `quelpa-build-verbose' is non-nil.
Otherwise do nothing."
(when quelpa-build-verbose
(apply 'message format-string args)))
(defun quelpa-build--slurp-file (file)
"Return the contents of FILE as a string, or nil if no such file exists."
(when (file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(buffer-substring-no-properties (point-min) (point-max)))))
(defun quelpa-build--string-rtrim (str)
"Remove trailing whitespace from `STR'."
(replace-regexp-in-string "[ \t\n\r]+$" "" str))
(defun quelpa-build--trim (str &optional chr)
"Return a copy of STR without any trailing CHR (or space if unspecified)."
(if (equal (elt str (1- (length str))) (or chr ? ))
(substring str 0 (1- (length str)))
str))
;;; Version Handling
(defun quelpa-build--valid-version (str &optional regexp)
"Apply to STR the REGEXP if defined, \
then pass the string to `version-to-list' and return the result, \
or nil if the version cannot be parsed."
(when (and regexp (string-match regexp str))
(setq str (match-string 1 str)))
(ignore-errors (version-to-list str)))
(defun quelpa-build--parse-time (str)
"Parse STR as a time, and format as a YYYYMMDD.HHMMSS string."
;; We remove zero-padding the HH portion, as it is lost
;; when stored in the archive-contents
(setq str (substring-no-properties str))
(let ((time (date-to-time
(if (string-match "\
^\\([0-9]\\{4\\}\\)/\\([0-9]\\{2\\}\\)/\\([0-9]\\{2\\}\\) \
\\([0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\)$" str)
(concat (match-string 1 str) "-" (match-string 2 str) "-"
(match-string 3 str) " " (match-string 4 str))
str))))
(replace-regexp-in-string
"\\.0+" "."
(format-time-string "%Y%m%d.%H%M%S" time))))
(defun quelpa-build--find-parse-time (regexp &optional bound)
"Find REGEXP in current buffer and format as a time-based version string.
An optional second argument BOUND bounds the search; it is a
buffer position. The match found must not end after that
position."
(and (re-search-backward regexp bound t)
(quelpa-build--parse-time (match-string-no-properties 1))))
(defun quelpa-build--find-parse-time-newest (regexp &optional bound)
"Find REGEXP in current buffer and format as a time-based version string.
An optional second argument BOUND bounds the search; it is a
buffer position. The match found must not end after that
position."
(save-match-data
(let (cur matches)
(while (setq cur (quelpa-build--find-parse-time regexp bound))
(push cur matches))
(car (nreverse (sort matches 'string<))))))
(defun quelpa-build--find-version-newest (regexp &optional bound)
"Find the newest version matching REGEXP before point.
An optional second argument BOUND bounds the search; it is a
buffer position. The match found must not before after that
position."
(let ((tags (split-string
(buffer-substring-no-properties
(or bound (point-min)) (point))
"\n")))
(setq tags (append
(mapcar
;; Because the default `version-separator' is ".",
;; version-strings like "1_4_5" will be parsed
;; wrongly as (1 -4 4 -4 5), so we set
;; `version-separator' to "_" below and run again.
(lambda (tag)
(when (quelpa-build--valid-version tag regexp)
(list (quelpa-build--valid-version tag regexp) tag)))
tags)
(mapcar
;; Check for valid versions again, this time using
;; "_" as a separator instead of "." to catch
;; version-strings like "1_4_5". Since "_" is
;; otherwise treated as a snapshot separator by
;; `version-regexp-alist', we don't have to worry
;; about the incorrect version list above—(1 -4 4 -4
;; 5)—since it will always be treated as older by
;; `version-list-<'.
(lambda (tag)
(let ((version-separator "_"))
(when (quelpa-build--valid-version tag regexp)
(list (quelpa-build--valid-version tag regexp) tag))))
tags)))
(setq tags (cl-remove-if nil tags))
;; Returns a list like ((0 1) ("v0.1")); the first element is used
;; for comparison and for `package-version-join', and the second
;; (the original tag) is used by git/hg/etc.
(car (nreverse (sort tags (lambda (v1 v2) (version-list-< (car v1) (car v2))))))))
;;; Run Process
(defun quelpa-build--run-process (dir command &rest args)
"In DIR run COMMAND with ARGS.
If DIR is unset, try to run from `quelpa-build-dir'
or variable `temporary-file-directory'.
Output is written to the current buffer."
(let ((default-directory (file-name-as-directory (or dir
quelpa-build-dir
temporary-file-directory)))
(argv (nconc (unless (eq system-type 'windows-nt)
(list "env" "LC_ALL=C"))
(if quelpa-build-timeout-executable
(nconc (list quelpa-build-timeout-executable
"-k" "60" (number-to-string
quelpa-build-timeout-secs)
command)
args)
(cons command args)))))
(unless (file-directory-p default-directory)
(error "Can't run process in non-existent directory: %s" default-directory))
(let ((exit-code (apply 'process-file
(car argv) nil (current-buffer) t
(cdr argv))))
(or (zerop exit-code)
(error "Command '%s' exited with non-zero status %d: %s"
argv exit-code (buffer-string))))))
(defun quelpa-build--run-process-match (regexp dir prog &rest args)
"Run PROG with args and return the first match for REGEXP in its output.
PROG is run in DIR, or if that is nil in `default-directory'."
(with-temp-buffer
(apply 'quelpa-build--run-process dir prog args)
(goto-char (point-min))
(re-search-forward regexp)
(match-string-no-properties 1)))
;;; Checkout
;;;; Common
(defun quelpa-build-checkout (package-name config working-dir)
"Check out source for PACKAGE-NAME with CONFIG under WORKING-DIR.
In turn, this function uses the :fetcher option in the CONFIG to
choose a source-specific fetcher function, which it calls with
the same arguments.
Returns the package version as a string."
(let ((fetcher (plist-get config :fetcher)))
(quelpa-build--message "Fetcher: %s" fetcher)
(unless (eq fetcher 'wiki)
(quelpa-build--message "Source: %s\n"
(or (plist-get config :repo)
(plist-get config :url))))
(funcall (intern (format "quelpa-build--checkout-%s" fetcher))
package-name config (file-name-as-directory working-dir))))
(defun quelpa-build--princ-exists (dir)
"Print a message that the contents of DIR will be updated."
(quelpa-build--message "Updating %s" dir))
(defun quelpa-build--princ-checkout (repo dir)
"Print a message that REPO will be checked out into DIR."
(quelpa-build--message "Cloning %s to %s" repo dir))
;;;; Wiki
(defvar quelpa-build--last-wiki-fetch-time 0
"The time at which an emacswiki URL was last requested.
This is used to avoid exceeding the rate limit of 1 request per 2
seconds; the server cuts off after 10 requests in 20 seconds.")
(defvar quelpa-build--wiki-min-request-interval 3
"The shortest permissible interval between successive requests for Emacswiki URLs.")
(defmacro quelpa-build--with-wiki-rate-limit (&rest body)
"Rate-limit BODY code passed to this macro to match EmacsWiki's rate limiting."
(let ((elapsed (cl-gensym)))
`(let ((,elapsed (- (float-time) quelpa-build--last-wiki-fetch-time)))
(when (< ,elapsed quelpa-build--wiki-min-request-interval)
(let ((wait (- quelpa-build--wiki-min-request-interval ,elapsed)))
(quelpa-build--message
"Waiting %.2f secs before hitting Emacswiki again" wait)
(sleep-for wait)))
(unwind-protect
(progn ,@body)
(setq quelpa-build--last-wiki-fetch-time (float-time))))))
(require 'mm-decode)
(defvar url-http-response-status)
(defvar url-http-end-of-headers)
(defun quelpa-build--url-copy-file (url newname &optional ok-if-already-exists)
"Copy URL to NEWNAME. Both args must be strings.
Returns the http request's header as a string.
Like `url-copy-file', but it produces an error if the http response is not 200.
Signals a `file-already-exists' error if file NEWNAME already exists,
unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
A number as third arg means request confirmation if NEWNAME already exists."
(if (and (file-exists-p newname)
(not ok-if-already-exists))
(error "Opening output file: File already exists, %s" newname))
(let ((buffer (url-retrieve-synchronously url))
(headers nil)
(handle nil))
(if (not buffer)
(error "Opening input file: No such file or directory, %s" url))
(with-current-buffer buffer
(unless (= 200 url-http-response-status)
(error "HTTP error %s fetching %s" url-http-response-status url))
(setq handle (mm-dissect-buffer t))
(mail-narrow-to-head)
(setq headers (buffer-string)))
(mm-save-part-to-file handle newname)
(kill-buffer buffer)
(mm-destroy-parts handle)
headers))
(defun quelpa-build--grab-wiki-file (filename)
"Download FILENAME from emacswiki, returning its last-modified time."
(let ((download-url
(format "https://www.emacswiki.org/emacs/download/%s" filename))
headers)
(quelpa-build--with-wiki-rate-limit
(setq headers (quelpa-build--url-copy-file download-url filename t)))
(when (zerop (nth 7 (file-attributes filename)))
(error "Wiki file %s was empty - has it been removed?" filename))
(quelpa-build--parse-time
(with-temp-buffer
(insert headers)
(mail-fetch-field "last-modified")))))
(defun quelpa-build--checkout-wiki (name config dir)
"Checkout package NAME with config CONFIG from the EmacsWiki into DIR."
(unless quelpa-build-stable
(with-current-buffer (get-buffer-create "*quelpa-build-checkout*")
(unless (file-exists-p dir)
(make-directory dir))
(let ((files (or (plist-get config :files)
(list (format "%s.el" name))))
(default-directory dir))
(car (nreverse (sort (mapcar 'quelpa-build--grab-wiki-file files)
'string-lessp)))))))
;;;; Darcs
(defun quelpa-build--darcs-repo (dir)
"Get the current darcs repo for DIR."
(quelpa-build--run-process-match "Default Remote: \\(.*\\)"
dir "darcs" "show" "repo"))
(defun quelpa-build--checkout-darcs (name config dir)
"Check package NAME with config CONFIG out of darcs into DIR."
(let ((repo (plist-get config :url)))
(with-current-buffer (get-buffer-create "*quelpa-build-checkout*")
(cond
((and (file-exists-p (expand-file-name "_darcs" dir))
(string-equal (quelpa-build--darcs-repo dir) repo))
(quelpa-build--princ-exists dir)
(quelpa-build--run-process dir "darcs" "pull" "--all"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(quelpa-build--princ-checkout repo dir)
(quelpa-build--run-process nil "darcs" "get" repo dir)))
(if quelpa-build-stable
(let* ((min-bound (goto-char (point-max)))
(tag-version
(and (quelpa-build--run-process dir "darcs" "show" "tags")
(or (quelpa-build--find-version-newest
(or (plist-get config :version-regexp)
quelpa-build-version-regexp)
min-bound)
(error "No valid stable versions found for %s" name)))))
(quelpa-build--run-process dir "darcs" "obliterate"
"--all" "--from-tag"
(cadr tag-version))
;; Return the parsed version as a string
(package-version-join (car tag-version)))
(apply 'quelpa-build--run-process
dir "darcs" "changes" "--max-count" "1"
(quelpa-build--expand-source-file-list dir config))
(quelpa-build--find-parse-time "\
\\([a-zA-Z]\\{3\\} [a-zA-Z]\\{3\\} \
\\( \\|[0-9]\\)[0-9] [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\} \
[A-Za-z]\\{3\\} [0-9]\\{4\\}\\)")))))
;;;; Fossil
(defun quelpa-build--fossil-repo (dir)
"Get the current fossil repo for DIR."
(quelpa-build--run-process-match "\\(.*\\)" dir "fossil" "remote-url"))
(defun quelpa-build--checkout-fossil (name config dir)
"Check package NAME with config CONFIG out of fossil into DIR."
(unless quelpa-build-stable
(let ((repo (plist-get config :url)))
(with-current-buffer (get-buffer-create "*quelpa-build-checkout*")
(cond
((and (or (file-exists-p (expand-file-name ".fslckout" dir))
(file-exists-p (expand-file-name "_FOSSIL_" dir)))
(string-equal (quelpa-build--fossil-repo dir) repo))
(quelpa-build--princ-exists dir)
(quelpa-build--run-process dir "fossil" "update"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(quelpa-build--princ-checkout repo dir)
(make-directory dir)
(quelpa-build--run-process dir "fossil" "clone" repo "repo.fossil")
(quelpa-build--run-process dir "fossil" "open" "repo.fossil")))
(quelpa-build--run-process dir "fossil" "timeline" "-n" "1" "-t" "ci")
(or (quelpa-build--find-parse-time "\
=== \\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ===\n\
[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\) ")
(error "No valid timestamps found!"))))))
;;;; Svn
(defun quelpa-build--svn-repo (dir)
"Get the current svn repo for DIR."
(quelpa-build--run-process-match "URL: \\(.*\\)" dir "svn" "info"))
(defun quelpa-build--checkout-svn (name config dir)
"Check package NAME with config CONFIG out of svn into DIR."
(unless quelpa-build-stable
(with-current-buffer (get-buffer-create "*quelpa-build-checkout*")
(let ((repo (quelpa-build--trim (plist-get config :url) ?/))
(bound (goto-char (point-max))))
(cond
((and (file-exists-p (expand-file-name ".svn" dir))
(string-equal (quelpa-build--svn-repo dir) repo))
(quelpa-build--princ-exists dir)
(quelpa-build--run-process dir "svn" "up"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(quelpa-build--princ-checkout repo dir)
(quelpa-build--run-process nil "svn" "checkout" repo dir)))
(apply 'quelpa-build--run-process dir "svn" "info"
(quelpa-build--expand-source-file-list dir config))
(or (quelpa-build--find-parse-time-newest "\
Last Changed Date: \\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} \
[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\( [+-][0-9]\\{4\\}\\)?\\)"
bound)
(error "No valid timestamps found!"))))))
;;;; Cvs
(defun quelpa-build--cvs-repo (dir)
"Get the current CVS root and repository for DIR.
Return a cons cell whose `car' is the root and whose `cdr' is the repository."
(apply 'cons
(mapcar (lambda (file)
(quelpa-build--string-rtrim
(quelpa-build--slurp-file (expand-file-name file dir))))
'("CVS/Root" "CVS/Repository"))))
(defun quelpa-build--checkout-cvs (name config dir)
"Check package NAME with config CONFIG out of cvs into DIR."
(unless quelpa-build-stable
(with-current-buffer (get-buffer-create "*quelpa-build-checkout*")
(let ((root (quelpa-build--trim (plist-get config :url) ?/))
(repo (or (plist-get config :module) (symbol-name name)))
(bound (goto-char (point-max)))
latest)
(cond
((and (file-exists-p (expand-file-name "CVS" dir))
(equal (quelpa-build--cvs-repo dir) (cons root repo)))
(quelpa-build--princ-exists dir)
(quelpa-build--run-process dir "cvs" "update" "-dP"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(quelpa-build--princ-checkout (format "%s from %s" repo root) dir)
;; CVS insists on relative paths as target directory for checkout (for
;; whatever reason), and puts "CVS" directories into every subdirectory
;; of the current working directory given in the target path. To get CVS
;; to just write to DIR, we need to execute CVS from the parent
;; directory of DIR, and specific DIR as relative path. Hence all the
;; following mucking around with paths. CVS is really horrid.
(let ((dir (directory-file-name dir)))
(quelpa-build--run-process (file-name-directory dir)
"env" "TZ=UTC" "cvs" "-z3"
"-d" root "checkout"
"-d" (file-name-nondirectory dir)
repo))))
(apply 'quelpa-build--run-process dir "cvs" "log"
(quelpa-build--expand-source-file-list dir config))
;; `cvs log` does not provide a way to view the previous N
;; revisions, so instead of parsing the entire log we examine
;; the Entries file, which looks like this:
;;
;; /.cvsignore/1.2/Thu Sep 1 12:42:02 2005//
;; /CHANGES/1.1/Tue Oct 4 11:47:54 2005//
;; /GNUmakefile/1.8/Tue Oct 4 11:47:54 2005//
;; /Makefile/1.14/Tue Oct 4 11:47:54 2005//
;;
(insert-file-contents (concat dir "/CVS/Entries"))
(setq latest
(car
(sort
(split-string (buffer-substring-no-properties (point) (point-max)) "\n")
(lambda (x y)
(when (string-match "^\\/[^\\/]*\\/[^\\/]*\\/\\([^\\/]*\\)\\/\\/$" x)
(setq x (quelpa-build--parse-time (match-string 1 x))))
(when (string-match "^\\/[^\\/]*\\/[^\\/]*\\/\\([^\\/]*\\)\\/\\/$" y)
(setq y (quelpa-build--parse-time (match-string 1 y))))
(version-list-<= (quelpa-build--valid-version y)
(quelpa-build--valid-version x))))))
(when (string-match "^\\/[^\\/]*\\/[^\\/]*\\/\\([^\\/]*\\)\\/\\/$" latest)
(setq latest (match-string 1 latest)))
(or (quelpa-build--parse-time latest)
(error "No valid timestamps found!"))))))
;;;; Git
(defun quelpa-build--git-repo (dir remote)
"Get the current git repo for DIR from REMOTE."
(quelpa-build--run-process-match
"Fetch URL: \\(.*\\)" dir "git" "remote" "show" "-n" remote))
(defvar quelpa--git-version :uninitialized)
(defun quelpa-build--checkout-git (name config dir)
"Check package NAME with config CONFIG out of git into DIR."
(let* ((version-regexp-alist `(,@version-regexp-alist ("^[-._+ ]?.*$" . 0)))
(git-version (or (when (not (eq quelpa--git-version :uninitialized))
quelpa--git-version)
(setq quelpa--git-version (version-to-list
(quelpa-build--run-process-match
"git version \\(.*\\)"
nil "git" "version")))))
(repo (plist-get config :url))
(remote (or (plist-get config :remote) "origin"))
(commit (or (plist-get config :commit)
(when-let ((branch (plist-get config :branch)))
(concat remote "/" branch))))
(depth (or (plist-get config :depth) quelpa-git-clone-depth))
(partial (and (or (plist-get config :partial) quelpa-git-clone-partial)
(version-list-<= '(2 20) git-version)))
(force (plist-get config :force))
(use-current-ref (plist-get config :use-current-ref)))
(when (string-match (rx bos "file://" (group (1+ anything))) repo)
;; Expand local file:// URLs
(setq repo (expand-file-name (match-string 1 repo))))
(setq quelpa--override-version-check use-current-ref)
(with-current-buffer (get-buffer-create "*quelpa-build-checkout*")
(goto-char (point-max))
(cond
((and (file-exists-p (expand-file-name ".git" dir))
(string-equal (quelpa-build--git-repo dir remote) repo))
(quelpa-build--princ-exists dir)
(quelpa-build--run-process dir "git" "fetch" "--tags" remote))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(quelpa-build--princ-checkout repo dir)
(apply #'quelpa-build--run-process
(append
`(nil "git" "clone" ,repo ,dir)
`("--origin" ,remote)
(pcase partial
(:blobless `("--filter=blob:none"))
(:treeless `("--filter=tree:0")))
(when (and depth (not (plist-get config :commit)))
`("--depth" ,(int-to-string depth)
"--no-single-branch"))
(when-let ((branch (plist-get config :branch)))
`("--branch" ,branch))))))
(if quelpa-build-stable
(let* ((min-bound (goto-char (point-max)))
(tag-version
(and (quelpa-build--run-process dir "git" "tag")
(or (quelpa-build--find-version-newest
(or (plist-get config :version-regexp)
quelpa-build-version-regexp)
min-bound)
(error "No valid stable versions found for %s" name)))))
;; Using reset --hard here to comply with what's used for
;; unstable, but maybe this should be a checkout?
(unless use-current-ref
(quelpa-build--update-git-to-ref
dir (concat "tags/" (cadr tag-version))
force))
;; Return the parsed version as a string
(package-version-join (car tag-version)))
(unless use-current-ref
(quelpa-build--update-git-to-ref
dir (or commit (concat remote "/" (quelpa-build--git-head-branch dir)))
force))
(apply 'quelpa-build--run-process
dir "git" "log" "--first-parent" "-n1" "--pretty=format:'\%ci'"
(quelpa-build--expand-source-file-list dir config))
(quelpa-build--find-parse-time "\
\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} \
[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\( [+-][0-9]\\{4\\}\\)?\\)")))))
(defun quelpa-build--git-head-branch (dir)
"Get the current git repo for DIR."
(or (ignore-errors
(quelpa-build--run-process-match