OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generates dart source files from a mojom.Module.""" | 5 """Generates dart source files from a mojom.Module.""" |
6 | 6 |
7 import os | |
7 import re | 8 import re |
8 | 9 |
9 import mojom.generate.generator as generator | 10 import mojom.generate.generator as generator |
10 import mojom.generate.module as mojom | 11 import mojom.generate.module as mojom |
11 import mojom.generate.pack as pack | 12 import mojom.generate.pack as pack |
12 from mojom.generate.template_expander import UseJinja | 13 from mojom.generate.template_expander import UseJinja |
13 | 14 |
14 GENERATOR_PREFIX = 'dart' | 15 GENERATOR_PREFIX = 'dart' |
15 | 16 |
16 _kind_to_dart_default_value = { | 17 _kind_to_dart_default_value = { |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
366 'is_pointer_array_kind': IsPointerArrayKind, | 367 'is_pointer_array_kind': IsPointerArrayKind, |
367 'is_struct_kind': mojom.IsStructKind, | 368 'is_struct_kind': mojom.IsStructKind, |
368 'dart_true_false': GetDartTrueFalse, | 369 'dart_true_false': GetDartTrueFalse, |
369 'dart_type': DartDeclType, | 370 'dart_type': DartDeclType, |
370 'name': GetNameForElement, | 371 'name': GetNameForElement, |
371 'interface_response_name': GetInterfaceResponseName, | 372 'interface_response_name': GetInterfaceResponseName, |
372 'response_struct_from_method': generator.GetResponseStructFromMethod, | 373 'response_struct_from_method': generator.GetResponseStructFromMethod, |
373 'struct_from_method': generator.GetStructFromMethod, | 374 'struct_from_method': generator.GetStructFromMethod, |
374 } | 375 } |
375 | 376 |
376 def GetParameters(self): | 377 def GetParameters(self, args): |
377 return { | 378 return { |
378 "namespace": self.module.namespace, | 379 "namespace": self.module.namespace, |
379 "imports": self.GetImports(), | 380 "imports": self.GetImports(args), |
380 "kinds": self.module.kinds, | 381 "kinds": self.module.kinds, |
381 "enums": self.module.enums, | 382 "enums": self.module.enums, |
382 "module": self.module, | 383 "module": self.module, |
383 "structs": self.GetStructs() + self.GetStructsFromMethods(), | 384 "structs": self.GetStructs() + self.GetStructsFromMethods(), |
384 "interfaces": self.module.interfaces, | 385 "interfaces": self.module.interfaces, |
385 "imported_interfaces": self.GetImportedInterfaces(), | 386 "imported_interfaces": self.GetImportedInterfaces(), |
386 "imported_from": self.ImportedFrom(), | 387 "imported_from": self.ImportedFrom(), |
387 } | 388 } |
388 | 389 |
389 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) | 390 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) |
390 def GenerateLibModule(self): | 391 def GenerateLibModule(self, args): |
391 return self.GetParameters() | 392 return self.GetParameters(args) |
392 | 393 |
393 def GenerateFiles(self, args): | 394 def GenerateFiles(self, args): |
394 self.Write(self.GenerateLibModule(), | 395 self.Write(self.GenerateLibModule(args), |
395 self.MatchMojomFilePath("%s.dart" % self.module.name)) | 396 self.MatchMojomFilePath("%s.dart" % self.module.name)) |
396 | 397 |
397 def GetImports(self): | 398 def GetImports(self, args): |
399 mojo_root_arg = next( | |
400 (x for x in args if x.startswith("--dart_mojo_root")), "") | |
401 (_, _, mojo_root_path) = mojo_root_arg.partition("=") | |
tonyg
2015/03/26 21:06:12
Not sure, but should we be using argparse here or
zra
2015/03/26 23:33:20
Anything that doesn't start with --dart is filtere
| |
402 if not mojo_root_path.startswith("//"): | |
403 raise Exception("Malformed mojo SDK root: " + mojo_root_path) | |
404 mojo_root_path = mojo_root_path[2:] # strip // | |
398 used_names = set() | 405 used_names = set() |
399 for each_import in self.module.imports: | 406 for each_import in self.module.imports: |
400 simple_name = each_import["module_name"].split(".")[0] | 407 simple_name = each_import["module_name"].split(".")[0] |
401 | 408 |
402 # Since each import is assigned a library in Dart, they need to have | 409 # Since each import is assigned a library in Dart, they need to have |
403 # unique names. | 410 # unique names. |
404 unique_name = simple_name | 411 unique_name = simple_name |
405 counter = 0 | 412 counter = 0 |
406 while unique_name in used_names: | 413 while unique_name in used_names: |
407 counter += 1 | 414 counter += 1 |
408 unique_name = simple_name + str(counter) | 415 unique_name = simple_name + str(counter) |
409 | 416 |
410 used_names.add(unique_name) | 417 used_names.add(unique_name) |
411 each_import["unique_name"] = unique_name + '_mojom' | 418 each_import["unique_name"] = unique_name + '_mojom' |
412 counter += 1 | 419 counter += 1 |
420 | |
421 # At this point, a module's path is reletive to the root of the repo. | |
422 # However, imports of libraries from the Mojo SDK are always reletive to | |
423 # root of the Mojo SDK, which may be different from the root of the repo. | |
424 # This code uses the --dart_mojo_root argument to ensure that Mojo SDK | |
425 # imports are reletive to the Mojo SDK root. | |
426 path = each_import['module'].path | |
427 if os.path.commonprefix([mojo_root_path, path]) == mojo_root_path: | |
428 path = os.path.relpath(path, mojo_root_path) | |
429 each_import["rebased_path"] = path | |
413 return self.module.imports | 430 return self.module.imports |
414 | 431 |
415 def GetImportedInterfaces(self): | 432 def GetImportedInterfaces(self): |
416 interface_to_import = {} | 433 interface_to_import = {} |
417 for each_import in self.module.imports: | 434 for each_import in self.module.imports: |
418 for each_interface in each_import["module"].interfaces: | 435 for each_interface in each_import["module"].interfaces: |
419 name = each_interface.name | 436 name = each_interface.name |
420 interface_to_import[name] = each_import["unique_name"] + "." + name | 437 interface_to_import[name] = each_import["unique_name"] + "." + name |
421 return interface_to_import | 438 return interface_to_import |
422 | 439 |
423 def ImportedFrom(self): | 440 def ImportedFrom(self): |
424 interface_to_import = {} | 441 interface_to_import = {} |
425 for each_import in self.module.imports: | 442 for each_import in self.module.imports: |
426 for each_interface in each_import["module"].interfaces: | 443 for each_interface in each_import["module"].interfaces: |
427 name = each_interface.name | 444 name = each_interface.name |
428 interface_to_import[name] = each_import["unique_name"] + "." | 445 interface_to_import[name] = each_import["unique_name"] + "." |
429 return interface_to_import | 446 return interface_to_import |
OLD | NEW |