-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.lisp
executable file
·69 lines (60 loc) · 2.18 KB
/
readme.lisp
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
#!/usr/local/bin/sbcl --script
(load (merge-pathnames #P"quicklisp/setup.lisp" (user-homedir-pathname)))
(defun readme-file-p (path)
(check-type path pathname)
(equal (pathname-name path) "README"))
(defun file-encoding (file)
(string-right-trim
'(#\Newline)
(uiop:run-program
`("file" "-b" "--mime-encoding" ,(uiop:native-namestring file))
:output :string)))
(defun supported-encoding-p (encoding)
(when (stringp encoding)
(handler-case
(and (uiop:run-program
(format nil "iconv -l | grep -i '^~A//$'"
encoding)
:output :string)
t)
(uiop:subprocess-error ()
nil))))
(defun file-size (file)
(with-open-file (in file)
(file-length in)))
(defun read-file-in-utf-8 (file)
(let ((size (file-size file)))
(when (= size 0)
(return-from read-file-in-utf-8 "")))
(let ((encoding (file-encoding file)))
(uiop:run-program `("iconv"
,@(when (supported-encoding-p encoding)
`("-f" ,encoding))
"-t" "utf-8"
"-c"
,(uiop:native-namestring file))
:ignore-error-status t
:output :string
:error-output *error-output*)))
(defun main ()
(destructuring-bind ($0 &optional name &rest args)
sb-ext:*posix-argv*
(declare (ignore $0 args))
(when (null name)
(error "At least one argument is required"))
(let ((release (ql-dist:find-release name)))
(unless release
(error "No release named '~A' found" name))
(let ((release-dir (ql-dist:base-directory release)))
(format t "~&~S~%"
`(("name" . ,name)
("readme_files" .
,(or (mapcar
(lambda (file)
`(("filename" . ,(file-namestring file))
("content" . ,(read-file-in-utf-8 file))))
(remove-if-not
#'readme-file-p
(uiop:directory-files release-dir)))
#()))))))))
(main)