Bumping manifests a=b2g-bump
[gecko.git] / addon-sdk / mach_commands.py
blobf0ab64b3bc3f939e44baac496d893b9407a776ac
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/.
5 # Integrates the xpcshell test runner with mach.
7 import os
8 import re
9 import sys
11 import mozpack.path as mozpath
13 from mozbuild.base import (
14 MachCommandBase,
15 MozbuildObject,
18 from mach.decorators import (
19 CommandArgument,
20 CommandProvider,
21 Command,
24 class JetpackRunner(MozbuildObject):
25 """Run jetpack tests."""
26 def run_tests(self, **kwargs):
27 self._run_make(target='jetpack-tests')
29 @CommandProvider
30 class MachCommands(MachCommandBase):
31 @Command('jetpack-test', category='testing',
32 description='Runs the jetpack test suite (Add-on SDK).')
33 def run_jetpack_test(self, **params):
34 # We should probably have a utility function to ensure the tree is
35 # ready to run tests. Until then, we just create the state dir (in
36 # case the tree wasn't built with mach).
37 self._ensure_state_subdir_exists('.')
39 jetpack = self._spawn(JetpackRunner)
41 jetpack.run_tests(**params)
43 @Command('generate-addon-sdk-moz-build', category='misc',
44 description='Generates the moz.build file for the addon-sdk/ directory.')
45 def run_addon_sdk_moz_build(self, **params):
46 addon_sdk_dir = mozpath.join(self.topsrcdir, 'addon-sdk')
47 js_src_dir = mozpath.join(addon_sdk_dir, 'source/lib')
48 dirs_to_files = {}
50 for path, dirs, files in os.walk(js_src_dir):
51 js_files = [f for f in files if f.endswith(('.js', '.jsm', '.html'))]
52 if not js_files:
53 continue
55 relative = mozpath.relpath(path, js_src_dir)
56 dirs_to_files[relative] = js_files
58 moz_build = """# AUTOMATICALLY GENERATED FROM mozbuild.template AND mach. DO NOT EDIT.
59 # This Source Code Form is subject to the terms of the Mozilla Public
60 # License, v. 2.0. If a copy of the MPL was not distributed with this
61 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
63 %(moz-build-template)s
64 if CONFIG['MOZ_WIDGET_TOOLKIT'] != "gonk":
65 %(non-b2g-modules)s
66 %(always-on-modules)s"""
68 non_b2g_paths = [
69 'method/test',
70 'sdk/ui',
71 'sdk/ui/button',
72 'sdk/ui/sidebar',
73 'sdk/places',
74 'sdk/places/host',
75 'sdk/tabs',
76 'sdk/panel',
77 'sdk/frame',
78 'sdk/test',
79 'sdk/window',
80 'sdk/windows',
81 'sdk/deprecated',
84 non_b2g_modules = []
85 always_on_modules = []
87 for d, files in sorted(dirs_to_files.items()):
88 if d in non_b2g_paths:
89 non_b2g_modules.append((d, files))
90 else:
91 always_on_modules.append((d, files))
93 def list_to_js_modules(l, indent=''):
94 js_modules = []
95 for d, files in l:
96 if d == '':
97 module_path = ''
98 dir_path = ''
99 else:
100 # Ensure that we don't have things like:
101 # EXTRA_JS_MODULES.commonjs.sdk.private-browsing
102 # which would be a Python syntax error.
103 path = d.split('/')
104 module_path = ''.join('.' + p if p.find('-') == -1 else "['%s']" % p for p in path)
105 dir_path = d + '/'
106 filelist = ["'source/lib/%s%s'" % (dir_path, f)
107 for f in sorted(files, key=lambda x: x.lower())]
108 js_modules.append("EXTRA_JS_MODULES.commonjs%s += [\n %s,\n]\n"
109 % (module_path, ',\n '.join(filelist)))
110 stringified = '\n'.join(js_modules)
111 # This isn't the same thing as |js_modules|, since |js_modules| had
112 # embedded newlines.
113 lines = stringified.split('\n')
114 # Indent lines while avoiding trailing whitespace.
115 lines = [indent + line if line else line for line in lines]
116 return '\n'.join(lines)
118 moz_build_output = mozpath.join(addon_sdk_dir, 'moz.build')
119 moz_build_template = mozpath.join(addon_sdk_dir, 'mozbuild.template')
120 with open(moz_build_output, 'w') as f, open(moz_build_template, 'r') as t:
121 substs = { 'moz-build-template': t.read(),
122 'non-b2g-modules': list_to_js_modules(non_b2g_modules,
123 indent=' '),
124 'always-on-modules': list_to_js_modules(always_on_modules) }
125 f.write(moz_build % substs)