flx.dart 4.85 KB
Newer Older
1 2 3 4 5 6
// 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';

7
import 'artifacts.dart';
8
import 'asset.dart';
9
import 'base/build.dart';
10
import 'base/common.dart';
11
import 'base/file_system.dart';
12
import 'build_info.dart';
13
import 'compile.dart';
14
import 'dart/package_map.dart';
15
import 'devfs.dart';
16
import 'globals.dart';
Devon Carew's avatar
Devon Carew committed
17
import 'zip.dart';
18 19

const String defaultMainPath = 'lib/main.dart';
20
const String defaultAssetBasePath = '.';
21
const String defaultManifestPath = 'pubspec.yaml';
22 23 24
String get defaultFlxOutputPath => fs.path.join(getBuildDirectory(), 'app.flx');
String get defaultSnapshotPath => fs.path.join(getBuildDirectory(), 'snapshot_blob.bin');
String get defaultDepfilePath => fs.path.join(getBuildDirectory(), 'snapshot_blob.bin.d');
25 26
const String defaultPrivateKeyPath = 'privatekey.der';

27
const String _kKernelKey = 'kernel_blob.bin';
28
const String _kSnapshotKey = 'snapshot_blob.bin';
29
const String _kDylibKey = 'libapp.so';
30
const String _kPlatformKernelKey = 'platform.dill';
31

32
Future<Null> build({
33 34
  String mainPath: defaultMainPath,
  String manifestPath: defaultManifestPath,
35 36 37
  String outputPath,
  String snapshotPath,
  String depfilePath,
38
  String privateKeyPath: defaultPrivateKeyPath,
39
  String workingDirPath,
40
  String packagesPath,
41
  bool previewDart2 : false,
42
  bool precompiledSnapshot: false,
43
  bool reportLicensedPackages: false
44
}) async {
45 46 47 48
  outputPath ??= defaultFlxOutputPath;
  snapshotPath ??= defaultSnapshotPath;
  depfilePath ??= defaultDepfilePath;
  workingDirPath ??= getAssetBuildDirectory();
49
  packagesPath ??= fs.path.absolute(PackageMap.globalPackagesPath);
50
  File snapshotFile;
Devon Carew's avatar
Devon Carew committed
51

52
  if (!precompiledSnapshot && !previewDart2) {
53 54 55 56
    ensureDirectoryExists(snapshotPath);

    // In a precompiled snapshot, the instruction buffer contains script
    // content equivalents
57 58
    final Snapshotter snapshotter = new Snapshotter();
    final int result = await snapshotter.buildScriptSnapshot(
59 60
      mainPath: mainPath,
      snapshotPath: snapshotPath,
61
      depfilePath: depfilePath,
62
      packagesPath: packagesPath,
63
    );
64 65
    if (result != 0)
      throwToolExit('Failed to run the Flutter compiler. Exit code: $result', exitCode: result);
66

67
    snapshotFile = fs.file(snapshotPath);
68
  }
69

70
  DevFSContent kernelContent;
71 72 73 74 75 76 77
  if (!precompiledSnapshot && previewDart2) {
    final String kernelBinaryFilename = await compile(
      sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath),
      mainPath: fs.file(mainPath).absolute.path
    );
    kernelContent = new DevFSFileContent(fs.file(kernelBinaryFilename));
  }
78

79
  return assemble(
80
    manifestPath: manifestPath,
81
    kernelContent: kernelContent,
Ian Hickson's avatar
Ian Hickson committed
82 83 84 85
    snapshotFile: snapshotFile,
    outputPath: outputPath,
    privateKeyPath: privateKeyPath,
    workingDirPath: workingDirPath,
86
    packagesPath: packagesPath,
87
    reportLicensedPackages: reportLicensedPackages
88
  ).then((_) => null);
89 90
}

91
Future<List<String>> assemble({
92
  String manifestPath,
93
  DevFSContent kernelContent,
Devon Carew's avatar
Devon Carew committed
94
  File snapshotFile,
95
  File dylibFile,
96
  String outputPath,
97
  String privateKeyPath: defaultPrivateKeyPath,
98
  String workingDirPath,
99
  String packagesPath,
100
  bool includeDefaultFonts: true,
101
  bool reportLicensedPackages: false
102
}) async {
103 104
  outputPath ??= defaultFlxOutputPath;
  workingDirPath ??= getAssetBuildDirectory();
105
  packagesPath ??= fs.path.absolute(PackageMap.globalPackagesPath);
106 107
  printTrace('Building $outputPath');

108
  // Build the asset bundle.
109 110
  final AssetBundle assetBundle = new AssetBundle();
  final int result = await assetBundle.build(
111 112
    manifestPath: manifestPath,
    workingDirPath: workingDirPath,
113
    packagesPath: packagesPath,
114
    includeDefaultFonts: includeDefaultFonts,
115 116
    reportLicensedPackages: reportLicensedPackages
  );
117 118
  if (result != 0)
    throwToolExit('Error building $outputPath: $result', exitCode: result);
119

120
  final ZipBuilder zipBuilder = new ZipBuilder();
121

122 123
  // Add all entries from the asset bundle.
  zipBuilder.entries.addAll(assetBundle.entries);
124

125 126 127 128
  final List<String> fileDependencies = assetBundle.entries.values
      .expand((DevFSContent content) => content.fileDependencies)
      .toList();

129 130
  if (kernelContent != null) {
    final String platformKernelDill = artifacts.getArtifactPath(Artifact.platformKernelDill);
131
    zipBuilder.entries[_kKernelKey] = kernelContent;
132 133
    zipBuilder.entries[_kPlatformKernelKey] = new DevFSFileContent(fs.file(platformKernelDill));
  }
134
  if (snapshotFile != null)
135
    zipBuilder.entries[_kSnapshotKey] = new DevFSFileContent(snapshotFile);
136 137
  if (dylibFile != null)
    zipBuilder.entries[_kDylibKey] = new DevFSFileContent(dylibFile);
Ian Hickson's avatar
Ian Hickson committed
138

139
  ensureDirectoryExists(outputPath);
140

141
  printTrace('Encoding zip file to $outputPath');
142
  await zipBuilder.createZip(fs.file(outputPath), fs.directory(workingDirPath));
143

Devon Carew's avatar
Devon Carew committed
144
  printTrace('Built $outputPath.');
145 146

  return fileDependencies;
147
}