# HG changeset patch # User Victor Porof # Date 1562244620 -7200 # Node ID 60169d9efd0d99bd6c9d891288c0b740f65a4571 # Parent e93723159f4787e021292297ce44e4c51504a691 Bug 1556393 - Pre 0 (m-c): Create a simple Prettier command for running format-source, r=andi Differential Revision: https://phabricator.services.mozilla.com/D36720 diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py --- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py +++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py @@ -24,16 +24,17 @@ from mach.decorators import ( SubCommand, ) from mach.main import Mach from mozbuild.base import MachCommandBase from mozbuild.build_commands import Build +from mozbuild.nodeutil import find_node_executable import mozpack.path as mozpath from mozversioncontrol import get_repository_object # Function used to run clang-format on a batch of files. It is a helper function # in order to integrate into the futures ecosystem clang-format. @@ -1322,16 +1323,43 @@ class StaticAnalysis(MachCommandBase): if rc != 0: return rc checkers, _ = self._get_infer_config() print('Infer checks:') for checker in checkers: print(' '*4 + checker) return 0 + @Command('prettier-format', category='misc', description='Run prettier on current changes') + @CommandArgument('--path', '-p', nargs=1, required=True, + help='Specify the path to reformat to stdout.') + @CommandArgument('--assume-filename', '-a', nargs=1, required=True, + help='This option is usually used in the context of hg-formatsource.' + 'When reading from stdin, Prettier assumes this ' + 'filename to decide which style and parser to use.') + def prettier_format(self, path, assume_filename): + # With assume_filename we want to have stdout clean since the result of the + # format will be redirected to stdout. + + binary, _ = find_node_executable() + prettier = os.path.join(self.topsrcdir, "node_modules", "prettier", "bin-prettier.js") + path = os.path.join(self.topsrcdir, path[0]) + + # We use --stdin-filepath in order to better determine the path for + # the prettier formatter when it is ran outside of the repo, for example + # by the extension hg-formatsource. + args = [binary, prettier, '--stdin-filepath', assume_filename[0]] + + process = subprocess.Popen(args, stdin=subprocess.PIPE) + with open(path, 'r') as fin: + process.stdin.write(fin.read()) + process.stdin.close() + process.wait() + return process.returncode + @Command('clang-format', category='misc', description='Run clang-format on current changes') @CommandArgument('--show', '-s', action='store_const', const='stdout', dest='output_path', help='Show diff output on stdout instead of applying changes') @CommandArgument('--assume-filename', '-a', nargs=1, default=None, help='This option is usually used in the context of hg-formatsource.' 'When reading from stdin, clang-format assumes this ' 'filename to look for a style config file (with ' '-style=file) and to determine the language. When '