| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 4 # for details. All rights reserved. Use of this source code is governed by a |
| 5 # BSD-style license that can be found in the LICENSE file. |
| 6 # |
| 7 |
| 8 """ |
| 9 Generates an html file (frogpad.html) that can be used to execute the frog |
| 10 compiler in a web browser or DumpRenderTree, |
| 11 |
| 12 The generated frogpad.html will contain: |
| 13 |
| 14 1. all the dart files that compose a dart program |
| 15 2. all the dart files of dart:core and other standard dart libraries |
| 16 3. frogpad.dart (compiled to javascript) |
| 17 |
| 18 The contents of each dart file is placed in a separate <script> tag. |
| 19 |
| 20 When the html page is loaded by a browser, the frog compiler will be invoked |
| 21 and the user's dart program will be compiled to javascript. The generated |
| 22 javascript will be placed in the <pre> element with id "output". |
| 23 |
| 24 If using DumpRenderTree, the output javascript can be obtained by dumping |
| 25 the page as text and looking for the contents of the output textarea. |
| 26 """ |
| 27 |
| 28 import optparse |
| 29 import os.path |
| 30 import re |
| 31 import sys |
| 32 |
| 33 # Template for the html page we're going to generate. |
| 34 HTML = """<html> |
| 35 <head> |
| 36 <style type="text/css"> |
| 37 textarea { |
| 38 width: 100%; |
| 39 height: 200px; |
| 40 } |
| 41 .label { |
| 42 margin-top: 5px; |
| 43 } |
| 44 pre { |
| 45 border: 2px solid black; |
| 46 } |
| 47 </style> |
| 48 {{script_tags}} |
| 49 <script type="text/javascript"> |
| 50 if (window.layoutTestController) { |
| 51 layoutTestController.dumpAsText(); |
| 52 } |
| 53 </script> |
| 54 </head> |
| 55 <body> |
| 56 <h1>Frogpad</h1> |
| 57 <div class="label">Input:</div> |
| 58 <textarea id="input"></textarea> |
| 59 <div class="label">Compiler Messages:</div> |
| 60 <pre id="warnings"></pre> |
| 61 <div class="label">Timing:</div> |
| 62 <pre id="timing"></pre> |
| 63 <div class="label">Output:</div> |
| 64 <pre id="output"></pre> |
| 65 <script type="text/javascript" src="frogpad.dart.js" ></script> |
| 66 </body> |
| 67 </html> |
| 68 """ |
| 69 |
| 70 # We use "application/inert" here to make the browser ignore the |
| 71 # these script tags. (frogpad.dart will fish out the contents as needed.) |
| 72 # |
| 73 SCRIPT_TAG = """<script type="application/inert" id="{{id}}"> |
| 74 {{contents}} |
| 75 </script> |
| 76 """ |
| 77 |
| 78 # Regex that finds #import, #source and #native directives in .dart files. |
| 79 # match.group(1) = "import", "source" or "native" |
| 80 # match.group(2) = url of file being imported |
| 81 DIRECTIVE_RE = re.compile(r"^#(import|source|native)\([\"']([^\"']*)[\"']") |
| 82 |
| 83 # id of script tag that holds name of the top dart file to be compiled, |
| 84 # (This file name passed will be passed to the frog compiler by frogpad.dart.) |
| 85 MAIN_ID = "main_id" |
| 86 |
| 87 DART_LIBRARIES = { |
| 88 "core": "lib/corelib.dart", |
| 89 "coreimpl": "lib/corelib_impl.dart", |
| 90 "dom": "../client/dom/frog/dom_frog.dart", |
| 91 "html": "../client/html/release/html.dart", |
| 92 "htmlimpl": "../client/html/release/htmlimpl.dart", |
| 93 "json": "../lib/json/json_frog.dart" |
| 94 } |
| 95 |
| 96 |
| 97 class Pad(object): |
| 98 """ |
| 99 Accumulates all source files that are needed to compile a dart program, |
| 100 and places them in <script> tags on an html page. |
| 101 """ |
| 102 |
| 103 def __init__(self, frog_dir, main_file): |
| 104 # directory of frog compiler source code |
| 105 self.frog_dir = frog_dir |
| 106 |
| 107 # which .dart file to compile |
| 108 self.main_file = main_file |
| 109 |
| 110 # map from file name to File object (contains entries for all corelib |
| 111 # and all other dart files needed to compile main_file) |
| 112 self.name_to_file = {} |
| 113 |
| 114 # map from script tag id to File object |
| 115 self.id_to_file = {} |
| 116 |
| 117 self.load_libraries() |
| 118 self.load_file(self.main_file) |
| 119 |
| 120 def generate_html(self): |
| 121 tags = [] |
| 122 for f in self.id_to_file.values(): |
| 123 tags.append(self._create_tag(f.id, f.contents)) |
| 124 tags.append(self._create_tag(MAIN_ID, self.main_file)) |
| 125 html = HTML.replace("{{script_tags}}", "".join(tags)) |
| 126 return html |
| 127 |
| 128 @staticmethod |
| 129 def _create_tag(id, contents): |
| 130 s = SCRIPT_TAG |
| 131 s = s.replace("{{id}}", id) |
| 132 s = s.replace("{{contents}}", contents) |
| 133 return s |
| 134 |
| 135 def dart_library(self, name): |
| 136 path = DART_LIBRARIES[name] |
| 137 if not path: |
| 138 raise Exception("unrecognized 'dart:%s'", name) |
| 139 return os.path.join(self.frog_dir, path) |
| 140 |
| 141 def load_libraries(self): |
| 142 for name in DART_LIBRARIES: |
| 143 self.load_file(self.dart_library(name)) |
| 144 |
| 145 def load_file(self, name): |
| 146 name = os.path.abspath(name) |
| 147 if name in self.name_to_file: |
| 148 print "already loaded %s, skipping" % name |
| 149 return |
| 150 f = File(self, name) |
| 151 self.name_to_file[f.name] = f |
| 152 if f.id in self.id_to_file: |
| 153 raise Exception("ambiguous id '%s'" % f.id) |
| 154 self.id_to_file[f.id] = f |
| 155 f.directives() |
| 156 |
| 157 class File(object): |
| 158 def __init__(self, pad, name): |
| 159 self.pad = pad |
| 160 self.name = name |
| 161 self.id = self._make_id() |
| 162 if not os.path.exists(name): |
| 163 raise Exception("cannot find file '%s'" % name) |
| 164 with open(self.name, "r") as f: |
| 165 self.contents = f.read() |
| 166 print "creating File '%s' (%d lines)" % (self.name, len(self.contents)) |
| 167 |
| 168 def _make_id(self): |
| 169 """ |
| 170 Generates an id (based on the file name) for the <script> tag that will |
| 171 hold the contents of this file. |
| 172 """ |
| 173 (dirname, name) = os.path.split(self.name) |
| 174 dirname = os.path.basename(dirname) |
| 175 name = name.replace(".", "_") |
| 176 return dirname + "_" + name |
| 177 |
| 178 def directives(self): |
| 179 """Load files referenced by #source, #import and #native directives.""" |
| 180 lines = self.contents.split("\n") |
| 181 self.line_number = 0 |
| 182 for line in lines: |
| 183 self.line_number += 1 |
| 184 self._directive(line) |
| 185 |
| 186 def _directive(self, line): |
| 187 match = DIRECTIVE_RE.match(line) |
| 188 if not match: |
| 189 return |
| 190 url = match.group(2) |
| 191 if url.startswith("dart:"): |
| 192 path = self.pad.dart_library(url[len("dart:"):]) |
| 193 else: |
| 194 path = os.path.join(os.path.dirname(self.name), url) |
| 195 self.pad.load_file(path) |
| 196 |
| 197 |
| 198 def main(argv): |
| 199 parser = optparse.OptionParser() |
| 200 parser.add_option("-o", "--out", dest="out_file") |
| 201 |
| 202 (options, args) = parser.parse_args(argv) |
| 203 main_file = os.path.abspath(args[1]) |
| 204 |
| 205 script_dir = os.path.abspath(os.path.dirname(argv[0])) |
| 206 frog_dir = os.path.abspath(os.path.join(script_dir, "../../../frog")) |
| 207 |
| 208 pad = Pad(frog_dir, main_file) |
| 209 html = pad.generate_html() |
| 210 |
| 211 filename = "frogpad.html" |
| 212 with open(filename, "w") as output: |
| 213 output.write(html) |
| 214 print "generated '%s' (%d bytes)" % (filename, len(html)) |
| 215 |
| 216 if __name__ == "__main__": |
| 217 sys.exit(main(sys.argv)) |
| 218 |
| OLD | NEW |