OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #import('../../frog/lib/node/node.dart'); | |
6 #import('../../frog/file_system_node.dart'); | |
7 #import('../../frog/lang.dart'); | |
8 | |
9 String compileDart(String basename, String filename) { | |
10 final basedir = path.dirname(basename); | |
11 final fullname = '$basedir/$filename'; | |
12 | |
13 world.reset(); | |
14 options.dartScript = fullname; | |
15 if (world.compile()) { | |
16 return world.getGeneratedCode(); | |
17 } else { | |
18 return 'alert("compilation of $filename failed, see console for errors");'; | |
19 } | |
20 } | |
21 | |
22 | |
23 String frogify(String filename) { | |
24 final s = @'<script\s+type="application/dart"(?:\s+src="(\w+.dart)")?>([\s\S]*
)</script>'; | |
25 | |
26 final text = fs.readFileSync(filename, 'utf8'); | |
27 final re = new RegExp(s, true, false); | |
28 var m = re.firstMatch(text); | |
29 if (m === null) return text; | |
30 | |
31 var dname = m.group(1); | |
32 var contents = m.group(2); | |
33 | |
34 print('compiling $dname'); | |
35 | |
36 var compiled = compileDart(filename, dname); | |
37 | |
38 return text.substring(0, m.start()) + | |
39 '<script type="application/javascript">${compiled}</script>' + | |
40 text.substring(m.start() + m.group(0).length); | |
41 } | |
42 | |
43 void initializeCompiler(String homedir) { | |
44 final filesystem = new NodeFileSystem(); | |
45 | |
46 parseOptions(homedir, [null, null], filesystem); | |
47 initializeWorld(filesystem); | |
48 } | |
49 | |
50 String dirToHtml(String url, String dirname) { | |
51 var names = new StringBuffer(); | |
52 for (var name in fs.readdirSync(dirname)) { | |
53 var link = '$url/$name'; | |
54 names.add('<li><a href=$link>$name</a></li>\n'); | |
55 } | |
56 | |
57 return ''' | |
58 <html><head><title>$dirname</title></head> | |
59 <h3>$dirname</h3> | |
60 <ul> | |
61 $names | |
62 </ul> | |
63 </html> | |
64 '''; | |
65 } | |
66 | |
67 | |
68 void main() { | |
69 final homedir = path.dirname(fs.realpathSync(process.argv[1])); | |
70 final dartdir = path.dirname(homedir); | |
71 print('running with dart root at ${dartdir}'); | |
72 | |
73 initializeCompiler(homedir); | |
74 | |
75 http.createServer((ServerRequest req, ServerResponse res) { | |
76 var filename; | |
77 if (req.url.endsWith('.html')) { | |
78 res.setHeader('Content-Type', 'text/html'); | |
79 } else if (req.url.endsWith('.css')) { | |
80 res.setHeader('Content-Type', 'text/css'); | |
81 } else if (req.url.endsWith('.svg')) { | |
82 res.setHeader('Content-Type', 'image/svg+xml'); | |
83 } else { | |
84 res.setHeader('Content-Type', 'text/plain'); | |
85 } | |
86 | |
87 filename = '$dartdir/${req.url}'; | |
88 | |
89 if (path.existsSync(filename)) { | |
90 var stat = fs.statSync(filename); | |
91 if (stat.isFile()) { | |
92 res.statusCode = 200; | |
93 String data; | |
94 if (filename.endsWith('.html')) { | |
95 res.end(frogify(filename)); | |
96 } else { | |
97 res.end(fs.readFileSync(filename, 'utf8')); | |
98 } | |
99 return; | |
100 } else { | |
101 res.setHeader('Content-Type', 'text/html'); | |
102 res.end(dirToHtml(req.url, filename)); | |
103 } | |
104 } | |
105 | |
106 | |
107 res.statusCode = 404; | |
108 res.end(''); | |
109 }).listen(1337, "localhost"); | |
110 print('Server running at http://localhost:1337/'); | |
111 } | |
OLD | NEW |