OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 'dart:io'; | 5 import 'dart:io'; |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 | 7 |
8 import 'package:archive/archive.dart'; | 8 import 'package:archive/archive.dart'; |
9 import 'package:args/args.dart'; | 9 import 'package:args/args.dart'; |
10 import 'package:yaml/yaml.dart'; | 10 import 'package:yaml/yaml.dart'; |
11 | 11 |
12 const String kSnapshotKey = 'snapshot_blob.bin'; | 12 const String kSnapshotKey = 'snapshot_blob.bin'; |
13 const List<String> kDensities = const ['drawable-xxhdpi']; | 13 const List<String> kDensities = const ['drawable-xxhdpi']; |
14 const List<String> kThemes = const ['white', 'black', 'grey600']; | 14 const List<String> kThemes = const ['white', 'black', 'grey600']; |
15 const List<int> kSizes = const [24]; | 15 const List<int> kSizes = const [24]; |
16 | 16 |
| 17 class Asset { |
| 18 final String base; |
| 19 final String key; |
| 20 |
| 21 Asset({ this.base, this.key }); |
| 22 } |
| 23 |
| 24 Iterable<Asset> parseAssets(Map manifestDescriptor, String manifestPath) sync* { |
| 25 if (manifestDescriptor == null || !manifestDescriptor.containsKey('assets')) |
| 26 return; |
| 27 String basePath = new File(manifestPath).parent.path; |
| 28 for (String asset in manifestDescriptor['assets']) |
| 29 yield new Asset(base: basePath, key: asset); |
| 30 } |
| 31 |
17 class MaterialAsset { | 32 class MaterialAsset { |
18 final String name; | 33 final String name; |
19 final String density; | 34 final String density; |
20 final String theme; | 35 final String theme; |
21 final int size; | 36 final int size; |
22 | 37 |
23 MaterialAsset(Map descriptor) | 38 MaterialAsset(Map descriptor) |
24 : name = descriptor['name'], | 39 : name = descriptor['name'], |
25 density = descriptor['density'], | 40 density = descriptor['density'], |
26 theme = descriptor['theme'], | 41 theme = descriptor['theme'], |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 79 } |
65 } | 80 } |
66 | 81 |
67 Future loadManifest(String manifestPath) async { | 82 Future loadManifest(String manifestPath) async { |
68 if (manifestPath == null) | 83 if (manifestPath == null) |
69 return null; | 84 return null; |
70 String manifestDescriptor = await new File(manifestPath).readAsString(); | 85 String manifestDescriptor = await new File(manifestPath).readAsString(); |
71 return loadYaml(manifestDescriptor); | 86 return loadYaml(manifestDescriptor); |
72 } | 87 } |
73 | 88 |
74 Future<ArchiveFile> createFile(MaterialAsset asset, String assetBase) async { | 89 Future<ArchiveFile> createFile(String key, String assetBase) async { |
75 File file = new File('${assetBase}/${asset.key}'); | 90 File file = new File('${assetBase}/${key}'); |
76 List<int> content = await file.readAsBytes(); | 91 List<int> content = await file.readAsBytes(); |
77 return new ArchiveFile.noCompress(asset.key, content.length, content); | 92 return new ArchiveFile.noCompress(key, content.length, content); |
78 } | 93 } |
79 | 94 |
80 Future<ArchiveFile> createSnapshotFile(String snapshotPath) async { | 95 Future<ArchiveFile> createSnapshotFile(String snapshotPath) async { |
81 File file = new File(snapshotPath); | 96 File file = new File(snapshotPath); |
82 List<int> content = await file.readAsBytes(); | 97 List<int> content = await file.readAsBytes(); |
83 return new ArchiveFile(kSnapshotKey, content.length, content); | 98 return new ArchiveFile(kSnapshotKey, content.length, content); |
84 } | 99 } |
85 | 100 |
86 main(List<String> argv) async { | 101 main(List<String> argv) async { |
87 ArgParser parser = new ArgParser(); | 102 ArgParser parser = new ArgParser(); |
88 parser.addFlag('help', abbr: 'h', negatable: false); | 103 parser.addFlag('help', abbr: 'h', negatable: false); |
89 parser.addOption('asset-base'); | 104 parser.addOption('asset-base'); |
90 parser.addOption('manifest'); | 105 parser.addOption('manifest'); |
91 parser.addOption('output-file', abbr: 'o'); | 106 parser.addOption('output-file', abbr: 'o'); |
92 parser.addOption('snapshot'); | 107 parser.addOption('snapshot'); |
93 | 108 |
94 ArgResults args = parser.parse(argv); | 109 ArgResults args = parser.parse(argv); |
95 if (args['help']) { | 110 if (args['help']) { |
96 print(parser.usage); | 111 print(parser.usage); |
97 return; | 112 return; |
98 } | 113 } |
99 | 114 |
100 String manifestPath = args['manifest']; | 115 String manifestPath = args['manifest']; |
101 Map manifestDescriptor = await loadManifest(manifestPath); | 116 Map manifestDescriptor = await loadManifest(manifestPath); |
| 117 Iterable<Asset> assets = parseAssets(manifestDescriptor, manifestPath); |
102 Iterable<MaterialAsset> materialAssets = parseMaterialAssets(manifestDescripto
r); | 118 Iterable<MaterialAsset> materialAssets = parseMaterialAssets(manifestDescripto
r); |
103 | 119 |
104 Archive archive = new Archive(); | 120 Archive archive = new Archive(); |
105 | 121 |
106 String snapshot = args['snapshot']; | 122 String snapshot = args['snapshot']; |
107 if (snapshot != null) | 123 if (snapshot != null) |
108 archive.addFile(await createSnapshotFile(snapshot)); | 124 archive.addFile(await createSnapshotFile(snapshot)); |
109 | 125 |
| 126 for (Asset asset in assets) |
| 127 archive.addFile(await createFile(asset.key, asset.base)); |
| 128 |
110 for (MaterialAsset asset in materialAssets) | 129 for (MaterialAsset asset in materialAssets) |
111 archive.addFile(await createFile(asset, args['asset-base'])); | 130 archive.addFile(await createFile(asset.key, args['asset-base'])); |
112 | 131 |
113 File outputFile = new File(args['output-file']); | 132 File outputFile = new File(args['output-file']); |
114 await outputFile.writeAsBytes(new ZipEncoder().encode(archive)); | 133 await outputFile.writeAsBytes(new ZipEncoder().encode(archive)); |
115 } | 134 } |
OLD | NEW |