OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 import 'dart:io'; |
| 6 import 'dart:async'; |
| 7 |
| 8 import 'package:archive/archive.dart'; |
| 9 import 'package:args/args.dart'; |
| 10 import 'package:yaml/yaml.dart'; |
| 11 |
| 12 const String kSnapshotKey = 'snapshot_blob.bin'; |
| 13 const List<String> kDensities = const ['drawable-xxhdpi']; |
| 14 const List<String> kThemes = const ['white', 'black', 'grey600']; |
| 15 const List<int> kSizes = const [24]; |
| 16 |
| 17 class MaterialAsset { |
| 18 final String name; |
| 19 final String density; |
| 20 final String theme; |
| 21 final int size; |
| 22 |
| 23 MaterialAsset(Map descriptor) |
| 24 : name = descriptor['name'], |
| 25 density = descriptor['density'], |
| 26 theme = descriptor['theme'], |
| 27 size = descriptor['size']; |
| 28 |
| 29 String get key { |
| 30 List<String> parts = name.split('/'); |
| 31 String category = parts[0]; |
| 32 String subtype = parts[1]; |
| 33 return '$category/$density/ic_${subtype}_${theme}_${size}dp.png'; |
| 34 } |
| 35 } |
| 36 |
| 37 List generateValues(Map assetDescriptor, String key, List defaults) { |
| 38 if (assetDescriptor.containsKey(key)) |
| 39 return [assetDescriptor[key]]; |
| 40 return defaults; |
| 41 } |
| 42 |
| 43 Iterable<MaterialAsset> generateMaterialAssets(Map assetDescriptor) sync* { |
| 44 Map currentAssetDescriptor = new Map.from(assetDescriptor); |
| 45 for (String density in generateValues(assetDescriptor, 'density', kDensities))
{ |
| 46 currentAssetDescriptor['density'] = density; |
| 47 for (String theme in generateValues(assetDescriptor, 'theme', kThemes)) { |
| 48 currentAssetDescriptor['theme'] = theme; |
| 49 for (String size in generateValues(assetDescriptor, 'size', kSizes)) { |
| 50 currentAssetDescriptor['size'] = size; |
| 51 yield new MaterialAsset(currentAssetDescriptor); |
| 52 } |
| 53 } |
| 54 } |
| 55 } |
| 56 |
| 57 Iterable<MaterialAsset> parseMaterialAssets(Map manifestDescriptor) sync* { |
| 58 for (Map assetDescriptor in manifestDescriptor['material-design-icons']) { |
| 59 for (MaterialAsset asset in generateMaterialAssets(assetDescriptor)) { |
| 60 yield asset; |
| 61 } |
| 62 } |
| 63 } |
| 64 |
| 65 Future loadManifest(String manifestPath) async { |
| 66 String manifestDescriptor = await new File(manifestPath).readAsString(); |
| 67 return loadYaml(manifestDescriptor); |
| 68 } |
| 69 |
| 70 Future<ArchiveFile> createFile(MaterialAsset asset, String assetBase) async { |
| 71 File file = new File('${assetBase}/${asset.key}'); |
| 72 List<int> content = await file.readAsBytes(); |
| 73 return new ArchiveFile.noCompress(asset.key, content.length, content); |
| 74 } |
| 75 |
| 76 Future<ArchiveFile> createSnapshotFile(String snapshotPath) async { |
| 77 File file = new File(snapshotPath); |
| 78 List<int> content = await file.readAsBytes(); |
| 79 return new ArchiveFile(kSnapshotKey, content.length, content); |
| 80 } |
| 81 |
| 82 main(List<String> argv) async { |
| 83 ArgParser parser = new ArgParser(); |
| 84 parser.addFlag('help', abbr: 'h', negatable: false); |
| 85 parser.addOption('asset-base'); |
| 86 parser.addOption('snapshot'); |
| 87 parser.addOption('output-file', abbr: 'o'); |
| 88 |
| 89 ArgResults args = parser.parse(argv); |
| 90 if (args['help']) { |
| 91 print(parser.usage); |
| 92 return; |
| 93 } |
| 94 |
| 95 String manifestPath = args.rest.first; |
| 96 |
| 97 Map manifestDescriptor = await loadManifest(manifestPath); |
| 98 Iterable<MaterialAsset> materialAssets = parseMaterialAssets(manifestDescripto
r); |
| 99 |
| 100 Archive archive = new Archive(); |
| 101 |
| 102 String snapshot = args['snapshot']; |
| 103 if (snapshot != null) |
| 104 archive.addFile(await createSnapshotFile(snapshot)); |
| 105 |
| 106 for (MaterialAsset asset in materialAssets) |
| 107 archive.addFile(await createFile(asset, args['asset-base'])); |
| 108 |
| 109 File outputFile = new File(args['output-file']); |
| 110 await outputFile.writeAsBytes(new ZipEncoder().encode(archive)); |
| 111 } |
OLD | NEW |