forked from emacs-eask/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.el
More file actions
178 lines (157 loc) · 6.17 KB
/
analyze.el
File metadata and controls
178 lines (157 loc) · 6.17 KB
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
;;; core/analyze.el --- Run eask checker -*- lexical-binding: t; -*-
;;; Commentary:
;;
;; Commmand use to run Eask checker
;;
;; $ eask analyze [FILES..]
;;
;;
;; Positionals:
;;
;; [files..] specify Eask-files for checker to lint
;;
;; Optional arguments:
;;
;; --json Output lint result in JSON format
;;
;;; Code:
(let ((dir (file-name-directory (nth 1 (member "-scriptload" command-line-args)))))
(load (expand-file-name "_prepare.el"
(locate-dominating-file dir "_prepare.el"))
nil t))
;; Plain Text
(defvar eask-analyze--log nil)
;; JSON format
(defvar eask-analyze--warnings nil)
(defvar eask-analyze--errors nil)
;; Warning flag
(defvar eask-analyze--warning-p nil)
;; Error flag
(defvar eask-analyze--error-p nil)
(defun eask-analyze--pretty-json (json)
"Return pretty JSON."
(with-temp-buffer (insert json) (json-pretty-print-buffer) (buffer-string)))
(defun eask-analyze--load-buffer ()
"Return the current file loading session."
(car (cl-remove-if-not (lambda (elm)
(string-prefix-p " *load*-" (buffer-name elm)))
(buffer-list))))
(defun eask-analyze--write-json-format (level msg)
"Prepare log for JSON format.
For arguments LEVEL and MSG, please see function `eask-analyze--write-log' for more
information."
(let* ((bounds (bounds-of-thing-at-point 'sexp))
(filename (or load-file-name eask-file))
(start (car bounds))
(end (cdr bounds))
(start-line (if load-file-name (line-number-at-pos start) 0))
(start-col (if load-file-name (eask--column-at-point start) 0))
(start-pos (if load-file-name start 0))
(end-line (if load-file-name (line-number-at-pos end) 0))
(end-col (if load-file-name (eask--column-at-point end) 0))
(end-pos (if load-file-name end 0))
(msg (ansi-color-filter-apply msg)))
(push `((range . ((start . ((line . ,start-line)
(col . ,start-col)
(pos . ,start-pos)))
(end . ((line . ,end-line)
(col . ,end-col)
(pos . ,end-pos)))))
(filename . ,filename)
(message . ,msg))
(cl-case level
(`error eask-analyze--errors)
(`warn eask-analyze--warnings)))))
(defun eask-analyze--write-plain-text (level msg)
"Prepare log for plain text format.
For arguments LEVEL and MSG, please see function `eask-analyze--write-log' for more
information."
(let* ((level-string (cl-case level
(`error "Error")
(`warn "Warning")))
(log (format "%s:%s:%s %s: %s"
(or load-file-name eask-file)
(if load-file-name (line-number-at-pos) 0)
(if load-file-name (current-column) 0)
level-string
msg)))
(push (ansi-color-filter-apply log) eask-analyze--log)))
(defun eask-analyze--write-log (level msg)
"Write the log.
Argument LEVEL and MSG are data from the debug log signal."
(unless (string= " *temp*" (buffer-name)) ; avoid error from `package-file' directive
(when (eq 'error level)
(setq eask-analyze--error-p t))
(when (eq 'warn level)
(setq eask-analyze--warning-p t))
(with-current-buffer (or (eask-analyze--load-buffer) (buffer-name))
(funcall
(cond ((eask-json-p) #'eask-analyze--write-json-format)
(t #'eask-analyze--write-plain-text))
level msg))))
(defun eask-analyze--file (files)
"Lint list of Eask FILES."
(let (checked-files content)
;; Linting
(dolist (file files)
(eask--silent-error
(eask--save-load-eask-file file
(push file checked-files)
;; also count files with errors in the total count
(push file checked-files))))
;; Print result
(eask-msg "")
(cond ((eask-json-p) ; JSON format
;; Fill content with result.
(when (or eask-analyze--warnings eask-analyze--errors)
(setq content
(eask-analyze--pretty-json (json-encode
`((warnings . ,eask-analyze--warnings)
(errors . ,eask-analyze--errors))))))
;; XXX: When printing the result, no color allow.
(eask--with-no-color
(eask-msg (or content "{}"))))
(eask-analyze--log ; Plain text
(setq content
(with-temp-buffer
(dolist (msg (reverse eask-analyze--log))
(insert msg "\n"))
(buffer-string)))
;; XXX: When printing the result, no color allow.
(eask--with-no-color
(mapc #'eask-msg (reverse eask-analyze--log)))))
(eask-info "(Checked %s file%s)"
(length checked-files)
(eask--sinr checked-files "" "s"))
;; Output file
(when (and content (eask-output))
(write-region content nil (eask-output)))))
;;
;;; Program Entry
;; Preparation
(add-hook 'eask-on-error-hook #'eask-analyze--write-log)
(add-hook 'eask-on-warning-hook #'eask-analyze--write-log)
(let* ((default-directory (cond ((eask-global-p) eask-homedir)
((eask-config-p) user-emacs-directory)
(t default-directory)))
(patterns (eask-args))
(files (if patterns
(eask-expand-file-specs patterns)
(eask-expand-file-specs '("Eask*" "**/Eask*")))))
(cond
;; Files found, do the action!
(files
(eask-analyze--file files)
(when (or eask-analyze--error-p
;; strict flag turns warnings into errors
(and eask-analyze--warning-p (eask-strict-p)))
(eask--exit 'failure)))
;; Pattern defined, but no file found!
(patterns
(eask-info "(No files match wildcard: %s)"
(mapconcat #'identity patterns " ")))
;; Default, print help!
(t
(eask-info "(No Eask-files have been checked)")
(eask-help "core/analyze"))))
;;; core/analyze.el ends here