-
Notifications
You must be signed in to change notification settings - Fork 148
/
cask-cli.el
388 lines (304 loc) · 12.2 KB
/
cask-cli.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
;;; cask-cli.el --- Cask: CLI interface -*- lexical-binding: t; -*-
;; Copyright (C) 2012-2014 Johan Andersson
;; Author: Johan Andersson <[email protected]>
;; Maintainer: Johan Andersson <[email protected]>
;; URL: http://github.com/cask/cask
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; CLI interface of Cask.
;;; Code:
(eval-and-compile
(defconst cask-directory
(expand-file-name
(file-name-directory
(or load-file-name byte-compile-current-file buffer-file-name)))
"Path to Cask root."))
(require 'cask-bootstrap (expand-file-name "cask-bootstrap" cask-directory))
(require 'cask (expand-file-name "cask" cask-directory))
(when noninteractive
(shut-up-silence-emacs))
(defconst cask-cli--table-padding 10
"Number of spaces to pad with when printing table.")
(defvar cask-cli--silent nil
"If Cask should suppress logging.")
(defvar cask-cli--path default-directory
"Cask commands will execute in this path.")
(defvar cask-cli--bundle-cache nil)
(defun cask-cli--bundle ()
"Setup in `cask-cli--path' and return bundle."
(or cask-cli--bundle-cache
(setq cask-cli--bundle-cache (cask-setup cask-cli--path))))
(defun cask-cli--print-dependency (dependency)
(let ((name (cask-dependency-name dependency))
(version (cask-dependency-version dependency)))
(princ
(if version
(format " - %s (%s)" name version)
(format " - %s" name)))
(princ "\n")))
(defun cask-cli--print-upgrade (upgrade)
(princ
(format
"%s %s -> %s\n"
(epl-package-name (epl-upgrade-installed upgrade))
(epl-package-version-string (epl-upgrade-installed upgrade))
(epl-package-version-string (epl-upgrade-available upgrade)))))
(defun cask-cli--print-table (table)
"Print TABLE, which is a list of alist's."
(let* ((lengths (mapcar #'length (mapcar #'car table)))
(max (when lengths (apply #'max lengths))))
(when max
(dolist (row table)
(let ((key (car row)) (value (cadr row)))
(princ (s-pad-right (+ max cask-cli--table-padding) " " key))
(princ (concat value "\n")))))))
;;;; Commands
(defmacro cask-cli/with-handled-errors (&rest body)
"Evaluate BODY and handle errors accordingly."
(declare (indent 0))
`(condition-case err
(progn ,@body)
(cask-missing-dependencies
(let ((missing-dependencies (cdr err)))
(error "Some dependencies were not available: %s"
(s-join
", "
(mapcar
#'symbol-name
(mapcar #'cask-dependency-name missing-dependencies))))))
(cask-failed-initialization
(let* ((data (cdr err))
(message (error-message-string (nth 0 data)))
(output (nth 1 data)))
(error "Package initialization failed: %s\nOutput:\n%s"
message output)))
(cask-failed-installation
(let* ((data (cdr err))
(message (nth 0 data))
(output (nth 1 data)))
(error "Package installation failed: %s\nOutput:\n%s"
message output)))))
(defun cask-cli/pkg-file ()
"Write a `define-package' file.
The file is written to the Cask project root path with name
{project-name}-pkg.el."
(f-write-text (cask-define-package-string (cask-cli--bundle)) 'utf-8
(cask-define-package-file (cask-cli--bundle))))
(defun cask-cli/install ()
"Install all packages specified in the Cask-file.
The dependencies to packages are also installed. If a package
already is installed, it will not be installed again."
(cask-cli/with-handled-errors
(cask-install (cask-cli--bundle))))
(defun cask-cli/upgrade-cask ()
"Upgrade Cask itself and its dependencies.
This command requires that Cask is installed using Git and that
Git is available in `exec-path'."
(unless (f-exists? (f-expand ".no-upgrade" cask-directory))
(unwind-protect
(progn
(epl-change-package-dir cask-bootstrap-dir)
(epl-initialize)
(epl-add-archive "gnu" "https://elpa.gnu.org/packages/")
(epl-add-archive "melpa" "https://melpa.org/packages/")
(epl-refresh)
(epl-upgrade))
(epl-reset))
(require 'git)
(let ((git-repo cask-directory))
(if (s-present? (git-run "status" "--porcelain"))
(error "Cannot update Cask because of dirty tree")
(git-pull)))))
(defun cask-cli/exec (&rest _args)
"Execute ARGS with correct `exec-path' and `load-path'.")
(defun cask-cli/update ()
"Update package versions.
All packages that are specified in the Cask-file will be updated
including their dependencies."
(cask-cli/with-handled-errors
(let ((upgrades (cask-update (cask-cli--bundle))))
(when upgrades
(princ "Updated packages:\n")
(mapc #'cask-cli--print-upgrade upgrades)))))
(defun cask-cli/list ()
"List this package dependencies."
(cask-cli/with-handled-errors
(cask-list (cask-cli--bundle))))
(defun cask-cli/version ()
"Print version for the current project."
(princ (concat (cask-package-version (cask-cli--bundle)) "\n")))
(defun cask-cli/info ()
"Show info about the current package."
(let ((name (cask-package-name (cask-cli--bundle)))
(version (cask-package-version (cask-cli--bundle)))
(description (cask-package-description (cask-cli--bundle))))
(princ (format "### %s (%s) ###" name version))
(princ "\n\n")
(princ description)
(princ "\n")))
(defun cask-cli/help (&optional command-name)
"Display usage information or documentation for COMMAND-NAME."
(if command-name
(commander-print-usage-for-and-exit command-name)
(commander-print-usage-and-exit)))
(defun cask-cli/load-path ()
"Print `load-path' for all packages and dependencies.
The output is formatted as a colon path."
(princ (concat (s-join path-separator (cask-load-path (cask-cli--bundle))) "\n")))
(defun cask-cli/exec-path ()
"Print `exec-path' for all packages and dependencies.
A dependency will be included in this list of the package has a
directory called bin in the root directory.
The output is formatted as a colon path."
(princ (concat (s-join path-separator (cask-exec-path (cask-cli--bundle))) "\n")))
(defmacro cask-cli--with-package-path (&rest body)
"Execute BODY with `load-path' set according to the project."
(declare (debug t))
`(let ((load-path
(cask-load-path (cask-cli--bundle))))
(add-to-list 'load-path
cask-cli--path)
,@body))
(defun cask-cli/eval (form)
"Eval FORM with the `load-path' set according to the project."
(cask-cli--with-package-path
(eval (read form))))
(defun cask-cli/package-directory ()
"Print current package installation directory."
(princ (concat (cask-elpa-path (cask-cli--bundle)) "\n")))
(defun cask-cli/outdated ()
"Print list of outdated packages.
That is packages that have a more recent version available for
installation."
(let ((outdated (cask-outdated (cask-cli--bundle))))
(when outdated
(princ "Outdated packages:\n")
(mapc #'cask-cli--print-upgrade outdated))))
(defun cask-cli/files ()
"Print list of files specified in the files directive.
If no files directive or no files, do nothing."
(dolist (file (cask-files (cask-cli--bundle)))
(princ (concat file "\n"))))
(defun cask-cli/build ()
"Build all Elisp files in the files directive."
(cask-cli/with-handled-errors
(cask-build (cask-cli--bundle))))
(defun cask-cli/clean-elc ()
"Remove all byte compiled Elisp files in the files directive."
(cask-clean-elc (cask-cli--bundle)))
(defun cask-cli/link (&optional command-or-name arg)
"Manage links.
A link is just that, a symbolic link. The purpose of the link
command is that you should be able to work with local
dependencies.
For example, let's say you are developing an Emacs package that
depends on f.el. Consider what happens if you need to extend f.el
with some function that your package requires.
With the link command, you can checkout f.el locally, add it as a
link in your local package. That means that when you require
f.el, you will require the local package instead of the one
fetched from the ELPA mirror. Now you add the desired function
to f.el and use your library to try it out.
COMMAND-OR-NAME can be one of: delete, list or a link name.
ARG is sent to some of the commands.
Commands:
$ cask link list
List all project links.
$ cask link name path
Add local link with NAME to PATH.
$ cask link delete name
Delete local link with NAME."
(cond ((and (stringp arg) (string= command-or-name "delete"))
(cask-link-delete (cask-cli--bundle) (intern arg)))
((string= command-or-name "list")
(cask-cli--print-table
(cask-links (cask-cli--bundle))))
((and (stringp arg) (stringp command-or-name))
(cask-link (cask-cli--bundle) (intern command-or-name) arg))
(t
(cask-cli/help "link"))))
(defun cask-cli/package (&optional target-dir)
"Build package and put in TARGET-DIR or dist if not specified."
(cask-package (cask-cli--bundle) target-dir))
(defun cask-cli/emacs ()
"Execute emacs with the appropriate environment.")
;;;; Options
(require 'url)
(defun cask-cli/cask-proxy (host)
"Set Emacs proxy for HTTP and HTTPS to HOST."
(cask-cli/cask-http-proxy host)
(cask-cli/cask-https-proxy host))
(defun cask-cli/cask-http-proxy (host)
"Set Emacs proxy for HTTP to HOST."
(push (cons "http" host) url-proxy-services))
(defun cask-cli/cask-https-proxy (host)
"Set Emacs proxy for HTTPS to HOST."
(push (cons "https" host) url-proxy-services))
(defun cask-cli/cask-no-proxy (host)
"Set Emacs no-proxy to HOST."
(push (cons "no_proxy" host) url-proxy-services))
(defun cask-cli/cask-version ()
"Print Cask's version."
(princ (concat (cask-version) "\n"))
(kill-emacs 0))
(defun cask-cli/set-path (path)
"Run command in this PATH instead of in `default-directory'."
(setq cask-cli--path path))
(defun cask-cli/debug ()
"Turn on debug output."
(setq debug-on-error t))
(defun cask-cli/verbose ()
"Be verbose and show debug output."
(setq shut-up-ignore t))
(defun cask-cli/silent ()
"Be silent and do not print anything."
(setq cask-cli--silent t))
;;;; Commander schedule
(commander
(name "cask")
(description "Emacs dependency management made easy")
(default "install")
(command "pkg-file" cask-cli/pkg-file)
(command "install" cask-cli/install)
(command "update" cask-cli/update)
(command "upgrade" cask-cli/upgrade-cask)
(command "upgrade-cask" cask-cli/upgrade-cask)
(command "exec [*]" cask-cli/exec)
(command "version" cask-cli/version)
(command "list" cask-cli/list)
(command "info" cask-cli/info)
(command "help [command]" cask-cli/help)
(command "load-path" cask-cli/load-path)
(command "exec-path" cask-cli/exec-path)
(command "eval <form>" cask-cli/eval)
(command "path" cask-cli/exec-path)
(command "package-directory" cask-cli/package-directory)
(command "outdated" cask-cli/outdated)
(command "files" cask-cli/files)
(command "build" cask-cli/build)
(command "clean-elc" cask-cli/clean-elc)
(command "link [*]" cask-cli/link)
(command "package [target-dir]" cask-cli/package)
(command "emacs [*]" cask-cli/emacs)
(option "--proxy <host>" cask-cli/cask-proxy)
(option "--http-proxy <host>" cask-cli/cask-http-proxy)
(option "--https-proxy <host>" cask-cli/cask-https-proxy)
(option "--no-proxy <host>" cask-cli/cask-no-proxy)
(option "--version" cask-cli/cask-version)
(option "-h [command], --help [command]" cask-cli/help)
(option "--debug" cask-cli/debug)
(option "--path <path>" cask-cli/set-path)
(option "--verbose" cask-cli/verbose)
(option "--silent" cask-cli/silent))
(provide 'cask-cli)
;;; cask-cli.el ends here