blob: 219211da7cbb4652a0f1bd5ab083a00482152872 [file] [log] [blame]
Olivier Robin0a6b5442022-04-07 07:25:04 +00001#!/usr/bin/env python3
2# Copyright 2022 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Redirects to the version of swift-format present in the Chrome tree.
6
7Swift format binaries are pulled down from CIPD whenever you sync Chrome.
8This script knows how to locate those tools, assuming the script is
9invoked from inside a Chromium checkout."""
10
11import gclient_paths
12import os
13import subprocess
14import sys
15
16
17class NotFoundError(Exception):
18 """A file could not be found."""
19
20 def __init__(self, e):
21 Exception.__init__(
22 self,
23 'Problem while looking for swift-format in Chromium source tree:\n'
24 '%s' % e)
25
26
27def FindSwiftFormatToolInChromiumTree():
28 """Return a path to the rustfmt executable, or die trying."""
29 chromium_src_path = gclient_paths.GetPrimarySolutionPath()
30 if not chromium_src_path:
31 raise NotFoundError(
32 'Could not find checkout in any parent of the current path.\n'
33 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.')
34
35 tool_path = os.path.join(chromium_src_path, 'third_party', 'swift-format',
36 'swift-format')
37 if not os.path.exists(tool_path):
38 raise NotFoundError('File does not exist: %s' % tool_path)
39 return tool_path
40
41
42def IsSwiftFormatSupported():
Olivier Robin7f39e3d2022-04-28 08:20:49 +000043 if sys.platform != 'darwin':
44 return False
Olivier Robin0a6b5442022-04-07 07:25:04 +000045 try:
46 FindSwiftFormatToolInChromiumTree()
47 return True
48 except NotFoundError:
49 return False
50
51
52def main(args):
53 try:
54 tool = FindSwiftFormatToolInChromiumTree()
55 except NotFoundError as e:
56 sys.stderr.write("%s\n" % str(e))
57 return 1
58
59 # Add some visibility to --help showing where the tool lives, since this
60 # redirection can be a little opaque.
61 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
62 if any(match in args for match in help_syntax):
63 print('\nDepot tools redirects you to the swift-format at:\n %s\n' %
64 tool)
65
66 return subprocess.call([tool] + args)
67
68
69if __name__ == '__main__':
70 try:
71 sys.exit(main(sys.argv[1:]))
72 except KeyboardInterrupt:
73 sys.stderr.write('interrupted\n')
74 sys.exit(1)