blob: df31e7cc48878e2bfd5d47a5dcfe5f7cf4310e24 [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):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000018 """A file could not be found."""
19 def __init__(self, e):
20 Exception.__init__(
21 self,
22 'Problem while looking for swift-format in Chromium source tree:\n'
23 '%s' % e)
Olivier Robin0a6b5442022-04-07 07:25:04 +000024
25
26def FindSwiftFormatToolInChromiumTree():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000027 """Return a path to the rustfmt executable, or die trying."""
28 chromium_src_path = gclient_paths.GetPrimarySolutionPath()
29 if not chromium_src_path:
30 raise NotFoundError(
31 'Could not find checkout in any parent of the current path.\n'
32 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium '
33 'checkout.')
Olivier Robin0a6b5442022-04-07 07:25:04 +000034
Mike Frysinger124bb8e2023-09-06 05:48:55 +000035 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
Olivier Robin0a6b5442022-04-07 07:25:04 +000040
41
42def IsSwiftFormatSupported():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000043 if sys.platform != 'darwin':
44 return False
45 try:
46 FindSwiftFormatToolInChromiumTree()
47 return True
48 except NotFoundError:
49 return False
Olivier Robin0a6b5442022-04-07 07:25:04 +000050
51
52def main(args):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000053 try:
54 tool = FindSwiftFormatToolInChromiumTree()
55 except NotFoundError as e:
56 sys.stderr.write("%s\n" % str(e))
57 return 1
Olivier Robin0a6b5442022-04-07 07:25:04 +000058
Mike Frysinger124bb8e2023-09-06 05:48:55 +000059 # 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)
Olivier Robin0a6b5442022-04-07 07:25:04 +000065
Mike Frysinger124bb8e2023-09-06 05:48:55 +000066 return subprocess.call([tool] + args)
Olivier Robin0a6b5442022-04-07 07:25:04 +000067
68
69if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000070 try:
71 sys.exit(main(sys.argv[1:]))
72 except KeyboardInterrupt:
73 sys.stderr.write('interrupted\n')
74 sys.exit(1)