flx.dart 13.6 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:convert';
7 8 9 10
import 'dart:io';

import 'package:flx/bundle.dart';
import 'package:flx/signing.dart';
11
import 'package:json_schema/json_schema.dart';
12 13 14
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart';

15
import 'artifacts.dart';
yjbanov's avatar
yjbanov committed
16
import 'base/file_system.dart' show ensureDirectoryExists;
17
import 'globals.dart';
18
import 'package_map.dart';
19
import 'toolchain.dart';
Devon Carew's avatar
Devon Carew committed
20
import 'zip.dart';
21 22

const String defaultMainPath = 'lib/main.dart';
23
const String defaultAssetBasePath = '.';
24 25 26
const String defaultManifestPath = 'flutter.yaml';
const String defaultFlxOutputPath = 'build/app.flx';
const String defaultSnapshotPath = 'build/snapshot_blob.bin';
27
const String defaultDepfilePath = 'build/snapshot_blob.bin.d';
28
const String defaultPrivateKeyPath = 'privatekey.der';
29
const String defaultWorkingDirPath = 'build/flx';
30 31 32

const String _kSnapshotKey = 'snapshot_blob.bin';

33 34 35
const String _kFontSetMaterial = 'material';
const String _kFontSetRoboto = 'roboto';

36
class _Asset {
Devon Carew's avatar
Devon Carew committed
37 38 39 40 41
  _Asset({ this.base, String assetEntry, this.relativePath, this.source }) {
    this._assetEntry = assetEntry;
  }

  String _assetEntry;
Devon Carew's avatar
Devon Carew committed
42

43
  final String base;
Devon Carew's avatar
Devon Carew committed
44 45 46 47 48 49 50 51 52

  /// The entry to list in the generated asset manifest.
  String get assetEntry => _assetEntry ?? relativePath;

  /// Where the resource is on disk realtive to [base].
  final String relativePath;

  final String source;

53 54 55 56 57 58
  File get assetFile {
    return new File(source != null ? '$base/$source' : '$base/$relativePath');
  }

  bool get assetFileExists => assetFile.existsSync();

Devon Carew's avatar
Devon Carew committed
59
  /// The delta between what the assetEntry is and the relativePath (e.g.,
60
  /// packages/flutter_gallery).
Devon Carew's avatar
Devon Carew committed
61 62 63 64 65 66
  String get symbolicPrefix {
    if (_assetEntry == null || _assetEntry == relativePath)
      return null;
    int index = _assetEntry.indexOf(relativePath);
    return index == -1 ? null : _assetEntry.substring(0, index);
  }
67 68

  @override
Devon Carew's avatar
Devon Carew committed
69
  String toString() => 'asset: $assetEntry';
70 71
}

