-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransient-compile.el
1129 lines (996 loc) · 47.6 KB
/
transient-compile.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
;;; transient-compile.el --- Dynamic transient menu for compilation -*- lexical-binding: t -*-
;; Copyright (C) 2025 Victor Gaydov and contributors
;; Author: Victor Gaydov <victor@enise.org>
;; Created: 26 Jan 2025
;; URL: https://github.com/gavv/transient-compile
;;; 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/>.
;;; Commentary:
;; transient-compile implements configurable, automatically built transient
;; menu for selecting target and running compilation.
;; When you invoke `M-x transient-compile', it searches for known build files
;; in current directory and parents, retrieves available targets, groups targets
;; by common prefixes, and displays a menu. After you select the target, it
;; formats command and passes it to compile or other function.
;; Please refer to README.org and docstrings for further details.
;;; Code:
(require 'cl-lib)
(require 'json)
(require 'seq)
(require 'transient)
(require 'f)
(require 's)
(defgroup transient-compile nil
"Dynamic transient menu for compilation."
:prefix "transient-compile-"
:group 'tools
:group 'processes
:link '(url-link "https://github.com/gavv/transient-compile"))
(defcustom transient-compile-function #'compile
"Function to run compilation command.
You can set it to `project-compile' if you're using `project'
or `projectile'."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type '(choice (const :tag "compile" compile)
(const :tag "project-compile" project-compile)
function))
(defcustom transient-compile-verbose nil
"Print what's happening to messages."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'boolean)
(defcustom transient-compile-tool-alist
`(
;; https://github.com/go-task/task
(task :match ,(lambda (directory)
(seq-some (lambda (f)
(string-match "^[Tt]askfile\\(\\.dist\\)?\\.ya?ml$" f))
(directory-files directory)))
:exe "task"
:chdir t
:targets transient-compile-taskfile-targets
:command transient-compile-taskfile-command)
;; https://github.com/casey/just
(just :match ,(lambda (directory)
(or (member-ignore-case "justfile" (directory-files directory))
(member-ignore-case ".justfile" (directory-files directory))))
:exe "just"
:chdir t
:targets transient-compile-justfile-targets
:command transient-compile-justfile-command)
;; https://github.com/pydoit/doit
(doit :match ("dodo.py")
:exe "doit"
:chdir t
:targets transient-compile-dodofile-targets
:command transient-compile-dodofile-command)
;; https://github.com/ruby/rake
(rake :match ("Rakefile" "rakefile" "Rakefile.rb" "rakefile.rb")
:exe "rake"
:chdir t
:targets transient-compile-rakefile-targets
:command transient-compile-rakefile-command)
;; any POSIX-compliant make
(make :match ("GNUmakefile" "BSDmakefile" "makefile" "Makefile")
:exe "make"
:chdir t
:targets transient-compile-makefile-targets
:command transient-compile-makefile-command)
)
"Assoc list of supported tools.
Alist key is a symbol, e.g. 'make.
Alist value is a plist with the following fields:
:match - list of file names or functions for auto-detection (see below)
:exe - executable name or path
:chdir - whether to change directory when running
:targets - function to get list of targets
:command - function to format build command
When you invoke `transient-compile', it performs a search from the current
directory through the parents, until it finds a match with any of the
commands registered in `transient-compile-tool-alist'.
A command is matched if any of the elements in its `:match' list is matched:
- If an element is a string, it matches if the directory contains a file
with that name.
- If an element is a function, then the function is invoked with the
directory path, and the element matches if it returned non-nil.
`:match' can be also just a string or a function, which is equivalent to
a single-element list.
If multiple tools can be matched, the order of `transient-compile-tool-alist'
keys defines their precedence.
After a command is matched, it is used to collect targets, build the
transient menu, and run the compilation command.
The `:targets' property defines a function that takes the matched directory
path as an argument (e.g. where Makefile is located in case of `make'), and
returns the list of string names of the available targets.
The `:command' property defines a function that takes two arguments: the
matched directory and the target name. It returns a string with the command
to run. The command is then passed to `compile' (or other function, as
defined by `transient-compile-function').
`:exe' and `:chdir' properties are used by the default implementations of
the functions set in `:targets' and `:command' properties, e.g.
`transient-compile-makefile-targets' and `transient-compile-makefile-command'.
`:exe' is useful when the tool is not available in PATH or is named
differently on your system.
`:chdir' defines how to pass matched directory path to the tool:
- when t, we'll run the tool from that directory
- when nil, we'll instead pass the directory as an argument
(`:command' function should do it)
"
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'sexp)
(defvar-local transient-compile-tool nil
"Currently active compilation tool.
This variable is holding a symbol key from `transient-compile-tool-alist'
(like 'make).
Normally, `transient-compile' automatically detects tool and directory and binds
`transient-compile-tool' and `transient-compile-directory' during the call.
If desired, you can manually bind one or both of the variables before calling
`transient-compile' to force using of specific tool and/or directory.")
(defvar-local transient-compile-directory nil
"Currently active compilation directory.
This variable is holding a directory path with the tool-specific build file
(e.g. for 'make it's the directory with Makefile).
Normally, `transient-compile' automatically detects tool and directory and binds
`transient-compile-tool' and `transient-compile-directory' during the call.
If desired, you can manually bind one or both of the variables before calling
`transient-compile' to force using of specific tool and/or directory.")
(defvar-local transient-compile-target nil
"Currently active compilation target.
After the user selects target in transient menu, `transient-compile' binds this
variable to the selected target during the call to `transient-compile-function'
(In addition to `transient-compile-tool' and `transient-compile-directory').
It may be useful if you provide your own compilation function.
Setting this variable manually has no effect.")
(defcustom transient-compile-detect-function #'transient-compile-default-detect-function
"Function that detects compilation tool and directory.
Should take no arguments and return a cons, where car is the tool (symbol key
from `transient-compile-tool-alist'), and cdr is directory path.
Default implementation is based on `:match' lists defined in
`transient-compile-tool-alist' for each tool.
For most cases, it should be enough to modify `transient-compile-tool-alist' and
there is no need to redefine this function.
You can also temporary bind local variables `transient-compile-tool' and/or
`transient-compile-directory' instead of redefining this function."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'function)
(defcustom transient-compile-group-fallback "default"
"The name of the fallback group for targets without group."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'string)
(defcustom transient-compile-group-regexp "^\\(.+\\)[^[:alnum:]][[:alnum:]]+$"
"Regexp to match group name from target name.
Group name should be captured by the first parenthesized sub-expression.
Used by `transient-compile-default-group-function'."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'regexp)
(defcustom transient-compile-group-function #'transient-compile-default-group-function
"Function that takes target name and returns group name.
If it returns nil, fallback group is used (`transient-compile-group-fallback').
Default implementation uses `transient-compile-group-regexp'."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'function)
(defcustom transient-compile-split-function #'transient-compile-default-split-function
"Function that takes list of targets names and returns assoc list, where key is
group name, and value is list of target names in this group.
Default implementation uses `transient-compile-group-function' with some
reasonable heuristics.
For most customizations, it should be enough to override either
`transient-compile-group-regexp' or `transient-compile-group-function'.
Providing custom `transient-compile-split-function' is useful when you need
custom groupping logic that takes into account all available targets."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'function)
(defcustom transient-compile-sort-function #'transient-compile-default-sort-function
"Function that takes assoc list returned by `transient-compile-split-function',
and returns its sorted version.
The function is allowed to sort both groups and targets inside groups.
Default implementation sorts groups alphabetically, does not sort targets, and places
fallback group first."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'function)
(defcustom transient-compile-merge-prefix-targets t
"If non-nil, if a target doesn't have a group, and target name is a prefix
of a group name, move target into that group.
Has effect only if you're using `transient-compile-default-split-function'."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'boolean)
(defcustom transient-compile-merge-prefix-groups 1
"If non-nil, if a group has no more than specified number of targets, and there
is another group which name is the prefix of the first one, move targets into
that prefix group.
Has effect only if you're using `transient-compile-default-split-function'."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type '(choice (const :tag "Disable" nil)
(integer :tag "Threshold")))
(defcustom transient-compile-merge-dangling-groups 1
"If non-nil, if a group has no more than given number of targets, move
targets into fallback group.
Has effect only if you're using `transient-compile-default-split-function'."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type '(choice (const :tag "Disable" nil)
(integer :tag "Threshold")))
(defcustom transient-compile-keychar-highlight t
"If non-nil, highlight key characters inside group and target names with
`transient-compile-keychar' face."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'boolean)
(defcustom transient-compile-keychar-unfold t
"If non-nil, allow using upcase and downcase variants of the original
character as the key character."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'boolean)
(defcustom transient-compile-keychar-regexp "[[:alnum:]]"
"Regexp for allowed key characters.
Only those characters in group and target names, which match this regex,
can become key characters."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'regexp)
(defcustom transient-compile-keychar-function nil
"Custom function that chooses unique key character for a word.
The function should take 3 arguments:
- name - group or target name for which we choose a key
- all-names - list of all names, among which the key must be unique
- key-map - hashtable of taken keys
- group-p - whether it's group or target
The function should return character to be used as a key.
Character must not be taken by other words (other groups
or other targets in group), i.e. it must not be present
in the key-map.
The function can return nil if it doesn't have a good key.
In this case default algorithm is used for this word."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type '(choice (const :tag "Default" nil)
function))
(defface transient-compile-heading
'((t :inherit font-lock-builtin-face))
"Face used for transient menu heading.
Applied by `transient-compile-default-menu-heading-function'."
:package-version '(transient-compile . "0.1")
:group 'transient-compile)
(defface transient-compile-keychar
'((t :inherit font-lock-string-face :underline t))
"Face to highlight key character inside group or target name.
Applied if `transient-compile-keychar-highlight' is t."
:package-version '(transient-compile . "0.1")
:group 'transient-compile)
(defcustom transient-compile-menu-heading-function
#'transient-compile-default-menu-heading-function
"Function that returns menu heading.
Takes 2 arguments:
- tool - symbol key from `transient-compile-tool-alist', e.g. 'make
- directory - path to dir where command will be executed
Returns propertized string heading or nil to hide heading."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'function)
(defcustom transient-compile-menu-columns-limit nil
"If non-nil, limits maximum allowed number of menu columns.
Used by `transient-compile-default-menu-columns-function'."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type '(choice (const :tag "Unlimited" nil)
(integer :tag "Limit")))
(defcustom transient-compile-menu-columns-function
#'transient-compile-default-menu-columns-function
"Function that returns menu column count.
Takes assoc list returned by `transient-compile-split-function'.
Returns desired number of columns.
`transient-compile' will arange groups into N columns by inserting
a break after each Nth group."
:package-version '(transient-compile . "0.1")
:group 'transient-compile
:type 'function)
;;;###autoload
(defun transient-compile ()
"Open transient menu for compilation.
The following steps are performed:
- Build tool and directory is detected. See `transient-compile-tool-alist'
and `transient-compile-detect-function'.
- Available targets are collected according to the `:targets' function
of the selected tool from `transient-compile-tool-alist'.
- Targets are organized into groups. See `transient-compile-group-function',
`transient-compile-split-function', `transient-compile-sort-function' and
other related options.
- For each target, a unique key sequence is assigned. See
`transient-compile-keychar-function' and other related options.
- Transient menu is built. See `transient-compile-menu-heading-function' and
`transient-compile-menu-columns-function' for altering its appearance.
- Transient menu is opened. Now we wait until selects target using its
key sequence, or cancels operation.
- After user have selected target, compilation command is formatted using
`:command' function of the selected tool from `transient-compile-tool-alist'.
- Formatted command is padded to `compile', or `project-compile', or other
function. See `transient-compile-function'.
After that, `transient-compile' closes menu and returns, while the command
keeps running in the compilation buffer."
(interactive)
;; Detect tool and dir.
(let* ((tool-and-dir (transient-compile--tool-detect))
(tool (car tool-and-dir))
(directory (cdr tool-and-dir)))
;; Bind values during the call.
;; If user already bound these variables, we'll keep the same values.
(let ((transient-compile-tool tool)
(transient-compile-directory directory))
;; Collect data.
(let* ((targets (transient-compile--tool-targets tool directory))
(grouped-targets (funcall transient-compile-sort-function
(funcall transient-compile-split-function
targets)))
(menu-heading
(funcall transient-compile-menu-heading-function
tool directory))
(menu-columns
(funcall transient-compile-menu-columns-function
grouped-targets)))
;; Rebuild menu.
(eval `(transient-define-prefix transient-compile--menu ()
,@(transient-compile--build-grid
menu-heading
menu-columns
(transient-compile--build-menu
tool
directory
grouped-targets))))
;; Clear echo area from our own logs.
(when transient-compile-verbose
(message ""))
;; Open menu.
(transient-compile--menu)))))
(defun transient-compile-default-detect-function ()
"Default implementation of `transient-compile-detect-function'.
Detects compilation tool and directory.
E.g. for this file layout:
/foo
Makefile
/bar <- current directory
it would return (make . “/foo“)
In that cons, 'make defines compilation tool (a symbol key of the
`transient-compile-tool-alist'), and “/foo“ defines tool-specific
compilation directory.
Detection is based on `:match' lists from `transient-compile-tool-alist'."
;; If both tool and directory are forced, there is nothing to do.
(if (and transient-compile-tool
transient-compile-directory)
(cons transient-compile-tool transient-compile-directory)
;; If only one of the tool and directory is forced, it will be
;; used during matching.
(transient-compile--apply-matchers
(transient-compile--build-matchers))))
(defun transient-compile-default-group-function (target)
"Default implementation for `transient-compile-group-function'.
Matches group using `transient-compile-group-regexp'."
(when (string-match transient-compile-group-regexp target)
(match-string 1 target)))
(defun transient-compile-default-split-function (targets)
"Default implementation for `transient-compile-split-function'.
Takes list of target names and returns assoc list, where key is
group name, and value is list of target names in this group.
Default implementation uses `transient-compile-group-function' to get group
names of the targets.
It also implements heruistics enabled by variables:
- `transient-compile-merge-prefix-targets'
- `transient-compile-merge-prefix-groups'
- `transient-compile-merge-dangling-groups'"
(let (groups fallback-group)
;; Split targets into groups.
;; Fallback group is for targets without group.
(dolist (target targets)
(unless (let ((group-name (funcall transient-compile-group-function target)))
(when (and (s-present-p group-name)
(not (string= group-name transient-compile-group-fallback)))
(if-let ((group (assoc group-name groups)))
(nconc (cdr group) (list target))
(setq groups
(nconc groups (list (cons group-name (list target))))))))
(setq fallback-group
(nconc fallback-group (list target)))))
;; If there is target "foo" in fallback group, and there is group "foo"
;; or "foo_bar", move target "foo" into that group.
(when transient-compile-merge-prefix-targets
(dolist (target (seq-copy fallback-group))
;; shortest prefix
(when-let ((group (car
(seq-sort (lambda (a b)
(string< (car a) (car b)))
(seq-filter (lambda (gr)
(s-prefix-p target (car gr)))
groups)))))
(push target (cdr group))
(setq fallback-group (seq-remove (lambda (tg)
(string= tg target))
fallback-group)))))
;; If there is a small group "foo_bar", and there is group "foo", then
;; move the elements of "foo_bar" group into group "foo".
(when transient-compile-merge-prefix-groups
(while (seq-find
(lambda (group)
(let ((group-name (car group))
(group-targets (cdr group)))
(when (<= (length group-targets)
transient-compile-merge-prefix-groups)
;; shortest prefix
(when-let ((prefix-group
(car
(seq-sort (lambda (a b)
(string< (car a) (car b)))
(seq-filter
(lambda (gr)
(and (not (string= (car gr) group-name))
(s-prefix-p (car gr) group-name)))
groups)))))
(nconc (cdr prefix-group) group-targets)
(setq groups (seq-remove (lambda (gr)
(string= (car gr) group-name))
groups))))))
groups)))
;; Merge remaining groups that are too small into fallback group.
(when transient-compile-merge-dangling-groups
(dolist (group (seq-copy groups))
(let ((group-name (car group))
(group-targets (cdr group)))
(when (<= (length group-targets)
transient-compile-merge-dangling-groups)
(setq fallback-group
(nconc fallback-group group-targets))
(setq groups (seq-remove (lambda (gr)
(string= (car gr) group-name))
groups))))))
;; Return groups.
(append (when fallback-group
(list (cons transient-compile-group-fallback
fallback-group)))
groups)))
(defun transient-compile-default-sort-function (groups)
"Default implementation for `transient-compile-sort-function'.
Takes assoc list returned by `transient-compile-split-function', and returns
sorted list.
Default implementation sorts groups alphabetically and does not sort targets
inside groups. Also it always places fallback group first."
(let ((fallback-group (assoc transient-compile-group-fallback groups)))
(append (when fallback-group
(list fallback-group))
(seq-sort (lambda (a b)
(string< (car a) (car b)))
(seq-remove (lambda (gr)
(eq gr fallback-group))
groups)))))
(defun transient-compile-default-menu-heading-function (tool directory)
"Default implementation for `transient-compile-menu-heading-function'."
(propertize
(format "Choose target for tool \"%s\"" tool)
'face 'transient-compile-heading))
(defun transient-compile-default-menu-columns-function (groups)
"Default implementation for `transient-compile-menu-columns-function'."
(let* ((max-width
(max
;; longest group name
(apply 'max (seq-map 'length
(seq-map 'car groups)))
;; longest target name
(apply 'max (seq-map 'length
(seq-mapcat 'cdr groups)))))
;; how much columns we can fit
(max-columns
(max (/ (window-body-width) (+ max-width 10))
1))) ; At least 1 column.
(if (and transient-compile-menu-columns-limit
(> transient-compile-menu-columns-limit 0))
(min transient-compile-menu-columns-limit
max-columns)
max-columns)))
(defun transient-compile-taskfile-targets (directory)
"Get list of targets from a taskfile."
(when-let* ((executable (transient-compile--tool-property 'task :exe))
(command (transient-compile--shell-join
executable
(unless (transient-compile--tool-property 'task :chdir)
`("-d" , directory))
"--json"
"--list-all"))
(output (transient-compile--shell-run command))
(json (json-read-from-string output)))
(seq-map (lambda (task)
(cdr (assoc 'name task)))
(cdr (assoc 'tasks json)))))
(defun transient-compile-taskfile-command (directory target)
"Format build command for a taskfile."
(when-let* ((executable (transient-compile--tool-property 'task :exe)))
(transient-compile--shell-join
executable
(unless (transient-compile--tool-property 'task :chdir)
`("-d" , directory))
target)))
(defun transient-compile-justfile-targets (directory)
"Get list of targets from a justfile."
(when-let* ((executable (transient-compile--tool-property 'just :exe))
(justfile (seq-find (lambda (f)
(member-ignore-case f '("justfile" ".justfile")))
(directory-files directory)))
(command (transient-compile--shell-join
executable
(unless (transient-compile--tool-property 'just :chdir)
`("-d" , directory
"-f" ,(f-join directory justfile)))
"--list"))
(output (transient-compile--shell-run command))
(lines (s-lines output)))
(seq-map (lambda (line)
(car (transient-compile--shell-tokens line)))
(seq-filter (lambda (line)
(s-starts-with-p " " line))
lines))))
(defun transient-compile-justfile-command (directory target)
"Format build command for a justfile."
(when-let* ((executable (transient-compile--tool-property 'just :exe))
(justfile (seq-find (lambda (f)
(member-ignore-case f '("justfile" ".justfile")))
(directory-files directory))))
(transient-compile--shell-join
executable
(unless (transient-compile--tool-property 'just :chdir)
`("-d" , directory
"-f" ,(f-join directory justfile)))
target)))
(defun transient-compile-dodofile-targets (directory)
"Get list of targets from a dodofile."
(when-let* ((executable (transient-compile--tool-property 'doit :exe))
(command (transient-compile--shell-join
executable
"list"))
(output (transient-compile--shell-run command))
(lines (s-lines output)))
(seq-map (lambda (line)
(car (transient-compile--shell-tokens line)))
lines)))
(defun transient-compile-dodofile-command (directory target)
"Format build command for a dodofile."
(when-let* ((executable (transient-compile--tool-property 'doit :exe)))
(transient-compile--shell-join
executable
target)))
(defun transient-compile-rakefile-targets (directory)
"Get list of targets from a rakefile."
(when-let* ((executable (transient-compile--tool-property 'rake :exe))
(command (transient-compile--shell-join
executable
(unless (transient-compile--tool-property 'rake :chdir)
`("-C" , directory))
"-P"))
(output (transient-compile--shell-run command))
(lines (s-lines output)))
(seq-map (lambda (line)
(cadr (transient-compile--shell-tokens line)))
lines)))
(defun transient-compile-rakefile-command (directory target)
"Format build command for a rakefile."
(when-let* ((executable (transient-compile--tool-property 'rake :exe)))
(transient-compile--shell-join
executable
(unless (transient-compile--tool-property 'rake :chdir)
`("-C" , directory))
target)))
(defun transient-compile-makefile-targets (directory)
"Get list of targets from a makefile."
(when-let* ((executable (transient-compile--tool-property 'make :exe))
(command (transient-compile--shell-join
executable
(unless (transient-compile--tool-property 'make :chdir)
`("-C" ,directory))
"-pq"
":"))
(output (transient-compile--shell-run command)))
(let (targets skip-target)
(with-temp-buffer
(insert output)
(goto-char (point-min))
(when (re-search-forward "^# Files" nil t)
(forward-line 1)
(while (not (looking-at-p "^# Finished Make data base"))
(if (looking-at-p "^# Not a target")
(setq skip-target t)
(unless skip-target
(let ((line (buffer-substring-no-properties (line-beginning-position)
(line-end-position))))
(when (string-match
(rx bol (group (not (any "#" ":" "." whitespace))
(zero-or-more (not (any ":" whitespace))))
":")
line)
(push (match-string 1 line) targets))))
(setq skip-target nil))
(forward-line 1))))
(seq-uniq (sort targets 'string<)))))
(defun transient-compile-makefile-command (directory target)
"Format build command for a makefile."
(when-let* ((executable (transient-compile--tool-property 'make :exe)))
(transient-compile--shell-join
executable
(unless (transient-compile--tool-property 'make :chdir)
`("-C" ,directory))
target)))
(defun transient-compile--log (&rest args)
"Print log message, if enabled."
(when transient-compile-verbose
(apply 'message args)))
(defun transient-compile--shell-run (command)
"Run shell command and return stdout as string."
(transient-compile--log "Running command: %s" command)
(with-temp-buffer
(let* ((process-environment
(cons "LC_ALL=C" process-environment))
(exit-code
(process-file-shell-command command nil (current-buffer) nil)))
(buffer-string))))
(defun transient-compile--shell-quote (arg)
"Quote argument for shell."
(let ((home (expand-file-name "~/"))
(unix (not (member system-type '(windows-nt ms-dos))))
(str (format "%s" arg)))
;; Minimize quoting if possible
(cond ((s-matches-p "^[a-zA-Z0-9/:.,_-]+$" str)
(or (when (and unix
home
(s-prefix-p home str))
(replace-regexp-in-string (s-concat "^" (regexp-quote home))
"~/"
str))
str))
((and (not (s-contains-p "'" str))
(eq system-type 'gnu/linux))
(format "'%s'" str))
(t
(shell-quote-argument str)))))
(defun transient-compile--shell-join (&rest args)
"Quote and concatenate arguments into a command.
Flatten nested lists.
Skip nil arguments (but not empty strings)."
(s-join
" "
(seq-map
'transient-compile--shell-quote
(seq-remove
'not
(seq-mapcat (lambda (arg)
(if (listp arg)
arg
(list arg)))
args)))))
(defun transient-compile--shell-tokens (arg)
"Split line by whitespace."
(split-string arg "[ \t\n]+" t "[ \t\n]*"))
(defun transient-compile--tool-detect ()
"Detect tool and directory."
(if-let* ((tool-and-dir (funcall transient-compile-detect-function))
(tool (car tool-and-dir))
(directory (cdr tool-and-dir)))
(cons tool directory)
(cond (transient-compile-tool
(user-error
"No build file for '%s tool found in current directory and parents."
transient-compile-tool))
(transient-compile-directory
(user-error
"No known build file found in %s" transient-compile-directory))
(t
(user-error
"No known build file found in current directory and parents.")))))
(defun transient-compile--tool-targets (tool directory)
"Retrieve list of compile targets."
(if-let* ((targets-fn (transient-compile--tool-property tool :targets))
(targets (seq-uniq
(seq-remove 's-blank-p
(funcall targets-fn directory)))))
(progn
(transient-compile--log
"Detected %s targets for \"%s\"" (length targets) tool)
targets)
(user-error "Failed to parse list of targets for \"%s\" tool." tool)))
(defun transient-compile--tool-command (tool directory target)
"Format compile command."
(if-let* ((command-fn (transient-compile--tool-property tool :command))
(command (funcall command-fn directory target)))
command
(user-error "Failed to format command for \"%s\" tool." tool)))
(defun transient-compile--tool-property (tool property)
"Get property for command."
(if-let* ((entry (assoc tool transient-compile-tool-alist))
(entry-props (cdr entry)))
(if (plist-member entry-props property)
(plist-get entry-props property)
(user-error "Missing property %S for key '%S in %S."
property tool 'transient-compile-tool-alist))
(user-error "Missing key '%S in %S."
tool 'transient-compile-tool-alist)))
(defun transient-compile--build-matchers ()
"Build list of matchers to run for every dominating directory."
(seq-mapcat (lambda (elem)
(let* ((tool (car elem))
(match-list (plist-get (cdr elem) :match)))
;; If transient-compile-tool is forced, ignore all other tools.
(when (or (not transient-compile-tool)
(eq tool transient-compile-tool))
(seq-map (lambda (matcher)
(cons tool matcher))
(if (and (listp match-list)
(not (functionp match-list)))
match-list
(list match-list))))))
transient-compile-tool-alist))
(defun transient-compile--apply-matchers (match-list)
"Find nearest dominating directory matched by any element of the MATCH-LIST.
MATCH-LIST element may be either string file name (like “Makefile“), or
function that takes directory path and returns t or nil."
(when default-directory
;; If transient-compile-directory is forced,
;; use it instead of default-directory.
(let ((dir (or transient-compile-directory
(f-full default-directory)))
result)
(while (and dir (not result))
(setq result (seq-some
(lambda (matcher)
(when-let ((tool (car matcher))
(file-or-func (cdr matcher)))
(cond
((functionp file-or-func)
(when (funcall file-or-func dir)
(cons tool dir)))
((stringp file-or-func)
(let ((file-path (f-join dir file-or-func)))
(when (f-exists-p file-path)
(cons tool dir))))
(t
(user-error "Bad property :match for key '%S in %S."
tool 'transient-compile-tool-alist)))))
match-list))
;; If transient-compile-directory is forced,
;; don't do directory search.
(if transient-compile-directory
(setq dir nil)
(setq dir (f-parent dir))))
(when result
(transient-compile--log
"Detected \"%s\" tool for %s" (car result) (cdr result)))
result)))
(defun transient-compile--keychar-p (char)
"Check if character can be used as a key."
(string-match-p
transient-compile-keychar-regexp (string char)))
(defconst transient-compile--keychar-table
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
(defun transient-compile--random-key (word key-map)
"Generate random key for WORD, trying to return same results for same words."
(let ((counter 0)
result)
(while (not result)
(cl-incf counter)
(let* ((hash (abs (sxhash word)))
(index (mod hash (length transient-compile--keychar-table)))
(char (elt transient-compile--keychar-table index)))
(if (and (transient-compile--keychar-p char)
(not (gethash char key-map)))
;; Hit!
(setq result char)
(if (< counter (length transient-compile--keychar-table))
;; Repeat with hash of hash, and so on.
(setq word (number-to-string hash))
;; Give up.
"_"))))
result))
(defun transient-compile--propertize-key (word word-index group-p)
"Highligh key character inside word."
(if (not group-p)
;; Target.
(add-text-properties word-index (1+ word-index)
'(face transient-compile-keychar) word)
;; Group.
(when (> word-index 0)
(add-text-properties 0 word-index
'(face transient-heading) word))
(add-text-properties word-index (1+ word-index)
'(face (transient-compile-keychar transient-heading)) word)
(when (< (1+ word-index) (length word))
(add-text-properties (1+ word-index) (length word)
'(face transient-heading) word))))
(defun transient-compile--assign-keys (words group-p)
"Map words to unique keys."
(let* ((key-map (make-hash-table :test 'equal))
(shared-prefix (seq-reduce 's-shared-start
words
(car words)))
(sorted-words (seq-sort
'string< words))
(max-len (seq-max (seq-map (lambda (w) (length w))
sorted-words)))
word-keys)
(while (< (length word-keys)
(length words))
(let (word
word-index
word-key)
(unless (and transient-compile-keychar-function
(seq-find
(lambda (w)
(when-let ((key (funcall transient-compile-keychar-function
w
words
key-map
group-p)))
;; Special case: custom user-provided key.
(unless (characterp key)
(user-error
"Got non-char key %S from transient-compile-keychar-function"
key))
(when (gethash key key-map)
(user-error
"Got duplicate key %s from transient-compile-keychar-function"
(string key)))
(setq word w
word-index (seq-position word key)
word-key key)))
(seq-remove (lambda (w)
(assoc w word-keys))
sorted-words)))
(if (and (seq-contains sorted-words shared-prefix)
(not (assoc shared-prefix word-keys)))
;; Special case: word = shared prefix.
(setq word shared-prefix
word-index 0
word-key (elt word 0))
;; Normal case.
(seq-find
(lambda (prefer-first)
(seq-find
(lambda (casefn)
;; If prefer-first is true: