Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 148 lines (127 sloc) 5.737 kb
78f3adf @chrisdone Simple interface for reading to Cabal packages.
chrisdone authored
1 ;;; haskell-package.el — Interface for working with Cabal packages.
2
3 ;; Copyright (C) 2011 Chris Done
4
5 ;; Author: Chris Done <chrisdone@gmail.com>
6
7 ;; This file is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3, or (at your option)
10 ;; any later version.
11
12 ;; This file is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
21
22 ;;; Commentary:
23
24 ;;; Todo:
25
26 ;;; Code:
27
28 ;;; Test data:
29
30 (defun haskell-package-conf-path-get (&optional project)
31 "Gets the user conf or the cabal-dev conf. Get the global conf elsewhere."
32 (if haskell-config-use-cabal-dev
33 (if project
34 (let* ((cabal-path (haskell-project-cabal-dir project)))
35 (format "%scabal-dev/packages-%s.conf/"
36 (if (string-match "/$" cabal-path)
37 cabal-path
38 (concat cabal-path "/"))
39 (haskell-ghc-version)))
40 (haskell-package-conf-user-path-get))
41 (haskell-package-conf-user-path-get)))
42
43 (defun haskell-package-conf-user-path-get ()
44 "Get the user conf path."
45 (let ((out (shell-command-to-string "ghc-pkg --user list no-results-please")))
46 (string-match "\n\\(.*\\):\n" out) (match-string 1 out)))
47
48 (defun haskell-package-conf-global-path-get ()
49 "Get the global conf path."
50 (let ((out (shell-command-to-string "ghc-pkg --global list no-results-please")))
51 (string-match "\n\\(.*\\):\n" out) (match-string 1 out)))
52
53 (defun haskell-package-get-all (conf)
54 "Get all package descriptions for the given `conf'."
55 (let ((all (shell-command-to-string (format "ghc-pkg -f %s dump" conf))))
56 (mapcar (lambda (text)
57 (haskell-package-parse text))
58 (split-string all "\n---\n"))))
59
60 (defun haskell-package-get (conf name version)
61 "Get a package description for the given `name' and `version' in the given `conf'."
62 (haskell-package-parse
63 (shell-command-to-string
64 (format "ghc-pkg -f %s describe %s-%s"
65 conf
66 name
67 version))))
68
69 (defun haskell-package-parse (text)
70 "Parse a package into a package object."
71 (let ((pkg (haskell-package-read-description text)))
72 (haskell-package-make
73 :name (cdr (assoc "name" pkg))
74 :version (cdr (assoc "version" pkg))
75 :id (cdr (assoc "id" pkg))
76 :license (cdr (assoc "license" pkg))
77 :copyright (cdr (assoc "copyright" pkg))
78 :maintainer (cdr (assoc "maintainer" pkg))
79 :stability (cdr (assoc "stability" pkg))
80 :homepage (cdr (assoc "homepage" pkg))
81 :package-url (cdr (assoc "package-url" pkg))
82 :description (cdr (assoc "description" pkg))
83 :categories (cdr (assoc "category" pkg))
84 :authors (cdr (assoc "author" pkg))
85 :is-exposed (equal "True" (cdr (assoc "exposed" pkg)))
86 :exposed-modules (split-string (or (cdr (assoc "exposed-modules" pkg)) "")
87 "[\n ]")
88 :hidden-modules (split-string (or (cdr (assoc "hidden-modules" pkg)) "")
89 "[\n ]")
90 :imports-dirs (cdr (assoc "imports-dirs" pkg))
91 :library-dirs (cdr (assoc "library-dirs" pkg))
92 :haskell-libraries (cdr (assoc "haskell-libraries" pkg))
93 :extra-libraries (cdr (assoc "extra-libraries" pkg))
94 :extra-ghci-libraries (cdr (assoc "extra-ghci-libraries" pkg))
95 :include-dirs (cdr (assoc "include-dirs" pkg))
96 :includes (cdr (assoc "includes" pkg))
97 :depends (cdr (assoc "depends" pkg))
98 :hugs-options (cdr (assoc "hugs-options" pkg))
99 :cc-options (cdr (assoc "cc-options" pkg))
100 :ld-options (cdr (assoc "ld-options" pkg))
101 :framework-dirs (cdr (assoc "framework-dirs" pkg))
102 :frameworks (cdr (assoc "frameworks" pkg))
103 :haddock-interfaces (cdr (assoc "haddock-interfaces" pkg))
104 :haddock-html (cdr (assoc "haddock-html" pkg)))))
105
106 (defun haskell-package-read-description (text)
107 "Return an association list of key-values from a pkg description string."
108 (let* ((marked (replace-regexp-in-string
109 "\n\\([^ ]\\)"
110 (lambda (match)
111 (concat "\n:" (substring match 1)))
112 text))
113 (alist (mapcar 'haskell-package-key-value
114 (split-string marked "\n:"))))
115 alist))
116
117 (defun haskell-package-key-value (entry)
118 "Return a (key . value) pair from an entry."
119 (let ((key-values (split-string entry ": ")))
120 (if (listp key-values)
121 (cons (car key-values)
122 (replace-regexp-in-string
123 "\n[ ]*"
124 " "
125 (mapconcat 'identity (cdr key-values) ": ")))
126 key-values)))
127
128 (defun haskell-package-list-get (conf)
129 "Get the list of packages in the given config."
130 (haskell-package-list-parse
131 (shell-command-to-string
132 (format "ghc-pkg -f %s list"
133 conf))))
134
135 (defun haskell-package-list-parse (text)
136 "Parse the list of installed packges."
137 (let* ((lines (split-string text "\n ")))
138 (mapcar
139 (lambda (line)
140 (string-match "^{?\\([a-zA-Z0-9-_]+\\)-\\([0-9.]+\\)}?$" line)
141 (cons (match-string 1 line) (match-string 2 line)))
142 (delete-if
143 (lambda (line)
144 (not (string-match "^{?[a-zA-Z0-9-_]+-[0-9.]+}?$" line)))
145 lines))))
146
147 (provide 'haskell-package)
Something went wrong with that request. Please try again.