-
Notifications
You must be signed in to change notification settings - Fork 5
/
makem.sh
executable file
·1276 lines (1071 loc) · 38.5 KB
/
makem.sh
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
#!/usr/bin/env bash
# * makem.sh --- Script to aid building and testing Emacs Lisp packages
# URL: https://github.com/alphapapa/makem.sh
# Version: 0.6-pre
# * Commentary:
# makem.sh is a script that helps to build, lint, and test Emacs Lisp
# packages. It aims to make linting and testing as simple as possible
# without requiring per-package configuration.
# It works similarly to a Makefile in that "rules" are called to
# perform actions such as byte-compiling, linting, testing, etc.
# Source and test files are discovered automatically from the
# project's Git repo, and package dependencies within them are parsed
# automatically.
# Output is simple: by default, there is no output unless errors
# occur. With increasing verbosity levels, more detail gives positive
# feedback. Output is colored by default to make reading easy.
# The script can run Emacs with the developer's local Emacs
# configuration, or with a clean, "sandbox" configuration that can be
# optionally removed afterward. This is especially helpful when
# upstream dependencies may have released new versions that differ
# from those installed in the developer's personal configuration.
# * License:
# 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/>.
# * Functions
function usage {
cat <<EOF
$0 [OPTIONS] RULES...
Linter- and test-specific rules will error when their linters or tests
are not found. With -vv, rules that run multiple rules will show a
message for unavailable linters or tests.
Rules:
all Run all lints and tests.
compile Byte-compile source files.
lint Run all linters, ignoring unavailable ones.
lint-checkdoc Run checkdoc.
lint-compile Byte-compile source files with warnings as errors.
lint-declare Run check-declare.
lint-elsa Run Elsa (not included in "lint" rule).
lint-indent Lint indentation.
lint-package Run package-lint.
lint-regexps Run relint.
test, tests Run all tests, ignoring missing test types.
test-buttercup Run Buttercup tests.
test-ert Run ERT tests.
test-ert-interactive Run ERT tests interactively.
batch Run Emacs in batch mode, loading project source and test files
automatically, with remaining args (after "--") passed to Emacs.
interactive Run Emacs interactively, loading project source and test files
automatically, with remaining args (after "--") passed to Emacs.
Options:
-d, --debug Print debug info.
-h, --help I need somebody!
-v, --verbose Increase verbosity, up to -vvv.
--no-color Disable color output.
--debug-load-path Print load-path from inside Emacs.
-E, --emacs PATH Run Emacs at PATH.
-e, --exclude FILE Exclude FILE from linting and testing.
-f, --file FILE Check FILE in addition to discovered files.
-c, --compile-batch Batch-compile files (instead of separately; quicker, but
may hide problems).
-C, --no-compile Don't compile files automatically.
Sandbox options:
-s[DIR], --sandbox[=DIR] Run Emacs with an empty config in a sandbox DIR.
If DIR does not exist, make it. If DIR is not
specified, use a temporary sandbox directory and
delete it afterward, implying --install-deps and
--install-linters.
--install-deps Automatically install package dependencies.
--install-linters Automatically install linters.
-i, --install PACKAGE Install PACKAGE before running rules.
An Emacs version-specific subdirectory is automatically made inside
the sandbox, allowing testing with multiple Emacs versions. When
specifying a sandbox directory, use options --install-deps and
--install-linters on first-run and omit them afterward to save time.
Source files are automatically discovered from git, or may be
specified with options. Package dependencies are discovered from
"Package-Requires" headers in source files, from -pkg.el files, and
from a Cask file.
EOF
}
# ** Elisp
# These functions return a path to an elisp file which can be loaded
# by Emacs on the command line with -l or --load.
function elisp-buttercup-file {
# The function buttercup-run, which is called by buttercup-run-discover,
# signals an error if it can't find any Buttercup test suites. We don't
# want that to be an error, so we define advice which ignores that error.
local file=$(mktemp)
cat >$file <<EOF
(defun makem-buttercup-run (oldfun &rest r)
"Call buttercup-run only if \`buttercup-suites' is non-nil."
(when buttercup-suites
(apply oldfun r)))
(advice-add #'buttercup-run :around #'makem-buttercup-run)
EOF
echo $file
}
function elisp-elint-file {
local file=$(mktemp)
cat >$file <<EOF
(require 'cl-lib)
(require 'elint)
(defun makem-elint-file (file)
(let ((errors 0))
(cl-letf (((symbol-function 'orig-message) (symbol-function 'message))
((symbol-function 'message) (symbol-function 'ignore))
((symbol-function 'elint-output)
(lambda (string)
(cl-incf errors)
(orig-message "%s" string))))
(elint-file file)
;; NOTE: \`errors' is not actually the number of errors, because
;; it's incremented for non-error header strings as well.
(kill-emacs errors))))
EOF
echo "$file"
}
function elisp-checkdoc-file {
# Since checkdoc doesn't have a batch function that exits non-zero
# when errors are found, we make one.
local file=$(mktemp)
cat >$file <<EOF
(defvar makem-checkdoc-errors-p nil)
(setq ispell-program-name (executable-find "hunspell"))
(defun makem-checkdoc-files-and-exit ()
"Run checkdoc-file on files remaining on command line, exiting non-zero if there are warnings."
(let* ((files (mapcar #'expand-file-name command-line-args-left))
(checkdoc-create-error-function
(lambda (text start end &optional unfixable)
(let ((msg (concat (checkdoc-buffer-label) ":"
(int-to-string (count-lines (point-min) (or start (point-min))))
": " text)))
(message msg)
(setq makem-checkdoc-errors-p t)
;; Return nil because we *are* generating a buffered list of errors.
nil))))
(mapcar #'checkdoc-file files)
(when makem-checkdoc-errors-p
(kill-emacs 1))))
(setq checkdoc-spellcheck-documentation-flag t)
(makem-checkdoc-files-and-exit)
EOF
echo $file
}
function elisp-byte-compile-file {
# This seems to be the only way to make byte-compilation signal
# errors for warnings AND display all warnings rather than only
# the first one.
local file=$(mktemp)
# TODO: Add file to $paths_temp in other elisp- functions.
paths_temp+=("$file")
cat >"$file" <<EOF
(defun makem-batch-byte-compile (&rest args)
""
(let ((num-errors 0)
(num-warnings 0))
;; NOTE: Only accepts files as args, not directories.
(dolist (file command-line-args-left)
(pcase-let ((\`(,errors ,warnings) (makem-byte-compile-file file)))
(cl-incf num-errors errors)
(cl-incf num-warnings warnings)))
(zerop num-errors)))
(defun makem-byte-compile-file (filename &optional load)
"Call \`byte-compile-warn', returning the number of errors and the number of warnings."
(let ((num-warnings 0)
(num-errors 0))
(cl-letf (((symbol-function 'byte-compile-warn)
(lambda (format &rest args)
;; Copied from \`byte-compile-warn'.
(cl-incf num-warnings)
(setq format (apply #'format-message format args))
(byte-compile-log-warning format t :warning)))
((symbol-function 'byte-compile-report-error)
(lambda (error-info &optional fill &rest args)
(cl-incf num-errors)
;; Copied from \`byte-compile-report-error'.
(setq byte-compiler-error-flag t)
(byte-compile-log-warning
(if (stringp error-info) error-info
(error-message-string error-info))
fill :error))))
(byte-compile-file filename load))
(list num-errors num-warnings)))
EOF
echo "$file"
}
function elisp-check-declare-file {
# Since check-declare doesn't have a batch function that exits
# non-zero when errors are found, we make one.
local file=$(mktemp)
cat >$file <<EOF
(require 'check-declare)
(defun makem-check-declare-files-and-exit ()
"Run check-declare-files on files remaining on command line, exiting non-zero if there are warnings."
(let* ((files (mapcar #'expand-file-name command-line-args-left))
(errors (apply #'check-declare-files files)))
(when errors
(with-current-buffer check-declare-warning-buffer
(print (buffer-string)))
(kill-emacs 1))))
EOF
echo $file
}
function elisp-lint-indent-file {
# This function prints warnings for indentation errors and exits
# non-zero when errors are found.
local file=$(mktemp)
cat >"$file" <<EOF
(require 'cl-lib)
(defun makem-lint-indent-batch-and-exit ()
"Print warnings for files which are not indented properly, then exit.
Exits non-zero if mis-indented lines are found. Checks files in
'command-line-args-left'."
(let ((errors-p))
(cl-labels ((lint-file (file)
(find-file file)
(let ((inhibit-message t))
(indent-region (point-min) (point-max)))
(when buffer-undo-list
;; Indentation changed: warn for each line.
(dolist (line (undo-lines buffer-undo-list))
(message "%s:%s: Indentation mismatch" (buffer-name) line))
(setf errors-p t)))
(undo-pos (entry)
(cl-typecase (car entry)
(number (car entry))
(string (abs (cdr entry)))))
(undo-lines (undo-list)
;; Return list of lines changed in UNDO-LIST.
(nreverse (cl-loop for elt in undo-list
for pos = (undo-pos elt)
when pos
collect (line-number-at-pos pos)))))
(mapc #'lint-file (mapcar #'expand-file-name command-line-args-left))
(when errors-p
(kill-emacs 1)))))
EOF
echo "$file"
}
function elisp-package-initialize-file {
local file=$(mktemp)
cat >$file <<EOF
(require 'package)
(setq package-archives (list (cons "gnu" "https://elpa.gnu.org/packages/")
(cons "melpa" "https://melpa.org/packages/")
(cons "melpa-stable" "https://stable.melpa.org/packages/")))
$elisp_org_package_archive
(package-initialize)
EOF
echo $file
}
# ** Emacs
function run_emacs {
# NOTE: The sandbox args need to come before the package
# initialization so Emacs will use the sandbox's packages.
local emacs_command=(
"${emacs_command[@]}"
-Q
--eval "(setq load-prefer-newer t)"
"${args_debug[@]}"
"${args_sandbox[@]}"
-l $package_initialize_file
$arg_batch
"${args_load_paths[@]}"
)
# Show debug message with load-path from inside Emacs.
[[ $debug_load_path ]] \
&& debug $("${emacs_command[@]}" \
--batch \
--eval "(message \"LOAD-PATH: %s\" load-path)" \
2>&1)
# Set output file.
output_file=$(mktemp) || die "Unable to make output file."
paths_temp+=("$output_file")
# Run Emacs.
debug "run_emacs: ${emacs_command[@]} $@ &>\"$output_file\""
"${emacs_command[@]}" "$@" &>"$output_file"
# Check exit code and output.
exit=$?
[[ $exit != 0 ]] \
&& debug "Emacs exited non-zero: $exit"
[[ $verbose -gt 1 || $exit != 0 ]] \
&& cat $output_file
return $exit
}
# ** Compilation
function batch-byte-compile {
debug "batch-byte-compile: ERROR-ON-WARN:$compile_error_on_warn"
[[ $compile_error_on_warn ]] && local error_on_warn=(--eval "(setq byte-compile-error-on-warn t)")
run_emacs \
--load "$(elisp-byte-compile-file)" \
"${error_on_warn[@]}" \
--eval "(unless (makem-batch-byte-compile) (kill-emacs 1))" \
"$@"
}
function byte-compile-file {
debug "byte-compile: ERROR-ON-WARN:$compile_error_on_warn"
local file="$1"
[[ $compile_error_on_warn ]] && local error_on_warn=(--eval "(setq byte-compile-error-on-warn t)")
# FIXME: Why is the line starting with "&& verbose 3" not indented properly? Emacs insists on indenting it back a level.
run_emacs \
--load "$(elisp-byte-compile-file)" \
"${error_on_warn[@]}" \
--eval "(pcase-let ((\`(,num-errors ,num-warnings) (makem-byte-compile-file \"$file\"))) (when (or (and byte-compile-error-on-warn (not (zerop num-warnings))) (not (zerop num-errors))) (kill-emacs 1)))" \
&& verbose 3 "Compiling $file finished without errors." \
|| { verbose 3 "Compiling file failed: $file"; return 1; }
}
# ** Files
function dirs-project {
# Echo list of directories to be used in load path.
files-project-feature | dirnames
files-project-test | dirnames
}
function files-project-elisp {
# Echo list of Elisp files in project.
git ls-files 2>/dev/null |
grep -E "\.el$" |
filter-files-exclude-default |
filter-files-exclude-args
}
function files-project-feature {
# Echo list of Elisp files that are not tests and provide a feature.
files-project-elisp |
grep -E -v "$test_files_regexp" |
filter-files-feature
}
function files-project-test {
# Echo list of Elisp test files.
files-project-elisp | grep -E "$test_files_regexp"
}
function dirnames {
# Echo directory names for files on STDIN.
while read file
do
dirname "$file"
done
}
function filter-files-exclude-default {
# Filter out paths (STDIN) which should be excluded by default.
grep -E -v "(/\.cask/|-autoloads.el|.dir-locals)"
}
function filter-files-exclude-args {
# Filter out paths (STDIN) which are excluded with --exclude.
if [[ ${files_exclude[@]} ]]
then
(
# We use a subshell to set IFS temporarily so we can send
# the list of files to grep -F. This is ugly but more
# correct than replacing spaces with line breaks. Note
# that, for some reason, using IFS="\n" or IFS='\n' doesn't
# work, and a literal line break seems to be required.
IFS="
"
grep -Fv "${files_exclude[*]}"
)
else
cat
fi
}
function filter-files-feature {
# Read paths on STDIN and echo ones that (provide 'a-feature).
while read path; do
grep -E "^\\(provide '" "$path" &>/dev/null &&
echo "$path"
done
}
function args-load-files {
# For file in $@, echo "--load $file".
for file in "$@"; do
sans_extension=${file%%.el}
printf -- '--load %q ' "$sans_extension"
done
}
function args-load-path {
# Echo load-path arguments.
for path in $(dirs-project | sort -u)
do
printf -- '-L %q ' "$path"
done
}
function test-files-p {
# Return 0 if $files_project_test is non-empty.
[[ "${files_project_test[@]}" ]]
}
function buttercup-tests-p {
# Return 0 if Buttercup tests are found.
test-files-p || die "No tests found."
debug "Checking for Buttercup tests..."
grep "(require 'buttercup)" "${files_project_test[@]}" &>/dev/null
}
function ert-tests-p {
# Return 0 if ERT tests are found.
test-files-p || die "No tests found."
debug "Checking for ERT tests..."
# We check for this rather than "(require 'ert)", because ERT may
# already be loaded in Emacs and might not be loaded with
# "require" in a test file.
grep "(ert-deftest" "${files_project_test[@]}" &>/dev/null
}
function package-main-file {
# Echo the package's main file.
file_pkg=$(git ls-files ./*-pkg.el 2>/dev/null)
if [[ $file_pkg ]]
then
# Use *-pkg.el file if it exists.
echo "$file_pkg"
else
# Use shortest filename (a sloppy heuristic that will do for now).
for file in "${files_project_feature[@]}"
do
echo ${#file} "$file"
done \
| sort -h \
| head -n1 \
| sed -r 's/^[[:digit:]]+ //'
fi
}
function dependencies {
# Echo list of package dependencies.
# Search package headers. Use -a so grep won't think that an Elisp file containing
# control characters (rare, but sometimes necessary) is binary and refuse to search it.
grep -E -a -i '^;; Package-Requires: ' $(files-project-feature) $(files-project-test) |
grep -E -o '\([^([:space:]][^)]*\)' |
grep -E -o '^[^[:space:])]+' |
sed -r 's/\(//g' |
grep -E -v '^emacs$' # Ignore Emacs version requirement.
# Search Cask file.
if [[ -r Cask ]]; then
grep -E '\(depends-on "[^"]+"' Cask |
sed -r -e 's/\(depends-on "([^"]+)".*/\1/g'
fi
# Search -pkg.el file.
if [[ $(git ls-files ./*-pkg.el 2>/dev/null) ]]
then
sed -nr 's/.*\(([-[:alnum:]]+)[[:blank:]]+"[.[:digit:]]+"\).*/\1/p' $(git ls-files ./*-pkg.el 2>/dev/null)
fi
}
# ** Sandbox
function sandbox {
verbose 2 "Initializing sandbox..."
# *** Sandbox arguments
# MAYBE: Optionally use branch-specific sandbox?
# Check or make user-emacs-directory.
if [[ $sandbox_dir ]]
then
# Directory given as argument: ensure it exists.
if ! [[ -d $sandbox_dir ]]
then
debug "Making sandbox directory: $sandbox_dir"
mkdir -p "$sandbox_dir" || die "Unable to make sandbox dir."
fi
# Add Emacs version-specific subdirectory, creating if necessary.
sandbox_dir="$sandbox_dir/$(emacs-version)"
if ! [[ -d $sandbox_dir ]]
then
mkdir "$sandbox_dir" || die "Unable to make sandbox subdir: $sandbox_dir"
fi
else
# Not given: make temp directory, and delete it on exit.
local sandbox_dir=$(mktemp -d) || die "Unable to make sandbox dir."
paths_temp+=("$sandbox_dir")
fi
# Make argument to load init file if it exists.
init_file="$sandbox_dir/init.el"
# Set sandbox args. This is a global variable used by the run_emacs function.
args_sandbox=(
--title "makem.sh: $(basename $(pwd)) (sandbox: $sandbox_dir)"
--eval "(setq user-emacs-directory (file-truename \"$sandbox_dir\"))"
--load package
--eval "(setq package-user-dir (expand-file-name \"elpa\" user-emacs-directory))"
--eval "(setq user-init-file (file-truename \"$init_file\"))"
)
# Add package-install arguments for dependencies.
if [[ $install_deps ]]
then
local deps=($(dependencies))
debug "Installing dependencies: ${deps[@]}"
for package in "${deps[@]}"
do
args_sandbox_package_install+=(--eval "(package-install '$package)")
done
fi
# Add package-install arguments for linters.
if [[ $install_linters ]]
then
debug "Installing linters: package-lint relint"
args_sandbox_package_install+=(
--eval "(package-install 'elsa)"
--eval "(package-install 'package-lint)"
--eval "(package-install 'relint)")
fi
# *** Install packages into sandbox
if [[ ${args_sandbox_package_install[@]} ]]
then
# Initialize the sandbox (installs packages once rather than for every rule).
verbose 1 "Installing packages into sandbox..."
run_emacs \
--eval "(package-refresh-contents)" \
"${args_sandbox_package_install[@]}" \
&& success "Packages installed." \
|| die "Unable to initialize sandbox."
fi
verbose 2 "Sandbox initialized."
}
# ** Utility
function cleanup {
# Remove temporary paths (${paths_temp[@]}).
for path in "${paths_temp[@]}"
do
if [[ $debug ]]
then
debug "Debugging enabled: not deleting temporary path: $path"
elif [[ -r $path ]]
then
rm -rf "$path"
else
debug "Temporary path doesn't exist, not deleting: $path"
fi
done
}
function echo-unset-p {
# Echo 0 if $1 is set, otherwise 1. IOW, this returns the exit
# code of [[ $1 ]] as STDOUT.
[[ $1 ]]
echo $?
}
function ensure-package-available {
# If package $1 is available, return 0. Otherwise, return 1, and
# if $2 is set, give error otherwise verbose. Outputting messages
# here avoids repetition in callers.
local package=$1
local direct_p=$2
if ! run_emacs --load $package &>/dev/null
then
if [[ $direct_p ]]
then
error "$package not available."
else
verbose 2 "$package not available."
fi
return 1
fi
}
function ensure-tests-available {
# If tests of type $1 (like "ERT") are available, return 0. Otherwise, if
# $2 is set, give an error and return 1; otherwise give verbose message. $1
# should have a corresponding predicate command, like ert-tests-p for ERT.
local test_name=$1
local test_command="${test_name,,}-tests-p" # Converts name to lowercase.
local direct_p=$2
if ! $test_command
then
if [[ $direct_p ]]
then
error "$test_name tests not found."
else
verbose 2 "$test_name tests not found."
fi
return 1
fi
}
function echo_color {
# This allows bold, italic, etc. without needing a function for
# each variation.
local color_code="COLOR_$1"
shift
if [[ $color ]]
then
echo -e "${!color_code}${@}${COLOR_off}"
else
echo "$@"
fi
}
function debug {
if [[ $debug ]]
then
function debug {
echo_color yellow "DEBUG ($(ts)): $@" >&2
}
debug "$@"
else
function debug {
true
}
fi
}
function error {
echo_color red "ERROR ($(ts)): $@" >&2
((errors++))
return 1
}
function die {
[[ $@ ]] && error "$@"
exit $errors
}
function log {
echo "LOG ($(ts)): $@" >&2
}
function log_color {
local color_name=$1
shift
echo_color $color_name "LOG ($(ts)): $@" >&2
}
function success {
if [[ $verbose -ge 2 ]]
then
log_color green "$@" >&2
fi
}
function verbose {
# $1 is the verbosity level, rest are echoed when appropriate.
if [[ $verbose -ge $1 ]]
then
[[ $1 -eq 1 ]] && local color_name=blue
[[ $1 -eq 2 ]] && local color_name=cyan
[[ $1 -ge 3 ]] && local color_name=white
shift
log_color $color_name "$@" >&2
fi
}
function ts {
date "+%Y-%m-%d %H:%M:%S"
}
function emacs-version {
# Echo Emacs version number.
# Don't use run_emacs function, which does more than we need.
"${emacs_command[@]}" -Q --batch --eval "(princ emacs-version)" \
|| die "Unable to get Emacs version."
}
function rule-p {
# Return 0 if $1 is a rule.
[[ $1 =~ ^(lint-?|tests?)$ ]] \
|| [[ $1 =~ ^(batch|interactive)$ ]] \
|| [[ $(type -t "$2" 2>/dev/null) =~ function ]]
}
# * Rules
# These functions are intended to be called as rules, like a Makefile.
# Some rules test $1 to determine whether the rule is being called
# directly or from a meta-rule; if directly, an error is given if the
# rule can't be run, otherwise it's skipped.
function all {
verbose 1 "Running all rules..."
lint
tests
}
function compile-batch {
[[ $compile ]] || return 0
unset compile # Only compile once.
verbose 1 "Compiling..."
verbose 2 "Batch-compiling files..."
debug "Byte-compile files: ${files_project_byte_compile[@]}"
batch-byte-compile "${files_project_byte_compile[@]}"
}
function compile-each {
[[ $compile ]] || return 0
unset compile # Only compile once.
verbose 1 "Compiling..."
debug "Byte-compile files: ${files_project_byte_compile[@]}"
local compile_errors
for file in "${files_project_byte_compile[@]}"
do
verbose 2 "Compiling file: $file..."
byte-compile-file "$file" \
|| compile_errors=t
done
[[ ! $compile_errors ]]
}
function compile {
if [[ $compile = batch ]]
then
compile-batch "$@"
else
compile-each "$@"
fi
local status=$?
if [[ $compile_error_on_warn ]]
then
# Linting: just return status code, because lint rule will print messages.
[[ $status = 0 ]]
else
# Not linting: print messages here.
[[ $status = 0 ]] \
&& success "Compiling finished without errors." \
|| error "Compiling failed."
fi
}
function batch {
# Run Emacs in batch mode with ${args_batch_interactive[@]} and
# with project source and test files loaded.
verbose 1 "Executing Emacs with arguments: ${args_batch_interactive[@]}"
run_emacs \
$(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}") \
"${args_batch_interactive[@]}"
}
function interactive {
# Run Emacs interactively. Most useful with --sandbox and --install-deps.
local load_file_args=$(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}")
verbose 1 "Running Emacs interactively..."
verbose 2 "Loading files: ${load_file_args//--load /}"
[[ $compile ]] && compile
unset arg_batch
run_emacs \
$load_file_args \
--eval "(load user-init-file)" \
"${args_batch_interactive[@]}"
arg_batch="--batch"
}
function lint {
verbose 1 "Linting..."
lint-checkdoc
lint-compile
lint-declare
# NOTE: Elint doesn't seem very useful at the moment. See comment
# in lint-elint function.
# lint-elint
lint-indent
lint-package
lint-regexps
}
function lint-checkdoc {
verbose 1 "Linting checkdoc..."
local checkdoc_file="$(elisp-checkdoc-file)"
paths_temp+=("$checkdoc_file")
run_emacs \
--load="$checkdoc_file" \
"${files_project_feature[@]}" \
&& success "Linting checkdoc finished without errors." \
|| error "Linting checkdoc failed."
}
function lint-compile {
verbose 1 "Linting compilation..."
compile_error_on_warn=true
compile "${files_project_byte_compile[@]}" \
&& success "Linting compilation finished without errors." \
|| error "Linting compilation failed."
unset compile_error_on_warn
}
function lint-declare {
verbose 1 "Linting declarations..."
local check_declare_file="$(elisp-check-declare-file)"
paths_temp+=("$check_declare_file")
run_emacs \
--load "$check_declare_file" \
-f makem-check-declare-files-and-exit \
"${files_project_feature[@]}" \
&& success "Linting declarations finished without errors." \
|| error "Linting declarations failed."
}
function lint-elsa {
verbose 1 "Linting with Elsa..."
# MAYBE: Install Elsa here rather than in sandbox init, to avoid installing
# it when not needed. However, we should be careful to be clear about when
# packages are installed, because installing them does execute code.
run_emacs \
--load elsa \
-f elsa-run-files-and-exit \
"${files_project_feature[@]}" \
&& success "Linting with Elsa finished without errors." \
|| error "Linting with Elsa failed."
}
function lint-elint {
# NOTE: Elint gives a lot of spurious warnings, apparently because it doesn't load files
# that are `require'd, so its output isn't very useful. But in case it's improved in
# the future, and since this wrapper code already works, we might as well leave it in.
verbose 1 "Linting with Elint..."
local errors=0
for file in "${files_project_feature[@]}"
do
verbose 2 "Linting with Elint: $file..."
run_emacs \
--load "$(elisp-elint-file)" \
--eval "(makem-elint-file \"$file\")" \
&& verbose 3 "Linting with Elint found no errors." \
|| { error "Linting with Elint failed: $file"; ((errors++)) ; }
done
[[ $errors = 0 ]] \
&& success "Linting with Elint finished without errors." \
|| error "Linting with Elint failed."
}
function lint-indent {
verbose 1 "Linting indentation..."
# We load project source files as well, because they may contain
# macros with (declare (indent)) rules which must be loaded to set
# indentation.
run_emacs \
--load "$(elisp-lint-indent-file)" \
$(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}") \
--funcall makem-lint-indent-batch-and-exit \
"${files_project_feature[@]}" "${files_project_test[@]}" \
&& success "Linting indentation finished without errors." \
|| error "Linting indentation failed."
}
function lint-package {
ensure-package-available package-lint $1 || return $(echo-unset-p $1)
verbose 1 "Linting package..."
run_emacs \
--load package-lint \
--eval "(setq package-lint-main-file \"$(package-main-file)\")" \
--funcall package-lint-batch-and-exit \
"${files_project_feature[@]}" \
&& success "Linting package finished without errors." \
|| error "Linting package failed."
}
function lint-regexps {
ensure-package-available relint $1 || return $(echo-unset-p $1)
verbose 1 "Linting regexps..."
run_emacs \
--load relint \
--funcall relint-batch \
"${files_project_source[@]}" \
&& success "Linting regexps finished without errors." \
|| error "Linting regexps failed."
}
function tests {
verbose 1 "Running all tests..."
test-ert
test-buttercup
}
function test-ert-interactive {
verbose 1 "Running ERT tests interactively..."
unset arg_batch
run_emacs \
$(args-load-files "${files_project_test[@]}") \
--eval "(ert-run-tests-interactively t)"
arg_batch="--batch"
}
function test-buttercup {
ensure-tests-available Buttercup $1 || return $(echo-unset-p $1)