-
Notifications
You must be signed in to change notification settings - Fork 0
/
amd-dep.el
162 lines (133 loc) · 5.38 KB
/
amd-dep.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
;;; amd-dep.el --- Emacs AMD/RequireJS dependency/plugin handling
;; Copyright 2013 Hendrik van Antwerpen
;; Author: Hendrik van Antwerpen <[email protected]>
;; Version: 0.2.0
;; Package-Requires: ((js-pkg "0.2.0") (s "1.3.1"))
;; This file is not part of GNU Emacs.
;; This file is part of amd-mode.
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Code:
(require 'js-pkg)
(require 's)
(require 'amd-util)
(setq amd-dep--create-handlers nil)
(setq amd-dep--to-var-handlers nil)
(setq amd-dep--to-files-handlers nil)
(setq amd-dep--from-file-handlers nil)
(defvar amd-dep--re
"^\\(?:\\(\\(?:[[:alnum:]-_.]+/\\)*[[:alnum:]-_]+\\)!\\)?\\(\\|.*\\)$")
(defun amd-dep-create (plugin resource)
(let ((dep (cons plugin resource)))
(let* ((module-create (amd--assoc
nil amd-dep--create-handlers))
(plugin-create (amd--assoc
plugin amd-dep--create-handlers))
(final-plugin (if (and plugin module-create)
(funcall module-create plugin)
plugin))
(final-resource (if plugin-create
(funcall plugin-create resource)
resource)))
(when final-resource
(cons final-plugin final-resource)))))
(defun amd-deps-from-file (file)
"For the given file, return possible AMD dependencies"
(let ((deps nil))
(mapcar (lambda (plugin-from-file)
(let ((dep (funcall plugin-from-file file)))
(if dep
(setq deps (cons dep deps)))))
amd-dep--from-file-handlers)
deps))
(defun amd-dep-to-var (dep)
"For the given dependency, return a variable name"
(setq dep (amd-dep-parse dep))
(let ((handler (amd--assoc (amd-dep-plugin dep)
amd-dep--to-var-handlers)))
(if handler
(funcall handler (amd-dep-resource dep)))))
(defun amd-dep-to-files (dep)
"For the given dependency, return a file"
(setq dep (amd-dep-parse dep))
(let ((handler (amd--assoc (amd-dep-plugin dep)
amd-dep--to-files-handlers)))
(if handler
(funcall handler (amd-dep-resource dep)))))
; Dependency
(defun amd-dep-parse (dep-or-string)
(if (listp dep-or-string)
dep-or-string
(when (string-match amd-dep--re dep-or-string)
(let* ((plugin (match-string-no-properties 1 dep-or-string))
(resource (match-string-no-properties 2 dep-or-string)))
(amd-dep-create plugin resource)))))
(defun amd-dep-format (dep)
(setq dep (amd-dep-parse dep))
(let ((plugin (amd-dep-plugin dep))
(resource (amd-dep-resource dep)))
(if (not plugin)
resource
(concat plugin "!" resource))))
(defun amd-dep-plugin (dep)
(setq dep (amd-dep-parse dep))
(car dep))
(defun amd-dep-resource (dep)
(setq dep (amd-dep-parse dep))
(cdr dep))
(defun amd-dep-register-plugin (plugin &optional create
to-var from-file to-files)
(when create (add-to-list 'amd-dep--create-handlers
(cons plugin create)))
(when to-var (add-to-list 'amd-dep--to-var-handlers
(cons plugin to-var)))
(when to-files (add-to-list 'amd-dep--to-files-handlers
(cons plugin to-files)))
(when from-file (add-to-list 'amd-dep--from-file-handlers
from-file)))
; Handling of AMD modules
(setq amd-dep--module-id-re
"^\\([[:alnum:]-_.]+://\\|/\\)?\\(?:[[:alnum:]-_.]+/\\)*\\([[:alnum:]-_.]+?\\)\\(\\.js\\)?$")
(defun amd-dep-module-create (resource)
(let ((match (s-match amd-dep--module-id-re resource)))
(when match
(if (or (nth 1 match)
(nth 3 match))
resource
(js-pkg-res-id-normalize resource)))))
(defun amd-dep-module-from-file (file)
(let ((resource (js-pkg-file-to-res file)))
(if (and resource
(string-match "^\\(.*\\)\\.js$" resource))
(let ((module (match-string-no-properties 1 resource)))
(amd-dep-create nil module)))))
(defun amd-dep-module-to-files (resource)
(let ((match (s-match amd-dep--module-id-re resource)))
(unless (or (not match)
(nth 1 match)
(nth 3 match))
(js-pkg-res-to-files (concat resource ".js")))))
(defun amd-dep-module-to-var (resource)
(let ((match (s-match amd-dep--module-id-re resource)))
(when match
(amd--camelize (nth 2 match)))))
(defun amd-dep-module-p (dep)
"Return t if the given dependency is a module dependency."
(setq dep (amd-dep-parse dep))
(null (amd-dep-plugin dep)))
(amd-dep-register-plugin nil
(lambda (resource) (amd-dep-module-create resource))
(lambda (resource) (amd-dep-module-to-var resource))
(lambda (file) (amd-dep-module-from-file file))
(lambda (resource) (amd-dep-module-to-files resource)))
(provide 'amd-dep)
;;; amd-dep.el ends here