]> code.delx.au - gnu-emacs-elpa/blob - admin/archive-contents.el
e94093a70c950c8afeb13a7ad5006136de967853
[gnu-emacs-elpa] / admin / archive-contents.el
1 ;;; archive-contents.el --- Auto-generate an Emacs Lisp package archive.
2
3 ;; Copyright (C) 2011 Free Software Foundation, Inc
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6
7 ;; This program 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 of the License, or
10 ;; (at your option) any later version.
11
12 ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; Commentary:
21
22 ;;; Code:
23
24 (require 'lisp-mnt)
25
26 (defconst archive-contents-subdirectory-regexp
27 "\\([^.].*?\\)-\\([0-9]+\\(?:[.][0-9]+\\|\\(?:pre\\|beta\\|alpha\\)[0-9]+\\)*\\)")
28
29 (defconst archive-re-no-dot "\\`\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"
30 "Regular expression matching all files except \".\" and \"..\".")
31
32 (defun archive--convert-require (elt)
33 (list (car elt)
34 (version-to-list (car (cdr elt)))))
35
36 (defun archive--strip-rcs-id (str)
37 "Strip RCS version ID from the version string STR.
38 If the result looks like a dotted numeric version, return it.
39 Otherwise return nil."
40 (when str
41 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
42 (setq str (substring str (match-end 0))))
43 (condition-case nil
44 (if (version-to-list str)
45 str)
46 (error nil))))
47
48 (defun archive--delete-elc-files (dir)
49 "Recursively delete all .elc files in DIR.
50 Delete backup files also."
51 (dolist (f (directory-files dir t archive-re-no-dot))
52 (cond ((file-directory-p f)
53 (archive--delete-elc-files f))
54 ((or (string-match "\\.elc\\'" f)
55 (backup-file-name-p f))
56 (delete-file f)))))
57
58 (defun batch-make-archive ()
59 "Process package content directories and generate the archive-contents file."
60 (let ((packages '(1))) ; format-version.
61 (dolist (dir (directory-files default-directory nil archive-re-no-dot))
62 (condition-case v
63 (if (not (file-directory-p dir))
64 (error "Skipping non-package file %s" dir)
65 (let* ((pkg (file-name-nondirectory dir))
66 (autoloads-file (expand-file-name (concat pkg "-autoloads.el") dir))
67 simple-p)
68 ;; Omit autoloads and .elc files from the package.
69 (if (file-exists-p autoloads-file)
70 (delete-file autoloads-file))
71 (archive--delete-elc-files dir)
72 ;; Test whether this is a simple or multi-file package.
73 (setq simple-p (archive--simple-package-p dir pkg))
74 (push (if simple-p
75 (apply 'archive--process-simple-package
76 dir pkg simple-p)
77 (archive--process-multi-file-package dir pkg))
78 packages)))
79 ;; Error handler
80 (error (message "%s" (cadr v)))))
81 (with-temp-buffer
82 (pp (nreverse packages) (current-buffer))
83 (write-region nil nil "archive-contents"))))
84
85 (defun archive--simple-package-p (dir pkg)
86 "Test whether DIR contains a simple package named PKG.
87 If so, return a list (VERSION DESCRIPTION REQ COMMENTARY), where
88 VERSION is the version string of the simple package, DESCRIPTION
89 is the brief description of the package, REQ is a list of
90 requirements, and COMMENTARY is the package commentary.
91 Otherwise, return nil."
92 (let* ((pkg-file (expand-file-name (concat pkg "-pkg.el") dir))
93 (mainfile (expand-file-name (concat pkg ".el") dir))
94 version description req commentary)
95 (when (and (or (not (file-exists-p pkg-file))
96 (= (length (directory-files dir nil archive-re-no-dot)) 2))
97 (file-exists-p mainfile))
98 (with-temp-buffer
99 (insert-file-contents mainfile)
100 (goto-char (point-min))
101 (and (looking-at ";;;.*---[ \t]*\\(.*\\)\\(-\\*-.*-\\*-[ \t]*\\)?$")
102 (progn
103 (setq description (match-string 1))
104 (setq version
105 (or (archive--strip-rcs-id (lm-header "package-version"))
106 (archive--strip-rcs-id (lm-header "version")))))
107 (progn
108 ;; Grab the other fields, which are not mandatory.
109 (let ((requires-str (lm-header "package-requires")))
110 (if requires-str
111 (setq req (mapcar 'archive--convert-require
112 (car (read-from-string requires-str))))))
113 (setq commentary (lm-commentary))
114 (list version description req commentary)))))))
115
116 (defun archive--process-simple-package (dir pkg vers desc req commentary)
117 "Deploy the contents of DIR into the archive as a simple package.
118 Rename DIR/PKG.el to PKG-VERS.el, delete DIR, and write the
119 package commentary to PKG-readme.txt. Return the descriptor."
120 ;; Write the readme file.
121 (with-temp-buffer
122 (erase-buffer)
123 (emacs-lisp-mode)
124 (insert (or commentary
125 (prog1 "No description"
126 (message "Missing commentary in package %s" pkg))))
127 (goto-char (point-min))
128 (while (looking-at ";*[ \t]*\\(commentary[: \t]*\\)?\n")
129 (delete-region (match-beginning 0)
130 (match-end 0)))
131 (uncomment-region (point-min) (point-max))
132 (goto-char (point-max))
133 (while (progn (forward-line -1)
134 (looking-at "[ \t]*\n"))
135 (delete-region (match-beginning 0)
136 (match-end 0)))
137 (write-region nil nil (concat pkg "-readme.txt")))
138 ;; Write DIR/foo.el to foo-VERS.el and delete DIR
139 (rename-file (expand-file-name (concat pkg ".el") dir)
140 (concat pkg "-" vers ".el"))
141 (delete-directory dir t)
142 (cons (intern pkg) (vector (version-to-list vers) req desc 'single)))
143
144 (defun archive--process-multi-file-package (dir pkg)
145 "Deploy the contents of DIR into the archive as a multi-file package.
146 Rename DIR/ to PKG-VERS/, and write the package commentary to
147 PKG-readme.txt. Return the descriptor."
148 (let* ((exp (archive--multi-file-package-def dir pkg))
149 (vers (nth 2 exp))
150 (req (mapcar 'archive--convert-require (nth 4 exp)))
151 (readme (expand-file-name "README" dir)))
152 (unless (equal (nth 1 exp) pkg)
153 (error (format "Package name %s doesn't match file name %s"
154 (nth 1 exp) pkg)))
155 ;; Write the readme file.
156 (when (file-exists-p readme)
157 (copy-file readme (concat pkg "-readme.txt") 'ok-if-already-exists))
158 (rename-file dir (concat pkg "-" vers))
159 (cons (intern pkg) (vector (version-to-list vers) req (nth 3 exp) 'tar))))
160
161 (defun archive--multi-file-package-def (dir pkg)
162 "Reurn the `define-package' form in the file DIR/PKG-pkg.el."
163 (let ((pkg-file (expand-file-name (concat pkg "-pkg.el") dir)))
164 (with-temp-buffer
165 (unless (file-exists-p pkg-file)
166 (error "File not found: %s" pkg-file))
167 (insert-file-contents pkg-file)
168 (goto-char (point-min))
169 (read (current-buffer)))))
170
171 (defun batch-make-site-dir (package-dir site-dir)
172 (require 'package)
173 (setq package-dir (expand-file-name package-dir default-directory))
174 (setq site-dir (expand-file-name site-dir default-directory))
175 (dolist (dir (directory-files package-dir t archive-re-no-dot))
176 (condition-case v
177 (if (not (file-directory-p dir))
178 (error "Skipping non-package file %s" dir)
179 (let* ((pkg (file-name-nondirectory dir))
180 (autoloads-file (expand-file-name (concat pkg "-autoloads.el") dir))
181 simple-p version)
182 ;; Omit autoloads and .elc files from the package.
183 (if (file-exists-p autoloads-file)
184 (delete-file autoloads-file))
185 (archive--delete-elc-files dir)
186 ;; Test whether this is a simple or multi-file package.
187 (setq simple-p (archive--simple-package-p dir pkg))
188 (if simple-p
189 (progn
190 (apply 'archive--write-pkg-file dir pkg simple-p)
191 (setq version (car simple-p)))
192 (setq version
193 (nth 2 (archive--multi-file-package-def dir pkg))))
194 (make-symbolic-link (expand-file-name dir package-dir)
195 (expand-file-name (concat pkg "-" version)
196 site-dir)
197 t)
198 (package-generate-autoloads pkg dir)
199 (let ((load-path (cons dir load-path)))
200 (byte-recompile-directory dir 0 t))))
201 ;; Error handler
202 (error (message "%s" (cadr v))))))
203
204 (defun archive--write-pkg-file (pkg-dir name version desc requires &rest ignored)
205 (let ((pkg-file (expand-file-name (concat name "-pkg.el") pkg-dir))
206 (print-level nil)
207 (print-length nil))
208 (write-region
209 (concat (format ";; Generated package description from %s.el\n"
210 name)
211 (prin1-to-string
212 (list 'define-package
213 name
214 version
215 desc
216 (list 'quote
217 ;; Turn version lists into string form.
218 (mapcar
219 (lambda (elt)
220 (list (car elt)
221 (package-version-join (cadr elt))))
222 requires))))
223 "\n")
224 nil
225 pkg-file)))
226
227
228 ;; Local Variables:
229 ;; no-byte-compile: t
230 ;; lexical-binding: t
231 ;; End:
232
233 (provide 'archive-contents)
234 ;;; archive-contents.el ends here