fuchsia_builder.dart 5.04 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 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';

import 'package:args/args.dart';
8
import 'package:process/process.dart';
9

10
import '../lib/src/base/common.dart';
11
import '../lib/src/base/config.dart';
12
import '../lib/src/base/context.dart';
13
import '../lib/src/base/file_system.dart';
14
import '../lib/src/base/io.dart';
15
import '../lib/src/base/logger.dart';
16
import '../lib/src/base/os.dart';
17
import '../lib/src/base/platform.dart';
18
import '../lib/src/base/terminal.dart';
19
import '../lib/src/cache.dart';
20 21
import '../lib/src/flx.dart';
import '../lib/src/globals.dart';
22
import '../lib/src/usage.dart';
23 24 25

const String _kOptionPackages = 'packages';
const String _kOptionOutput = 'output-file';
26
const String _kOptionHeader = 'header';
27
const String _kOptionSnapshot = 'snapshot';
28
const String _kOptionDylib = 'dylib';
29
const String _kOptionWorking = 'working-dir';
30 31 32
const String _kOptionManifest = 'manifest';
const String _kOptionDepFile = 'depfile';
const String _kOptionBuildRoot = 'build-root';
33
const List<String> _kRequiredOptions = const <String>[
34 35
  _kOptionPackages,
  _kOptionOutput,
36
  _kOptionHeader,
37
  _kOptionWorking,
38 39
  _kOptionDepFile,
  _kOptionBuildRoot,
40 41
];

42
Future<Null> main(List<String> args) async {
43
  final AppContext executableContext = new AppContext();
44
  executableContext.setVariable(Logger, new StdoutLogger());
45
  await executableContext.runInZone(() {
46
    // Initialize the context with some defaults.
47
    // This list must be kept in sync with lib/executable.dart.
48
    context.putIfAbsent(Stdio, () => const Stdio());
49 50 51
    context.putIfAbsent(Platform, () => const LocalPlatform());
    context.putIfAbsent(FileSystem, () => const LocalFileSystem());
    context.putIfAbsent(ProcessManager, () => const LocalProcessManager());
52
    context.putIfAbsent(AnsiTerminal, () => new AnsiTerminal());
53 54 55 56 57
    context.putIfAbsent(Logger, () => new StdoutLogger());
    context.putIfAbsent(Cache, () => new Cache());
    context.putIfAbsent(Config, () => new Config());
    context.putIfAbsent(OperatingSystemUtils, () => new OperatingSystemUtils());
    context.putIfAbsent(Usage, () => new Usage());
58 59 60 61 62
    return run(args);
  });
}

Future<Null> run(List<String> args) async {
63 64 65
  final ArgParser parser = new ArgParser()
    ..addOption(_kOptionPackages, help: 'The .packages file')
    ..addOption(_kOptionOutput, help: 'The generated flx file')
66
    ..addOption(_kOptionHeader, help: 'The header of the flx file')
67
    ..addOption(_kOptionDylib, help: 'The generated AOT dylib file')
68 69
    ..addOption(_kOptionSnapshot, help: 'The generated snapshot file')
    ..addOption(_kOptionWorking,
70
        help: 'The directory where to put temporary files')
71 72 73
    ..addOption(_kOptionManifest, help: 'The manifest file')
    ..addOption(_kOptionDepFile, help: 'The generated depfile')
    ..addOption(_kOptionBuildRoot, help: 'The build\'s root directory');
74
  final ArgResults argResults = parser.parse(args);
75 76
  if (_kRequiredOptions
      .any((String option) => !argResults.options.contains(option))) {
77 78 79
    printError('Missing option! All options must be specified.');
    exit(1);
  }
80
  Cache.flutterRoot = platform.environment['FLUTTER_ROOT'];
81
  final String outputPath = argResults[_kOptionOutput];
82
  try {
83 84
    final String snapshotPath = argResults[_kOptionSnapshot];
    final String dylibPath = argResults[_kOptionDylib];
85
    final List<String> dependencies = await assemble(
86
      outputPath: outputPath,
87 88
      snapshotFile: snapshotPath == null ? null : fs.file(snapshotPath),
      dylibFile: dylibPath == null ? null : fs.file(dylibPath),
89 90
      workingDirPath: argResults[_kOptionWorking],
      packagesPath: argResults[_kOptionPackages],
91
      manifestPath: argResults[_kOptionManifest] ?? defaultManifestPath,
92 93
      includeDefaultFonts: false,
    );
94 95 96 97 98 99 100 101 102 103
    final String depFilePath = argResults[_kOptionDepFile];
    final int depFileResult = _createDepfile(
        depFilePath,
        fs.path.relative(argResults[_kOptionOutput],
            from: argResults[_kOptionBuildRoot]),
        dependencies);
    if (depFileResult != 0) {
      printError('Error creating depfile $depFilePath: $depFileResult.');
      exit(depFileResult);
    }
104 105 106
  } on ToolExit catch (e) {
    printError(e.message);
    exit(e.exitCode);
107 108 109 110 111 112 113 114
  }
  final int headerResult = _addHeader(outputPath, argResults[_kOptionHeader]);
  if (headerResult != 0) {
    printError('Error adding header to $outputPath: $headerResult.');
  }
  exit(headerResult);
}

115 116 117 118 119 120 121 122 123 124 125
int _createDepfile(
    String depFilePath, String target, List<String> dependencies) {
  try {
    final File depFile = fs.file(depFilePath);
    depFile.writeAsStringSync('$target: ${dependencies.join(' ')}\n');
    return 0;
  } catch (_) {
    return 1;
  }
}

126 127
int _addHeader(String outputPath, String header) {
  try {
128
    final File outputFile = fs.file(outputPath);
129 130 131 132 133 134
    final List<int> content = outputFile.readAsBytesSync();
    outputFile.writeAsStringSync('$header\n');
    outputFile.writeAsBytesSync(content, mode: FileMode.APPEND);
    return 0;
  } catch (_) {
    return 1;
135 136
  }
}