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.
This commit is contained in:
@@ -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())
|
||||
|
||||
2
engine/src/flutter/tools/skyx/.gitignore
vendored
Normal file
2
engine/src/flutter/tools/skyx/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
packages
|
||||
bin/packages
|
||||
27
engine/src/flutter/tools/skyx/LICENSE
Normal file
27
engine/src/flutter/tools/skyx/LICENSE
Normal file
@@ -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.
|
||||
111
engine/src/flutter/tools/skyx/bin/skyx.dart
Normal file
111
engine/src/flutter/tools/skyx/bin/skyx.dart
Normal file
@@ -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<String> kDensities = const ['drawable-xxhdpi'];
|
||||
const List<String> kThemes = const ['white', 'black', 'grey600'];
|
||||
const List<int> 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<String> 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<MaterialAsset> 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<MaterialAsset> 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<ArchiveFile> createFile(MaterialAsset asset, String assetBase) async {
|
||||
File file = new File('${assetBase}/${asset.key}');
|
||||
List<int> content = await file.readAsBytes();
|
||||
return new ArchiveFile.noCompress(asset.key, content.length, content);
|
||||
}
|
||||
|
||||
Future<ArchiveFile> createSnapshotFile(String snapshotPath) async {
|
||||
File file = new File(snapshotPath);
|
||||
List<int> content = await file.readAsBytes();
|
||||
return new ArchiveFile(kSnapshotKey, content.length, content);
|
||||
}
|
||||
|
||||
main(List<String> 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<MaterialAsset> 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));
|
||||
}
|
||||
35
engine/src/flutter/tools/skyx/pubspec.lock
Normal file
35
engine/src/flutter/tools/skyx/pubspec.lock
Normal file
@@ -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"
|
||||
9
engine/src/flutter/tools/skyx/pubspec.yaml
Normal file
9
engine/src/flutter/tools/skyx/pubspec.yaml
Normal file
@@ -0,0 +1,9 @@
|
||||
author: Chromium Authors <sky-dev@googlegroups.com>
|
||||
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
|
||||
Reference in New Issue
Block a user