Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_dart_generator.py

Issue 1071693003: Uses mojom module names as Dart's package: import URI (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 os
8 import re 8 import re
9 9
10 import mojom.generate.generator as generator 10 import mojom.generate.generator as generator
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 return getattr(kind, 'dart_map_size', str(kind.length)) 345 return getattr(kind, 'dart_map_size', str(kind.length))
346 else: 346 else:
347 return 'bindings.kUnspecifiedArrayLength' 347 return 'bindings.kUnspecifiedArrayLength'
348 348
349 def IsPointerArrayKind(kind): 349 def IsPointerArrayKind(kind):
350 if not mojom.IsArrayKind(kind): 350 if not mojom.IsArrayKind(kind):
351 return False 351 return False
352 sub_kind = kind.kind 352 sub_kind = kind.kind
353 return mojom.IsObjectKind(sub_kind) 353 return mojom.IsObjectKind(sub_kind)
354 354
355 def ParseStringAttribute(attribute):
356 assert isinstance(attribute, basestring)
357 return attribute
358
359 def GetDartUriBase(module):
360 if module.attributes and 'DartUriBase' in module.attributes:
361 return ParseStringAttribute(module.attributes['DartUriBase'])
362 # Default Uri is the module's path in the host repo.
363 return os.path.dirname(module.path)
364
355 class Generator(generator.Generator): 365 class Generator(generator.Generator):
356 366
357 dart_filters = { 367 dart_filters = {
358 'array_expected_length': GetArrayExpectedLength, 368 'array_expected_length': GetArrayExpectedLength,
359 'array': GetArrayKind, 369 'array': GetArrayKind,
360 'decode_method': DecodeMethod, 370 'decode_method': DecodeMethod,
361 'default_value': DartDefaultValue, 371 'default_value': DartDefaultValue,
362 'encode_method': EncodeMethod, 372 'encode_method': EncodeMethod,
363 'expression_to_text': ExpressionToText, 373 'expression_to_text': ExpressionToText,
364 'is_handle': mojom.IsNonInterfaceHandleKind, 374 'is_handle': mojom.IsNonInterfaceHandleKind,
(...skipping 24 matching lines...) Expand all
389 399
390 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) 400 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters)
391 def GenerateLibModule(self, args): 401 def GenerateLibModule(self, args):
392 return self.GetParameters(args) 402 return self.GetParameters(args)
393 403
394 def GenerateFiles(self, args): 404 def GenerateFiles(self, args):
395 self.Write(self.GenerateLibModule(args), 405 self.Write(self.GenerateLibModule(args),
396 self.MatchMojomFilePath("%s.dart" % self.module.name)) 406 self.MatchMojomFilePath("%s.dart" % self.module.name))
397 407
398 def GetImports(self, args): 408 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("=")
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 //
405 used_names = set() 409 used_names = set()
406 for each_import in self.module.imports: 410 for each_import in self.module.imports:
407 simple_name = each_import["module_name"].split(".")[0] 411 simple_name = each_import["module_name"].split(".")[0]
408 412
409 # Since each import is assigned a library in Dart, they need to have 413 # Since each import is assigned a library in Dart, they need to have
410 # unique names. 414 # unique names.
411 unique_name = simple_name 415 unique_name = simple_name
412 counter = 0 416 counter = 0
413 while unique_name in used_names: 417 while unique_name in used_names:
414 counter += 1 418 counter += 1
415 unique_name = simple_name + str(counter) 419 unique_name = simple_name + str(counter)
416 420
417 used_names.add(unique_name) 421 used_names.add(unique_name)
418 each_import["unique_name"] = unique_name + '_mojom' 422 each_import["unique_name"] = unique_name + '_mojom'
419 counter += 1 423 counter += 1
420 424
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 425 path = each_import['module'].path
427 if os.path.commonprefix([mojo_root_path, path]) == mojo_root_path: 426 dart_uri = (GetDartUriBase(each_import['module']) + '/' +
428 path = os.path.relpath(path, mojo_root_path) 427 os.path.basename(path))
429 each_import["rebased_path"] = path 428
429 each_import["rebased_path"] = dart_uri
430 return self.module.imports 430 return self.module.imports
431 431
432 def GetImportedInterfaces(self): 432 def GetImportedInterfaces(self):
433 interface_to_import = {} 433 interface_to_import = {}
434 for each_import in self.module.imports: 434 for each_import in self.module.imports:
435 for each_interface in each_import["module"].interfaces: 435 for each_interface in each_import["module"].interfaces:
436 name = each_interface.name 436 name = each_interface.name
437 interface_to_import[name] = each_import["unique_name"] + "." + name 437 interface_to_import[name] = each_import["unique_name"] + "." + name
438 return interface_to_import 438 return interface_to_import
439 439
440 def ImportedFrom(self): 440 def ImportedFrom(self):
441 interface_to_import = {} 441 interface_to_import = {}
442 for each_import in self.module.imports: 442 for each_import in self.module.imports:
443 for each_interface in each_import["module"].interfaces: 443 for each_interface in each_import["module"].interfaces:
444 name = each_interface.name 444 name = each_interface.name
445 interface_to_import[name] = each_import["unique_name"] + "." 445 interface_to_import[name] = each_import["unique_name"] + "."
446 return interface_to_import 446 return interface_to_import
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698