72 73 74
Map<String, dynamic> _readMaterialFontsManifest() {
  String fontsPath = path.join(path.absolute(ArtifactStore.flutterRoot),
      'packages', 'flutter_tools', 'schema', 'material_fonts.yaml');
75

76
  return loadYaml(new File(fontsPath).readAsStringSync());
77 78
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
final Map<String, dynamic> _materialFontsManifest = _readMaterialFontsManifest();

List<Map<String, dynamic>> _getMaterialFonts(String fontSet) {
  return _materialFontsManifest[fontSet];
}

List<_Asset> _getMaterialAssets(String fontSet) {
  List<_Asset> result = <_Asset>[];

  for (Map<String, dynamic> family in _getMaterialFonts(fontSet)) {
    for (Map<String, dynamic> font in family['fonts']) {
      String assetKey = font['asset'];
      result.add(new _Asset(
        base: '${ArtifactStore.flutterRoot}/bin/cache/artifacts/material_fonts',
        source: path.basename(assetKey),
Devon Carew's avatar
Devon Carew committed
94
        relativePath: assetKey
95 96 97 98 99
      ));
    }
  }

  return result;
100 101
}

102 103
/// Given an assetBase location and a flutter.yaml manifest, return a map of
/// assets to asset variants.
104 105
///
/// Returns `null` on missing assets.
106 107 108
Map<_Asset, List<_Asset>> _parseAssets(
  PackageMap packageMap,
  Map<String, dynamic> manifestDescriptor,
109 110 111
  String assetBase, {
  List<String> excludeDirs: const <String>[]
}) {
112
  Map<_Asset, List<_Asset>> result = <_Asset, List<_Asset>>{};
113

114 115
  if (manifestDescriptor == null)
    return result;
116 117 118 119

  excludeDirs = excludeDirs.map(
    (String exclude) => path.absolute(exclude) + Platform.pathSeparator).toList();

120 121
  if (manifestDescriptor.containsKey('assets')) {
    for (String asset in manifestDescriptor['assets']) {
122 123
      _Asset baseAsset = _resolveAsset(packageMap, assetBase, asset);

124 125 126 127 128
      if (!baseAsset.assetFileExists) {
        printError('Error: unable to locate asset entry in flutter.yaml: "$asset".');
        return null;
      }

129 130
      List<_Asset> variants = <_Asset>[];
      result[baseAsset] = variants;
131

132
      // Find asset variants
Devon Carew's avatar
Devon Carew committed
133
      String assetPath = path.join(baseAsset.base, baseAsset.relativePath);
134 135
      String assetFilename = path.basename(assetPath);
      Directory assetDir = new Directory(path.dirname(assetPath));
136

137
      List<FileSystemEntity> files = assetDir.listSync(recursive: true);
138

139
      for (FileSystemEntity entity in files) {
140 141 142 143 144 145 146 147
        if (!FileSystemEntity.isFileSync(entity.path))
          continue;

        // Exclude any files in the given directories.
        if (excludeDirs.any((String exclude) => entity.path.startsWith(exclude)))
          continue;

        if (path.basename(entity.path) == assetFilename && entity.path != assetPath) {
148
          String key = path.relative(entity.path, from: baseAsset.base);
Devon Carew's avatar
Devon Carew committed
149 150 151 152
          String assetEntry;
          if (baseAsset.symbolicPrefix != null)
            assetEntry = path.join(baseAsset.symbolicPrefix, key);
          variants.add(new _Asset(base: baseAsset.base, assetEntry: assetEntry, relativePath: key));
153 154 155 156
        }
      }
    }
  }
157 158 159 160 161 162 163 164 165 166 167

  // Add assets referenced in the fonts section of the manifest.
  if (manifestDescriptor.containsKey('fonts')) {
    for (Map<String, dynamic> family in manifestDescriptor['fonts']) {
      List<Map<String, dynamic>> fonts = family['fonts'];
      if (fonts == null) continue;

      for (Map<String, dynamic> font in fonts) {
        String asset = font['asset'];
        if (asset == null) continue;

Devon Carew's avatar
Devon Carew committed
168
        _Asset baseAsset = new _Asset(base: assetBase, relativePath: asset);
169 170 171 172 173
        result[baseAsset] = <_Asset>[];
      }
    }
  }

174
  return result;
175 176
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
_Asset _resolveAsset(PackageMap packageMap, String assetBase, String asset) {
  if (asset.startsWith('packages/')) {
    // Convert packages/flutter_gallery_assets/clouds-0.png to clouds-0.png.
    String packageKey = asset.substring(9);
    String relativeAsset = asset;

    int index = packageKey.indexOf('/');
    if (index != -1) {
      relativeAsset = packageKey.substring(index + 1);
      packageKey = packageKey.substring(0, index);
    }

    Uri uri = packageMap.map[packageKey];
    if (uri != null && uri.scheme == 'file') {
      File file = new File.fromUri(uri);
Devon Carew's avatar
Devon Carew committed
192
      return new _Asset(base: file.path, assetEntry: asset, relativePath: relativeAsset);
193 194 195
    }
  }

Devon Carew's avatar
Devon Carew committed
196
  return new _Asset(base: assetBase, relativePath: asset);
197 198
}

199 200 201 202 203 204 205
dynamic _loadManifest(String manifestPath) {
  if (manifestPath == null || !FileSystemEntity.isFileSync(manifestPath))
    return null;
  String manifestDescriptor = new File(manifestPath).readAsStringSync();
  return loadYaml(manifestDescriptor);
}

206 207 208 209 210 211
Future<int> _validateManifest(Object manifest) async {
  String schemaPath = path.join(path.absolute(ArtifactStore.flutterRoot),
      'packages', 'flutter_tools', 'schema', 'flutter_yaml.json');
  Schema schema = await Schema.createSchemaFromUrl('file://$schemaPath');

  Validator validator = new Validator(schema);
212
  if (validator.validate(manifest)) {
213
    return 0;
214 215 216 217 218 219 220
  } else {
    if (validator.errors.length == 1) {
      printError('Error in flutter.yaml: ${validator.errors.first}');
    } else {
      printError('Error in flutter.yaml:');
      printError('  ' + validator.errors.join('\n  '));
    }
221

222 223
    return 1;
  }
224 225
}

226
/// Create a [ZipEntry] from the given [_Asset]; the asset must exist.
Devon Carew's avatar
Devon Carew committed
227
ZipEntry _createAssetEntry(_Asset asset) {
228 229
  assert(asset.assetFileExists);
  return new ZipEntry.fromFile(asset.assetEntry, asset.assetFile);
230 231
}

232
ZipEntry _createAssetManifest(Map<_Asset, List<_Asset>> assetVariants) {
233
  Map<String, List<String>> json = <String, List<String>>{};
234
  for (_Asset main in assetVariants.keys) {
235
    List<String> variants = <String>[];
236
    for (_Asset variant in assetVariants[main])
Devon Carew's avatar
Devon Carew committed
237 238
      variants.add(variant.relativePath);
    json[main.relativePath] = variants;
239
  }
Devon Carew's avatar
Devon Carew committed
240
  return new ZipEntry.fromString('AssetManifest.json', JSON.encode(json));
241 242
}

243 244 245
ZipEntry _createFontManifest(Map<String, dynamic> manifestDescriptor,
                             bool usesMaterialDesign,
                             bool includeRobotoFonts) {
Ian Hickson's avatar
Ian Hickson committed
246
  List<Map<String, dynamic>> fonts = <Map<String, dynamic>>[];
247 248 249 250 251
  if (usesMaterialDesign) {
    fonts.addAll(_getMaterialFonts(_kFontSetMaterial));
    if (includeRobotoFonts)
      fonts.addAll(_getMaterialFonts(_kFontSetRoboto));
  }
252 253 254
  if (manifestDescriptor != null && manifestDescriptor.containsKey('fonts'))
    fonts.addAll(manifestDescriptor['fonts']);
  if (fonts.isEmpty)
255
    return null;
256
  return new ZipEntry.fromString('FontManifest.json', JSON.encode(fonts));
257 258
}

Devon Carew's avatar
Devon Carew committed
259
/// Build the flx in the build/ directory and return `localBundlePath` on success.
260 261
///
/// Return `null` on failure.
Devon Carew's avatar
Devon Carew committed
262
Future<String> buildFlx(
263
  Toolchain toolchain, {
264
  String mainPath: defaultMainPath,
265
  bool precompiledSnapshot: false,
266
  bool includeRobotoFonts: true
267 268
}) async {
  int result;
Devon Carew's avatar
Devon Carew committed
269 270
  String localBundlePath = path.join('build', 'app.flx');
  String localSnapshotPath = path.join('build', 'snapshot_blob.bin');
271 272 273 274
  result = await build(
    toolchain,
    snapshotPath: localSnapshotPath,
    outputPath: localBundlePath,
275
    mainPath: mainPath,
276
    precompiledSnapshot: precompiledSnapshot,
277
    includeRobotoFonts: includeRobotoFonts
278
  );
279
  return result == 0 ? localBundlePath : null;
280 281 282 283
}

/// The result from [buildInTempDir]. Note that this object should be disposed after use.
class DirectoryResult {
Devon Carew's avatar
Devon Carew committed
284 285
  DirectoryResult(this.directory, this.localBundlePath);

286 287 288 289 290 291
  final Directory directory;
  final String localBundlePath;

  /// Call this to delete the temporary directory.
  void dispose() {
    directory.deleteSync(recursive: true);
292 293 294 295 296 297 298 299 300
  }
}

Future<int> build(
  Toolchain toolchain, {
  String mainPath: defaultMainPath,
  String manifestPath: defaultManifestPath,
  String outputPath: defaultFlxOutputPath,
  String snapshotPath: defaultSnapshotPath,
301
  String depfilePath: defaultDepfilePath,
302
  String privateKeyPath: defaultPrivateKeyPath,
303
  String workingDirPath: defaultWorkingDirPath,
304 305
  bool precompiledSnapshot: false,
  bool includeRobotoFonts: true
306
}) async {
307 308 309 310 311 312 313 314
  Object manifest = _loadManifest(manifestPath);
  if (manifest != null) {
    int result = await _validateManifest(manifest);
    if (result != 0)
      return result;
  }
  Map<String, dynamic> manifestDescriptor = manifest;

315
  String assetBasePath = path.dirname(path.absolute(manifestPath));
316

Devon Carew's avatar
Devon Carew committed
317 318
  File snapshotFile;

319 320 321 322 323
  if (!precompiledSnapshot) {
    ensureDirectoryExists(snapshotPath);

    // In a precompiled snapshot, the instruction buffer contains script
    // content equivalents
324 325 326 327 328
    int result = await toolchain.compiler.createSnapshot(
      mainPath: mainPath,
      snapshotPath: snapshotPath,
      depfilePath: depfilePath
    );
329
    if (result != 0) {
330
      printError('Failed to run the Flutter compiler. Exit code: $result');
331 332 333
      return result;
    }

Devon Carew's avatar
Devon Carew committed
334
    snapshotFile = new File(snapshotPath);
335 336
  }

337 338 339 340 341
  return assemble(
      manifestDescriptor: manifestDescriptor,
      snapshotFile: snapshotFile,
      assetBasePath: assetBasePath,
      outputPath: outputPath,
342
      privateKeyPath: privateKeyPath,
343 344
      workingDirPath: workingDirPath,
      includeRobotoFonts: includeRobotoFonts
345 346 347 348
  );
}

Future<int> assemble({
Ian Hickson's avatar
Ian Hickson committed
349
  Map<String, dynamic> manifestDescriptor: const <String, dynamic>{},
Devon Carew's avatar
Devon Carew committed
350
  File snapshotFile,
351 352
  String assetBasePath: defaultAssetBasePath,
  String outputPath: defaultFlxOutputPath,
353
  String privateKeyPath: defaultPrivateKeyPath,
354 355
  String workingDirPath: defaultWorkingDirPath,
  bool includeRobotoFonts: true
356 357 358
}) async {
  printTrace('Building $outputPath');

359 360 361 362 363 364
  Map<_Asset, List<_Asset>> assetVariants = _parseAssets(
    new PackageMap(path.join(assetBasePath, '.packages')),
    manifestDescriptor,
    assetBasePath,
    excludeDirs: <String>[workingDirPath, path.join(assetBasePath, 'build')]
  );
365

366 367 368
  if (assetVariants == null)
    return 1;

369 370
  final bool usesMaterialDesign = manifestDescriptor != null &&
    manifestDescriptor['uses-material-design'] == true;
371

Devon Carew's avatar
Devon Carew committed
372
  ZipBuilder zipBuilder = new ZipBuilder();
373 374

  if (snapshotFile != null)
Devon Carew's avatar
Devon Carew committed
375
    zipBuilder.addEntry(new ZipEntry.fromFile(_kSnapshotKey, snapshotFile));
376

377
  for (_Asset asset in assetVariants.keys) {
Devon Carew's avatar
Devon Carew committed
378 379
    ZipEntry assetEntry = _createAssetEntry(asset);
    if (assetEntry == null)
380
      return 1;
381
    zipBuilder.addEntry(assetEntry);
Devon Carew's avatar
Devon Carew committed
382

383
    for (_Asset variant in assetVariants[asset]) {
Devon Carew's avatar
Devon Carew committed
384 385
      ZipEntry variantEntry = _createAssetEntry(variant);
      if (variantEntry == null)
386
        return 1;
387 388 389 390
      zipBuilder.addEntry(variantEntry);
    }
  }

391
  List<_Asset> materialAssets = <_Asset>[];
392
  if (usesMaterialDesign) {
393 394 395 396 397 398 399 400 401
    materialAssets.addAll(_getMaterialAssets(_kFontSetMaterial));
    if (includeRobotoFonts)
      materialAssets.addAll(_getMaterialAssets(_kFontSetRoboto));
  }
  for (_Asset asset in materialAssets) {
    ZipEntry assetEntry = _createAssetEntry(asset);
    if (assetEntry == null)
      return 1;
    zipBuilder.addEntry(assetEntry);
402 403
  }

404
  zipBuilder.addEntry(_createAssetManifest(assetVariants));
405

406
  ZipEntry fontManifest = _createFontManifest(manifestDescriptor, usesMaterialDesign, includeRobotoFonts);
407
  if (fontManifest != null)
Devon Carew's avatar
Devon Carew committed
408
    zipBuilder.addEntry(fontManifest);
409

Ian Hickson's avatar
Ian Hickson committed
410
  AsymmetricKeyPair<PublicKey, PrivateKey> keyPair = keyPairFromPrivateKeyFileSync(privateKeyPath);
411
  printTrace('KeyPair from $privateKeyPath: $keyPair.');
412 413 414 415 416 417

  if (keyPair != null) {
    printTrace('Calling CipherParameters.seedRandom().');
    CipherParameters.get().seedRandom();
  }

Devon Carew's avatar
Devon Carew committed
418 419
  File zipFile = new File(outputPath.substring(0, outputPath.length - 4) + '.zip');
  printTrace('Encoding zip file to ${zipFile.path}');
420
  zipBuilder.createZip(zipFile, new Directory(workingDirPath));
Devon Carew's avatar
Devon Carew committed
421
  List<int> zipBytes = zipFile.readAsBytesSync();
422

423
  ensureDirectoryExists(outputPath);
424

Devon Carew's avatar
Devon Carew committed
425
  printTrace('Creating flx at $outputPath.');
426 427 428 429 430 431 432
  Bundle bundle = new Bundle.fromContent(
    path: outputPath,
    manifest: manifestDescriptor,
    contentBytes: zipBytes,
    keyPair: keyPair
  );
  bundle.writeSync();
433

Devon Carew's avatar
Devon Carew committed
434
  printTrace('Built $outputPath.');
435

436 437
  return 0;
}