forked from haskell/haskell-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haskell-package.el
161 lines (136 loc) · 5.95 KB
/
haskell-package.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
;;; haskell-package.el --- Interface for working with Cabal packages
;; Copyright (C) 2011 Chris Done
;; Author: Chris Done <[email protected]>
;; This file is not part of GNU Emacs.
;; This file 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;; Todo:
;;; Code:
(with-no-warnings (require 'cl))
;; Dynamically scoped variables.
;; TODO What actually sets this?
(defvar haskell-config-use-cabal-dev)
;; (defun haskell-package-conf-path-get (&optional project)
;; "Gets the user conf or the cabal-dev conf. Get the global conf elsewhere."
;; (if haskell-config-use-cabal-dev
;; (if project
;; (let* ((cabal-path (haskell-project-cabal-dir project)))
;; (format "%scabal-dev/packages-%s.conf/"
;; (if (string-match "/$" cabal-path)
;; cabal-path
;; (concat cabal-path "/"))
;; (haskell-ghc-version)))
;; (haskell-package-conf-user-path-get))
;; (haskell-package-conf-user-path-get)))
(defun haskell-package-conf-user-path-get ()
"Get the user conf path."
(let ((out (shell-command-to-string "ghc-pkg --user list no-results-please")))
(string-match "\n\\(.*\\):\n" out) (match-string 1 out)))
(defun haskell-package-conf-global-path-get ()
"Get the global conf path."
(let ((out (shell-command-to-string "ghc-pkg --global list no-results-please")))
(string-match "\n\\(.*\\):\n" out) (match-string 1 out)))
(defun haskell-package-get-all (conf)
"Get all package descriptions for the given `conf'."
(let ((all (shell-command-to-string (format "ghc-pkg -f %s dump" conf))))
(mapcar (lambda (text)
(haskell-package-parse text))
(split-string all "\n---\n"))))
(defun haskell-package-get (conf name version)
"Get a package description for the given `name' and `version' in the given `conf'."
(haskell-package-parse
(shell-command-to-string
(format "ghc-pkg -f %s describe %s-%s"
conf
name
version))))
(defstruct haskell-package "Haskell package object.")
(defun haskell-package-parse (text)
"Parse a package into a package object."
(let ((pkg (haskell-package-read-description text)))
(make-haskell-package
:name (cdr (assoc "name" pkg))
:version (cdr (assoc "version" pkg))
:id (cdr (assoc "id" pkg))
:license (cdr (assoc "license" pkg))
:copyright (cdr (assoc "copyright" pkg))
:maintainer (cdr (assoc "maintainer" pkg))
:stability (cdr (assoc "stability" pkg))
:homepage (cdr (assoc "homepage" pkg))
:package-url (cdr (assoc "package-url" pkg))
:description (cdr (assoc "description" pkg))
:categories (cdr (assoc "category" pkg))
:authors (cdr (assoc "author" pkg))
:is-exposed (equal "True" (cdr (assoc "exposed" pkg)))
:exposed-modules (split-string (or (cdr (assoc "exposed-modules" pkg)) "")
"[\n ]")
:hidden-modules (split-string (or (cdr (assoc "hidden-modules" pkg)) "")
"[\n ]")
:imports-dirs (cdr (assoc "imports-dirs" pkg))
:library-dirs (cdr (assoc "library-dirs" pkg))
:haskell-libraries (cdr (assoc "haskell-libraries" pkg))
:extra-libraries (cdr (assoc "extra-libraries" pkg))
:extra-ghci-libraries (cdr (assoc "extra-ghci-libraries" pkg))
:include-dirs (cdr (assoc "include-dirs" pkg))
:includes (cdr (assoc "includes" pkg))
:depends (cdr (assoc "depends" pkg))
:hugs-options (cdr (assoc "hugs-options" pkg))
:cc-options (cdr (assoc "cc-options" pkg))
:ld-options (cdr (assoc "ld-options" pkg))
:framework-dirs (cdr (assoc "framework-dirs" pkg))
:frameworks (cdr (assoc "frameworks" pkg))
:haddock-interfaces (cdr (assoc "haddock-interfaces" pkg))
:haddock-html (cdr (assoc "haddock-html" pkg)))))
(defun haskell-package-read-description (text)
"Return an association list of key-values from a pkg description string."
(let* ((marked (replace-regexp-in-string
"\n\\([^ ]\\)"
(lambda (match)
(concat "\n:" (substring match 1)))
text))
(alist (mapcar 'haskell-package-key-value
(split-string marked "\n:"))))
alist))
(defun haskell-package-key-value (entry)
"Return a (key . value) pair from an entry."
(let ((key-values (split-string entry ": ")))
(if (listp key-values)
(cons (car key-values)
(replace-regexp-in-string
"\n[ ]*"
" "
(mapconcat 'identity (cdr key-values) ": ")))
key-values)))
(defun haskell-package-list-get (conf)
"Get the list of packages in the given config."
(haskell-package-list-parse
(shell-command-to-string
(format "ghc-pkg -f %s list"
conf))))
(defun haskell-package-list-parse (text)
"Parse the list of installed packges."
(let* ((lines (split-string text "\n ")))
(mapcar
(lambda (line)
(string-match "^{?\\([a-zA-Z0-9-_]+\\)-\\([0-9.]+\\)}?$" line)
(cons (match-string 1 line) (match-string 2 line)))
(delete-if
(lambda (line)
(not (string-match "^{?[a-zA-Z0-9-_]+-[0-9.]+}?$" line)))
lines))))
(provide 'haskell-package)
;; Local Variables:
;; byte-compile-warnings: (not cl-functions)
;; End:
;;; haskell-package.el ends here