OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # BSD-style license that can be found in the LICENSE file. | |
5 | |
6 from __future__ import with_statement | |
7 import StringIO | |
8 import os | |
9 import sys | |
10 import re | |
11 from collections import deque | |
12 | |
13 class GenerateError(Exception): | |
14 | |
15 def __init__(self, value): | |
16 self.value = value | |
17 | |
18 def __str__(self): | |
19 return repr(self.value) | |
20 | |
21 | |
22 class Generator: | |
23 | |
24 def __init__(self, base_directory, name, output, path, *includes): | |
25 self.base_directory = base_directory | |
26 self.name = name | |
27 self.output = output | |
28 self.path = path | |
29 self.includes = set() | |
30 for x in includes: | |
31 self.includes.add(x) | |
32 self.sources = [] | |
33 self.resources = [] | |
34 | |
35 def _list_files(self): | |
36 drain = deque() | |
37 drain.extend(self.includes) | |
38 while len(drain) > 0: | |
39 target = drain.popleft() | |
40 # Avoid circular dependencies | |
41 if target in self.resources: | |
42 continue | |
43 if (target.startswith("dart:")): | |
44 continue | |
45 self.resources.append(target) | |
46 if (target.endswith(".dart")): | |
47 with open(os.path.join(self.base_directory,self.path,target),"r") as fob
j: | |
48 text = fobj.read() | |
49 file_sources = re.findall(r"#source\(['\"](?P<name>.*)['\"]\);",text) | |
50 file_native = re.findall(r"#native\(['\"](?P<name>.*)['\"]\);",text) | |
51 file_imports = re.findall(r"#import\(['\"](?P<name>.*)['\"]\);",text) | |
52 self.resources.extend(file_sources) | |
53 self.resources.extend(file_native) | |
54 drain.extend(file_imports) | |
55 self.sources.sort() | |
56 self.resources.sort() | |
57 | |
58 def _print_gypi_files(self, out, name, files): | |
59 out.write(" '%s': [\n" % name) | |
60 for filename in files: | |
61 out.write(" '%s/%s',\n" % (self.path, filename)) | |
62 out.write(" ],\n") | |
63 | |
64 def _print_ant_files(self, out, name, files): | |
65 out.write(" <filelist id='%s' dir='%s'>\n" % (name, self.path)) | |
66 for filename in files: | |
67 out.write(" <file name='%s'/>\n" % filename) | |
68 out.write(" </filelist>\n") | |
69 out.write(" <pathconvert pathsep=',' property='%s' refid='%s'>\n" | |
70 % (name, name)) | |
71 out.write(" <map from='${basedir}/%s/' to=''/>\n" % self.path) | |
72 out.write(" </pathconvert>\n") | |
73 | |
74 def _make_output(self, file_name): | |
75 if os.path.exists(file_name): | |
76 return StringIO.StringIO() | |
77 else: | |
78 return file(file_name, 'w') | |
79 | |
80 def _close(self, file_name, output): | |
81 if not isinstance(output, StringIO.StringIO): | |
82 output.close() | |
83 return | |
84 new_text = output.getvalue() | |
85 output.close() | |
86 with open(file_name, 'r') as f: | |
87 old_text = f.read() | |
88 if old_text == new_text: | |
89 return | |
90 sys.stderr.write('Updating %s\n' % file_name) | |
91 with open(file_name, 'w') as f: | |
92 f.write(new_text) | |
93 | |
94 def generate(self): | |
95 self._list_files() | |
96 file_name = self.output + '.gypi'; | |
97 gypi = self._make_output(file_name) | |
98 gypi.write("{\n 'variables': {\n") | |
99 self._print_gypi_files(gypi, self.name + '_sources', self.sources) | |
100 self._print_gypi_files(gypi, self.name + '_resources', self.resources) | |
101 gypi.write(" },\n}\n") | |
102 self._close(file_name, gypi) | |
103 file_name = self.output + '.xml' | |
104 ant = self._make_output(file_name) | |
105 ant.write("<project>\n") | |
106 self._print_ant_files(ant, self.name + '_sources', self.sources) | |
107 self._print_ant_files(ant, self.name + '_resources', self.resources) | |
108 ant.write("</project>\n") | |
109 self._close(file_name, ant) | |
110 | |
111 | |
112 def Main(script_name = None, name = None, output = None, path = None, | |
113 *rest): | |
114 if not path: | |
115 raise GenerateError("usage: %s NAME OUTPUT PATH EXCLUDE_DIR_NAME ..." | |
116 % script_name) | |
117 base_directory = os.path.dirname(output) | |
118 Generator(base_directory, name, output, path, *rest).generate() | |
119 | |
120 | |
121 if __name__ == '__main__': | |
122 sys.exit(Main(*sys.argv)) | |
OLD | NEW |