mach_commands.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. """
  5. This script implements the `mach devtools-css-db` command. It runs an xpcshell script
  6. that uses inIDOMUtils to query the CSS properties used by the browser. This information
  7. is used to generate the properties-db.js file.
  8. """
  9. import json
  10. import os
  11. import sys
  12. import string
  13. import subprocess
  14. from mozbuild.base import (
  15. MozbuildObject,
  16. MachCommandBase,
  17. )
  18. from mach.decorators import (
  19. CommandProvider,
  20. Command,
  21. )
  22. def resolve_path(start, relativePath):
  23. """Helper to resolve a path from a start, and a relative path"""
  24. return os.path.normpath(os.path.join(start, relativePath))
  25. def stringify(obj):
  26. """Helper to stringify to JSON"""
  27. return json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': '))
  28. @CommandProvider
  29. class MachCommands(MachCommandBase):
  30. @Command(
  31. 'devtools-css-db', category='post-build',
  32. description='Rebuild the devtool\'s static css properties database.')
  33. def generate_css_db(self):
  34. """Generate the static css properties database for devtools and write it to file."""
  35. print("Re-generating the css properties database...")
  36. db = self.get_properties_db_from_xpcshell()
  37. self.output_template({
  38. 'cssProperties': stringify(db['cssProperties']),
  39. 'pseudoElements': stringify(db['pseudoElements'])})
  40. def get_properties_db_from_xpcshell(self):
  41. """Generate the static css properties db for devtools from an xpcshell script."""
  42. build = MozbuildObject.from_environment()
  43. # Get the paths
  44. script_path = resolve_path(self.topsrcdir,
  45. 'devtools/shared/css/generated/generate-properties-db.js')
  46. gre_path = resolve_path(self.topobjdir, 'dist/bin')
  47. browser_path = resolve_path(self.topobjdir, 'dist/bin/browser')
  48. xpcshell_path = build.get_binary_path(what='xpcshell')
  49. print(browser_path)
  50. sub_env = dict(os.environ)
  51. if sys.platform.startswith('linux'):
  52. sub_env["LD_LIBRARY_PATH"] = gre_path
  53. # Run the xcpshell script, and set the appdir flag to the browser path so that
  54. # we have the proper dependencies for requiring the loader.
  55. contents = subprocess.check_output([xpcshell_path, '-g', gre_path,
  56. '-a', browser_path, script_path],
  57. env = sub_env)
  58. # Extract just the output between the delimiters as the xpcshell output can
  59. # have extra output that we don't want.
  60. contents = contents.split('DEVTOOLS_CSS_DB_DELIMITER')[1]
  61. return json.loads(contents)
  62. def output_template(self, substitutions):
  63. """Output a the properties-db.js from a template."""
  64. js_template_path = resolve_path(self.topsrcdir,
  65. 'devtools/shared/css/generated/properties-db.js.in')
  66. destination_path = resolve_path(self.topsrcdir,
  67. 'devtools/shared/css/generated/properties-db.js')
  68. with open(js_template_path, 'rb') as handle:
  69. js_template = handle.read()
  70. preamble = '/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT */\n\n'
  71. contents = string.Template(js_template).substitute(substitutions)
  72. with open(destination_path, 'wb') as destination:
  73. destination.write(preamble + contents)
  74. print('The database was successfully generated at ' + destination_path)