summaryrefslogtreecommitdiff
path: root/dh-elpa.el
blob: a0385c98f5819faac42babc99213d436636f4776 (plain)
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
;;; dh-elpa.el --- package.el style packages for Debian  -*- lexical-binding:t -*-

;; Copyright (C) 2015 David Bremner
;; Portions Copyright 2007-2015 The Free Software Foundation

;; Author: David Bremner <bremner@debian.org>
;; Created: 11 July 2015
;; Version: 0.0.2
;; Keywords: tools
;; Package-Requires: ((tabulated-list "1.0"))

;; This file is NOT part of GNU Emacs.

;; dh-elpa  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 of the License, or
;; (at your option) any later version.

;; dh-elpa 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 dh-elpa.  If not, see <http://www.gnu.org/licenses/>.

(require 'package)

;; Originally package-unpack from package.el in Emacs 24.5
(defun dhelpa-unpack (pkg-desc destdir)
  "Install the contents of the current buffer as a package."
  (let* ((name (package-desc-name pkg-desc))
         (dirname (package-desc-full-name pkg-desc))
	 (pkg-dir (expand-file-name dirname destdir))
	 (backup-inhibited t))
    (make-directory pkg-dir t)
    (pcase (package-desc-kind pkg-desc)
      (`tar
       (let ((default-directory (file-name-as-directory destdir)))
	 (package-untar-buffer dirname)))
      (`single
       (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
         (package--write-file-no-coding el-file)))
      (kind (error "Unknown package kind: %S" kind)))
    (package--make-autoloads-and-stuff pkg-desc pkg-dir)
    pkg-dir))

;; Write out (partial) package description in a form easily parsed by
;; non-lisp tools.
(defun dhelpa-write-desc (desc dest)
  (let* ((name (package-desc-name desc))
	 (version (package-version-join (package-desc-version desc)))
	 (desc-file (expand-file-name (format "%s.desc" name) dest)))
    (with-temp-file desc-file
      (insert (format "ELPA-Name: %s\n" name))
      (insert (format "ELPA-Version: %s\n" version)))))

;;;###autoload
(defun dhelpa-install-from-buffer (destdir)
  "Install a package from the current buffer.
The current buffer is assumed to be a single .el or .tar file that follows the
packaging guidelines; see info node `(elisp)Packaging'."
  (interactive "D")
  (let ((pkg-desc (if (derived-mode-p 'tar-mode)
                      (package-tar-file-info)
                    (package-buffer-info))))
    (dhelpa-unpack pkg-desc destdir)
    pkg-desc))

;;;###autoload
(defun dhelpa-batch-install-file ()
  "Install third command line argument (an emacs lisp file or tar
file) into second command line argument (a directory). The
optional fourth argument specifies a destination for a package
description file."
  (apply #'dhelpa-install-file command-line-args-left))

;;;###autoload
(defun dhelpa-batch-install-directory ()
  "Install third command line argument (a directory containing a
multifile elpa package) into second command line argument (a
directory). An optional third command line argument specifies
where to make temporary files and write a descriptor file."
  (apply #'dhelpa-install-directory command-line-args-left))

;;;###autoload
(defun dhelpa-install-file (dest el-file &optional desc-dir)
  "Install second argument (an emacs lisp file or tar file) into
first command line argument (a directory). The optional third
argument specifies where to write a simplified package description file."
  (with-temp-buffer
    (insert-file-contents-literally el-file)
    (when (string-match "\\.tar\\'" el-file) (tar-mode))
    (let ((desc (dhelpa-install-from-buffer (expand-file-name dest))))
      (when desc-dir
	(dhelpa-write-desc desc desc-dir)))))

;;;###autoload
(defun dhelpa-install-directory (dest elpa-dir &optional work-dir)
  "Install second argument (an unpacked elpa tarball) into first
command line argument (a directory). The directory must either be
named `package' or `package-version'. If a working directory is
specified, cleaning up is the caller's responsibility."
  (let* ((desc (package-load-descriptor elpa-dir)))
    (if (not desc)
	(message "Could not compute version from directory %s" elpa-dir)
      (let* ((canonical-dir (package-desc-full-name desc))
	     (base-dir (file-name-nondirectory elpa-dir))
	     (parent-dir (file-name-directory elpa-dir))
	     (temp-dir (or work-dir (make-temp-file nil t)))
	     (tar-file (concat (expand-file-name canonical-dir temp-dir) ".tar"))
	     ;; this relies on GNU tar features.
	     (transform-command (concat "--transform=s/"
					(regexp-quote base-dir) "/" canonical-dir "/")))
	(call-process "tar" nil nil nil "--create" "-C" parent-dir transform-command
		      "--file" tar-file base-dir)
	(dhelpa-install-file dest tar-file work-dir)
	(unless work-dir
	  (delete-file tar-file)
	  (delete-directory temp-dir))))))

;;; dh-elpa.el ends here