-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathesy.el
149 lines (125 loc) · 5.6 KB
/
esy.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
;;; esy.el --- library to interact with esy CLI. -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Manas Jayanth
;; Author: Manas Jayanth <[email protected]>
;; Created: 11 November 2024
;; Keywords: Reason, OCaml
;; Package-Requires: ((emacs "25.1") (transient "0.3.7.50"))
;; Package-Version: 20230415
;; Homepage: http://example.com/foo
;;; Commentary:
;;; Change Log: TODO
;;; Code:
(require 'transient)
(defun run-cmd (buffer-name cmd-and-args callback)
(let ((compilation-buffer
(compilation-start (string-join cmd-and-args " ") 'compilation-mode)))
(if (get-buffer buffer-name) nil (with-current-buffer compilation-buffer (rename-buffer buffer-name)))))
(defun run-esy (args callback)
"Runs esy command in *esy* buffer"
(let ((command (if args (push esy-command args) (list esy-command))))
(run-cmd
"*esy*"
command
(lambda ()
(with-current-buffer
"*esy*"
; (make-local-variable 'compilation-directory-matcher) ; Buffer local not working :(
(setq
compilation-directory-matcher
'("^\s+\\(# esy-build-package: pwd: \\| esy-build-package: exiting with errors above\\)\\([^\n]+\\)$" (2 . nil))))))))
(defun propertized-flag (flag-value)
"Given a boolean value, it turns into a human readable 'yes' | 'no' with appropriate faces"
(propertize (if flag-value "yes" "no") 'face (if flag-value '(:foreground "green") '(:foreground "red"))))
(defun esy-status ()
"Show status (parsed from 'esy status') of the current buffer"
(interactive)
(esy/macro--with-esy-project
(current-buffer)
project
(let* ((esy-status (plist-get project 'json))
(manifest (esy/status--get-manifest-file-path esy-status))
(manifest-propertized (propertize manifest 'face 'bold))
(is-project-propertized (propertized-flag (esy/status--project-p esy-status)))
(is-solved-propertized (propertized-flag (esy/status--dependency-constraints-solved-p esy-status)))
(is-fetched-propertized (propertized-flag (esy/status--dependencies-installed-p esy-status)))
(is-ready-for-dev-propertized (propertized-flag (esy/status--ready-for-dev-p esy-status))))
(message
"manifest: %s valid-project: %s solved: %s dependencies-fetched: %s ready-for-dev: %s"
manifest-propertized is-project-propertized is-solved-propertized is-fetched-propertized is-ready-for-dev-propertized))))
(defun esy-pesy ()
"Run esy pesy"
(interactive)
(run-esy (list "pesy") (lambda () (message "[esy] Ran esy pesy"))))
(defun esy/cmd-install (&optional args)
"Run esy install"
(interactive (list (transient-args 'esy-install)))
(run-esy (append '("install") args) (lambda () (message "[esy] Installed"))))
(defun esy/cmd-build (&optional args)
"Run esy build"
(interactive (list (transient-args 'esy-build)))
(run-esy (append '("build") args) (lambda () (message "[esy] Built"))))
(defun esy-build-and-install ()
"Run esy"
(interactive)
(run-esy '() (lambda () (message "[esy] Build and install done"))))
(defun esy-npm-release ()
"Run esy npm-release"
(interactive)
(run-esy (list "npm-release") (lambda () (message "[esy] NPM release done"))))
(transient-define-prefix esy-install ()
"Open esy install transient menu pop up."
["Arguments"
("-p" "Package name providing the ocaml compiler" "--ocaml-pkg-name=")
("-v" "OCaml compiler version" "--ocaml-version=")
("-rl" "Local path to opam repository" "--opam-repository-local=")
("-rr" "HTTP url to remote opam repository" "--opam-repository-remote=")
("-ol" "Local path to opam override repository. For more info, see (TODO document this at esy.sh)" "--opam-override-repository-local=")
("-or" "HTTP url to remote opam override repository. For more info, see (TODO document this at esy.sh)" "--opam-override-repository-remote=")
]
[["Command"
("i" "Install" esy/cmd-install)]])
(transient-define-prefix esy-build ()
"Open esy build transient menu pop up."
["Arguments"
("-p" "Package name providing the ocaml compiler" "--ocaml-pkg-name=")
("-v" " OCaml compiler version" "--ocaml-version=")
]
[["Command"
("b" "Build" esy/cmd-build)]])
(defun esy-test ()
"Run esy test"
(interactive)
(run-esy (list "test") (lambda () (message "[esy] done"))))
;; TODO: minibuffer completions
;; It must autocomplete to only one of entries from
;; "scripts" field in package.json/esy.json
;; (defun esy-run-script (command)
;; "Run esy run-script. See https://esy.sh/docs/en/configuration.html#scripts"
;; (interactive)
;; (run-esy (list command) (lambda () (message "[esy] done"))))
;; Entrypoint menu
(transient-define-prefix esy-menu ()
"Open esy transient menu pop up."
[["Command"
("e" "Build and install" esy-build-and-install)
("s" "Status" esy-status)
("b" "Build" esy-build)
("i" "Install" esy-install)
("n" "Run npm-release" esy-npm-release)
("t" "Test" esy-test)
]])
(defun esy-init (project-directory)
"Run esy"
(interactive "sProject Directory: ")
(run-cmd "*esy-init*" (list "pesy" "-d" project-directory) (lambda () (message "[esy-init] Finished"))))
;;;###autoload
(defun esy ()
"Entrypoint function to the esy-mode interactive functions
First checks if file backing the current buffer is a part of an esy project, then opens the menu. Else, recommends initialising a new project"
(interactive)
(let ((project (esy/project--of-buffer (current-buffer))))
(if project
(call-interactively #'esy-menu)
(if (y-or-n-p "You are not in an esy project, would you like to initialize one? ")
(call-interactively #'esy-init)))))
(provide 'esy)