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

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: Merge 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
« no previous file with comments | « mojo/public/interfaces/application/BUILD.gn ('k') | mojo/public/tools/bindings/mojom.gni » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 GetImportUri(module):
356 elements = module.namespace.split('.')
357 elements.append("%s" % module.name)
358 return os.path.join(*elements)
359
355 class Generator(generator.Generator): 360 class Generator(generator.Generator):
356 361
357 dart_filters = { 362 dart_filters = {
358 'array_expected_length': GetArrayExpectedLength, 363 'array_expected_length': GetArrayExpectedLength,
359 'array': GetArrayKind, 364 'array': GetArrayKind,
360 'decode_method': DecodeMethod, 365 'decode_method': DecodeMethod,
361 'default_value': DartDefaultValue, 366 'default_value': DartDefaultValue,
362 'encode_method': EncodeMethod, 367 'encode_method': EncodeMethod,
363 'expression_to_text': ExpressionToText, 368 'expression_to_text': ExpressionToText,
364 'is_handle': mojom.IsNonInterfaceHandleKind, 369 'is_handle': mojom.IsNonInterfaceHandleKind,
(...skipping 18 matching lines...) Expand all
383 "interfaces": self.GetInterfaces(), 388 "interfaces": self.GetInterfaces(),
384 "imported_interfaces": self.GetImportedInterfaces(), 389 "imported_interfaces": self.GetImportedInterfaces(),
385 "imported_from": self.ImportedFrom(), 390 "imported_from": self.ImportedFrom(),
386 } 391 }
387 392
388 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) 393 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters)
389 def GenerateLibModule(self, args): 394 def GenerateLibModule(self, args):
390 return self.GetParameters(args) 395 return self.GetParameters(args)
391 396
392 def GenerateFiles(self, args): 397 def GenerateFiles(self, args):
393 self.Write(self.GenerateLibModule(args), 398 elements = self.module.namespace.split('.')
394 self.MatchMojomFilePath("%s.dart" % self.module.name)) 399 elements.append("%s.dart" % self.module.name)
400 path = os.path.join("dart-gen", *elements)
401 self.Write(self.GenerateLibModule(args), path)
402 link = self.MatchMojomFilePath("%s.dart" % self.module.name)
403 if os.path.exists(os.path.join(self.output_dir, link)):
404 os.unlink(os.path.join(self.output_dir, link))
405 os.symlink(os.path.join(self.output_dir, path),
406 os.path.join(self.output_dir, link))
395 407
396 def GetImports(self, args): 408 def GetImports(self, args):
397 mojo_root_arg = next(
398 (x for x in args if x.startswith("--dart_mojo_root")), "")
399 (_, _, mojo_root_path) = mojo_root_arg.partition("=")
400 if not mojo_root_path.startswith("//"):
401 raise Exception("Malformed mojo SDK root: " + mojo_root_path)
402 mojo_root_path = mojo_root_path[2:] # strip //
403 used_names = set() 409 used_names = set()
404 for each_import in self.module.imports: 410 for each_import in self.module.imports:
405 simple_name = each_import["module_name"].split(".")[0] 411 simple_name = each_import["module_name"].split(".")[0]
406 412
407 # 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
408 # unique names. 414 # unique names.
409 unique_name = simple_name 415 unique_name = simple_name
410 counter = 0 416 counter = 0
411 while unique_name in used_names: 417 while unique_name in used_names:
412 counter += 1 418 counter += 1
413 unique_name = simple_name + str(counter) 419 unique_name = simple_name + str(counter)
414 420
415 used_names.add(unique_name) 421 used_names.add(unique_name)
416 each_import["unique_name"] = unique_name + '_mojom' 422 each_import["unique_name"] = unique_name + '_mojom'
417 counter += 1 423 counter += 1
418 424
419 # At this point, a module's path is reletive to the root of the repo. 425 each_import["rebased_path"] = GetImportUri(each_import['module'])
420 # However, imports of libraries from the Mojo SDK are always reletive to
421 # root of the Mojo SDK, which may be different from the root of the repo.
422 # This code uses the --dart_mojo_root argument to ensure that Mojo SDK
423 # imports are reletive to the Mojo SDK root.
424 path = each_import['module'].path
425 if os.path.commonprefix([mojo_root_path, path]) == mojo_root_path:
426 path = os.path.relpath(path, mojo_root_path)
427 each_import["rebased_path"] = path
428 return self.module.imports 426 return self.module.imports
429 427
430 def GetImportedInterfaces(self): 428 def GetImportedInterfaces(self):
431 interface_to_import = {} 429 interface_to_import = {}
432 for each_import in self.module.imports: 430 for each_import in self.module.imports:
433 for each_interface in each_import["module"].interfaces: 431 for each_interface in each_import["module"].interfaces:
434 name = each_interface.name 432 name = each_interface.name
435 interface_to_import[name] = each_import["unique_name"] + "." + name 433 interface_to_import[name] = each_import["unique_name"] + "." + name
436 return interface_to_import 434 return interface_to_import
437 435
438 def ImportedFrom(self): 436 def ImportedFrom(self):
439 interface_to_import = {} 437 interface_to_import = {}
440 for each_import in self.module.imports: 438 for each_import in self.module.imports:
441 for each_interface in each_import["module"].interfaces: 439 for each_interface in each_import["module"].interfaces:
442 name = each_interface.name 440 name = each_interface.name
443 interface_to_import[name] = each_import["unique_name"] + "." 441 interface_to_import[name] = each_import["unique_name"] + "."
444 return interface_to_import 442 return interface_to_import
OLDNEW
« no previous file with comments | « mojo/public/interfaces/application/BUILD.gn ('k') | mojo/public/tools/bindings/mojom.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698