OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 """Used to merge and copy dart source files for deployment to AppEngine""" | 5 """Used to merge and copy dart source files for deployment to AppEngine""" |
6 | 6 |
7 import fileinput | 7 import fileinput |
8 import sys | 8 import sys |
9 import shutil | 9 import shutil |
10 import os | 10 import os |
11 import re | 11 import re |
12 from os.path import abspath, basename, dirname, exists, isabs, join | 12 from os.path import abspath, basename, dirname, exists, isabs, join |
13 from glob import glob | 13 from glob import glob |
14 | 14 |
15 re_directive = re.compile( | 15 re_directive = re.compile( |
16 r'^#(library|import|source|native|resource)\([\'"]([^\'"]*)[\'"](.*)\);$') | 16 r'^(library|import|part|native|resource)\s+(.*);$') |
17 | 17 |
18 class Library(object): | 18 class Library(object): |
19 def __init__(self, name, imports, sources, natives, code): | 19 def __init__(self, name, imports, sources, natives, code): |
20 self.name = name | 20 self.name = name |
21 self.imports = imports | 21 self.imports = imports |
22 self.sources = sources | 22 self.sources = sources |
23 self.natives = natives | 23 self.natives = natives |
24 self.code = code | 24 self.code = code |
25 | 25 |
26 def parseLibrary(library): | 26 def parseLibrary(library): |
27 """ Parses a .dart source file that is the root of a library, and returns | 27 """ Parses a .dart source file that is the root of a library, and returns |
28 information about it: the name, the imports, included sources, and any | 28 information about it: the name, the imports, included sources, and any |
29 code in the file. | 29 code in the file. |
30 """ | 30 """ |
31 libraryname = None | 31 libraryname = None |
32 imports = [] | 32 imports = [] |
33 sources = [] | 33 sources = [] |
34 natives = [] | 34 natives = [] |
35 inlinecode = [] | 35 inlinecode = [] |
36 if exists(library): | 36 if exists(library): |
37 # TODO(sigmund): stop parsing when import/source | 37 # TODO(sigmund): stop parsing when import/source |
38 for line in fileinput.input(library): | 38 for line in fileinput.input(library): |
39 match = re_directive.match(line) | 39 match = re_directive.match(line) |
40 if match: | 40 if match: |
41 directive = match.group(1) | 41 directive = match.group(1) |
42 if directive == 'library': | 42 if directive == 'library': |
43 assert libraryname is None | 43 assert libraryname is None |
44 libraryname = match.group(2) | 44 libraryname = match.group(2) |
45 elif directive == 'source': | 45 elif directive == 'part': |
46 sources.append(match.group(2)) | 46 sources.append(match.group(2).strip('"\'')) |
47 elif directive == 'import': | 47 elif directive == 'import': |
48 imports.append((match.group(2), match.group(3))) | 48 imports.append(match.group(2)) |
49 elif directive == 'native': | |
50 natives.append(match.group(2)) | |
51 elif directive == 'resource': | |
52 # currently ignored | |
53 pass | |
54 else: | 49 else: |
55 raise 'unknown directive %s' % directive | 50 raise Exception('unknown directive %s in %s' % (directive, line)) |
56 else: | 51 else: |
57 inlinecode.append(line) | 52 inlinecode.append(line) |
58 fileinput.close() | 53 fileinput.close() |
59 return Library(libraryname, imports, sources, natives, inlinecode) | 54 return Library(libraryname, imports, sources, natives, inlinecode) |
60 | 55 |
61 def normjoin(*args): | 56 def normjoin(*args): |
62 return os.path.normpath(os.path.join(*args)) | 57 return os.path.normpath(os.path.join(*args)) |
63 | 58 |
64 def mergefiles(srcs, dstfile): | 59 def mergefiles(srcs, dstfile): |
65 for src in srcs: | 60 for src in srcs: |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 # Ensure output directory exists | 113 # Ensure output directory exists |
119 outpath = join(outdir, lib[1:] if isabs(lib) else lib) | 114 outpath = join(outdir, lib[1:] if isabs(lib) else lib) |
120 dstpath = dirname(outpath) | 115 dstpath = dirname(outpath) |
121 if not exists(dstpath): | 116 if not exists(dstpath): |
122 os.makedirs(dstpath) | 117 os.makedirs(dstpath) |
123 | 118 |
124 | 119 |
125 # Create file containing all imports, and inlining all sources | 120 # Create file containing all imports, and inlining all sources |
126 with open(outpath, 'w') as f: | 121 with open(outpath, 'w') as f: |
127 if library.name: | 122 if library.name: |
128 f.write("#library('%s');\n\n" % library.name) | 123 f.write("library %s;\n\n" % library.name) |
129 else: | 124 else: |
130 f.write("#library('%s');\n\n" % basename(lib)) | 125 f.write("library %s;\n\n" % basename(lib)) |
131 for (importfile, optional_prefix) in library.imports: | 126 for importfile in library.imports: |
podivilov
2012/10/23 11:57:55
Maybe keep support for prefixed import in case we'
Anton Muhin
2012/10/23 13:03:27
We actually dump the whole suffix after import, so
| |
132 f.write("#import('%s'%s);\n" % (importfile, optional_prefix)) | 127 f.write("import %s;\n" % (importfile)) |
133 for native in library.natives: | |
134 if isabs(native): | |
135 npath = native[1:] | |
136 else: | |
137 npath = native | |
138 f.write("#native('%s');\n" % npath) | |
139 copyfile(normjoin(dirname(lib), native), | |
140 join(dirname(outpath), npath)) | |
141 f.write('%s' % (''.join(library.code))) | 128 f.write('%s' % (''.join(library.code))) |
142 mergefiles([normjoin(dirname(lib), s) for s in library.sources], f) | 129 mergefiles([normjoin(dirname(lib), s) for s in library.sources], f) |
143 | 130 |
144 for (i, prefix) in library.imports: | 131 for suffix in library.imports: |
145 if not i.startswith('dart:'): | 132 m = re.match(r'[\'"]([^\'"]+)[\'"](\s+as\s+\w+)?$', suffix) |
146 worklist.append(normjoin(dirname(lib), i)); | 133 uri = m.group(0) |
134 if not uri.startswith('dart:'): | |
135 worklist.append(normjoin(dirname(lib), uri)); | |
147 | 136 |
148 return 0 | 137 return 0 |
149 | 138 |
150 if __name__ == '__main__': | 139 if __name__ == '__main__': |
151 sys.exit(main(*sys.argv[1:])) | 140 sys.exit(main(*sys.argv[1:])) |
OLD | NEW |