| 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|part|native|resource)\s+(.*);$') | 16 r'^(library|import|part|native|resource)\s+(.*);$') |
| 17 re_comment = re.compile( |
| 18 r'^(///|/\*| \*).*$') |
| 17 | 19 |
| 18 class Library(object): | 20 class Library(object): |
| 19 def __init__(self, name, imports, sources, natives, code): | 21 def __init__(self, name, imports, sources, natives, code, comment): |
| 20 self.name = name | 22 self.name = name |
| 21 self.imports = imports | 23 self.imports = imports |
| 22 self.sources = sources | 24 self.sources = sources |
| 23 self.natives = natives | 25 self.natives = natives |
| 24 self.code = code | 26 self.code = code |
| 27 self.comment = comment |
| 25 | 28 |
| 26 def parseLibrary(library): | 29 def parseLibrary(library): |
| 27 """ Parses a .dart source file that is the root of a library, and returns | 30 """ 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 | 31 information about it: the name, the imports, included sources, and any |
| 29 code in the file. | 32 code in the file. |
| 30 """ | 33 """ |
| 31 libraryname = None | 34 libraryname = None |
| 32 imports = [] | 35 imports = [] |
| 33 sources = [] | 36 sources = [] |
| 34 natives = [] | 37 natives = [] |
| 35 inlinecode = [] | 38 inlinecode = [] |
| 39 librarycomment = [] |
| 36 if exists(library): | 40 if exists(library): |
| 37 # TODO(sigmund): stop parsing when import/source | 41 # TODO(sigmund): stop parsing when import/source |
| 38 for line in fileinput.input(library): | 42 for line in fileinput.input(library): |
| 39 match = re_directive.match(line) | 43 match = re_directive.match(line) |
| 40 if match: | 44 if match: |
| 41 directive = match.group(1) | 45 directive = match.group(1) |
| 42 if directive == 'library': | 46 if directive == 'library': |
| 43 assert libraryname is None | 47 assert libraryname is None |
| 44 libraryname = match.group(2) | 48 libraryname = match.group(2) |
| 45 elif directive == 'part': | 49 elif directive == 'part': |
| 46 suffix = match.group(2) | 50 suffix = match.group(2) |
| 47 if not suffix.startswith('of '): | 51 if not suffix.startswith('of '): |
| 48 sources.append(match.group(2).strip('"\'')) | 52 sources.append(match.group(2).strip('"\'')) |
| 49 elif directive == 'import': | 53 elif directive == 'import': |
| 50 imports.append(match.group(2)) | 54 imports.append(match.group(2)) |
| 51 else: | 55 else: |
| 52 raise Exception('unknown directive %s in %s' % (directive, line)) | 56 raise Exception('unknown directive %s in %s' % (directive, line)) |
| 53 else: | 57 else: |
| 54 inlinecode.append(line) | 58 # Check for library comment. |
| 59 if not libraryname and re_comment.match(line): |
| 60 librarycomment.append(line) |
| 61 else: |
| 62 inlinecode.append(line) |
| 55 fileinput.close() | 63 fileinput.close() |
| 56 return Library(libraryname, imports, sources, natives, inlinecode) | 64 return Library(libraryname, imports, sources, natives, inlinecode, |
| 65 librarycomment) |
| 57 | 66 |
| 58 def normjoin(*args): | 67 def normjoin(*args): |
| 59 return os.path.normpath(os.path.join(*args)) | 68 return os.path.normpath(os.path.join(*args)) |
| 60 | 69 |
| 61 def mergefiles(srcs, dstfile): | 70 def mergefiles(srcs, dstfile): |
| 62 for src in srcs: | 71 for src in srcs: |
| 63 with open(src, 'r') as s: | 72 with open(src, 'r') as s: |
| 64 for line in s: | 73 for line in s: |
| 65 if not line.startswith('part of '): | 74 if not line.startswith('part of '): |
| 66 dstfile.write(line) | 75 dstfile.write(line) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 # Ensure output directory exists | 116 # Ensure output directory exists |
| 108 outpath = join(outdir, lib[1:] if isabs(lib) else lib) | 117 outpath = join(outdir, lib[1:] if isabs(lib) else lib) |
| 109 dstpath = dirname(outpath) | 118 dstpath = dirname(outpath) |
| 110 if not exists(dstpath): | 119 if not exists(dstpath): |
| 111 os.makedirs(dstpath) | 120 os.makedirs(dstpath) |
| 112 | 121 |
| 113 | 122 |
| 114 # Create file containing all imports, and inlining all sources | 123 # Create file containing all imports, and inlining all sources |
| 115 with open(outpath, 'w') as f: | 124 with open(outpath, 'w') as f: |
| 116 if library.name: | 125 if library.name: |
| 126 if library.comment: |
| 127 f.write('%s' % (''.join(library.comment))) |
| 117 f.write("library %s;\n\n" % library.name) | 128 f.write("library %s;\n\n" % library.name) |
| 118 else: | 129 else: |
| 119 f.write("library %s;\n\n" % basename(lib)) | 130 f.write("library %s;\n\n" % basename(lib)) |
| 120 for importfile in library.imports: | 131 for importfile in library.imports: |
| 121 f.write("import %s;\n" % (importfile)) | 132 f.write("import %s;\n" % (importfile)) |
| 122 f.write('%s' % (''.join(library.code))) | 133 f.write('%s' % (''.join(library.code))) |
| 123 mergefiles([normjoin(dirname(lib), s) for s in library.sources], f) | 134 mergefiles([normjoin(dirname(lib), s) for s in library.sources], f) |
| 124 | 135 |
| 125 for suffix in library.imports: | 136 for suffix in library.imports: |
| 126 m = re.match(r'[\'"]([^\'"]+)[\'"](\s+as\s+\w+)?.*$', suffix) | 137 m = re.match(r'[\'"]([^\'"]+)[\'"](\s+as\s+\w+)?.*$', suffix) |
| 127 uri = m.group(1) | 138 uri = m.group(1) |
| 128 if not uri.startswith('dart:'): | 139 if not uri.startswith('dart:'): |
| 129 worklist.append(normjoin(dirname(lib), uri)); | 140 worklist.append(normjoin(dirname(lib), uri)); |
| 130 | 141 |
| 131 return 0 | 142 return 0 |
| 132 | 143 |
| 133 if __name__ == '__main__': | 144 if __name__ == '__main__': |
| 134 sys.exit(main(*sys.argv[1:])) | 145 sys.exit(main(*sys.argv[1:])) |
| OLD | NEW |