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

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

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

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

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

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

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

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

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

82 83
  final String outputMan = argResults[_kOptionAssetManifestOut] as String;
  await writeFuchsiaManifest(assets, argResults[_kOptionAsset] as String, outputMan, argResults[_kOptionComponentName] as String);
84

85 86 87
  final String? depfilePath = argResults[_kOptionDepfile] as String?;
  if (depfilePath != null) {
    await writeDepfile(assets, outputMan, depfilePath);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  }
}

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);
104 105
}

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

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

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