-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-bazel.el
99 lines (82 loc) · 3.67 KB
/
build-bazel.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
;;; bazel.el --- Build Bazel projects in Emacs -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Justin Andreas Lacoste
;; Author: Justin Andreas Lacoste <[email protected]>
;; URL: https://github.com/27justin/build.el
;; Version: 0.1
;; Keywords: compile, build-system, bazel
;;; Commentary:
;; This package implements a transient menu for the bazel build system.
;; Alongside three interactive commands.
;;; Requirements
(require 'build-api)
;;; Code
(defun build-bazel-project-p ()
(build--project-file-exists-p "BUILD"))
(defun build-bazel--get-targets (callback query)
"Call `callback' with all bazel targets that match `query'"
(let* ((buffer (get-buffer-create "*bazel-query*" t)) ;; Create a persistent buffer
(base-command (list "bazel" "query" query "--keep_going")))
(with-current-buffer buffer
(erase-buffer))
(make-process
:name "Bazel Query"
:buffer buffer
:command base-command
:filter (lambda (proc out)
;; Process each line of the output individually
(with-current-buffer (process-buffer proc)
(dolist (line (split-string out "\n" t)) ;; Split output into lines
(when (string-match "^//" line) ;; Filter out lines that do not start with "//"
(insert line "\n"))))) ;; Insert line if it matches
:sentinel (lambda (proc _)
(funcall callback (with-current-buffer (process-buffer proc)
(split-string (buffer-string) "\n" t)))))))
(defun build-bazel-build (&optional args)
"`bazel build' a target"
(interactive
(list (transient-args 'build-bazel-transient)))
(build-bazel--get-targets (lambda(targets)
(let* ((choice (funcall build--completing-read "Target: " targets)))
(funcall build--compile (format "bazel build %s %s" choice (string-join args " ")))))
"//..."))
(defun build-bazel-run (&optional args)
"`bazel run' a target"
(interactive
(list (transient-args 'build-bazel-transient)))
(build-bazel--get-targets (lambda(targets)
(let* ((choice (funcall build--completing-read "Target: " targets)))
(funcall build--compile (format "bazel run %s %s" choice (string-join args " ")))))
"kind(\".*_binary|oci_tarball|container_image|.*_deploy\", //...)"))
(defun build-bazel-test (&optional args)
"`bazel test' a target"
(interactive
(list (transient-args 'build-bazel-transient)))
(build-bazel--get-targets (lambda(targets)
(let* ((choice (funcall build--completing-read "Target: " targets)))
(funcall build--compile (format "bazel test %s %s" choice (string-join args " ")))))
"kind(\".*_test\", //...)"))
;; Bazel transient definition
(transient-define-prefix build-bazel-transient ()
"Bazel Build Commands"
:value '("-c fastbuild")
["Bazel Options\n"
["Generic"
("-s" "Sandbox debug" "--sandbox_debug")
("-c" "Profile" "-c " :choices ("fastbuild" "opt" "dbg") :always-read t :class transient-option)
("-o" "Output base" "--output_base=" :prompt "Folder: " :always-read t)
]
["Test related"
("-to" "Test output" "--test_output=" :choices ("summary" "all" "errors" "streamed") :always-read t)
("-tc" "Cache test results" "--cache_test_results=" :choices ("no" "yes") :always-read t)
]
]
[""
["Build"
("b" "Build" build-bazel-build)]
["Test"
("t" "Test" build-bazel-test)]
["Run"
("r" "Run" build-bazel-run)]
])
(add-to-list 'build--systems '(build-bazel-project-p . build-bazel-transient))
(provide 'build-bazel)