build.dart 7.75 KB
Newer Older
1 2 3 4 5
// 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:async';
6
import 'dart:io';
7
import 'dart:typed_data';
8 9

import 'package:archive/archive.dart';
10 11
import 'package:flx/bundle.dart';
import 'package:flx/signing.dart';
Adam Barth's avatar
Adam Barth committed
12
import 'package:path/path.dart' as path;
13 14
import 'package:yaml/yaml.dart';

15 16 17
import '../base/file_system.dart';
import '../base/logging.dart';
import '../runner/flutter_command.dart';
18
import '../toolchain.dart';
19

20 21 22
const String _kSnapshotKey = 'snapshot_blob.bin';
const List<String> _kDensities = const ['drawable-xxhdpi'];
const List<String> _kThemes = const ['white', 'black'];
23
const List<int> _kSizes = const [18, 24, 36, 48];
24

25
class _Asset {
26 27 28
  final String base;
  final String key;

29
  _Asset({ this.base, this.key });
30 31
}

32
Iterable<_Asset> _parseAssets(Map manifestDescriptor, String manifestPath) sync* {
33 34
  if (manifestDescriptor == null || !manifestDescriptor.containsKey('assets'))
    return;
35
  String basePath = path.dirname(path.absolute(manifestPath));
36
  for (String asset in manifestDescriptor['assets'])
37
    yield new _Asset(base: basePath, key: asset);
38 39
}

40
class _MaterialAsset {
41 42 43 44 45
  final String name;
  final String density;
  final String theme;
  final int size;

46
  _MaterialAsset(Map descriptor)
47 48 49 50 51 52 53 54 55 56 57 58 59
    : 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';
  }
}

60
List _generateValues(Map assetDescriptor, String key, List defaults) {
61 62 63 64 65
  if (assetDescriptor.containsKey(key))
    return [assetDescriptor[key]];
  return defaults;
}

66
Iterable<_MaterialAsset> _generateMaterialAssets(Map assetDescriptor) sync* {
67
  Map currentAssetDescriptor = new Map.from(assetDescriptor);
68
  for (String density in _generateValues(assetDescriptor, 'density', _kDensities)) {
69
    currentAssetDescriptor['density'] = density;
70
    for (String theme in _generateValues(assetDescriptor, 'theme', _kThemes)) {
71
      currentAssetDescriptor['theme'] = theme;
72
      for (int size in _generateValues(assetDescriptor, 'size', _kSizes)) {
73
        currentAssetDescriptor['size'] = size;
74
        yield new _MaterialAsset(currentAssetDescriptor);
75 76 77 78 79
      }
    }
  }
}

80
Iterable<_MaterialAsset> _parseMaterialAssets(Map manifestDescriptor) sync* {
81 82 83
  if (manifestDescriptor == null || !manifestDescriptor.containsKey('material-design-icons'))
    return;
  for (Map assetDescriptor in manifestDescriptor['material-design-icons']) {
84
    for (_MaterialAsset asset in _generateMaterialAssets(assetDescriptor)) {
85 86 87 88 89
      yield asset;
    }
  }
}

90
dynamic _loadManifest(String manifestPath) {
91
  if (manifestPath == null || !FileSystemEntity.isFileSync(manifestPath))
92
    return null;
93
  String manifestDescriptor = new File(manifestPath).readAsStringSync();
94 95 96
  return loadYaml(manifestDescriptor);
}

97
ArchiveFile _createFile(String key, String assetBase) {
Hixie's avatar
Hixie committed
98
  File file = new File('$assetBase/$key');
99
  if (!file.existsSync())
100
    return null;
101
  List<int> content = file.readAsBytesSync();
102 103 104
  return new ArchiveFile.noCompress(key, content.length, content);
}

105
ArchiveFile _createSnapshotFile(String snapshotPath) {
106
  File file = new File(snapshotPath);
107
  List<int> content = file.readAsBytesSync();
108
  return new ArchiveFile(_kSnapshotKey, content.length, content);
109 110
}

111 112
const String _kDefaultAssetBase = 'packages/material_design_icons/icons';
const String _kDefaultMainPath = 'lib/main.dart';
113
const String _kDefaultManifestPath = 'flutter.yaml';
114 115
const String _kDefaultOutputPath = 'build/app.flx';
const String _kDefaultSnapshotPath = 'build/snapshot_blob.bin';
116
const String _kDefaultPrivateKeyPath = 'privatekey.der';
117 118 119

