-
Notifications
You must be signed in to change notification settings - Fork 0
/
pie.el
620 lines (563 loc) · 22.6 KB
/
pie.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
;;; pie.el --- Package installer for Emacs -*- lexical-binding: t -*-
;; Author: zbelial
;; Maintainer: zbelial
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.2"))
;; Homepage: https://bitbucket.org/zbelial/pie
;; Keywords: Package Emacs
;; 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:
;; commentary
;;; Code:
(require 'vc)
(require 'cl-generic)
(eval-when-compile
(require 'cl-macs))
(require 'subr-x)
(eval-and-compile
(or (require 'loaddefs-gen nil t)
(require 'autoload)))
(require 'pie-compat)
(defgroup pie nil
"Package installer for Emacs."
:group 'package
:version "28.2")
(defcustom pie-vc-heuristic-alist
`((,(rx bos "http" (? "s") "://"
(or (: (? "www.") "github.com"
"/" (+ (or alnum "-" "." "_"))
"/" (+ (or alnum "-" "." "_")))
(: "codeberg.org"
"/" (+ (or alnum "-" "." "_"))
"/" (+ (or alnum "-" "." "_")))
(: (? "www.") "gitlab" (+ "." (+ alnum))
"/" (+ (or alnum "-" "." "_"))
"/" (+ (or alnum "-" "." "_")))
(: "git.sr.ht"
"/~" (+ (or alnum "-" "." "_"))
"/" (+ (or alnum "-" "." "_")))
(: "git." (or "savannah" "sv") "." (? "non") "gnu.org/"
(or "r" "git") "/"
(+ (or alnum "-" "." "_")) (? "/")))
(or (? "/") ".git") eos)
. Git)
(,(rx bos "http" (? "s") "://"
(or (: "hg.sr.ht"
"/~" (+ (or alnum "-" "." "_"))
"/" (+ (or alnum "-" "." "_")))
(: "hg." (or "savannah" "sv") "." (? "non") "gnu.org/hgweb/"
(+ (or alnum "-" "." "_")) (? "/")))
eos)
. Hg)
(,(rx bos "http" (? "s") "://"
(or (: "bzr." (or "savannah" "sv") "." (? "non") "gnu.org/r/"
(+ (or alnum "-" "." "_")) (? "/")))
eos)
. Bzr))
"Heuristic mapping URL regular expressions to VC backends."
:type `(alist :key-type (regexp :tag "Regular expression matching URLs")
:value-type (choice :tag "VC Backend"
,@(mapcar (lambda (b) `(const ,b))
vc-handled-backends)))
:version "28.2")
(defcustom pie-vc-default-backend 'Git
"Default VC backend used when cloning a package repository.
If no repository type was specified or could be guessed by
`pie-vc-heuristic-alist', this is the default VC backend
used as fallback. The value must be a member of
`vc-handled-backends' and the named backend must implement
the `clone' function."
:type `(choice ,@(mapcar (lambda (b) (list 'const b))
vc-handled-backends))
:version "28.2")
(defcustom pie-git-depth 1
"Default depth used with git backend.
If no depth is specified explicitly, this value will be used."
:type 'integer
:group 'pie)
(defcustom pie-activite-package t
"Whether activite packages after installing them."
:type 'boolean
:group 'pie)
(defcustom pie-repos-directory (expand-file-name "pie/repos" user-emacs-directory)
"The directory used to store packages' repos."
:type 'directory
:group 'pie)
(defcustom pie-builds-directory (expand-file-name "pie/builds" user-emacs-directory)
"The directory used to store built packages."
:type 'directory
:group 'pie)
(defvar pie--packages (make-hash-table :test #'equal)
"Key is the package name, value is an instance of `pie-package'.")
(defvar pie--activate-cache (make-hash-table :test #'equal)
"Key is the package name.
value is 0 or 1, where 0 means a package has been activated,
but is during rebuilding, 1 means a package has been activeted.")
(cl-defstruct pie-package
(name) ;; string
(package) ;; string
(url) ;; string
(backend) ;; symbol
(branch) ;; string
(rev) ;; string, if provided, rev will override branch
(depth) ;; used with git clone. If specified, the value should be t (all history) or a positive integer. If omitted, use `pie-git-depth'
(build) ;; function
(deps) ;; list of symbol
(repo-dir) ;; which directory this package is/will be cloned to.
(build-dir) ;; which directory this package is/will be installed to.
(lisp-dir) ;; optional, which directory elisp files are in
(build-type) ;; optional
(ignore-files) ;; optional, files ignored when building
)
(defun pie--add-to-packages (pp)
"Add package PP to package hash table."
(let ((package (pie-package-package pp)))
(puthash package pp pie--packages)
nil))
;;;###autoload
(cl-defun pie (package url &key backend rev branch depth build deps lisp-dir condition build-type ignore-files)
"Fetch PACKAGE and (optionally) build it for using with Emacs.
Usage:
(pie package-name
[:keyword [option]]...)
:url String.
The URL of the repository used to fetch the package source.
:backend Symbol, optional.
Which BACKEND to clone.
Can be 'http or a VC backend in `vc-handled-backends'.
If not specified, use `pie-vc-heuristic-alist'
to determine the value,
(or `pie-vc-default-backend' if no backend can be determined).
:branch String, optional.
Which BRANCH or tag to check out.
:rev String, optional.
Which REV to clone. Has higher priority than :branch.
:depth t or a positve nubmer, optional.
Only works with git for now.
If specified, the value should be t (all history),
or a positive integer.
If omitted, use `pie-git-depth'
:build Function, optional.
Specify how to build the package.
If not specified, use `pie-default-build'.
:deps List of string or a function returning a list of string.
Optional.
:lisp-dir String, optional.
Subdirectory containing elisp files inside the repository.
:build-type If it's 'repo, then build the package in the repos directory.
Else, build it in the builds directory.
:ignore-files Files (no directory part) that should be ignored when building with `pie-default-build'
:condition A function without any parameter, optional.
Only when it (if specified) returns t,
this package will be installed."
(let (pp
repo-dir
build-dir)
(when (and condition
(functionp condition))
(when (not (funcall condition))
(message "Package [%s] need not to install." package)
(cl-return-from pie)))
(when (null backend)
(setq backend (or (alist-get url pie-vc-heuristic-alist
nil nil #'string-match-p)
pie-vc-default-backend)))
(when (and (not (member backend vc-handled-backends))
(not (eq backend 'http)))
(user-error "Invalid backend %s" backend))
(setq repo-dir (expand-file-name package pie-repos-directory))
(if (eq build-type 'repo)
(setq build-dir repo-dir)
(setq build-dir (expand-file-name package pie-builds-directory)))
(if lisp-dir
(setq lisp-dir (expand-file-name lisp-dir build-dir))
(setq lisp-dir build-dir))
(when (functionp deps)
(setq deps (funcall deps)))
(setq pp (make-pie-package :name package
:package package
:url url
:rev rev
:backend backend
:branch branch
:depth depth
:build build
:deps deps
:repo-dir repo-dir
:build-dir build-dir
:build-type build-type
:lisp-dir lisp-dir
:ignore-files ignore-files))
(pie--add-to-packages pp)))
(defsubst pie--use-package-concat (&rest elems)
"Delete all empty lists from ELEMS (nil or (list nil)), and append them."
(macroexp-progn (apply #'list (delete nil (delete (list nil) elems)))))
(defsubst pie--use-package-concat2 (elems)
"Delete all empty lists from ELEMS (nil or (list nil)), and append them."
(macroexp-progn (apply #'list (delete nil (delete (list nil) elems)))))
(defun pie--use-package-after (features &rest body)
(cl-callf nreverse features)
(let ((result (pie--use-package-concat2 body)))
(dolist (f features)
(setq result `(with-eval-after-load ',f ,result)))
result))
;; (pie--use-package-after '(a b) '(setq ab 1) '(setq bc 2))
(defun pie--use-package-autoloads (autoloads package)
(cl-callf nreverse autoloads)
(let ((name (symbol-name package))
result)
(dolist (al autoloads)
(push `(autoload ',al ,name nil t) result))
(macroexp-progn result)))
(defun pie--use-package-load-path-and-pie (load-path pie package-name)
(let ((pie-name (cond
((stringp pie) pie)
((and (booleanp pie) pie)
(symbol-name package-name))
((and (symbolp pie) (not (booleanp pie)))
(symbol-name pie)))))
(if load-path
`(add-to-list 'load-path ,load-path)
(if pie-name
`(pie--install-package-by-name ,pie-name)
(when (consp pie)
`(when ,pie
(pie--install-package-by-name ,(symbol-name package-name))))))))
(defun pie--use-package-init (init)
(when init
init))
(defun pie--use-package-config (config package-name)
(when config
`(with-eval-after-load ',package-name
,config)))
;;;###autoload
(cl-defmacro pie-use-package (package-name &key disabled when pie load-path init config autoloads after demand)
"To configure a package by specifying a group of options."
(declare (indent 1) (debug t))
(when (and (booleanp disabled)
(not disabled))
(let ((autoloads-result (pie--use-package-autoloads autoloads package-name))
(load-path-and-pie-result (pie--use-package-load-path-and-pie load-path pie package-name))
(init-result (pie--use-package-init init))
(demand-result (when demand `(require ',package-name)))
(config-result (pie--use-package-config config package-name))
(after-result))
(if when
(progn
(if after
(progn
(setq after-result (pie--use-package-after `,after init-result demand-result config-result))
`(when ,when
,(pie--use-package-concat
load-path-and-pie-result
autoloads-result
after-result)))
`(when ,when
,(pie--use-package-concat
load-path-and-pie-result
autoloads-result
init-result
demand-result
config-result))))
(if after
(progn
(setq after-result (pie--use-package-after `,after init-result demand-result config-result))
(pie--use-package-concat
load-path-and-pie-result
autoloads-result
after-result))
(pie--use-package-concat
load-path-and-pie-result
autoloads-result
init-result
demand-result
config-result))))))
(defun pie--installed-p (pp)
"Check whether package PP has been installed. PP is an instance of `pie-package'."
(and pp
(pie--fetched-p pp)
(pie--built-p pp)))
(defun pie--fetched-p (pp)
"Check whether package PP has been fetched. PP is an instance of `pie-package'."
(when pp
(let ((dir (pie-package-repo-dir pp)))
(and dir
(file-directory-p dir)
(not (directory-empty-p dir))))))
(defun pie--built-p (pp)
"Check whether package PP has been built. PP is an instance of `pie-package'."
(when pp
(let ((build-dir (pie-package-build-dir pp)))
(and build-dir
(file-directory-p build-dir)
(not (directory-empty-p build-dir))))))
(defun pie--installed-by-name-p (name)
"Check whether package with NAME has been fetched."
(let ((pp (gethash name pie--packages)))
(pie--installed-p pp)))
(defun pie--install-package-by-name (name)
"Install package with name NAME."
(let ((pp (gethash name pie--packages)))
(if pp
(pie--install-package pp)
(user-error "No package named %s is defined" name))))
(defun pie--git-depth (&optional depth)
"Determine git depth, use DEPTH if available."
(let ((depth-str "")
(depth depth))
(cond
((eq depth t)
;; full history
)
((and depth
(> depth 0))
(setq depth-str (format " --depth %d " depth)))
(t
(when (and pie-git-depth
(> pie-git-depth 0))
(setq depth-str (format " --depth %d " pie-git-depth)))))
depth-str))
(defun pie--build-package (pp &optional buildp)
"Build package PP. If BUILDP is t, build forcefully."
(let ((dir (pie-package-repo-dir pp))
(lisp-dir (pie-package-lisp-dir pp))
(build-type (pie-package-build-type pp))
(build-dir (pie-package-build-dir pp))
(build (pie-package-build pp))
(name (pie-package-package pp)))
(when (not (member lisp-dir load-path))
(add-to-list 'load-path lisp-dir))
(when (and (pie--fetched-p pp)
(or (not (pie--built-p pp))
buildp))
(message "build package %s" name)
(when (not (eq build-type 'repo))
(delete-directory build-dir t)
(make-directory build-dir t)
(copy-directory dir build-dir t t t))
(let ((default-directory build-dir))
(if build
(funcall build pp)
(pie-default-build pp)))
(message "Finish building %s" name))))
(defun pie--install-package (pp)
"Install(fetch and then build) package PP."
(let ((name (pie-package-package pp))
(deps (pie-package-deps pp))
buildp)
;; install all deps first
(when deps
(cl-dolist (dep deps)
(pie--install-package-by-name dep)))
;; fetch package
(unless (pie--fetched-p pp)
(pie--fetch-package pp)
(setq buildp t))
;; build package
(pie--build-package pp buildp)
(when pie-activite-package
(pie--activate-package pp))
(message "Finish installing %s" name)))
(defun pie--git-clone (url dir branch rev depth)
"Use git to clone a repo from URL, save it to DIR.
If REV is specified, fetch that commit.
If BRANCH is specified, fetch that branch.
DEPTH determine how many commits will be cloned."
(let (cmd
(url (shell-quote-argument url))
(dir (shell-quote-argument dir))
(branch (when branch (shell-quote-argument branch)))
(rev (when rev (shell-quote-argument rev))))
(cond
(rev
(if branch
(setq cmd (concat "git --no-pager clone --branch " branch " --single-branch " url " " dir))
(setq cmd (concat "git --no-pager clone " url " " dir)))
(when (zerop (call-process-shell-command cmd nil nil))
(let ((default-directory dir))
(call-process-shell-command (concat "git checkout " rev)))))
(branch
(setq cmd (concat "git --no-pager clone " (pie--git-depth depth) " --branch " branch " --single-branch " url " " dir))
(call-process-shell-command cmd nil nil))
(t
(setq cmd (concat "git --no-pager clone " (pie--git-depth depth) url " " dir))
(call-process-shell-command cmd nil nil)))))
(defun pie--download (url dir)
"Download a file from URL to DIR."
(let (name
newname)
(setq name (substring url (- (length url) (string-search "/" (reverse url)))))
(setq newname (expand-file-name name dir))
(url-copy-file url newname)))
(defun pie--fetch-package (pp)
"Fetch package PP, where PP is an instance of `pie-package'."
(let ((dir (pie-package-repo-dir pp))
(url (pie-package-url pp))
(rev (pie-package-rev pp))
(backend (pie-package-backend pp))
(branch (pie-package-branch pp))
(depth (pie-package-depth pp))
(name (pie-package-package pp)))
(message "Start to fetch %s" name)
(unless (file-exists-p dir)
(make-directory dir t))
(cond
((eq backend 'Git)
(pie--git-clone url dir branch rev depth))
((eq backend 'http)
(pie--download url dir))
((and (fboundp 'vc-clone)
(member backend vc-handled-backends))
(vc-clone url backend dir branch))
(t
(error "Unsupported backend %s" backend)))
(if (not (pie--fetched-p pp))
(error "Failed to clone %s from %s" name url)
(message "Finish fetching %s" name))))
(defun pie--generate-autoloads (dir output-file)
(cond
((fboundp 'loaddefs-generate)
(loaddefs-generate dir output-file))
((fboundp 'make-directory-autoloads)
(make-directory-autoloads dir output-file))
((fboundp 'update-directory-autoloads)
(let ((generated-autoload-file output-file))
(update-directory-autoloads dir))))
(when-let ((buf (find-buffer-visiting output-file)))
(kill-buffer buf)))
(defun pie-empty-build (pp)
"Do nothing.")
(defun pie-autoloads-build (pp)
"Just generate autoloads files."
(let* ((name (pie-package-package pp))
(lisp-dir (pie-package-lisp-dir pp))
(default-directory lisp-dir)
(feature (concat name "-autoloads"))
(autoloads (expand-file-name (concat feature ".el") lisp-dir)))
(add-to-list 'load-path lisp-dir)
(pie--generate-autoloads lisp-dir autoloads)))
(defun pie-default-build (pp)
"Compile elisp files of PP."
(let* ((name (pie-package-package pp))
(lisp-dir (pie-package-lisp-dir pp))
(default-directory lisp-dir)
(files (directory-files lisp-dir t "\\.el$"))
(ignore-files (append (list ".dir-locals.el") (pie-package-ignore-files pp)))
(feature (concat name "-autoloads"))
(autoloads (expand-file-name (concat feature ".el") lisp-dir)))
(add-to-list 'load-path lisp-dir)
(cl-dolist (file files)
(when (not (member (file-name-nondirectory file) ignore-files))
(byte-compile-file file)))
(pie--generate-autoloads lisp-dir autoloads)))
;;;###autoload
(defun pie-update-package ()
"Update a package in `pie--packages'."
(interactive)
(let (name
pp
pp-tmp
packages
dir
dir-tmp)
(setq packages (hash-table-keys pie--packages))
(setq name (completing-read "Package Name: " packages))
(when name
(setq pp (gethash name pie--packages))
(if pp
(progn
(remhash name pie--activate-cache)
;; first, clone the package to a tmp directory, then
;; delete the original directory and rename the tmp directory
(setq dir (pie-package-repo-dir pp))
(setq dir-tmp (concat dir "-tmp"))
(setq pp-tmp (make-pie-package :name (pie-package-name pp)
:package (pie-package-package pp)
:url (pie-package-url pp)
:rev (pie-package-rev pp)
:backend (pie-package-backend pp)
:depth (pie-package-depth pp)
:branch (pie-package-branch pp)
:build (pie-package-build pp)
:deps (pie-package-deps pp)
:repo-dir dir-tmp))
(delete-directory dir-tmp t)
(pie--fetch-package pp-tmp)
(if (pie--fetched-p pp-tmp)
(progn
(delete-directory dir t)
(rename-file dir-tmp dir)
;; build
(pie--build-package pp t)
(when pie-activite-package
(pie--activate-package pp)))
(error "Failed to clone %s" name)))
(user-error "No package named %s is defined" name)))))
;;;###autoload
(defun pie-install-package ()
"Install a package in `pie--packages'."
(interactive)
(let (name
packages)
(setq packages (hash-table-keys pie--packages))
(setq name (completing-read "Package Name: " packages))
(when name
(pie--install-package-by-name name))))
;;;###autoload
(defun pie-rebuild-package ()
"Rebuild the selected package."
(interactive)
(let (name
pp
packages)
(setq packages (hash-table-keys pie--packages))
(setq name (completing-read "Package Name: " packages))
(when name
(setq pp (gethash name pie--packages))
(if pp
(progn
(pie--build-package pp t)
(puthash name 0 pie--activate-cache)
(when pie-activite-package
(pie--activate-package pp)))
(user-error "No package named %s is defined" name)))))
(defun pie--activate-package (pp)
"Activate package PP."
(let* ((name (pie-package-package pp))
(cache (gethash name pie--activate-cache))
(feature (concat name "-autoloads")))
(when (and (or (null cache)
(= cache 0))
(pie--built-p pp))
(let* ((lisp-dir (pie-package-lisp-dir pp))
(autoloads (expand-file-name (concat feature ".el") lisp-dir)))
(ignore-errors (unload-feature (intern feature) t))
(when (not (member lisp-dir load-path))
(add-to-list 'load-path lisp-dir))
(when (file-exists-p autoloads)
(require (intern feature))))
(puthash name 1 pie--activate-cache))))
;;;###autoload
(defun pie-install-packages ()
"Fetch all packages in `pie--packages' if they have not been installed.
Called after the last `pie' invoking."
(interactive)
(condition-case err
(progn
(cl-dolist (pp (hash-table-values pie--packages))
(pie--install-package pp))
(message "All packages have been installed."))
(error
(message "Error when installing packages: %S" err))))
(provide 'pie)
;;; pie.el ends here