fuchsia_asset_builder.dart 4.46 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:args/args.dart';
8
import 'package:flutter_tools/src/asset.dart' hide defaultManifestPath;
9
import 'package:flutter_tools/src/base/common.dart';
10
import 'package:flutter_tools/src/base/context.dart';
11 12
import 'package:flutter_tools/src/base/file_system.dart' as libfs;
import 'package:flutter_tools/src/base/io.dart';
13
import 'package:flutter_tools/src/build_info.dart';
14
import 'package:flutter_tools/src/build_system/depfile.dart';
15
import 'package:flutter_tools/src/bundle.dart';
16
import 'package:flutter_tools/src/bundle_builder.dart';
17 18 19
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/context_runner.dart';
import 'package:flutter_tools/src/devfs.dart';
20
import 'package:flutter_tools/src/globals.dart' as globals;
21
import 'package:flutter_tools/src/reporting/reporting.dart';
22 23

const String _kOptionPackages = 'packages';
24
const String _kOptionAsset = 'asset-dir';
25 26
const String _kOptionManifest = 'manifest';
const String _kOptionAssetManifestOut = 'asset-manifest-out';
27
const String _kOptionComponentName = 'component-name';
28
const String _kOptionDepfile = 'depfile';
29
const List<String> _kRequiredOptions = <String>[
30
  _kOptionPackages,
31
  _kOptionAsset,
32
  _kOptionAssetManifestOut,
33
  _kOptionComponentName,
34 35
];

36 37
Future<void> main(List<String> args) {
  return runInContext<void>(() => run(args), overrides: <Type, Generator>{
38
    Usage: () => DisabledUsage(),
39
  });
40 41
}

42
Future<void> writeFile(libfs.File outputFile, DevFSContent content) async {
43
  outputFile.createSync(recursive: true);
44 45
  final List<int> data = await content.contentsAsBytes();
  outputFile.writeAsBytesSync(data);
46 47
}

48
Future<void> run(List<String> args) async {
49
  final ArgParser parser = ArgParser()
50
    ..addOption(_kOptionPackages, help: 'The .packages file')
51
    ..addOption(_kOptionAsset,
52 53
        help: 'The directory where to put temporary files')
    ..addOption(_kOptionManifest, help: 'The manifest file')
54
    ..addOption(_kOptionAssetManifestOut)
55 56
    ..addOption(_kOptionComponentName)
    ..addOption(_kOptionDepfile);
57 58 59
  final ArgResults argResults = parser.parse(args);
  if (_kRequiredOptions
      .any((String option) => !argResults.options.contains(option))) {
60
    globals.printError('Missing option! All options must be specified.');
61 62
    exit(1);
  }
63
  Cache.flutterRoot = globals.platform.environment['FLUTTER_ROOT'];
64

65
  final String assetDir = argResults[_kOptionAsset] as String;
66
  final AssetBundle assets = await buildAssets(
67
    manifestPath: argResults[_kOptionManifest] as String ?? defaultManifestPath,
68
    assetDirPath: assetDir,
69
    packagesPath: argResults[_kOptionPackages] as String,
70
    targetPlatform: TargetPlatform.fuchsia_arm64 // This is not arch specific.
71 72 73
  );

  if (assets == null) {
74
    throwToolExit('Unable to find assets.', exitCode: 1);
75 76
  }

77
  final List<Future<void>> calls = <Future<void>>[];
78
  assets.entries.forEach((String fileName, DevFSContent content) {
79
    final libfs.File outputFile = globals.fs.file(globals.fs.path.join(assetDir, fileName));
80
    calls.add(writeFile(outputFile, content));
81
  });
82
  await Future.wait<void>(calls);
83

84 85
  final String outputMan = argResults[_kOptionAssetManifestOut] as String;
  await writeFuchsiaManifest(assets, argResults[_kOptionAsset] as String, outputMan, argResults[_kOptionComponentName] as String);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

  if (argResults.options.contains(_kOptionDepfile)) {
    await writeDepfile(assets, outputMan, argResults[_kOptionDepfile] as String);
  }
}

Future<void> writeDepfile(AssetBundle assets, String outputManifest, String depfilePath) async {
  final Depfile depfileContent = Depfile(
    assets.inputFiles,
    <libfs.File>[globals.fs.file(outputManifest)],
  );
  final DepfileService depfileService = DepfileService(
    fileSystem: globals.fs,
    logger: globals.logger,
  );

  final libfs.File depfile = globals.fs.file(depfilePath);
  await depfile.create(recursive: true);
  depfileService.writeToFile(depfileContent, depfile);
105 106
}

107
Future<void> writeFuchsiaManifest(AssetBundle assets, String outputBase, String fileDest, String componentName) async {
108

109
  final libfs.File destFile = globals.fs.file(fileDest);
110 111 112
  await destFile.create(recursive: true);
  final libfs.IOSink outFile = destFile.openWrite();

113
  for (final String path in assets.entries.keys) {
114
    outFile.write('data/$componentName/$path=$outputBase/$path\n');
115 116 117 118
  }
  await outFile.flush();
  await outFile.close();
}