class BuildCommand extends FlutterCommand {
  final String name = 'build';
120
  final String description = 'Packages your Flutter app into an FLX.';
121

122
  BuildCommand() {
123
    argParser.addFlag('precompiled', negatable: false);
124
    argParser.addOption('asset-base', defaultsTo: _kDefaultAssetBase);
125
    argParser.addOption('compiler');
126
    argParser.addOption('main', defaultsTo: _kDefaultMainPath);
127
    argParser.addOption('manifest', defaultsTo: _kDefaultManifestPath);
128
    argParser.addOption('private-key', defaultsTo: _kDefaultPrivateKeyPath);
129 130
    argParser.addOption('output-file', abbr: 'o', defaultsTo: _kDefaultOutputPath);
    argParser.addOption('snapshot', defaultsTo: _kDefaultSnapshotPath);
131 132 133
  }

  @override
134
  Future<int> runInProject() async {
135 136 137 138 139 140 141 142 143 144 145 146
    String compilerPath = argResults['compiler'];

    if (compilerPath == null)
      await downloadToolchain();
    else
      toolchain = new Toolchain(compiler: new Compiler(compilerPath));

    return await build(
      assetBase: argResults['asset-base'],
      mainPath: argResults['main'],
      manifestPath: argResults['manifest'],
      outputPath: argResults['output-file'],
147
      snapshotPath: argResults['snapshot'],
148 149
      privateKeyPath: argResults['private-key'],
      precompiledSnapshot: argResults['precompiled']
150 151 152
    );
  }

Adam Barth's avatar
Adam Barth committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166
  Future<int> buildInTempDir({
    String mainPath: _kDefaultMainPath,
    void onBundleAvailable(String bundlePath)
  }) async {
    int result;
    Directory tempDir = await Directory.systemTemp.createTemp('flutter_tools');
    try {
      String localBundlePath = path.join(tempDir.path, 'app.flx');
      String localSnapshotPath = path.join(tempDir.path, 'snapshot_blob.bin');
      result = await build(
        snapshotPath: localSnapshotPath,
        outputPath: localBundlePath,
        mainPath: mainPath
      );
167 168
      if (result == 0)
        onBundleAvailable(localBundlePath);
Adam Barth's avatar
Adam Barth committed
169 170 171 172 173 174
    } finally {
      tempDir.deleteSync(recursive: true);
    }
    return result;
  }

175 176 177
  Future<int> build({
    String assetBase: _kDefaultAssetBase,
    String mainPath: _kDefaultMainPath,
178
    String manifestPath: _kDefaultManifestPath,
179
    String outputPath: _kDefaultOutputPath,
180
    String snapshotPath: _kDefaultSnapshotPath,
181 182
    String privateKeyPath: _kDefaultPrivateKeyPath,
    bool precompiledSnapshot: false
183
  }) async {
184
    logging.fine('Building $outputPath');
185

186
    Map manifestDescriptor = _loadManifest(manifestPath);
187

188 189
    Iterable<_Asset> assets = _parseAssets(manifestDescriptor, manifestPath);
    Iterable<_MaterialAsset> materialAssets = _parseMaterialAssets(manifestDescriptor);
190 191 192

    Archive archive = new Archive();

193
    if (!precompiledSnapshot) {
194 195
      ensureDirectoryExists(snapshotPath);

196 197 198
      // In a precompiled snapshot, the instruction buffer contains script
      // content equivalents
      int result = await toolchain.compiler.compile(mainPath: mainPath, snapshotPath: snapshotPath);
199 200
      if (result != 0) {
        logging.severe('Failed to run the Flutter compiler. Exit code: $result');
201
        return result;
202
      }
203

204 205
      archive.addFile(_createSnapshotFile(snapshotPath));
    }
206

207 208 209 210 211 212 213 214
    for (_Asset asset in assets) {
      ArchiveFile file = _createFile(asset.key, asset.base);
      if (file == null) {
        stderr.writeln('Cannot find asset "${asset.key}" in directory "${path.absolute(asset.base)}".');
        return 1;
      }
      archive.addFile(file);
    }
215

216
    for (_MaterialAsset asset in materialAssets) {
217
      ArchiveFile file = _createFile(asset.key, assetBase);
218 219 220 221
      if (file != null)
        archive.addFile(file);
    }

222 223
    await CipherParameters.get().seedRandom();

224
    AsymmetricKeyPair keyPair = keyPairFromPrivateKeyFileSync(privateKeyPath);
225
    Uint8List zipBytes = new Uint8List.fromList(new ZipEncoder().encode(archive));
226
    ensureDirectoryExists(outputPath);
227 228 229 230 231 232 233
    Bundle bundle = new Bundle.fromContent(
      path: outputPath,
      manifest: manifestDescriptor,
      contentBytes: zipBytes,
      keyPair: keyPair
    );
    bundle.writeSync();
234 235 236
    return 0;
  }
}