-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
eask-core.el
3640 lines (3423 loc) · 149 KB
/
eask-core.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
;;; eask-core.el --- Core Eask APIs -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2023 Shen, Jen-Chieh
;; 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:
;;
;; Core Eask APIs
;;
;; This file is generated from a template file, see templates/source.el file.
;;
;;; Code:
;; ~/lisp/_prepare.el
(defvar ansi-inhibit-ansi)
(defvar github-elpa-archive-dir)
(defvar github-elpa-recipes-dir)
(defvar package-build-default-files-spec)
(declare-function package-build-expand-files-spec "ext:package-build.el")
(declare-function github-elpa-build "ext:github-elpa.el")
(declare-function ansi-red "ext:ansi.el")
(declare-function ansi-blue "ext:ansi.el")
(declare-function ansi-green "ext:ansi.el")
(declare-function ansi-yellow "ext:ansi.el")
(declare-function ansi-white "ext:ansi.el")
(defvar eask-dot-emacs-file nil
"Variable hold .emacs file location.")
(defcustom eask-import-timeout 10
"Number of seconds before timing out elisp importation attempts.
If nil, never time out."
:type '(choice (number :tag "Number of seconds")
(const :tag "Never time out" nil))
:group 'eask)
(defvar eask-loading-file-p nil
"This became t; if we are loading script from another file and not expecting
the `eask-start' execution.")
(defmacro eask-defvc< (version &rest body)
"Define scope if Emacs version is below VERSION.
Argument BODY are forms for execution."
(declare (indent 1) (debug t))
`(when (< emacs-major-version ,version) ,@body))
(defmacro eask--silent (&rest body)
"Execute BODY without message."
(declare (indent 0) (debug t))
`(let ((inhibit-message t) message-log-max) ,@body))
(defmacro eask--unsilent (&rest body)
"Execute BODY with message."
(declare (indent 0) (debug t))
`(let (inhibit-message) ,@body))
(defmacro eask-with-buffer (&rest body)
"Create a temporary buffer (for this program), and evaluate BODY there."
(declare (indent 0) (debug t))
`(with-current-buffer (get-buffer-create ,eask-buffer-name) ,@body))
(defmacro eask-with-temp-buffer (&rest body)
"Create a temporary buffer (for this program), and evaluate BODY there."
(declare (indent 0) (debug t))
`(eask-with-buffer (erase-buffer) ,@body))
(defcustom eask-elapsed-time nil
"Log with elapsed time."
:type 'boolean
:group 'eask)
(defcustom eask-minimum-reported-time 0.1
"Minimal load time that will be reported."
:type 'number
:group 'eask)
(defmacro eask-with-progress (msg-start body msg-end)
"Progress BODY wrapper with prefix (MSG-START) and suffix (MSG-END) messages."
(declare (indent 0) (debug t))
`(if eask-elapsed-time
(let ((now (current-time)))
(ignore-errors (eask-write ,msg-start)) ,body
(let ((elapsed (float-time (time-subtract (current-time) now))))
(if (< elapsed eask-minimum-reported-time)
(ignore-errors (eask-msg ,msg-end))
(ignore-errors (eask-write ,msg-end))
(eask-msg (ansi-white (format " (%.3fs)" elapsed))))))
(ignore-errors (eask-write ,msg-start)) ,body
(ignore-errors (eask-msg ,msg-end))))
(defvar eask--action-prefix ""
"The prefix to display before each package action.")
(defvar eask--action-index 0
"The index ID for each task.")
(defvar eask--package-initialized nil
"Flag for package initialization in global scope.")
(defmacro eask--pkg-process (pkg &rest body)
"Execute BODY with PKG's related variables."
(declare (indent 1) (debug t))
`(let* ((pkg-info (eask--pkg-transaction-vars ,pkg))
(pkg (nth 0 pkg-info))
(name (nth 1 pkg-info))
(version (nth 2 pkg-info)))
,@body))
(defmacro eask-with-archives (archives &rest body)
"Scope that temporary make ARCHIVES available.
Argument BODY are forms for execution."
(declare (indent 1) (debug t))
`(let ((package-archives package-archives)
(archives (eask-listify ,archives))
(added))
(dolist (archive archives)
(unless (assoc archive package-archives)
(setq added t)
(eask-with-progress
(format "Adding required archives (%s)... " (ansi-yellow archive))
(eask-f-source archive)
"done ✓")))
(when added
(eask-with-progress
"Refresh archives information... "
(eask--silent (eask-pkg-init t))
"done ✓"))
,@body))
(defvar eask--first-init-p nil
"Is non-nil if .eask does not exists; meaning users haven't called eask in the
current workspace.")
(defvar eask--initialized-p nil
"Set to t once the environment setup has done; this is used when calling
other scripts internally. See function `eask-call'.")
(defmacro eask--batch-mode (&rest body)
"Execute forms BODY in batch-mode."
(declare (indent 0) (debug t))
`(let ((argv (eask-args))
load-file-name buffer-file-name)
,@body))
(defmacro eask--setup-env (&rest body)
"Execute BODY with workspace setup."
(declare (indent 0) (debug t))
`(eask--batch-mode
(let ((alist))
(dolist (cmd eask--command-list)
(push (cons cmd (lambda (&rest _))) alist))
(setq command-switch-alist (append command-switch-alist alist))
,@body)))
(defmacro eask--alias-env (&rest body)
"Replace all Eask file functions temporary; this is only used when loading
Eask file in the workspace.
Argument BODY are forms for execution."
(declare (indent 0) (debug t))
`(let (result)
;; XXX: Magic here is we replace all keyword functions with `eask-xxx'...
(eask--loop-file-keywords
(lambda (keyword api old)
(defalias old (symbol-function keyword))
(defalias keyword (symbol-function api))))
(setq result (progn ,@body))
;; XXX: after loading Eask file, we revert those functions back to normal!
(eask--loop-file-keywords
(lambda (keyword _api old)
(defalias keyword (symbol-function old))))
result))
(defvar eask-file nil "The Eask file's filename.")
(defvar eask-file-root nil "The Eask file's directory.")
(defmacro eask--with-hooks (&rest body)
"Execute BODY with before/after hooks."
(declare (indent 0) (debug t))
`(let* ((command (eask-command))
(before (concat "eask-before-" command "-hook"))
(after (concat "eask-after-" command "-hook")))
(eask--unsilent
(run-hooks 'eask-before-command-hook)
(run-hooks (intern before))
,@body
(run-hooks (intern after))
(run-hooks 'eask-after-command-hook))))
(defmacro eask--setup-home (dir &rest body)
"Set up config directory in DIR, then execute BODY."
(declare (indent 1) (debug t))
`(let* ((user-emacs-directory (expand-file-name (concat ".eask/" emacs-version "/") ,dir))
(package-user-dir (expand-file-name "elpa" user-emacs-directory))
(early-init-file (locate-user-emacs-file "early-init.el"))
(eask-dot-emacs-file (locate-user-emacs-file ".emacs"))
(user-init-file (locate-user-emacs-file "init.el"))
(custom-file (locate-user-emacs-file "custom.el")))
,@body))
(defmacro eask-start (&rest body)
"Execute BODY with workspace setup."
(declare (indent 0) (debug t))
`(unless eask-loading-file-p
(if eask--initialized-p (progn ,@body)
(setq eask--initialized-p t)
(eask--setup-env
(eask--handle-global-options)
(cond
((eask-config-p)
(let ((early-init-file (locate-user-emacs-file "early-init.el"))
(eask-dot-emacs-file (locate-user-emacs-file "../.emacs"))
(user-init-file (locate-user-emacs-file "init.el")))
;; We accept Eask-file in `config' scope, but it shouldn't be used
;; for the sandbox.
(eask-with-verbosity 'debug
(if (eask-file-try-load user-emacs-directory)
(eask-msg "✓ Loading config Eask file in %s... done!" eask-file)
(eask-msg "✗ Loading config Eask file... missing!"))
(eask-msg ""))
(package-activate-all)
(eask--load-config)
(eask--with-hooks ,@body)))
((eask-global-p)
(eask--setup-home (concat eask-homedir "../") ; `/home/user/', escape `.eask'
(let ((eask--first-init-p (not (file-directory-p user-emacs-directory))))
;; We accept Eask-file in `global' scope, but it shouldn't be used
;; for the sandbox.
(eask-with-verbosity 'debug
(eask-ignore-errors ; Eask-file is optional!
(if (eask-file-try-load eask-homedir)
(eask-msg "✓ Loading global Eask file in %s... done!" eask-file)
(eask-msg "✗ Loading global Eask file... missing!")))
(eask-msg ""))
(package-activate-all)
(ignore-errors (make-directory package-user-dir t))
(eask-with-verbosity 'debug (eask--load-config))
(eask--with-hooks ,@body))))
((eask-special-p) ; Commands without Eask-file needed!
;; First, try to find a valid Eask-file!
(eask-file-try-load default-directory)
;; Then setup the user directory according to the Eask-file!
(eask--setup-home (or eask-file-root
(concat eask-homedir "../"))
(let ((eask--first-init-p (not (file-directory-p user-emacs-directory)))
(scope (if eask-file-root "" "global ")))
(eask-with-verbosity 'debug
(eask-ignore-errors ; Again, without Eask-file needed!
(if (or eask-file-root
(eask-file-try-load eask-homedir))
(eask-msg "✓ Loading %sEask file in %s... done!" scope eask-file)
(eask-msg "✗ Loading %sEask file... missing!" scope)))
(eask-msg ""))
(package-activate-all)
(ignore-errors (make-directory package-user-dir t))
(eask-with-verbosity 'debug (eask--load-config))
(eask--with-hooks ,@body))))
(t
(eask--setup-home nil ; `nil' is the `default-directory'
(let ((eask--first-init-p (not (file-directory-p user-emacs-directory))))
(eask-with-verbosity 'debug
(if (eask-file-try-load default-directory)
(eask-msg "✓ Loading Eask file in %s... done!" eask-file)
(eask-msg "✗ Loading Eask file... missing!"))
(eask-msg ""))
(if (not eask-file)
(eask-help "core/init")
(package-activate-all)
(ignore-errors (make-directory package-user-dir t))
(eask--silent (eask-setup-paths))
(eask-with-verbosity 'debug (eask--load-config))
(eask--with-hooks ,@body))))))))))
(defvar eask-package nil)
(defvar eask-package-desc nil) (defvar eask-package-descriptor nil)
(defvar eask-website-url nil)
(defvar eask-keywords nil)
(defvar eask-authors nil)
(defvar eask-licenses nil)
(defvar eask-package-file nil)
(defvar eask-files nil)
(defvar eask-scripts nil)
(defvar eask-depends-on-emacs nil)
(defvar eask-depends-on nil)
(defvar eask-depends-on-dev nil)
(defmacro eask--save-eask-file-state (&rest body)
"Execute BODY without touching the Eask-file global variables."
(declare (indent 0) (debug t))
`(let (package-archives
package-archive-priorities
eask-file
eask-file-root
eask-package
eask-package-desc
eask-website-url
eask-keywords
eask-authors
eask-licenses
eask-package-file
eask-package-descriptor
eask-files
eask-scripts
eask-depends-on-emacs
eask-depends-on
eask-depends-on-dev)
,@body))
(defmacro eask--save-load-eask-file (file success &rest error)
"Load an Eask FILE and execute forms SUCCESS or ERROR."
(declare (indent 2) (debug t))
`(eask--save-eask-file-state
(eask--setup-env
(eask--alias-env
(if (let ((default-directory (file-name-directory ,file)))
(ignore-errors (eask-file-load ,file)))
(progn ,success)
,@error)))))
(defvar eask-depends-on-recipe-p nil
"Set to t if package depends on recipe.")
(defcustom eask-verbosity 3
"Log level for all messages; 4 means trace most anything, 0 means nothing.
Standard is, 0 (error), 1 (warning), 2 (info), 3 (log), 4 (debug), 5 (all)."
:type 'integer
:group 'eask)
(defcustom eask-timestamps nil
"Log messages with timestamps."
:type 'boolean
:group 'eask)
(defcustom eask-log-level nil
"Log messages with level."
:type 'boolean
:group 'eask)
(defcustom eask-level-color
'((all . ansi-magenta)
(debug . ansi-blue)
(log . ansi-white)
(info . ansi-cyan)
(warn . ansi-yellow)
(error . ansi-red))
"Alist of each log level's color, in (SYMBOL . ANSI-FUNCTION)."
:type 'alist
:group 'eask)
(defmacro eask-with-verbosity (symbol &rest body)
"Define verbosity scope.
Execute forms BODY limit by the verbosity level (SYMBOL)."
(declare (indent 1) (debug t))
`(if (eask-reach-verbosity-p ,symbol) (progn ,@body)
(eask--silent ,@body)))
(defmacro eask-with-verbosity-override (symbol &rest body)
"Define override verbosity scope.
Execute forms BODY limit by the verbosity level (SYMBOL)."
(declare (indent 1) (debug t))
`(if (eask-reach-verbosity-p ,symbol) (eask--unsilent ,@body)
(eask--silent ,@body)))
(defvar eask--ignore-error-p nil
"Don't trigger error when this is non-nil.")
(defvar eask-inhibit-error-message nil
"Non-nil to stop error/warning message.")
(defmacro eask-ignore-errors (&rest body)
"Execute BODY without killing the process."
(declare (indent 0) (debug t))
`(let ((eask--ignore-error-p t)) ,@body))
(defmacro eask--silent-error (&rest body)
"Execute BODY and inhibit all error messages."
(declare (indent 0) (debug t))
`(let ((eask-inhibit-error-message t)) ,@body))
(defmacro eask-ignore-errors-silent (&rest body)
"Execute BODY by completely ignore errors."
(declare (indent 0) (debug t))
`(eask-ignore-errors (eask--silent-error ,@body)))
(defcustom eask-log-file nil
"Weather to generate log files."
:type 'boolean
:group 'eask)
(defmacro eask--log-write-buffer (buffer file)
"Write BUFFER to FILE."
`(when (get-buffer-create ,buffer)
(let ((buffer-file-coding-system 'utf-8))
(write-region (with-current-buffer ,buffer (buffer-string)) nil
(expand-file-name ,file log-dir)))))
(defcustom eask-file-loaded-hook nil
"Hook runs after Easkfile is loaded."
:type 'hook
:group 'eask)
(defcustom eask-before-command-hook nil
"Hook runs before command is executed."
:type 'hook
:group 'eask)
(defcustom eask-after-command-hook nil
"Hook runs after command is executed."
:type 'hook
:group 'eask)
(defcustom eask-on-error-hook nil
"Hook runs when error is triggered."
:type 'hook
:group 'eask)
(defcustom eask-on-warning-hook nil
"Hook runs when warning is triggered."
:type 'hook
:group 'eask)
(defcustom eask-dist-path "dist"
"Default path where to place the package artifact."
:type 'string
:group 'eask)
(defcustom eask-docs-path "docs/public/"
"Default path where to place the documentation."
:type 'string
:group 'eask)
(defcustom eask-recipe-path "recipes"
"Name of default target directory for placing recipes."
:type 'string
:group 'eask)
(defvar eask-lint-first-file-p nil
"Set the flag to t after the first file is linted.")
(defvar eask-commands nil
"List of defined commands.")
(defmacro eask-defcommand (name &rest body)
"Define an Eask command."
(declare (doc-string 2) (indent 1))
(or name (error "Cannot define '%s' as a command" name))
(push name eask-commands)
(setq eask-commands (delete-dups eask-commands))
`(defun ,name nil ,@body))
(require 'ansi-color nil t)
(require 'package nil t)
(require 'project nil t)
(require 'json nil t)
(require 'nsm nil t)
(require 'url-vars nil t)
(require 'cl-lib nil t)
(require 'files nil t)
(require 'ls-lisp nil t)
(require 'pp nil t)
(require 'rect nil t)
(require 'subr-x nil t)
(defconst eask-is-windows (memq system-type '(cygwin windows-nt ms-dos))
"The system is Windows.")
(defconst eask-is-mac (eq system-type 'darwin)
"The system is macOS.")
(defconst eask-is-linux (eq system-type 'gnu/linux)
"The system is GNU Linux.")
(defconst eask-is-bsd (or eask-is-mac (eq system-type 'berkeley-unix))
"The system is BSD.")
(defconst eask-system-type
(cond (eask-is-windows 'dos)
(eask-is-bsd 'mac)
(eask-is-linux 'unix)
(t 'unknown))
"Return current OS type.")
(defun eask--load--adv (fnc &rest args)
"Prevent `_prepare.el' loading twice.
Arguments FNC and ARGS are used for advice `:around'."
(unless (string= (nth 0 args) (eask-script "_prepare")) (apply fnc args)))
(defconst eask-has-colors (getenv "EASK_HASCOLORS")
"Return non-nil if terminal supports colors.")
(defconst eask-homedir (getenv "EASK_HOMEDIR")
"Eask's home directory path.")
(defconst eask-invocation (getenv "EASK_INVOCATION")
"Eask's invocation program path.")
(defconst eask-is-pkg (getenv "EASK_IS_PKG")
"Return non-nil if Eask is packaged.")
(defconst eask-rest
(let ((args (getenv "EASK_REST_ARGS")))
(setq args (ignore-errors (split-string args ",")))
args)
"Eask's arguments after command separator `--'; return a list.
If the argument is `-- arg0 arg1'; it will return `(arg0 arg1)'.")
(defun eask-rest ()
"Eask's arguments after command separator `--'; return a string.
If the argument is `-- arg0 arg1'; it will return `arg0 arg1'."
(mapconcat #'identity eask-rest " "))
(defconst eask-argv argv
"This stores the real argv; the argv will soon be replaced with `(eask-args)'.")
(defconst eask--script (nth 1 (or (member "-scriptload" command-line-args)
(member "-l" command-line-args)))
"Script currently executing.")
(defconst eask-lisp-root
(let* ((script (ignore-errors (file-name-directory eask--script)))
(dir (ignore-errors (expand-file-name (concat script "../"))))
(basename (file-name-nondirectory (directory-file-name dir)))
(root (expand-file-name "/")))
(while (and (not (string= root dir))
(not (string= basename "lisp")))
(setq dir (expand-file-name (concat dir "../"))
basename (file-name-nondirectory (directory-file-name dir))))
dir)
"Source `lisp' directory; should always end with slash.")
(defun eask-command ()
"What's the current command?
If the command is with subcommand, it will return command with concatenate with
slash separator. For example, the following:
$ eask lint checkdoc [FILES..]
will return `lint/checkdoc' with a dash between two subcommands."
(let* ((script-dir (file-name-directory eask--script))
(script-file (file-name-sans-extension (file-name-nondirectory eask--script)))
(module-name (eask-s-replace eask-lisp-root "" script-dir))
(module-names (split-string module-name "/" t)))
;; Make certain commands the root command; e.g. `core', `checker', etc.
(if (member (nth 0 module-names) '("core" "checker")) script-file
(mapconcat #'identity (append module-names
(list script-file))
"/"))))
(defun eask-command-p (commands)
"Return t if COMMANDS is the current command."
(member (eask-command) (eask-listify commands)))
(defun eask-special-p ()
"Return t if the command that can be run without Eask-file existence.
These commands will first respect the current workspace. If the current
workspace has no valid Eask-file; it will load global workspace instead."
(eask-command-p '("init" "init/source" "init/cask" "init/eldev" "init/keg"
"create/package" "create/elpa"
"bump" "cat" "keywords" "repl"
"generate/ignore" "generate/license"
"test/melpazoid")))
(defun eask-execution-p ()
"Return t if the command is the execution command.
This is added because we don't want to pollute `error' and `warn' functions."
(eask-command-p '("load" "exec" "emacs" "eval" "repl"
"run/script" "run/command"
;; NOTE: These test commands handle the exit code themselves;
;; therefore, we don't need to handle it for them!
"test/ert" "test/ert-runner")))
(defun eask-checker-p ()
"Return t if running Eask as the checker.
Without this flag, the process will be terminated once the error is occurred.
This flag allows you to run through operations without reporting errors."
(eask-command-p '("analyze")))
(defun eask-script (script)
"Return full SCRIPT filename."
(concat eask-lisp-root script ".el"))
(defun eask-load (script)
"Load another eask SCRIPT; so we can reuse functions across all scripts."
(let ((eask-loading-file-p t)) (eask-call script)))
(defun eask-call (script)
"Call another eask SCRIPT."
(if-let* ((script-file (eask-script script))
((file-exists-p script-file)))
(load script-file nil t)
(eask-error "Script missing %s" script-file)))
(defun eask-import (url)
"Load and eval the script from a URL."
(with-current-buffer (url-retrieve-synchronously url t nil eask-import-timeout)
(goto-char (point-min))
(re-search-forward "^$" nil 'move)
(forward-char)
(delete-region (point-min) (point))
(eval-buffer)))
(defun eask-2str (obj)
"Convert OBJ to string."
(format "%s" obj))
(defun eask-2url (url)
"Convert secure/insecure URL."
(if (and url
(gnutls-available-p)
(eask-network-insecure-p))
(eask-s-replace "https://" "http://" url)
url))
(defun eask-listify (obj)
"Turn OBJ to list."
(if (listp obj) obj (list obj)))
(defun eask-intern (obj)
"Safely intern OBJ."
(if (stringp obj) (intern obj) obj))
(defun eask--sinr (len-or-list form-1 form-2)
"If LEN-OR-LIST has length of 1; return FORM-1, else FORM-2."
(let ((len (if (numberp len-or-list) len-or-list (length len-or-list))))
(if (<= len 1) form-1 form-2)))
(defun eask-current-time ()
"Return current time."
(let ((now (current-time))) (logior (ash (car now) 16) (cadr now))))
(defun eask-seq-str-max (sequence)
"Return max length in SEQUENCE of strings."
(let ((result 0))
(mapc (lambda (elm) (setq result (max result (length (eask-2str elm))))) sequence)
result))
(defun eask-s-replace (old new s)
"Replace OLD with NEW in S each time it occurs."
(replace-regexp-in-string (regexp-quote old) new s t t))
(defun eask-f-filename (path)
"Return the name of PATH."
(file-name-nondirectory (directory-file-name path)))
(defun eask-directory-empty-p (dir)
"Return t if DIR names an existing directory containing no other files.
The function `directory-empty-p' only exists 28.1 or above; copied it."
(if (fboundp #'directory-empty-p)
(directory-empty-p dir)
(and (file-directory-p dir)
;; XXX: Do not pass in the 5th argument COUNT; it doesn't compatbile to
;; 27.2 or lower!
(null (directory-files dir nil directory-files-no-dot-files-regexp t)))))
(defun eask--guess-package-name (basename)
"Convert the BASENAME to a valid, commonly seen package name."
(when-let* ((name (ignore-errors (downcase basename))))
(setq name (eask-s-replace "emacs-" "" name)
name (eask-s-replace "-emacs" "" name)
name (replace-regexp-in-string "[.-]el$" "" name))
name))
(defun eask-guess-package-name (&optional basename)
"Return the possible package name.
Optional argument BASENAME accepts a string; it will convert the string to a
valid, commonly seen package name."
(or (eask--guess-package-name basename)
(eask-package-name)
(eask--guess-package-name
(ignore-errors (file-name-nondirectory
(file-name-sans-extension eask-package-file))))))
(defun eask-guess-entry-point (&optional basename)
"Return the guess entry point by its BASENAME."
(let ((name (eask-guess-package-name basename)))
(format "%s.el" name)))
(defun eask-read-string (prompt &optional
initial-input
history
default-value
inherit-input-method)
"Wrapper for function `read-string'.
Argument PROMPT and all optional arguments INITIAL-INPUT, HISTORY, DEFAULT-VALUE
and INHERIT-INPUT-METHOD see function `read-string' for more information."
(let ((str (read-string prompt initial-input history default-value inherit-input-method)))
(eask-s-replace "\"" "" str)))
(defun eask--goto-line (line)
"Go to LINE."
(goto-char (point-min))
(forward-line (1- line)))
(defun eask--column-at-point (point)
"Get column at POINT."
(save-excursion (goto-char point) (current-column)))
(defconst eask-buffer-name "*eask*"
"Buffer name is used for temporary storage throughout the life cycle.")
(defun eask-progress-seq (prefix sequence suffix func)
"Shorthand to progress SEQUENCE of task.
Arguments PREFIX and SUFFIX are strings to print before and after each progress.
Argument FUNC are execution for eash progress; this is generally the actual
task work."
(let* ((total (length sequence)) (count 0)
(offset (eask-2str (length (eask-2str total)))))
(mapc
(lambda (item)
(cl-incf count)
(eask-with-progress
(format (concat "%s [%" offset "d/%d] %s... ") prefix count total
(ansi-green item))
(when func (funcall func item))
suffix))
sequence)))
(defun eask-print-log-buffer (&optional buffer-or-name)
"Loop through each line and print each line with corresponds log level.
You can pass BUFFER-OR-NAME to replace current buffer."
(with-current-buffer (or buffer-or-name (current-buffer))
(goto-char (point-min))
(while (not (eobp))
(let ((line (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(cond ((string-match-p "[: ][Ee]rror: " line) (eask-error line))
((string-match-p "[: ][Ww]arning: " line) (eask-warn line))
(t (eask-log line))))
(forward-line 1))))
(defun eask-delete-file (filename)
"Delete a FILENAME from disk."
(let (deleted)
(eask-with-progress
(format "Deleting %s... " filename)
(eask-with-verbosity 'log
(setq deleted (file-exists-p filename))
(ignore-errors (delete-file filename))
(setq deleted (and deleted (not (file-exists-p filename)))))
(if deleted "done ✓" "skipped ✗"))
deleted))
(defun eask--action-format (len)
"Construct action format by LEN."
(setq len (eask-2str len))
(concat "[%" (eask-2str (length len)) "d/" len "] "))
(defun eask--locate-archive-contents (archive)
"Locate ARCHIVE's contents file."
(let* ((name (cond ((consp archive) (car archive))
(t archive)))
(file "archive-contents")
(dir (expand-file-name (concat "archives/" name) package-user-dir)))
(expand-file-name file dir)))
(defun eask--package-download-one-archive (fnc &rest args)
"Execution around function `package-download-one-archive'.
Arguments FNC and ARGS are used for advice `:around'."
(cl-incf eask--action-index)
(let* ((archive (nth 0 args))
(name (car archive))
(url (cdr archive))
(fmt (eask--action-format (length package-archives)))
(download-p))
(eask-with-verbosity-override 'log
(when (= 1 eask--action-index) (eask-msg ""))
(eask-with-progress
(format " - %sDownloading %s (%s)... "
(format fmt eask--action-index)
(ansi-green (eask-2str name))
(ansi-yellow (eask-2str url)))
(eask-with-verbosity 'debug
(apply fnc args)
(setq download-p t))
(cond (download-p "done ✓")
(t "failed ✗"))))))
(defun eask--download-archives ()
"If archives download failed; download it manually."
(dolist (archive package-archives)
(cl-incf eask--action-index)
(let* ((name (car archive))
(local-file (eask--locate-archive-contents archive))
(dir (file-name-directory local-file)) ; ~/.emacs.d/elpa/archives/{name}
(file (file-name-nondirectory local-file)) ; archive-contents
(url (format "https://raw.githubusercontent.com/emacs-eask/archives/master/%s/" name))
(url-file (concat url file))
(download-p)
(local-archive-p (string= name "local")) ; exclude local elpa
(fmt (eask--action-format (length package-archives))))
(unless (file-exists-p local-file)
(eask-with-verbosity-override 'log
(when (= 1 eask--action-index) (eask-msg ""))
(eask-with-progress
(format " - %sDownloading %s (%s) manually... "
(format fmt eask--action-index)
(ansi-green name)
(ansi-yellow url))
(eask-with-verbosity 'debug
(unless local-archive-p
(if (url-file-exists-p url-file)
(progn
(ignore-errors (make-directory dir t))
(url-copy-file url-file local-file t)
(setq download-p t))
(eask-debug "No archive-contents found in `%s'" (ansi-green name)))))
(cond (download-p "done ✓")
(local-archive-p "skipped ✗")
(t "failed ✗")))))
(when download-p (eask-pkg-init t)))))
(defun eask-use-legacy-package-build ()
"Return t if using the legacy `package-build' package."
(version< emacs-version "27.1"))
(defun eask-load-legacy-package-build ()
"Load the legacy `package-build' package."
(when (eask-use-legacy-package-build)
(add-to-list 'load-path
(format "%sextern/package-build/%s/"
eask-lisp-root
emacs-major-version)
t)))
(defun eask--update-exec-path ()
"Add all bin directory to the variable `exec-path'."
(dolist (entry (directory-files package-user-dir t directory-files-no-dot-files-regexp))
(when-let* ((bin (expand-file-name "bin" entry))
((file-directory-p bin)))
(add-to-list 'exec-path bin t)))
(delete-dups exec-path))
(defun eask--update-load-path ()
"Add all .el files to the variable `load-path'."
(dolist (filename (eask-package-el-files))
(add-to-list 'load-path (file-name-directory filename) t))
(delete-dups load-path))
(defun eask-dependencies ()
"Return list of dependencies."
(append eask-depends-on (and (eask-dev-p) eask-depends-on-dev)))
(defun eask--package-mapc (func deps)
"Like function `mapc' but for process package transaction specifically.
For arguments FUNC and DEPS, see function `mapc' for more information."
(let* ((eask--action-prefix) ; remain untouch
(len (length deps))
(fmt (eask--action-format len))
(count 0))
(dolist (pkg deps)
(cl-incf count)
(setq eask--action-prefix (format fmt count))
(funcall func pkg))))
(defun eask--install-deps (dependencies msg)
"Install DEPENDENCIES.
Argument MSG are extra information to display in the header; mainly define the
scope of the dependencies (it's either `production' or `development')."
(let* ((names (mapcar #'car dependencies))
(names (mapcar #'eask-intern names))
(len (length dependencies))
(ies (eask--sinr len "y" "ies"))
(pkg-installed (cl-remove-if #'package-installed-p names))
(installed (length pkg-installed)) (skipped (- len installed)))
(eask-log "Installing %s %s dependenc%s..." len msg ies)
(eask-msg "")
(eask--package-mapc #'eask-package-install names)
(eask-msg "")
(eask-info "(Total of %s dependenc%s installed, %s skipped)"
installed ies skipped)))
(defun eask-install-dependencies ()
"Install dependencies defined in Eask file."
(eask-defvc< 27 (eask-pkg-init)) ; XXX: remove this after we drop 26.x
(when eask-depends-on-recipe-p
(eask-log "Installing required external packages...")
(eask-archive-install-packages '("gnu" "melpa")
'package-build)
(eask-with-progress
"Building temporary archives (this may take a while)... "
(eask-with-verbosity 'debug (github-elpa-build))
"done ✓")
(eask-pkg-init t))
(when eask-depends-on
(eask--install-deps eask-depends-on "package"))
(when (and eask-depends-on-dev (eask-dev-p))
(eask-msg "")
(eask--install-deps eask-depends-on-dev "development")))
(defun eask-setup-paths ()
"Setup both variables `exec-path' and `load-path'."
(eask-with-progress
(ansi-green "Updating environment variables... ")
(eask-with-verbosity 'debug
(eask--update-exec-path) (eask--update-load-path)
(setenv "PATH" (string-join exec-path path-separator))
(setenv "EMACSLOADPATH" (string-join load-path path-separator)))
(ansi-green "done ✓")))
(defun eask-pkg-init (&optional force)
"Package initialization.
If the argument FORCE is non-nil, force initialize packages in this session."
(when (or (not package--initialized) (not package-archive-contents) force
;; XXX: we need to initialize once in global scope since most Emacs
;; configuration would likely to set `package-archives' variable
;; themselves.
(and (eask-config-p) (not eask--package-initialized)))
(setq eask--package-initialized t)
(eask-with-progress
(ansi-green "Loading package information... ")
(eask-with-verbosity 'debug
(package-initialize t)
(let ((eask--action-index 0)) (package-refresh-contents))
(let ((eask--action-index 0)) (eask--download-archives)))
(ansi-green "done ✓"))))
(defun eask--pkg-transaction-vars (pkg)
"Return 1 symbol and 2 strings.
Argument PKG is the name of the package."
(let* (;; Ensure symbol
(pkg (eask-intern pkg))
;; Wrap package name with color
(pkg-string (ansi-green (eask-2str pkg)))
;; Wrap version number with color
(pkg-version (ansi-yellow (eask-package--version-string pkg))))
(list pkg pkg-string pkg-version)))
(defun eask-archive-install-packages (archives names)
"Install package NAMES with ARCHIVES setup."
(eask-defvc< 27 (eask-pkg-init)) ; XXX: remove this after we drop 26.x
(when-let* ((names (eask-listify names))
((cl-some (lambda (pkg) (not (package-installed-p pkg))) names)))
(eask-with-archives archives
(eask--package-mapc #'eask-package-install names))))
(defun eask-package-installable-p (pkg)
"Return non-nil if package (PKG) is installable."
(assq (eask-intern pkg) package-archive-contents))
(defun eask-package-install (pkg)
"Install the package (PKG)."
(eask-defvc< 27 (eask-pkg-init)) ; XXX: remove this after we drop 26.x
(eask--pkg-process pkg
(cond
((package-installed-p pkg)
(eask-msg " - %sSkipping %s (%s)... already installed ✗"
eask--action-prefix
name version))
((progn
(eask-pkg-init)
(unless (eask-package-installable-p pkg)
(eask-error "Package not installable `%s'; make sure the package archive (source) is included" pkg))))
((when-let* ((desc (eask-package-desc pkg))
(req-emacs (assoc 'emacs (package-desc-reqs desc)))
(req-emacs (package-version-join (nth 0 (cdr req-emacs))))
((version< emacs-version req-emacs)))
(if (eask-strict-p)
(eask-error " - %sSkipping %s (%s)... it requires Emacs %s and above ✗"
eask--action-prefix
pkg (eask-package--version-string pkg) req-emacs)
(eask-msg " - %sSkipping %s (%s)... it requires Emacs %s and above ✗"
eask--action-prefix
name version (ansi-yellow req-emacs)))))
(t
(eask--pkg-process pkg
(eask-with-progress
(format " - %sInstalling %s (%s)... " eask--action-prefix name version)
(eask-with-verbosity 'debug
;; XXX: Without ignore-errors guard, it will trigger error
;;
;; Can't find library xxxxxxx.el
;;
;; But we can remove this after Emacs 28, since function `find-library-name'
;; has replaced the function `signal' instead of the `error'.
(eask-ignore-errors (package-install pkg)))
"done ✓"))))))
(defun eask-package-delete (pkg)
"Delete the package (PKG)."
(eask-defvc< 27 (eask-pkg-init)) ; XXX: remove this after we drop 26.x
(eask--pkg-process pkg
(cond
((not (package-installed-p pkg))
(eask-msg " - %sSkipping %s (%s)... not installed ✗" eask--action-prefix name version))
(t
(eask--pkg-process pkg
(eask-with-progress
(format " - %sUninstalling %s (%s)... " eask--action-prefix name version)
(eask-with-verbosity 'debug
(package-delete (eask-package-desc pkg t) (eask-force-p)))
"done ✓"))))))
(defun eask-package-reinstall (pkg)
"Reinstall the package (PKG)."
(eask-defvc< 27 (eask-pkg-init)) ; XXX: remove this after we drop 26.x
(eask--pkg-process pkg
(cond
((not (package-installed-p pkg))
(eask-msg " - %sSkipping %s (%s)... not installed ✗" eask--action-prefix name version))
(t
(eask-pkg-init)
(eask--pkg-process pkg
(eask-with-progress
(format " - %sReinstalling %s (%s)... " eask--action-prefix name version)
(eask-with-verbosity 'debug
(package-delete (eask-package-desc pkg t) t)
(eask-ignore-errors (package-install pkg)))
"done ✓"))))))
(defun eask-package-desc (name &optional current)
"Build package description by its NAME.
The argument NAME must be the package's name. If the argument CURRENT is
non-nil, we retrieve version number from the variable `package-alist';
otherwise, we retrieve it from the variable `package-archive-contents'."
(cadr (assq name (if current package-alist
(or package-archive-contents package-alist)))))
(defun eask-package--version (name &optional current)
"Return package's version.
For arguments NAME and CURRENT, please see function `eask-package-desc' for
full detials."
(when-let* ((desc (eask-package-desc name current)))
(package-desc-version desc)))
(defun eask-package--version-string (pkg)
"Return PKG's version."
(if-let* ((version (or (eask-package--version pkg t)
(eask-package--version pkg nil))))
(package-version-join version)
;; Just in case, but this should never happens!
"0"))
(defun eask-package-desc-url ()
"Return url from package descriptor."
(when eask-package-desc
(when-let* ((extras (package-desc-extras eask-package-desc)))
(cdr (assoc :url extras)))))
(defun eask-package-desc-keywords ()
"Return keywords from package descriptor."
(when eask-package-desc
(or (package-desc--keywords eask-package-desc)
;; XXX: Handle Emacs 26.x keywords cannot be parsed issue.
(and eask-package-file
(with-temp-buffer
(insert-file-contents eask-package-file)
(lm-keywords-list))))))
(defun eask-pkg-el ()
"Return package description file if exists."
(let ((pkg-el (package--description-file default-directory)))
(when (file-readable-p pkg-el) pkg-el)))
(defun eask--str2num (str)
"Convert string (STR) to number."
(ignore-errors (string-to-number str)))
(defun eask--flag (flag)
"Return non-nil if FLAG exists."
(member (concat "--eask" flag) eask-argv))
(defun eask--flag-value (flag)
"Return value for FLAG."
(nth 1 (eask--flag flag)))
(defun eask-global-p ()
"Non-nil when in global space (`-g', `--global')."
(eask--flag "-g"))
(defun eask-config-p ()
"Non-nil when in config space (`-c', `--config')."
(eask--flag "-c"))
(defun eask-local-p ()
"Non-nil when in local space (default)."
(and (not (eask-global-p))
(not (eask-config-p))))
(defun eask-all-p ()
"Non-nil when flag is on (`-a', `--all')."
(eask--flag "-a"))
(defun eask-quick-p ()
"Non-nil when flag is on (`-q', `--quick')."
(eask--flag "-q"))
(defun eask-force-p ()
"Non-nil when flag is on (`-f', `--force')."
(eask--flag "-f"))
(defun eask-dev-p ()
"Non-nil when flag is on (`--dev', `--development')."
(eask--flag "--dev"))
(defun eask-debug-p ()
"Non-nil when flag is on (`--debug')."
(eask--flag "--debug"))