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

Side by Side Diff: third_party/WebKit/Source/platform/inspector_protocol/CodeGenerator.py

Issue 2238423002: [DevTools] Generate all files in inspector_protocol. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2240663003
Patch Set: Created 4 years, 4 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 2016 The Chromium Authors. All rights reserved. 1 # Copyright 2016 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 import os.path 5 import os.path
6 import sys 6 import sys
7 import optparse 7 import optparse
8 try: 8 try:
9 import json 9 import json
10 except ImportError: 10 except ImportError:
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 if "export" in config: 99 if "export" in config:
100 exporting = True 100 exporting = True
101 exported_dirname = config["export"]["output"] 101 exported_dirname = config["export"]["output"]
102 if not exported_dirname: 102 if not exported_dirname:
103 raise Exception("Config is missing export.output") 103 raise Exception("Config is missing export.output")
104 exported_dirname = os.path.join(output_base, exported_dirname) 104 exported_dirname = os.path.join(output_base, exported_dirname)
105 exported_package = config["export"]["package"] 105 exported_package = config["export"]["package"]
106 if not exported_package: 106 if not exported_package:
107 raise Exception("Config is missing export.package") 107 raise Exception("Config is missing export.package")
108 108
109 lib = False
110 if "lib" in config:
111 lib = True
112 lib_dirname = config["lib"]["output"]
113 if not lib_dirname:
114 raise Exception("Config is missing lib.output")
115 lib_dirname = os.path.join(output_base, lib_dirname)
116 lib_string16_include = config["lib"]["string16_impl_header_path"]
117 if not lib_string16_include:
118 raise Exception("Config is missing lib.string16_impl_header_path")
119 lib_platform_include = config["lib"]["platform_impl_header_path"]
120 if not lib_platform_include:
121 raise Exception("Config is missing lib.platform_impl_header_path")
122
109 string_type = config["string"]["class_name"] 123 string_type = config["string"]["class_name"]
110 if not string_type: 124 if not string_type:
111 raise Exception("Config is missing string.class_name") 125 raise Exception("Config is missing string.class_name")
112 126
113 export_macro = config["export_macro"] 127 export_macro = config["class_export"]["macro"]
114 if not export_macro: 128 if not export_macro:
115 raise Exception("Config is missing export_macro") 129 raise Exception("Config is missing class_export.macro")
130 export_macro_include = config["class_export"]["header_path"]
131 if not export_macro_include:
132 raise Exception("Config is missing class_export.header_path")
133
134 lib_package = config["lib_package"]
135 if not lib_package:
136 raise Exception("Config is missing lib_package")
116 except Exception: 137 except Exception:
117 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html 138 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html
118 exc = sys.exc_info()[1] 139 exc = sys.exc_info()[1]
119 sys.stderr.write("Failed to parse config file: %s\n\n" % exc) 140 sys.stderr.write("Failed to parse config file: %s\n\n" % exc)
120 exit(1) 141 exit(1)
121 142
122 143
123 # Make gyp / make generatos happy, otherwise make rebuilds world. 144 # Make gyp / make generatos happy, otherwise make rebuilds world.
124 def up_to_date(): 145 def up_to_date():
125 template_ts = max( 146 template_ts = max(
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 return result 418 return result
398 419
399 420
400 def has_disable(commands): 421 def has_disable(commands):
401 for command in commands: 422 for command in commands:
402 if command["name"] == "disable": 423 if command["name"] == "disable":
403 return True 424 return True
404 return False 425 return False
405 426
406 427
407 def generate_with_context(template_context, template, file_name):
408 out_file = output_file(file_name)
409 out_file.write(template.render(template_context))
410 out_file.close()
411
412
413 def generate(domain_object, template, file_name): 428 def generate(domain_object, template, file_name):
414 template_context = { 429 template_context = {
415 "domain": domain_object, 430 "domain": domain_object,
416 "join_arrays": join_arrays, 431 "join_arrays": join_arrays,
417 "resolve_type": resolve_type, 432 "resolve_type": resolve_type,
418 "type_definition": type_definition, 433 "type_definition": type_definition,
419 "has_disable": has_disable, 434 "has_disable": has_disable,
420 "export_macro": export_macro, 435 "export_macro": export_macro,
421 "output_package": output_package 436 "export_macro_include": export_macro_include,
437 "output_package": output_package,
438 "lib_package": lib_package
422 } 439 }
423 if exporting: 440 if exporting:
424 template_context["exported_package"] = exported_package 441 template_context["exported_package"] = exported_package
425 if importing: 442 if importing:
426 template_context["imported_package"] = imported_package 443 template_context["imported_package"] = imported_package
427 generate_with_context(template_context, template, file_name) 444 out_file = output_file(file_name)
445 out_file.write(template.render(template_context))
446 out_file.close()
428 447
429 448
430 def read_protocol_file(file_name, all_domains): 449 def read_protocol_file(file_name, all_domains):
431 input_file = open(file_name, "r") 450 input_file = open(file_name, "r")
432 json_string = input_file.read() 451 json_string = input_file.read()
433 parsed_json = json.loads(json_string) 452 parsed_json = json.loads(json_string)
434 domains = [] 453 domains = []
435 for domain in parsed_json["domains"]: 454 for domain in parsed_json["domains"]:
436 domains.append(domain["domain"]) 455 domains.append(domain["domain"])
437 all_domains["domains"] += parsed_json["domains"] 456 all_domains["domains"] += parsed_json["domains"]
438 return domains 457 return domains
439 458
440 459
460 def generate_lib():
461 template_context = {
462 "string16_impl_h_include": lib_string16_include,
463 "platform_impl_h_include": lib_platform_include,
464 "lib_package": lib_package,
465 "export_macro": export_macro,
466 "export_macro_include": export_macro_include
467 }
468
469 def generate_file(file_name, template_files):
470 out_file = output_file(file_name)
471 for template_file in template_files:
472 template = jinja_env.get_template("/" + template_file)
473 out_file.write(template.render(template_context))
474 out_file.write("\n\n")
475 out_file.close()
476
477 # Note these should be sorted in the right order.
478 # TODO(dgozman): sort them programmatically based on commented includes.
479 lib_h_templates = [
480 "Allocator_h.template",
481 "Platform_h.template",
482 "Collections_h.template",
483 "String16_h.template",
484
485 "ErrorSupport_h.template",
486 "Values_h.template",
487 "Object_h.template",
488 "ValueConversions_h.template",
489 "Maybe_h.template",
490 "Array_h.template",
491
492 "FrontendChannel_h.template",
493 "BackendCallback_h.template",
494 "DispatcherBase_h.template",
495
496 "Parser_h.template",
497 ]
498
499 lib_cpp_templates = [
500 "InspectorProtocol_cpp.template",
501
502 "String16_cpp.template",
503
504 "ErrorSupport_cpp.template",
505 "Values_cpp.template",
506 "Object_cpp.template",
507
508 "DispatcherBase_cpp.template",
509
510 "Parser_cpp.template",
511 ]
512
513 generate_file(os.path.join(lib_dirname, "InspectorProtocol.h"), lib_h_templa tes)
514 generate_file(os.path.join(lib_dirname, "InspectorProtocol.cpp"), lib_cpp_te mplates)
515
516
441 json_api = {"domains": []} 517 json_api = {"domains": []}
442 generate_domains = read_protocol_file(protocol_file, json_api) 518 generate_domains = read_protocol_file(protocol_file, json_api)
443 imported_domains = read_protocol_file(imported_file, json_api) if importing else [] 519 imported_domains = read_protocol_file(imported_file, json_api) if importing else []
444 patch_full_qualified_refs() 520 patch_full_qualified_refs()
445 calculate_exports() 521 calculate_exports()
446 create_type_definitions() 522 create_type_definitions()
447 523
448 if up_to_date(): 524 if up_to_date():
449 sys.exit() 525 sys.exit()
450 if not os.path.exists(output_dirname): 526 if not os.path.exists(output_dirname):
451 os.mkdir(output_dirname) 527 os.mkdir(output_dirname)
452 if json_api["has_exports"] and not os.path.exists(exported_dirname): 528 if json_api["has_exports"] and not os.path.exists(exported_dirname):
453 os.mkdir(exported_dirname) 529 os.mkdir(exported_dirname)
454 530
455 jinja_env = initialize_jinja_env(output_dirname) 531 jinja_env = initialize_jinja_env(output_dirname)
456 h_template = jinja_env.get_template("/TypeBuilder_h.template") 532 h_template = jinja_env.get_template("/TypeBuilder_h.template")
457 cpp_template = jinja_env.get_template("/TypeBuilder_cpp.template") 533 cpp_template = jinja_env.get_template("/TypeBuilder_cpp.template")
458 exported_template = jinja_env.get_template("/Exported_h.template") 534 exported_template = jinja_env.get_template("/Exported_h.template")
459 imported_template = jinja_env.get_template("/Imported_h.template") 535 imported_template = jinja_env.get_template("/Imported_h.template")
460 536
461 for domain in json_api["domains"]: 537 for domain in json_api["domains"]:
462 class_name = domain["domain"] 538 class_name = domain["domain"]
463 if domain["domain"] in generate_domains: 539 if domain["domain"] in generate_domains:
464 generate(domain, h_template, os.path.join(output_dirname, class_name + " .h")) 540 generate(domain, h_template, os.path.join(output_dirname, class_name + " .h"))
465 generate(domain, cpp_template, os.path.join(output_dirname, class_name + ".cpp")) 541 generate(domain, cpp_template, os.path.join(output_dirname, class_name + ".cpp"))
466 if domain["has_exports"]: 542 if domain["has_exports"]:
467 generate(domain, exported_template, os.path.join(exported_dirname, c lass_name + ".h")) 543 generate(domain, exported_template, os.path.join(exported_dirname, c lass_name + ".h"))
468 if domain["domain"] in imported_domains and domain["has_exports"]: 544 if domain["domain"] in imported_domains and domain["has_exports"]:
469 generate(domain, imported_template, os.path.join(output_dirname, class_n ame + ".h")) 545 generate(domain, imported_template, os.path.join(output_dirname, class_n ame + ".h"))
546
547 if lib:
548 generate_lib()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698