-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathservant.el
120 lines (95 loc) · 4.09 KB
/
servant.el
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
;;; servant.el --- ELPA server written in Emacs Lisp -*- lexical-binding: t; -*-
;; Copyright (C) 2013 Johan Andersson
;; Copyright (C) 2013 Sebastian Wiesner <[email protected]>
;; Author: Johan Andersson <[email protected]>
;; Sebastian Wiesner <[email protected]>
;; Maintainer: Johan Andersson <[email protected]>
;; Sebastian Wiesner <[email protected]>
;; Version: 0.3.0
;; Keywords: elpa, server
;; URL: http://github.com/rejeep/servant.el
;; Package-Requires: ((s "1.8.0") (dash "2.2.0") (f "0.11.0") (ansi "0.3.0") (commander "0.5.0") (epl "0.2") (shut-up "0.2.1") (web-server "0.0.1"))
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program 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, or (at your option)
;; any later version.
;; This program 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; An ELPA server.
;; Serves ELPA packages from ELPA. Builds the package index on the fly, if no
;; index file is present.
;;; Code:
(require 's)
(require 'f)
(require 'dash)
(require 'epl)
;;;; Package Index functions
(defun servant-create-index-entry (file-name)
"Create a package index entry for the package at FILE-NAME.
FILE-NAME can be either an Emacs Lisp file or a tar file with an
Emacs Lisp file or PKG file in it.
Return a package index entry."
(-when-let* ((format (servant-package-type file-name))
(package (epl-package-from-file file-name)))
(cons (epl-package-name package)
(vector (epl-package-version package)
(--map (list (epl-requirement-name it)
(epl-requirement-version it))
(epl-package-requirements package))
(epl-package-summary package)
format))))
(defun servant-package-type (file-name)
"Determine the package type of FILE-NAME.
Return `tar' for tarball packages, `single' for single file
packages, or nil, if FILE-NAME is not a package."
(let ((ext (f-ext file-name)))
(cond
((string= ext "tar") 'tar)
((string= ext "el") 'single)
(:else nil))))
(defun servant-create-index (directory)
"Generate a package index for DIRECTORY."
(let* ((package-files (f-files directory #'servant-package-type))
(entries (-map 'servant-create-index-entry package-files)))
(append (list 1) entries)))
(defun servant--create-index-string (directory)
"Generate a package index for DIRECTORY as string."
(let ((print-level nil)
(print-length nil))
(concat "\n" (prin1-to-string (servant-create-index directory)) "\n")))
(defun servant-start ()
"Start server."
(unless (f-dir? (servant-path))
(error (ansi-red "Servant not initialized, run `servant init`.")))
(let ((docroot (servant-path)))
(ws-start
(lambda (request)
(with-slots (process headers) request
(let ((path (substring (cdr (assoc :GET headers)) 1)))
(if (ws-in-directory-p docroot path)
(if (f-dir? path)
(ws-send-directory-list process (f-expand path docroot) "^[^\.]")
(ws-send-file process (f-expand path docroot)))
(ws-send-404 process)))))
servant-port))
(with-temp-file (servant-pid-file)
(insert (format "%s" (emacs-pid)))))
(defun servant-stop ()
"Stop server."
(when (f-file? (servant-pid-file))
(let ((pid (f-read-text (servant-pid-file))))
(with-temp-buffer
(let ((exit-code (call-process "kill" nil t nil pid)))
(unless (zerop exit-code)
(error (buffer-string))))))))
(provide 'servant)
;;; servant.el ends here