From 670dd8ff0b5f0ab834047b25550525a2011164be Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 2 Jul 2015 09:12:39 -0700 Subject: [PATCH] Port skyx to Dart Rather than require Sky developers to have a Python environment, we should write our tooling for Sky in Dart. This CL converts skyx.py into skyx.dart and makes skyx.py into a wrapper for skyx.dart. We still need the Python wrapper because gn requires that build actions are written in Python. This code isn't wired into the main build yet. We'll need to add some more pub packages to our buildbot environment first. TBR=eseidel@google.com Review URL: https://codereview.chromium.org/1213213003. --- engine/src/flutter/tools/skyx.py | 70 +++--------- engine/src/flutter/tools/skyx/.gitignore | 2 + engine/src/flutter/tools/skyx/LICENSE | 27 +++++ engine/src/flutter/tools/skyx/bin/skyx.dart | 111 ++++++++++++++++++++ engine/src/flutter/tools/skyx/pubspec.lock | 35 ++++++ engine/src/flutter/tools/skyx/pubspec.yaml | 9 ++ 6 files changed, 198 insertions(+), 56 deletions(-) create mode 100644 engine/src/flutter/tools/skyx/.gitignore create mode 100644 engine/src/flutter/tools/skyx/LICENSE create mode 100644 engine/src/flutter/tools/skyx/bin/skyx.dart create mode 100644 engine/src/flutter/tools/skyx/pubspec.lock create mode 100644 engine/src/flutter/tools/skyx/pubspec.yaml diff --git a/engine/src/flutter/tools/skyx.py b/engine/src/flutter/tools/skyx.py index cc4cfac341..f15edaa97e 100755 --- a/engine/src/flutter/tools/skyx.py +++ b/engine/src/flutter/tools/skyx.py @@ -3,73 +3,31 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import argparse +import os import subprocess import sys -import yaml -import zipfile -import os -import argparse -import copy -SNAPSHOT_KEY = 'snapshot_blob.bin' -DENSITIES = ['drawable-xxhdpi'] -THEMES = ['white', 'black', 'grey600'] -SIZES = [24] - -class MaterialAsset(object): - def __init__(self, descriptor): - self.name = descriptor['name'] - self.size = descriptor['size'] - self.theme = descriptor['theme'] - self.density = descriptor['density'] - - @property - def key(self): - category, subtype = self.name.split('/', 1) - return os.path.join(category, self.density, 'ic_%(subtype)s_%(theme)s_%(size)sdp.png' % { - 'subtype': subtype, 'theme': self.theme, 'size': self.size, - }) - -def generate_values(asset_descriptor, key, default): - if key in asset_descriptor: - return [asset_descriptor[key]] - return default - -def generate_material_assets(asset_descriptor): - current_asset_descriptor = copy.deepcopy(asset_descriptor) - for density in generate_values(asset_descriptor, 'density', DENSITIES): - current_asset_descriptor['density'] = density - for theme in generate_values(asset_descriptor, 'theme', THEMES): - current_asset_descriptor['theme'] = theme - for size in generate_values(asset_descriptor, 'size', SIZES): - current_asset_descriptor['size'] = size - yield MaterialAsset(current_asset_descriptor) - -def load_manifest(manifest): - with open(manifest) as manifest_file: - return yaml.load(manifest_file) - -def parse_material_assets(manifest_descriptor): - for asset_descriptor in manifest_descriptor['material-design-icons']: - for asset in generate_material_assets(asset_descriptor): - yield asset +SKY_TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) +SRC_ROOT = os.path.dirname(os.path.dirname(SKY_TOOLS_DIR)) +DART_SDK = os.path.join(SRC_ROOT, 'third_party', 'dart-sdk', 'dart-sdk', 'bin') def main(): - parser = argparse.ArgumentParser(description='Taco') + parser = argparse.ArgumentParser(description='Packaging tool for Sky apps') parser.add_argument('manifest', type=str) parser.add_argument('--asset-base', type=str) parser.add_argument('--snapshot', type=str) parser.add_argument('-o', '--output-file', type=str) args = parser.parse_args() - manifest_descriptor = load_manifest(args.manifest) - material_assets = list(parse_material_assets( manifest_descriptor)) - - with zipfile.ZipFile(args.output_file, 'w', zipfile.ZIP_DEFLATED) as archive: - if args.snapshot is not None: - archive.write(args.snapshot, SNAPSHOT_KEY) - for asset in material_assets: - archive.write(os.path.join(args.asset_base, asset.key), asset.key, zipfile.ZIP_STORED) + subprocess.check_call([ + os.path.join(DART_SDK, 'dart'), + os.path.join(SKY_TOOLS_DIR, 'skyx', 'bin', 'skyx.dart'), + '--asset-base', args.asset_base, + '--snapshot', args.snapshot, + '--output-file', args.output_file, + args.manifest, + ]) if __name__ == '__main__': sys.exit(main()) diff --git a/engine/src/flutter/tools/skyx/.gitignore b/engine/src/flutter/tools/skyx/.gitignore new file mode 100644 index 0000000000..dc1c66d64b --- /dev/null +++ b/engine/src/flutter/tools/skyx/.gitignore @@ -0,0 +1,2 @@ +packages +bin/packages diff --git a/engine/src/flutter/tools/skyx/LICENSE b/engine/src/flutter/tools/skyx/LICENSE new file mode 100644 index 0000000000..972bb2edb0 --- /dev/null +++ b/engine/src/flutter/tools/skyx/LICENSE @@ -0,0 +1,27 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/engine/src/flutter/tools/skyx/bin/skyx.dart b/engine/src/flutter/tools/skyx/bin/skyx.dart new file mode 100644 index 0000000000..1bc27b8aa1 --- /dev/null +++ b/engine/src/flutter/tools/skyx/bin/skyx.dart @@ -0,0 +1,111 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:io'; +import 'dart:async'; + +import 'package:archive/archive.dart'; +import 'package:args/args.dart'; +import 'package:yaml/yaml.dart'; + +const String kSnapshotKey = 'snapshot_blob.bin'; +const List kDensities = const ['drawable-xxhdpi']; +const List kThemes = const ['white', 'black', 'grey600']; +const List kSizes = const [24]; + +class MaterialAsset { + final String name; + final String density; + final String theme; + final int size; + + MaterialAsset(Map descriptor) + : name = descriptor['name'], + density = descriptor['density'], + theme = descriptor['theme'], + size = descriptor['size']; + + String get key { + List parts = name.split('/'); + String category = parts[0]; + String subtype = parts[1]; + return '$category/$density/ic_${subtype}_${theme}_${size}dp.png'; + } +} + +List generateValues(Map assetDescriptor, String key, List defaults) { + if (assetDescriptor.containsKey(key)) + return [assetDescriptor[key]]; + return defaults; +} + +Iterable generateMaterialAssets(Map assetDescriptor) sync* { + Map currentAssetDescriptor = new Map.from(assetDescriptor); + for (String density in generateValues(assetDescriptor, 'density', kDensities)) { + currentAssetDescriptor['density'] = density; + for (String theme in generateValues(assetDescriptor, 'theme', kThemes)) { + currentAssetDescriptor['theme'] = theme; + for (String size in generateValues(assetDescriptor, 'size', kSizes)) { + currentAssetDescriptor['size'] = size; + yield new MaterialAsset(currentAssetDescriptor); + } + } + } +} + +Iterable parseMaterialAssets(Map manifestDescriptor) sync* { + for (Map assetDescriptor in manifestDescriptor['material-design-icons']) { + for (MaterialAsset asset in generateMaterialAssets(assetDescriptor)) { + yield asset; + } + } +} + +Future loadManifest(String manifestPath) async { + String manifestDescriptor = await new File(manifestPath).readAsString(); + return loadYaml(manifestDescriptor); +} + +Future createFile(MaterialAsset asset, String assetBase) async { + File file = new File('${assetBase}/${asset.key}'); + List content = await file.readAsBytes(); + return new ArchiveFile.noCompress(asset.key, content.length, content); +} + +Future createSnapshotFile(String snapshotPath) async { + File file = new File(snapshotPath); + List content = await file.readAsBytes(); + return new ArchiveFile(kSnapshotKey, content.length, content); +} + +main(List argv) async { + ArgParser parser = new ArgParser(); + parser.addFlag('help', abbr: 'h', negatable: false); + parser.addOption('asset-base'); + parser.addOption('snapshot'); + parser.addOption('output-file', abbr: 'o'); + + ArgResults args = parser.parse(argv); + if (args['help']) { + print(parser.usage); + return; + } + + String manifestPath = args.rest.first; + + Map manifestDescriptor = await loadManifest(manifestPath); + Iterable materialAssets = parseMaterialAssets(manifestDescriptor); + + Archive archive = new Archive(); + + String snapshot = args['snapshot']; + if (snapshot != null) + archive.addFile(await createSnapshotFile(snapshot)); + + for (MaterialAsset asset in materialAssets) + archive.addFile(await createFile(asset, args['asset-base'])); + + File outputFile = new File(args['output-file']); + await outputFile.writeAsBytes(new ZipEncoder().encode(archive)); +} diff --git a/engine/src/flutter/tools/skyx/pubspec.lock b/engine/src/flutter/tools/skyx/pubspec.lock new file mode 100644 index 0000000000..36d5306871 --- /dev/null +++ b/engine/src/flutter/tools/skyx/pubspec.lock @@ -0,0 +1,35 @@ +# Generated by pub +# See http://pub.dartlang.org/doc/glossary.html#lockfile +packages: + archive: + description: archive + source: hosted + version: "1.0.20" + args: + description: args + source: hosted + version: "0.13.2" + collection: + description: collection + source: hosted + version: "1.1.1" + crypto: + description: crypto + source: hosted + version: "0.9.0" + path: + description: path + source: hosted + version: "1.3.5" + source_span: + description: source_span + source: hosted + version: "1.1.2" + string_scanner: + description: string_scanner + source: hosted + version: "0.1.3+1" + yaml: + description: yaml + source: hosted + version: "2.1.3" diff --git a/engine/src/flutter/tools/skyx/pubspec.yaml b/engine/src/flutter/tools/skyx/pubspec.yaml new file mode 100644 index 0000000000..56c168dcbe --- /dev/null +++ b/engine/src/flutter/tools/skyx/pubspec.yaml @@ -0,0 +1,9 @@ +author: Chromium Authors +dependencies: + archive: '>=1.0.20 <2.0.0' + args: '>=0.13.0 <1.0.0' + yaml: '>=2.1.3 <3.0.0' +description: Developer tool for packaging Sky applications +homepage: https://github.com/domokit/mojo/tree/master/sky/tools/skyx +name: skyx +version: 0.0.1