Commit f0379df5 authored by Todd Volkert's avatar Todd Volkert

Refactor flx.build() to allow for external toolchain hooks.

This splits flx.build() into two methods, flx.build() and
flx.assemble().  builD() now does the following:
1) constructs the manifest map by reading the manifest from the
   file system
2) "compiles" the dart code into the snapshot file
3) Invokes assemble()

This allows external build toolchains to construct their own
manifest map (possibly using a different manifest syntax)
and create their own snapshot file
parent 10e323f7
...@@ -15,7 +15,7 @@ class BuildCommand extends FlutterCommand { ...@@ -15,7 +15,7 @@ class BuildCommand extends FlutterCommand {
BuildCommand() { BuildCommand() {
argParser.addFlag('precompiled', negatable: false); argParser.addFlag('precompiled', negatable: false);
argParser.addOption('asset-base', defaultsTo: defaultAssetBase); argParser.addOption('asset-base', defaultsTo: defaultMaterialAssetBasePath);
argParser.addOption('compiler'); argParser.addOption('compiler');
argParser.addOption('target', argParser.addOption('target',
abbr: 't', abbr: 't',
...@@ -42,7 +42,7 @@ class BuildCommand extends FlutterCommand { ...@@ -42,7 +42,7 @@ class BuildCommand extends FlutterCommand {
return await build( return await build(
toolchain, toolchain,
assetBase: argResults['asset-base'], materialAssetBasePath: argResults['asset-base'],
mainPath: argResults.wasParsed('main') ? argResults['main'] : argResults['target'], mainPath: argResults.wasParsed('main') ? argResults['main'] : argResults['target'],
manifestPath: argResults['manifest'], manifestPath: argResults['manifest'],
outputPath: outputPath, outputPath: outputPath,
......
...@@ -18,7 +18,8 @@ import 'base/file_system.dart'; ...@@ -18,7 +18,8 @@ import 'base/file_system.dart';
import 'toolchain.dart'; import 'toolchain.dart';
const String defaultMainPath = 'lib/main.dart'; const String defaultMainPath = 'lib/main.dart';
const String defaultAssetBase = 'packages/material_design_icons/icons'; const String defaultAssetBasePath = '.';
const String defaultMaterialAssetBasePath = 'packages/material_design_icons/icons';
const String defaultManifestPath = 'flutter.yaml'; const String defaultManifestPath = 'flutter.yaml';
const String defaultFlxOutputPath = 'build/app.flx'; const String defaultFlxOutputPath = 'build/app.flx';
const String defaultSnapshotPath = 'build/snapshot_blob.bin'; const String defaultSnapshotPath = 'build/snapshot_blob.bin';
...@@ -43,18 +44,17 @@ class _Asset { ...@@ -43,18 +44,17 @@ class _Asset {
_Asset({ this.source, this.base, this.key }); _Asset({ this.source, this.base, this.key });
} }
Map<_Asset, List<_Asset>> _parseAssets(Map manifestDescriptor, String manifestPath) { Map<_Asset, List<_Asset>> _parseAssets(Map manifestDescriptor, String assetBase) {
Map<_Asset, List<_Asset>> result = <_Asset, List<_Asset>>{}; Map<_Asset, List<_Asset>> result = <_Asset, List<_Asset>>{};
if (manifestDescriptor == null) if (manifestDescriptor == null)
return result; return result;
String basePath = path.dirname(path.absolute(manifestPath));
if (manifestDescriptor.containsKey('assets')) { if (manifestDescriptor.containsKey('assets')) {
for (String asset in manifestDescriptor['assets']) { for (String asset in manifestDescriptor['assets']) {
_Asset baseAsset = new _Asset(base: basePath, key: asset); _Asset baseAsset = new _Asset(base: assetBase, key: asset);
List<_Asset> variants = <_Asset>[]; List<_Asset> variants = <_Asset>[];
result[baseAsset] = variants; result[baseAsset] = variants;
// Find asset variants // Find asset variants
String assetPath = path.join(basePath, asset); String assetPath = path.join(assetBase, asset);
String assetFilename = path.basename(assetPath); String assetFilename = path.basename(assetPath);
Directory assetDir = new Directory(path.dirname(assetPath)); Directory assetDir = new Directory(path.dirname(assetPath));
List<FileSystemEntity> files = assetDir.listSync(recursive: true); List<FileSystemEntity> files = assetDir.listSync(recursive: true);
...@@ -62,8 +62,8 @@ Map<_Asset, List<_Asset>> _parseAssets(Map manifestDescriptor, String manifestPa ...@@ -62,8 +62,8 @@ Map<_Asset, List<_Asset>> _parseAssets(Map manifestDescriptor, String manifestPa
if (path.basename(entity.path) == assetFilename && if (path.basename(entity.path) == assetFilename &&
FileSystemEntity.isFileSync(entity.path) && FileSystemEntity.isFileSync(entity.path) &&
entity.path != assetPath) { entity.path != assetPath) {
String key = path.relative(entity.path, from: basePath); String key = path.relative(entity.path, from: assetBase);
variants.add(new _Asset(base: basePath, key: key)); variants.add(new _Asset(base: assetBase, key: key));
} }
} }
} }
...@@ -207,7 +207,7 @@ class DirectoryResult { ...@@ -207,7 +207,7 @@ class DirectoryResult {
Future<int> build( Future<int> build(
Toolchain toolchain, { Toolchain toolchain, {
String assetBase: defaultAssetBase, String materialAssetBasePath: defaultMaterialAssetBasePath,
String mainPath: defaultMainPath, String mainPath: defaultMainPath,
String manifestPath: defaultManifestPath, String manifestPath: defaultManifestPath,
String outputPath: defaultFlxOutputPath, String outputPath: defaultFlxOutputPath,
...@@ -215,15 +215,10 @@ Future<int> build( ...@@ -215,15 +215,10 @@ Future<int> build(
String privateKeyPath: defaultPrivateKeyPath, String privateKeyPath: defaultPrivateKeyPath,
bool precompiledSnapshot: false bool precompiledSnapshot: false
}) async { }) async {
printTrace('Building $outputPath');
Map manifestDescriptor = _loadManifest(manifestPath); Map manifestDescriptor = _loadManifest(manifestPath);
String assetBasePath = path.dirname(path.absolute(manifestPath));
Map<_Asset, List<_Asset>> assets = _parseAssets(manifestDescriptor, manifestPath); ArchiveFile snapshotFile = null;
assets.addAll(_parseMaterialAssets(manifestDescriptor, assetBase));
Archive archive = new Archive();
if (!precompiledSnapshot) { if (!precompiledSnapshot) {
ensureDirectoryExists(snapshotPath); ensureDirectoryExists(snapshotPath);
...@@ -235,9 +230,42 @@ Future<int> build( ...@@ -235,9 +230,42 @@ Future<int> build(
return result; return result;
} }
archive.addFile(_createSnapshotFile(snapshotPath)); snapshotFile = _createSnapshotFile(snapshotPath);
} }
return assemble(
manifestDescriptor: manifestDescriptor,
snapshotFile: snapshotFile,
assetBasePath: assetBasePath,
materialAssetBasePath: materialAssetBasePath,
outputPath: outputPath,
privateKeyPath: privateKeyPath
);
}
/// Assembles a Flutter .flx file from a pre-existing manifest descriptor
/// and a pre-compiled snapshot.
///
/// This may be called by external build toolchains, so practice caution
/// when changing this method signature (alert flutter-dev).
Future<int> assemble({
Map manifestDescriptor: const {},
ArchiveFile snapshotFile: null,
String assetBasePath: defaultAssetBasePath,
String materialAssetBasePath: defaultMaterialAssetBasePath,
String outputPath: defaultFlxOutputPath,
String privateKeyPath: defaultPrivateKeyPath
}) async {
printTrace('Building $outputPath');
Map<_Asset, List<_Asset>> assets = _parseAssets(manifestDescriptor, assetBasePath);
assets.addAll(_parseMaterialAssets(manifestDescriptor, materialAssetBasePath));
Archive archive = new Archive();
if (snapshotFile != null)
archive.addFile(snapshotFile);
for (_Asset asset in assets.keys) { for (_Asset asset in assets.keys) {
if (!_addAssetFile(archive, asset)) if (!_addAssetFile(archive, asset))
return 1; return 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment