forked from emacs-eask/eask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eask-core.el
2290 lines (2202 loc) · 95.1 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
(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."
(unless (string= (nth 0 args) (eask-script "_prepare")) (apply fnc args)))
(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-special-p ()
"Return t if the command that can be run without Eask-file existence."
(member (eask-command) '("init/cask" "cat" "keywords"
"generate/ignore" "generate/license")))
(defun eask-checker-p ()
"Return t if running Eask as the checker."
(member (eask-command) '("check-eask")))
(defun eask-script (script)
"Return full script filename."
(concat eask-lisp-root script ".el"))
(defvar eask-loading-file-p nil
"This became t; if we are loading script from another file and not expecting
the `eask-start' execution.")
(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 "Scripting missing %s..." script-file)))
(defmacro eask-defvc< (version &rest body)
"Define scope if Emacs version is below VERSION."
(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))
(defun eask-2str (obj)
"Convert OBJ to string."
(format "%s" obj))
(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 (lsh (car now) 16) (cadr now))))
(defun eask-seq-str-max (sequence)
"Return max length in list of strings."
(let ((result 0))
(mapc (lambda (elm) (setq result (max result (length (eask-2str elm))))) sequence)
result))
(defun eask-f-filename (path)
"Return the name of PATH."
(file-name-nondirectory (directory-file-name path)))
(defun eask-s-replace (old new s)
"Replace OLD with NEW in S each time it occurs."
(if (fboundp #'string-replace)
(string-replace old new s)
(replace-regexp-in-string (regexp-quote old) new s t t)))
(defun eask--download-archives ()
"If archives download failed; download it manually."
(dolist (archive package-archives)
(let* ((location (cdr archive))
(name (car archive))
(file "archive-contents")
(dir (expand-file-name (concat "archives/" name) package-user-dir))
(local-file (expand-file-name file dir))
(url (format
"https://raw.githubusercontent.com/emacs-eask/archives/master/%s/%s" name file))
(download-p)
(local-archive-p (string= name "local"))) ; exclude local elpa
(unless (file-exists-p local-file)
(eask-with-progress
(format "Downloading archive `%s' manually... " (ansi-yellow name))
(unless local-archive-p
(if (url-file-exists-p url)
(progn
(ignore-errors (make-directory dir t))
(url-copy-file url local-file t)
(setq download-p t))
(eask-debug "No archive-contents found in `%s'" (ansi-yellow name))))
(cond (download-p "done ✓")
(local-archive-p "skipped ✗")
(t "failed ✗"))))
(when download-p (eask-pkg-init t)))))
(defun eask--update-exec-path ()
"Add all bin directory to `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 load-path for all .el files."
(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--package-prefix) ; remain untouch
(len (length deps))
(len-str (eask-2str len))
(fmt (concat "[%" (eask-2str (length len-str)) "d/" len-str "] "))
(count 0))
(dolist (pkg deps)
(cl-incf count)
(setq eask--package-prefix (format fmt count))
(funcall func pkg))))
(defun eask--install-deps (dependencies msg)
"Install DEPENDENCIES."
(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-with-archives "melpa"
(eask-package-install '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 `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 ✓")))
(defvar eask--package-initialized nil
"Flag for package initialization in global scope.")
(defun eask-pkg-init (&optional force)
"Package initialization."
(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) (package-refresh-contents)
(eask--download-archives))
(ansi-green "done ✓"))))
(defun eask--pkg-transaction-vars (pkg)
"Return 1 symbol and 2 strings."
(let* (;; Ensure symbol
(pkg (if (stringp pkg) (intern pkg) 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)))
(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 makes ARCHIVES available."
(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))
(defun eask-package-installable-p (pkg)
"Return non-nil if package is installable."
(assq (if (stringp pkg) (intern pkg) pkg) package-archive-contents))
(defvar eask--package-prefix ""
"The prefix to display before each package action.")
(defun eask-package-install (pkg)
"Install the package."
(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--package-prefix
name version))
((progn
(eask-pkg-init)
(unless (eask-package-installable-p pkg)
(eask-error "Package not installable `%s'; make sure package archive 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--package-prefix
pkg (eask-package--version-string pkg) emacs-version)
(eask-msg " - %sSkipping %s (%s)... it requires Emacs %s and above ✗"
eask--package-prefix
name version (ansi-yellow emacs-version)))))
(t
(eask--pkg-process pkg
(eask-with-progress
(format " - %sInstalling %s (%s)... " eask--package-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."
(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--package-prefix name version))
(t
(eask--pkg-process pkg
(eask-with-progress
(format " - %sUninstalling %s (%s)... " eask--package-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."
(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--package-prefix name version))
(t
(eask-pkg-init)
(eask--pkg-process pkg
(eask-with-progress
(format " - %sReinstalling %s (%s)... " eask--package-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 PKG-NAME."
(cadr (assq name (if current package-alist
(or package-archive-contents package-alist)))))
(defun eask-package--version (name &optional current)
"Return PKG's version."
(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 (package-desc--keywords eask-package-desc)))
(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)))
(defconst eask-has-colors (getenv "EASK_HASCOLORS")
"Return non-nil if terminal support colors.")
(defconst eask-homedir (getenv "EASK_HOMEDIR")
"Eask temporary storage.")
(defconst eask-invocation (getenv "EASK_INVOCATION")
"Eask invocation program.")
(defun eask--str2num (str) (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 () (eask--flag "-g")) ; -g, --global
(defun eask-config-p () (eask--flag "-c")) ; -c, --config
(defun eask-local-p () (and (not (eask-global-p))
(not (eask-config-p)))) ; local project space
(defun eask-all-p () (eask--flag "-a")) ; -a, --all
(defun eask-quick-p () (eask--flag "-q")) ; -q, --quick
(defun eask-force-p () (eask--flag "-f")) ; -f, --force
(defun eask-dev-p () (eask--flag "--dev")) ; --dev, --development
(defun eask-debug-p () (eask--flag "--debug")) ; --debug
(defun eask-strict-p () (eask--flag "--strict")) ; --strict
(defun eask-timestamps-p () (eask--flag "--timestamps")) ; --timestamps
(defun eask-log-level-p () (eask--flag "--log-level")) ; --log-level
(defun eask-log-file-p () (eask--flag "--log-file")) ; --log-file, --lf
(defun eask-elapsed-time-p () (eask--flag "--elapsed-time")) ; --elapsed-time, --et
(defun eask-allow-error-p () (eask--flag "--allow-error")) ; --allow-error
(defun eask-insecure-p () (eask--flag "--insecure")) ; --insecure
(defun eask-no-color-p () (eask--flag "--no-color")) ; --no-color
(defun eask-clean-p () (eask--flag "--clean")) ; -c, --clean
(defun eask-json-p () (eask--flag "--json")) ; --json
(defun eask-number-p () (eask--flag "--number")) ; -n, --number
(defun eask-output () (eask--flag-value "--output")) ; --o, --output
(defun eask-proxy () (eask--flag-value "--proxy")) ; --proxy
(defun eask-http-proxy () (eask--flag-value "--http-proxy")) ; --http-proxy
(defun eask-https-proxy () (eask--flag-value "--https-proxy")) ; --https-proxy
(defun eask-no-proxy () (eask--flag-value "--no-proxy")) ; --no-proxy
(defun eask-destination () (eask--flag-value "--dest")) ; --dest, --destination
(defun eask-from () (eask--flag-value "--from")) ; --from
(defun eask-depth () (eask--str2num (eask--flag-value "--depth"))) ; --depth
(defun eask-verbose () (eask--str2num (eask--flag-value "--verbose"))) ; -v, --verbose
(defun eask--handle-global-options ()
"Handle global options."
(when (eask-debug-p) (setq debug-on-error t))
(when (eask-verbose) (setq eask-verbosity (eask-verbose)))
(when (eask-insecure-p) (setq network-security-level 'low))
(when (eask-timestamps-p) (setq eask-timestamps t))
(when (eask-log-level-p) (setq eask-log-level t))
(when (eask-log-file-p) (setq eask-log-file t))
(when (eask-elapsed-time-p) (setq eask-elapsed-time t))
(when (eask-no-color-p) (setq ansi-inhibit-ansi t))
(unless eask-has-colors (setq ansi-inhibit-ansi t))
(when (display-graphic-p) (setq ansi-inhibit-ansi t))
(eask--add-proxy "http" (eask-proxy))
(eask--add-proxy "https" (eask-proxy))
(eask--add-proxy "http" (eask-http-proxy))
(eask--add-proxy "https" (eask-https-proxy))
(eask--add-proxy "no_proxy" (eask-no-proxy)))
(defun eask--add-proxy (protocal host)
"Add proxy."
(when host (push (cons protocal (eask-proxy)) url-proxy-services)))
(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'.")
(defun eask--form-options (options)
"Add --eask to all OPTIONS."
(mapcar (lambda (elm) (concat "--eask" elm)) options))
(defconst eask--option-switches
(eask--form-options
'("-g" "-c" "-a" "-q" "-f" "--dev"
"--debug" "--strict"
"--allow-error"
"--insecure"
"--timestamps" "--log-level"
"--log-file"
"--elapsed-time"
"--no-color"
"--clean"
"--json"
"--number"))
"List of boolean type options.")
(defconst eask--option-args
(eask--form-options
'("--output"
"--proxy" "--http-proxy" "--https-proxy" "--no-proxy"
"--verbose" "--silent"
"--depth" "--dest" "--from"))
"List of arguments (number/string) type options.")
(defconst eask--command-list
(append eask--option-switches eask--option-args)
"List of commands to accept, so we can avoid unknown option error.")
(defun eask-self-command-p (arg)
"Return non-nil if ARG is known internal command."
(member arg eask--command-list))
(defun eask-argv (index)
"Return argument value by INDEX."
(elt eask-argv index))
(defun eask-argv-out ()
"Convert all internal arguments to external arguments.
Simply remove `--eask' for each option, like `--eask--strict' to `--strict'."
(mapcar (lambda (arg)
(eask-s-replace "--eask" "" arg))
eask-argv))
(defun eask-args ()
"Get all arguments except options."
(let ((argv (cl-remove-if (lambda (arg) (member arg eask--option-switches)) eask-argv))
(args) (skip-next))
(dolist (arg argv)
(if skip-next (setq skip-next nil)
(if (member arg eask--option-args)
(setq skip-next t)
(push arg args))))
(reverse args)))
(defmacro eask--batch-mode (&rest body)
"Initialize for 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 (;; XXX: this will make command `info', `files' work as expected;
;; but the relative paths file spec will be lost...
;;
;; So commands like `load' would NOT work!
(default-directory (cond ((eask-global-p) eask-homedir)
((eask-config-p) user-emacs-directory)
(t default-directory)))
(alist))
(dolist (cmd eask--command-list)
(push (cons cmd '(lambda (&rest _))) alist))
(setq command-switch-alist (append command-switch-alist alist))
,@body)))
(defconst eask-file-keywords
'("package" "website-url" "keywords"
"author" "license"
"package-file" "package-descriptor" "files"
"script"
"source" "source-priority"
"depends-on" "development"
"exec-paths" "load-paths")
"List of Eask file keywords.")
(defun eask--loop-file-keywords (func)
"Loop through Eask file keywords for environment replacement. Internal used
for function `eask--alias-env'."
(dolist (keyword eask-file-keywords)
(let ((keyword-sym (intern keyword))
(api (intern (concat "eask-f-" keyword))) ; existing function
(old (intern (concat "eask--f-" keyword)))) ; variable that holds function pointer
(funcall func keyword-sym api old))))
(defmacro eask--alias-env (&rest body)
"Replace all Eask file functions temporary; this is only used when loading
Eask file in the workspace."
(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.")
(defun eask-root-del (filename)
"Remove Eask file root path from FILENAME."
(when (stringp filename)
(eask-s-replace (or eask-file-root default-directory) "" filename)))
(defun eask-file-load (location &optional noerror)
"Load Eask file in the LOCATION."
(when-let* ((target-eask-file (expand-file-name location user-emacs-directory))
(result (eask--alias-env (load target-eask-file 'noerror t t))))
(setq eask-file target-eask-file ; assign eask file only if success
eask-file-root (file-name-directory target-eask-file))
(run-hooks 'eask-file-loaded-hook)
result))
(defun eask--match-file (name)
"Check to see if NAME is our target Eask-file, then return it."
(let (;; Ensure path to filename
(name (file-name-nondirectory (directory-file-name name)))
;; `p-' stards for pattern
(p-easkfile-full (format "Easkfile.%s" emacs-version))
(p-easkfile-major (format "Easkfile.%s" emacs-major-version))
(p-easkfile "Easkfile")
(p-eask-full (format "Eask.%s" emacs-version))
(p-eask-major (format "Eask.%s" emacs-major-version))
(p-eask "Eask"))
(car (member name (list p-easkfile-full p-easkfile-major p-easkfile
p-eask-full p-eask-major p-eask)))))
(defun eask--all-files (&optional dir)
"Return a list of Eask files from DIR.
If argument DIR is nil, we use `default-directory' instead."
(setq dir (or dir default-directory))
(when-let* ((files (append
(ignore-errors (directory-files dir t "Easkfile[.0-9]*\\'"))
(ignore-errors (directory-files dir t "Eask[.0-9]*\\'"))))
(files (cl-remove-if #'file-directory-p files)))
(cl-remove-if-not #'eask--match-file files)))
(defun eask--find-files (start-path)
"Find the Eask-file from START-PATH.
This uses function `locate-dominating-file' to look up directory tree."
(when-let*
(;; XXX: This is redundant, but the simplest way to find the root path!
(root (locate-dominating-file start-path #'eask--all-files))
(files (eask--all-files root)) ; get all available Eask-files
;; Filter it to restrict to this Emacs version!
(files (cl-remove-if-not #'eask--match-file files))
;; Make `Easkfile.29.1' > `Easkfile.29' > `Easkfile' (same with `Eask' file)
(files (sort files #'string-greaterp))
;; Make `Easkfile' > `Eask' higher precedent!
(files (sort files (lambda (item1 item2)
(and (string-prefix-p "Easkfile" item1)
(not (string-prefix-p "Easkfile" item2)))))))
files))
(defun eask-file-try-load (start-path)
"Try load the Eask-file in START-PATH."
(when-let* ((files (eask--find-files start-path))
(file (car files)))
(eask-file-load file)))
(defun eask--print-env-info ()
"Display environment information at the very top of the execution."
(eask-msg "")
(eask-msg "✓ Checking Emacs version %s... done!" emacs-version)
(eask-with-verbosity 'debug
(eask-msg " 💡 Invoke from %s" invocation-directory)
(eask-msg " 💡 Build No. %s" emacs-build-number)
(eask-msg " 💡 System configuration %s" system-configuration)
(when-let ((emacs-build-time)
(time (format-time-string "%Y-%m-%d" emacs-build-time)))
(eask-msg " 💡 Build time %s" time)))
(eask-msg "✓ Checking system %s... done!" system-type))
(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")))
(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))
(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)
(eask--print-env-info)
(cond
((or (eask-global-p) (eask-special-p)) ; Commands without Eask-file needed
(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.
(if (eask-file-try-load "./")
(eask-msg "✓ Loading global Eask file in %s... done!" eask-file)
(eask-msg "✗ Loading global Eask file... missing!"))
(message "")
(package-activate-all)
(ignore-errors (make-directory package-user-dir t))
(eask--with-hooks ,@body))))
((eask-config-p)
(let ((inhibit-config (eask-quick-p)))
;; We accept Eask-file in `config' scope, but it shouldn't be used
;; for the sandbox.
(if (eask-file-try-load "./")
(eask-msg "✓ Loading config Eask file in %s... done!" eask-file)
(eask-msg "✗ Loading config Eask file... missing!"))
(message "")
(package-activate-all)
(eask-with-progress
(ansi-green "Loading your configuration... ")
(eask-with-verbosity 'debug
(unless inhibit-config
(load (locate-user-emacs-file "early-init.el") t)
(load (locate-user-emacs-file "../.emacs") t)
(load (locate-user-emacs-file "init.el") t)))
(ansi-green (if inhibit-config "skipped ✗" "done ✓")))
(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))))
(if (eask-file-try-load "./")
(eask-msg "✓ Loading Eask file in %s... done!" eask-file)
(eask-msg "✗ Loading Eask file... missing!")
(eask-help "core/init"))
(when eask-file
(message "")
(package-activate-all)
(ignore-errors (make-directory package-user-dir t))
(eask--silent (eask-setup-paths))
(eask--with-hooks ,@body))))))))))
(defun eask-network-insecure-p ()
"Are we attempt to use insecure connection?"
(eq network-security-level 'low))
(defconst eask-source-mapping
`((gnu . "https://elpa.gnu.org/packages/")
(nongnu . "https://elpa.nongnu.org/nongnu/")
(celpa . "https://celpa.conao3.com/packages/")
(jcs-elpa . "https://jcs-emacs.github.io/jcs-elpa/packages/")
(marmalade . "https://marmalade-repo.org/packages/")
(melpa . "https://melpa.org/packages/")
(melpa-stable . "https://stable.melpa.org/packages/")
(org . "https://orgmode.org/elpa/")
(shmelpa . "https://shmelpa.commandlinesystems.com/packages/"))
"Mapping of source name and url.")
(defvar eask-package nil)
(defvar eask-package-desc nil) ; package descriptor
(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 BODY with"
(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)))))
(defun eask-package--get (key)
"Return package info by KEY."
(plist-get eask-package key))
(defun eask-package-name () (eask-package--get :name))
(defun eask-package-version () (eask-package--get :version))
(defun eask-package-description () (eask-package--get :description))
(defun eask-depends-emacs-version ()
"Get Eask-file Emacs version string."
(nth 0 (cdar eask-depends-on-emacs)))
(defun eask-f-package (name version description)
"Set the package information."
(if eask-package
(eask-error "Multiple definition of `package'")
(setq eask-package `(:name ,name :version ,version :description ,description))
(progn ; Run checker
(eask--checker-string "Name" name)
(version= version "0.1.0")
(eask--checker-string "Description" description))))
(defun eask-f-website-url (url)
"Set website URL."
(if eask-website-url
(eask-error "Multiple definition of `website-url'")
(setq eask-website-url url)))
(defun eask-f-keywords (&rest keywords)
"Set package KEYWORDS."
(if eask-keywords
(eask-error "Multiple definition of `keywords'")
(setq eask-keywords keywords)))
(defun eask-f-author (name &optional email)
"Set package author's NAME and EMAIL."
(if (member name (mapcar #'car eask-authors))
(eask-warn "Warning regarding duplicate author name, %s" name)
(when (and email
(not (string-match-p "@" email)))
(eask-warn "Email seems to be invalid, %s" email))
(push (cons name email) eask-authors)))
(defun eask-f-license (name)
"Set package license NAME."
(if (member name eask-licenses)
(eask-warn "Warning regarding duplicate license name, %s" name)
(push name eask-licenses)))
(defun eask--try-construct-package-desc (file)
"Try construct the package descriptor from FILE."
(let (skipped)
(with-temp-buffer
(insert-file-contents file)
(setq eask-package-desc
(ignore-errors
(cond ((string-suffix-p "-pkg.el" file) ; if ensure -pkg.el
(package--read-pkg-desc 'dir))
((eask-pkg-el) ; if -pkg.el is presented,
(setq skipped t) nil) ; skip it
(t (package-buffer-info)))))) ; default read main package file
(eask-msg (concat
(if eask-package-desc "✓ " "✗ ")
"Try constructing the package-descriptor (%s)... "
(cond (eask-package-desc "succeeded!")
(skipped "skipped!")
(t "failed!")))
(file-name-nondirectory file))))
(defun eask-f-package-file (file)
"Set package FILE."
(if eask-package-file
(eask-error "Multiple definition of `package-file'")
(setq eask-package-file (expand-file-name file))
(if (file-exists-p eask-package-file)
(eask--try-construct-package-desc eask-package-file)
(eask-warn "Package-file seems to be missing `%s'" file))
(when-let
(((and (not eask-package-descriptor) ; prevent multiple definition error
(not eask-package-desc))) ; check if constructed
(pkg-file (eask-pkg-el)))
(eask-f-package-descriptor pkg-file)
;; XXX: Make sure DSL package descriptor is set back to `nil'
(setq eask-package-descriptor nil))))
(defun eask-f-package-descriptor (pkg-file)
"Set package PKG-FILE."
(cond
(eask-package-descriptor
(eask-error "Multiple definition of `package-descriptor'"))
((and eask-package-desc ; check if construct successfully
(equal (eask-pkg-el) pkg-file)) ; check filename the same
) ; ignore
(t
(setq eask-package-descriptor (expand-file-name pkg-file))
(cond ((not (string-suffix-p "-pkg.el" eask-package-descriptor))
(eask-error "Pkg-file must end with `-pkg.el'"))
((not (file-exists-p eask-package-descriptor))
(eask-warn "Pkg-file seems to be missing `%s'" pkg-file))
(t
(eask--try-construct-package-desc eask-package-descriptor))))))
(defun eask-f-files (&rest patterns)
"Set files PATTERNS."
(setq eask-files (append eask-files patterns)))
(defun eask-f-script (name command &rest args)
"Add scripts' command."
(when (symbolp name) (setq name (eask-2str name))) ; ensure to string, accept symbol
(when (assoc name eask-scripts)
(eask-error "Run-script with the same key name is not allowed: `%s`" name))
(push (cons name
(mapconcat #'identity (append (list command) args) " "))
eask-scripts))
(defun eask-f-source (name &optional location)
"Add archive NAME with LOCATION."
(when (symbolp name) (setq name (eask-2str name))) ; ensure to string, accept symbol
(when (assoc name package-archives)
(eask-error "Multiple definition of source `%s'" name))
(setq location (or location (cdr (assq (intern name) eask-source-mapping))))
(unless location (eask-error "Unknown package archive `%s'" name))
(when (and location
(gnutls-available-p)
(not (eask-network-insecure-p)))
(setq location (eask-s-replace "https://" "http://" location)))
(add-to-list 'package-archives (cons name location) t))
(defun eask-f-source-priority (archive-id &optional priority)
"Add PRIORITY for to ARCHIVE-ID."
(add-to-list 'package-archive-priorities (cons archive-id priority) t))
(defvar eask-depends-on-recipe-p nil
"Set to t if package depends on recipe.")
(defun eask--setup-dependencies ()
"Setup dependencies list."
(setq eask-depends-on (reverse eask-depends-on)
eask-depends-on-dev (reverse eask-depends-on-dev))
(when eask-depends-on-recipe-p
(eask-with-progress
"✓ Checking local archives... "
(eask-with-verbosity 'debug
(add-to-list 'package-archives `("local" . ,github-elpa-archive-dir) t)
;; If the local archives is added, we set the priority to a very
;; high number so user we always use the specified dependencies!
(add-to-list 'package-archive-priorities `("local" . 90) t))
"done!")))
(defun eask-f-depends-on (pkg &rest args)
"Specify a dependency of this package."
(cond
((string= pkg "emacs")
(if eask-depends-on-emacs
(eask-error "Define dependencies with the same name `%s'" pkg)
(let* ((minimum-version (car args))
(recipe (list pkg minimum-version)))
(if (version< emacs-version minimum-version)
(eask-error "This requires Emacs %s and above!" minimum-version)
(push recipe eask-depends-on-emacs))
recipe)))
;; No argument specify
((<= (length args) 1)
(let* ((minimum-version (or (car args) "0"))
(recipe (list pkg minimum-version)))
(if (member recipe eask-depends-on)
(eask-error "Define dependencies with the same name `%s'" pkg)
(push recipe eask-depends-on))
recipe))
;; recipe are entered
(t
(let ((recipe (append (list (intern pkg)) args)))
(if (member recipe eask-depends-on)
(eask-error "Define dependencies with the same name `%s'" pkg)
(push recipe eask-depends-on)
(eask-load "extern/github-elpa")
(eask-with-verbosity 'debug
(eask-with-progress
(ansi-blue (format "Generating recipe for package %s... " (ansi-yellow pkg)))
(write-region (pp-to-string recipe) nil (expand-file-name pkg github-elpa-recipes-dir))
(ansi-blue "done ✓")))
(setq eask-depends-on-recipe-p t))
recipe))))
(defun eask-f-development (&rest dep)
"Development scope with list of DEP."
(setq dep (cl-remove-if #'null dep)) ; make sure no `nil', see #143
(dolist (pkg dep)
(push pkg eask-depends-on-dev)
(delete-dups eask-depends-on-dev)
(setq eask-depends-on (remove pkg eask-depends-on))))
(defun eask-f-exec-paths (&rest dirs)
"Add all DIRS to exec-path."
(dolist (dir dirs) (add-to-list 'exec-path (expand-file-name dir) t)))
(defun eask-f-load-paths (&rest dirs)
"Add all DIRS to load-path."
(dolist (dir dirs) (add-to-list 'load-path (expand-file-name dir) t)))
(defvar eask--ignore-error-p nil
"Don't trigger error when this is non-nil.")
(defmacro eask-ignore-errors (&rest body)
"Execute BODY but ignore all errors."
(declare (indent 0) (debug t))
`(let ((eask--ignore-error-p t)) ,@body))
(defun eask--exit (&rest _) "Send exit code." (kill-emacs 1))
(defun eask--trigger-error ()
"Trigger error event."
(when (and (not eask--ignore-error-p)
(not (eask-checker-p))) ; ignore when checking Eask-file
(if (eask-allow-error-p) ; Trigger error at the right time
(add-hook 'eask-after-command-hook #'eask--exit)
(eask--exit))))
(defun eask--error (fnc &rest args)
"On error."
(let ((msg (eask--ansi 'error (apply #'format-message args))))
(eask--unsilent (eask-msg "%s" msg))
(run-hook-with-args 'eask-on-error-hook 'error msg)
(eask--trigger-error))
(when debug-on-error (apply fnc args)))
(defun eask--warn (fnc &rest args)
"On warn."
(let ((msg (eask--ansi 'warn (apply #'format-message args))))
(eask--unsilent (eask-msg "%s" msg))
(run-hook-with-args 'eask-on-warning-hook 'warn msg))
(eask--silent (apply fnc args)))
(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 or above (debug)."
:type 'integer)
(defcustom eask-timestamps nil
"Log messages with timestamps."
:type 'boolean)
(defcustom eask-log-level nil
"Log messages with level."
:type 'boolean)
(defcustom eask-level-color
'((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)
(defun eask--verb2lvl (symbol)
"Convert verbosity SYMBOL to level."
(cl-case symbol
(`debug 4)
(`log 3)
(`info 2)
(`warn 1)
(`error 0)
(t symbol)))
(defun eask--reach-verbosity-p (symbol)
"Return t if SYMBOL reach verbosity (should be printed)."
(>= eask-verbosity (eask--verb2lvl symbol)))
(defmacro eask-with-verbosity (symbol &rest body)