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('../lib/node/node.dart'); | |
6 #import('../file_system_node.dart'); | |
7 #import('../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 { | |
82 res.setHeader('Content-Type', 'text/plain'); | |
83 } | |
84 | |
85 filename = '$dartdir/${req.url}'; | |
86 | |
87 if (path.existsSync(filename)) { | |
88 var stat = fs.statSync(filename); | |
89 if (stat.isFile()) { | |
90 res.statusCode = 200; | |
91 String data; | |
92 if (filename.endsWith('.html')) { | |
93 res.end(frogify(filename)); | |
94 } else { | |
95 res.end(fs.readFileSync(filename, 'utf8')); | |
96 } | |
97 return; | |
98 } else { | |
99 res.setHeader('Content-Type', 'text/html'); | |
100 res.end(dirToHtml(req.url, filename)); | |
101 } | |
102 } | |
103 | |
104 | |
105 res.statusCode = 404; | |
106 res.end(''); | |
107 }).listen(1337, "localhost"); | |
108 print('Server running at http://localhost:1337/'); | |
109 } | |
OLD | NEW |