build_bundle.dart 4.78 KB
Newer Older
1 2 3 4 5 6
// Copyright 2018 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 '../base/common.dart';
8
import '../base/file_system.dart';
9 10
import '../build_info.dart';
import '../bundle.dart';
11
import '../features.dart';
12
import '../project.dart';
13
import '../reporting/reporting.dart';
14
import '../runner/flutter_command.dart' show FlutterOptions, FlutterCommandResult;
15 16 17
import 'build.dart';

class BuildBundleCommand extends BuildSubCommand {
18
  BuildBundleCommand({bool verboseHelp = false, this.bundleBuilder}) {
19
    usesTargetOption();
20
    usesFilesystemOptions(hide: !verboseHelp);
21 22
    usesBuildNumberOption();
    addBuildModeFlags(verboseHelp: verboseHelp);
23 24 25 26 27 28 29
    argParser
      ..addFlag('precompiled', negatable: false)
      // This option is still referenced by the iOS build scripts. We should
      // remove it once we've updated those build scripts.
      ..addOption('asset-base', help: 'Ignored. Will be removed.', hide: !verboseHelp)
      ..addOption('manifest', defaultsTo: defaultManifestPath)
      ..addOption('private-key', defaultsTo: defaultPrivateKeyPath)
30
      ..addOption('depfile', defaultsTo: defaultDepfilePath)
31 32
      ..addOption('target-platform',
        defaultsTo: 'android-arm',
33 34 35 36 37 38 39 40 41 42
        allowed: const <String>[
          'android-arm',
          'android-arm64',
          'android-x86',
          'android-x64',
          'ios',
          'darwin-x64',
          'linux-x64',
          'windows-x64',
        ],
43
      )
44 45 46 47
      ..addMultiOption(FlutterOptions.kExtraFrontEndOptions,
        splitCommas: true,
        hide: true,
      )
48 49 50 51
      ..addMultiOption(FlutterOptions.kExtraGenSnapshotOptions,
        splitCommas: true,
        hide: true,
      )
52 53 54 55
      ..addOption('asset-dir', defaultsTo: getAssetBuildDirectory())
      ..addFlag('report-licensed-packages',
        help: 'Whether to report the names of all the packages that are included '
              'in the application\'s LICENSE file.',
56
        defaultsTo: false);
57
    usesPubOption();
58
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
59 60

    bundleBuilder ??= BundleBuilder();
61 62
  }

63 64
  BundleBuilder bundleBuilder;

65 66 67 68 69 70 71 72 73 74 75
  @override
  final String name = 'bundle';

  @override
  final String description = 'Build the Flutter assets directory from your app.';

  @override
  final String usageFooter = 'The Flutter assets directory contains your '
      'application code and resources; they are used by some Flutter Android and'
      ' iOS runtimes.';

76
  @override
77
  Future<Map<CustomDimensions, String>> get usageValues async {
78 79 80
    final String projectDir = fs.file(targetFile).parent.parent.path;
    final FlutterProject futterProject = FlutterProject.fromPath(projectDir);
    if (futterProject == null) {
81
      return const <CustomDimensions, String>{};
82
    }
83 84 85
    return <CustomDimensions, String>{
      CustomDimensions.commandBuildBundleTargetPlatform: argResults['target-platform'],
      CustomDimensions.commandBuildBundleIsModule: '${futterProject.isModule}'
86 87 88
    };
  }

89
  @override
90
  Future<FlutterCommandResult> runCommand() async {
91 92
    final String targetPlatform = argResults['target-platform'];
    final TargetPlatform platform = getTargetPlatformForName(targetPlatform);
93
    if (platform == null) {
94
      throwToolExit('Unknown platform: $targetPlatform');
95
    }
96
    // Check for target platforms that are only allowed via feature flags.
97 98
    switch (platform) {
      case TargetPlatform.darwin_x64:
99 100 101 102
        if (!featureFlags.isMacOSEnabled) {
          throwToolExit('macOS is not a supported target platform.');
        }
        break;
103
      case TargetPlatform.windows_x64:
104 105 106 107
        if (!featureFlags.isWindowsEnabled) {
          throwToolExit('Windows is not a supported target platform.');
        }
        break;
108
      case TargetPlatform.linux_x64:
109 110
        if (!featureFlags.isLinuxEnabled) {
          throwToolExit('Linux is not a supported target platform.');
111 112 113 114 115
        }
        break;
      default:
        break;
    }
116 117 118

    final BuildMode buildMode = getBuildMode();

119
    await bundleBuilder.build(
120 121
      platform: platform,
      buildMode: buildMode,
122 123
      mainPath: targetFile,
      manifestPath: argResults['manifest'],
124
      depfilePath: argResults['depfile'],
125 126 127 128 129
      privateKeyPath: argResults['private-key'],
      assetDirPath: argResults['asset-dir'],
      precompiledSnapshot: argResults['precompiled'],
      reportLicensedPackages: argResults['report-licensed-packages'],
      trackWidgetCreation: argResults['track-widget-creation'],
130
      extraFrontEndOptions: argResults[FlutterOptions.kExtraFrontEndOptions],
131
      extraGenSnapshotOptions: argResults[FlutterOptions.kExtraGenSnapshotOptions],
132 133 134
      fileSystemScheme: argResults['filesystem-scheme'],
      fileSystemRoots: argResults['filesystem-root'],
    );
135
    return null;
136 137
  }
}