summaryrefslogtreecommitdiffstats
path: root/chrome/test/chromedriver/embed_js_in_cpp.py
blob: 1425e4d49070dfda4d7e189e746fadf6448d9d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Embeds standalone JavaScript snippets in C++ code.

Each argument to the script must be a file containing an associated JavaScript
function (e.g., evaluate_script.js should contain an evaluateScript function).
This is called the exported function of the script. The entire script will be
put into a C-style string in the form of an anonymous function which invokes
the exported function when called.
"""

import optparse
import os
import sys

import cpp_source


def main():
  parser = optparse.OptionParser()
  parser.add_option(
      '', '--directory', type='string', default='.',
      help='Path to directory where the cc/h js file should be created')
  options, args = parser.parse_args()

  global_string_map = {}
  for js_file in args:
    base_name = os.path.basename(js_file)[:-3].title().replace('_', '')
    func_name = base_name[0].lower() + base_name[1:]
    script_name = 'k%sScript' % base_name
    with open(js_file, 'r') as f:
      contents = f.read()
    script = 'function() { %s; return %s.apply(null, arguments) }' % (
        contents, func_name)
    global_string_map[script_name] = script

  cpp_source.WriteSource('js', 'chrome/test/chromedriver/chrome',
                         options.directory, global_string_map)


if __name__ == '__main__':
  sys.exit(main())