1 ;;; archive-contents.el --- Auto-generate an Emacs Lisp package archive.
3 ;; Copyright (C) 2011 Free Software Foundation, Inc
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
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.
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.
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/>.
26 (defconst archive-contents-subdirectory-regexp
27 "\\([^.].*?\\)-\\([0-9]+\\(?:[.][0-9]+\\|\\(?:pre\\|beta\\|alpha\\)[0-9]+\\)*\\)")
29 (defconst archive-re-no-dot "\\`\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"
30 "Regular expression matching all files except \".\" and \"..\".")
32 (defun archive--convert-require (elt)
34 (version-to-list (car (cdr elt)))))
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."
41 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
42 (setq str (substring str (match-end 0))))
44 (if (version-to-list str)
48 (defun archive--delete-elc-files (dir)
49 "Recursively delete all .elc files in DIR."
50 (dolist (f (directory-files dir t archive-re-no-dot))
51 (cond ((file-directory-p f)
52 (archive--delete-elc-files f))
53 ((string-match "\\.elc\\'" f)
56 (defun batch-make-archive ()
57 "Process package content directories and generate the archive-contents file."
58 (let ((packages '(1))) ; format-version.
59 (dolist (dir (directory-files default-directory nil archive-re-no-dot))
61 (if (not (file-directory-p dir))
62 (error "Skipping non-package file %s" dir)
63 (let* ((pkg (file-name-nondirectory dir))
64 (autoloads-file (expand-file-name (concat pkg "-autoloads.el") dir))
66 ;; Omit autoloads and .elc files from the package.
67 (if (file-exists-p autoloads-file)
68 (delete-file autoloads-file))
69 (archive--delete-elc-files dir)
70 ;; Test whether this is a simple or multi-file package.
71 (setq simple-p (archive--simple-package-p dir pkg))
73 (apply 'archive--process-simple-package
75 (archive--process-multi-file-package dir pkg))
78 (error (message "%s" (cadr v)))))
80 (pp (nreverse packages) (current-buffer))
81 (write-region nil nil "archive-contents"))))
83 (defun archive--simple-package-p (dir pkg)
84 "Test whether DIR contains a simple package named PKG.
85 If so, return a list (VERSION DESCRIPTION REQ COMMENTARY), where
86 VERSION is the version string of the simple package, DESCRIPTION
87 is the brief description of the package, REQ is a list of
88 requirements, and COMMENTARY is the package commentary.
89 Otherwise, return nil."
90 (let* ((pkg-file (expand-file-name (concat pkg "-pkg.el") dir))
91 (mainfile (expand-file-name (concat pkg ".el") dir))
92 version description req commentary)
93 (when (and (or (not (file-exists-p pkg-file))
94 (= (length (directory-files dir nil archive-re-no-dot)) 2))
95 (file-exists-p mainfile))
97 (insert-file-contents mainfile)
98 (goto-char (point-min))
99 (and (looking-at ";;;.*---[ \t]*\\(.*\\)\\(-\\*-.*-\\*-[ \t]*\\)?$")
101 (setq description (match-string 1))
103 (or (archive--strip-rcs-id (lm-header "package-version"))
104 (archive--strip-rcs-id (lm-header "version")))))
106 ;; Grab the other fields, which are not mandatory.
107 (let ((requires-str (lm-header "package-requires")))
109 (setq req (mapcar 'archive--convert-require
110 (car (read-from-string requires-str))))))
111 (setq commentary (lm-commentary))
112 (list version description req commentary)))))))
114 (defun archive--process-simple-package (dir pkg vers desc req commentary)
115 "Deploy the contents of DIR into the archive as a simple package.
116 Rename DIR/PKG.el to PKG-VERS.el, delete DIR, and write the
117 package commentary to PKG-readme.txt. Return the descriptor."
118 ;; Write the readme file.
122 (insert (or commentary
123 (prog1 "No description"
124 (message "Missing commentary in package %s" pkg))))
125 (goto-char (point-min))
126 (while (looking-at ";*[ \t]*\\(commentary[: \t]*\\)?\n")
127 (delete-region (match-beginning 0)
129 (uncomment-region (point-min) (point-max))
130 (goto-char (point-max))
131 (while (progn (forward-line -1)
132 (looking-at "[ \t]*\n"))
133 (delete-region (match-beginning 0)
135 (write-region nil nil (concat pkg "-readme.txt")))
136 ;; Write DIR/foo.el to foo-VERS.el and delete DIR
137 (rename-file (expand-file-name (concat pkg ".el") dir)
138 (concat pkg "-" vers ".el"))
139 (delete-directory dir t)
140 (cons (intern pkg) (vector (version-to-list vers) req desc 'single)))
142 (defun archive--process-multi-file-package (dir pkg)
143 "Deploy the contents of DIR into the archive as a multi-file package.
144 Rename DIR/ to PKG-VERS/, and write the package commentary to
145 PKG-readme.txt. Return the descriptor."
146 (let* ((exp (archive--multi-file-package-def dir pkg))
148 (req (mapcar 'archive--convert-require (nth 4 exp)))
149 (readme (expand-file-name "README" dir)))
150 (unless (equal (nth 1 exp) pkg)
151 (error (format "Package name %s doesn't match file name %s"
153 ;; Write the readme file.
154 (when (file-exists-p readme)
155 (copy-file readme (concat pkg "-readme.txt") 'ok-if-already-exists))
156 (rename-file dir (concat pkg "-" vers))
157 (cons (intern pkg) (vector (version-to-list vers) req (nth 3 exp) 'tar))))
159 (defun archive--multi-file-package-def (dir pkg)
160 "Reurn the `define-package' form in the file DIR/PKG-pkg.el."
161 (let ((pkg-file (expand-file-name (concat pkg "-pkg.el") dir)))
163 (unless (file-exists-p pkg-file)
164 (error "File not found: %s" pkg-file))
165 (insert-file-contents pkg-file)
166 (goto-char (point-min))
167 (read (current-buffer)))))
169 (defun batch-make-site-dir (package-dir site-dir)
171 (setq package-dir (expand-file-name package-dir default-directory))
172 (setq site-dir (expand-file-name site-dir default-directory))
173 (dolist (dir (directory-files package-dir t archive-re-no-dot))
175 (if (not (file-directory-p dir))
176 (error "Skipping non-package file %s" dir)
177 (let* ((pkg (file-name-nondirectory dir))
178 (autoloads-file (expand-file-name (concat pkg "-autoloads.el") dir))
180 ;; Omit autoloads and .elc files from the package.
181 (if (file-exists-p autoloads-file)
182 (delete-file autoloads-file))
183 (archive--delete-elc-files dir)
184 ;; Test whether this is a simple or multi-file package.
185 (setq simple-p (archive--simple-package-p dir pkg))
188 (apply 'archive--write-pkg-file dir pkg simple-p)
189 (setq version (car simple-p)))
191 (nth 2 (archive--multi-file-package-def dir pkg))))
192 (make-symbolic-link (expand-file-name dir package-dir)
193 (expand-file-name (concat pkg "-" version)
196 (package-generate-autoloads pkg dir)
197 (let ((load-path (cons dir load-path)))
198 (byte-recompile-directory dir 0 t))))
200 (error (message "%s" (cadr v))))))
202 (defun archive--write-pkg-file (pkg-dir name version desc requires &rest ignored)
203 (let ((pkg-file (expand-file-name (concat name "-pkg.el") pkg-dir))
207 (concat (format ";; Generated package description from %s.el\n"
210 (list 'define-package
215 ;; Turn version lists into string form.
219 (package-version-join (cadr elt))))
227 ;; no-byte-compile: t
228 ;; lexical-binding: t
231 (provide 'archive-contents)
232 ;;; archive-contents.el ends here