]> code.delx.au - gnu-emacs-elpa/blob - admin/archive-contents.el
Add coffee-mode.
[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 &optional only-orphans)
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 (and (string-match "\\.elc\\'" f)
55 (not (and only-orphans
56 (file-readable-p (replace-match ".el" t t f)))))
57 (backup-file-name-p f))
58 (delete-file f)))))
59
60 (defun batch-make-archive ()
61 "Process package content directories and generate the archive-contents file."
62 (let ((packages '(1))) ; format-version.
63 (dolist (dir (directory-files default-directory nil archive-re-no-dot))
64 (condition-case v
65 (if (not (file-directory-p dir))
66 (error "Skipping non-package file %s" dir)
67 (let* ((pkg (file-name-nondirectory dir))
68 (autoloads-file (expand-file-name (concat pkg "-autoloads.el") dir))
69 simple-p)
70 ;; Omit autoloads and .elc files from the package.
71 (if (file-exists-p autoloads-file)
72 (delete-file autoloads-file))
73 (archive--delete-elc-files dir)
74 ;; Test whether this is a simple or multi-file package.
75 (setq simple-p (archive--simple-package-p dir pkg))
76 (push (if simple-p
77 (apply 'archive--process-simple-package
78 dir pkg simple-p)
79 (archive--process-multi-file-package dir pkg))
80 packages)))
81 ;; Error handler
82 (error (message "%s" (cadr v)))))
83 (with-temp-buffer
84 (pp (nreverse packages) (current-buffer))
85 (write-region nil nil "archive-contents"))))
86
87 (defun archive--simple-package-p (dir pkg)
88 "Test whether DIR contains a simple package named PKG.
89 If so, return a list (VERSION DESCRIPTION REQ COMMENTARY), where
90 VERSION is the version string of the simple package, DESCRIPTION
91 is the brief description of the package, REQ is a list of
92 requirements, and COMMENTARY is the package commentary.
93 Otherwise, return nil."
94 (let* ((pkg-file (expand-file-name (concat pkg "-pkg.el") dir))
95 (mainfile (expand-file-name (concat pkg ".el") dir))
96 (files (directory-files dir nil archive-re-no-dot))
97 version description req commentary)
98 (dolist (file (prog1 files (setq files ())))
99 (unless (string-match "\\.elc\\'" file)
100 (push file files)))
101 (when (and (or (not (file-exists-p pkg-file))
102 (= (length files) 2))
103 (file-exists-p mainfile))
104 (with-temp-buffer
105 (insert-file-contents mainfile)
106 (goto-char (point-min))
107 (and (looking-at ";;;.*---[ \t]*\\(.*\\)\\(-\\*-.*-\\*-[ \t]*\\)?$")
108 (progn
109 (setq description (match-string 1))
110 (setq version
111 (or (archive--strip-rcs-id (lm-header "package-version"))
112 (archive--strip-rcs-id (lm-header "version"))
113 "0.0")))
114 (progn
115 ;; Grab the other fields, which are not mandatory.
116 (let ((requires-str (lm-header "package-requires")))
117 (if requires-str
118 (setq req (mapcar 'archive--convert-require
119 (car (read-from-string requires-str))))))
120 (setq commentary (lm-commentary))
121 (list version description req commentary)))))))
122
123 (defun archive--process-simple-package (dir pkg vers desc req commentary)
124 "Deploy the contents of DIR into the archive as a simple package.
125 Rename DIR/PKG.el to PKG-VERS.el, delete DIR, and write the
126 package commentary to PKG-readme.txt. Return the descriptor."
127 ;; Write the readme file.
128 (with-temp-buffer
129 (erase-buffer)
130 (emacs-lisp-mode)
131 (insert (or commentary
132 (prog1 "No description"
133 (message "Missing commentary in package %s" pkg))))
134 (goto-char (point-min))
135 (while (looking-at ";*[ \t]*\\(commentary[: \t]*\\)?\n")
136 (delete-region (match-beginning 0)
137 (match-end 0)))
138 (uncomment-region (point-min) (point-max))
139 (goto-char (point-max))
140 (while (progn (forward-line -1)
141 (looking-at "[ \t]*\n"))
142 (delete-region (match-beginning 0)
143 (match-end 0)))
144 (write-region nil nil (concat pkg "-readme.txt")))
145 ;; Write DIR/foo.el to foo-VERS.el and delete DIR
146 (rename-file (expand-file-name (concat pkg ".el") dir)
147 (concat pkg "-" vers ".el"))
148 (delete-directory dir t)
149 (cons (intern pkg) (vector (version-to-list vers) req desc 'single)))
150
151 (defun archive--make-changelog (dir)
152 "Export Bzr log info of DIR into a ChangeLog file."
153 (let ((default-directory (file-name-as-directory (expand-file-name dir))))
154 (call-process "bzr" nil '(:file "ChangeLog") nil
155 "log" "--gnu-changelog" ".")))
156
157 (defun archive--process-multi-file-package (dir pkg)
158 "Deploy the contents of DIR into the archive as a multi-file package.
159 Rename DIR/ to PKG-VERS/, and write the package commentary to
160 PKG-readme.txt. Return the descriptor."
161 (let* ((exp (archive--multi-file-package-def dir pkg))
162 (vers (nth 2 exp))
163 (req (mapcar 'archive--convert-require (nth 4 exp)))
164 (readme (expand-file-name "README" dir)))
165 (archive--make-changelog dir)
166 (unless (equal (nth 1 exp) pkg)
167 (error (format "Package name %s doesn't match file name %s"
168 (nth 1 exp) pkg)))
169 ;; Write the readme file.
170 (when (file-exists-p readme)
171 (copy-file readme (concat pkg "-readme.txt") 'ok-if-already-exists))
172 (rename-file dir (concat pkg "-" vers))
173 (cons (intern pkg) (vector (version-to-list vers) req (nth 3 exp) 'tar))))
174
175 (defun archive--multi-file-package-def (dir pkg)
176 "Reurn the `define-package' form in the file DIR/PKG-pkg.el."
177 (let ((pkg-file (expand-file-name (concat pkg "-pkg.el") dir)))
178 (with-temp-buffer
179 (unless (file-exists-p pkg-file)
180 (error "File not found: %s" pkg-file))
181 (insert-file-contents pkg-file)
182 (goto-char (point-min))
183 (read (current-buffer)))))
184
185 (defun batch-make-site-dir (package-dir site-dir)
186 (require 'package)
187 (setq package-dir (expand-file-name package-dir default-directory))
188 (setq site-dir (expand-file-name site-dir default-directory))
189 (dolist (dir (directory-files package-dir t archive-re-no-dot))
190 (condition-case v
191 (if (not (file-directory-p dir))
192 (error "Skipping non-package file %s" dir)
193 (let* ((pkg (file-name-nondirectory dir))
194 (autoloads-file (expand-file-name
195 (concat pkg "-autoloads.el") dir))
196 simple-p version)
197 ;; Omit autoloads and .elc files from the package.
198 (if (file-exists-p autoloads-file)
199 (delete-file autoloads-file))
200 (archive--delete-elc-files dir 'only-orphans)
201 ;; Test whether this is a simple or multi-file package.
202 (setq simple-p (archive--simple-package-p dir pkg))
203 (if simple-p
204 (progn
205 (apply 'archive--write-pkg-file dir pkg simple-p)
206 (setq version (car simple-p)))
207 (setq version
208 (nth 2 (archive--multi-file-package-def dir pkg))))
209 (make-symbolic-link (expand-file-name dir package-dir)
210 (expand-file-name (concat pkg "-" version)
211 site-dir)
212 t)
213 (let ((make-backup-files nil))
214 (package-generate-autoloads pkg dir))
215 (let ((load-path (cons dir load-path)))
216 ;; FIXME: Don't compile the -pkg.el files!
217 (byte-recompile-directory dir 0))))
218 ;; Error handler
219 (error (message "%s" (cadr v))))))
220
221 (defun archive--write-pkg-file (pkg-dir name version desc requires &rest ignored)
222 (let ((pkg-file (expand-file-name (concat name "-pkg.el") pkg-dir))
223 (print-level nil)
224 (print-length nil))
225 (write-region
226 (concat (format ";; Generated package description from %s.el\n"
227 name)
228 (prin1-to-string
229 (list 'define-package
230 name
231 version
232 desc
233 (list 'quote
234 ;; Turn version lists into string form.
235 (mapcar
236 (lambda (elt)
237 (list (car elt)
238 (package-version-join (cadr elt))))
239 requires))))
240 "\n")
241 nil
242 pkg-file)))
243
244
245 ;; Local Variables:
246 ;; no-byte-compile: t
247 ;; lexical-binding: t
248 ;; End:
249
250 (provide 'archive-contents)
251 ;;; archive-contents.el ends here