OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import argparse | 5 import argparse |
6 import os | 6 import os |
| 7 import subprocess |
7 import sys | 8 import sys |
8 | 9 |
9 | 10 |
| 11 def CompileXCAssets(output, platform, min_deployment_target, inputs): |
| 12 command = [ |
| 13 'xcrun', 'actool', '--output-format=human-readable-text', |
| 14 '--compress-pngs', '--notices', '--warnings', '--errors', |
| 15 '--platform', platform, '--minimum-deployment-target', |
| 16 min_deployment_target, |
| 17 ] |
| 18 |
| 19 if platform == 'macosx': |
| 20 command.extend(['--target-device', 'mac']) |
| 21 else: |
| 22 command.extend(['--target-device', 'iphone', '--target-device', 'ipad']) |
| 23 |
| 24 # actool crashes if paths are relative, so convert input and output paths |
| 25 # to absolute paths. |
| 26 command.extend(['--compile', os.path.dirname(os.path.abspath(output))]) |
| 27 command.extend(map(os.path.abspath, inputs)) |
| 28 |
| 29 # Run actool and redirect stdout and stderr to the same pipe (as actool |
| 30 # is confused about what should go to stderr/stdout). |
| 31 process = subprocess.Popen( |
| 32 command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 33 stdout, _ = process.communicate() |
| 34 |
| 35 if process.returncode: |
| 36 sys.stderr.write(stdout) |
| 37 sys.exit(process.returncode) |
| 38 |
| 39 # In case of success, the output looks like the following: |
| 40 # /* com.apple.actool.compilation-results */ |
| 41 # /Full/Path/To/Bundle.app/Assets.car |
| 42 # |
| 43 # Ignore any lines in the output matching those (last line is an empty line) |
| 44 # and consider that the build failed if the output contains any other lines. |
| 45 for line in stdout.splitlines(): |
| 46 if not line: |
| 47 continue |
| 48 if line == '/* com.apple.actool.compilation-results */': |
| 49 continue |
| 50 if line == os.path.abspath(output): |
| 51 continue |
| 52 sys.stderr.write(stdout) |
| 53 sys.exit(1) |
| 54 |
| 55 |
10 def Main(): | 56 def Main(): |
11 parser = argparse.ArgumentParser( | 57 parser = argparse.ArgumentParser( |
12 description='compile assets catalog for a bundle') | 58 description='compile assets catalog for a bundle') |
13 parser.add_argument( | 59 parser.add_argument( |
14 '--platform', '-p', required=True, | 60 '--platform', '-p', required=True, |
15 choices=('macosx', 'iphoneos', 'iphonesimulator'), | 61 choices=('macosx', 'iphoneos', 'iphonesimulator'), |
16 help='target platform for the compiled assets catalog') | 62 help='target platform for the compiled assets catalog') |
17 parser.add_argument( | 63 parser.add_argument( |
18 '--minimum-deployment-target', '-t', required=True, | 64 '--minimum-deployment-target', '-t', required=True, |
19 help='minimum deployment target for the compiled assets catalog') | 65 help='minimum deployment target for the compiled assets catalog') |
20 parser.add_argument( | 66 parser.add_argument( |
21 '--output', '-o', required=True, | 67 '--output', '-o', required=True, |
22 help='path to the compiled assets catalog') | 68 help='path to the compiled assets catalog') |
23 parser.add_argument( | 69 parser.add_argument( |
24 'inputs', nargs='+', | 70 'inputs', nargs='+', |
25 help='path to input assets catalog sources') | 71 help='path to input assets catalog sources') |
26 args = parser.parse_args() | 72 args = parser.parse_args() |
27 | 73 |
28 if os.path.basename(args.output) != 'Assets.car': | 74 if os.path.basename(args.output) != 'Assets.car': |
29 sys.stderr.write( | 75 sys.stderr.write( |
30 'output should be path to compiled asset catalog, not ' | 76 'output should be path to compiled asset catalog, not ' |
31 'to the containing bundle: %s\n' % (args.output,)) | 77 'to the containing bundle: %s\n' % (args.output,)) |
| 78 sys.exit(1) |
32 | 79 |
33 command = [ | 80 CompileXCAssets( |
34 'xcrun', 'actool', '--output-format', 'human-readable-text', | 81 args.output, |
35 '--compress-pngs', '--notices', '--warnings', '--errors', | 82 args.platform, |
36 '--platform', args.platform, '--minimum-deployment-target', | |
37 args.minimum_deployment_target, | 83 args.minimum_deployment_target, |
38 ] | 84 args.inputs) |
39 | 85 |
40 if args.platform == 'macosx': | |
41 command.extend(['--target-device', 'mac']) | |
42 else: | |
43 command.extend(['--target-device', 'iphone', '--target-device', 'ipad']) | |
44 | |
45 # actool crashes if paths are relative, so use os.path.abspath to get absolute | |
46 # path for input and outputs. | |
47 command.extend(['--compile', os.path.abspath(os.path.dirname(args.output))]) | |
48 command.extend(map(os.path.abspath, args.inputs)) | |
49 | |
50 os.execvp('xcrun', command) | |
51 sys.exit(1) | |
52 | 86 |
53 if __name__ == '__main__': | 87 if __name__ == '__main__': |
54 sys.exit(Main()) | 88 sys.exit(Main()) |
OLD | NEW |