build_bundle.dart 4.72 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
import '../base/common.dart';
6 7
import '../build_info.dart';
import '../bundle.dart';
8
import '../bundle_builder.dart';
9
import '../features.dart';
10
import '../globals.dart' as globals;
11
import '../project.dart';
12
import '../reporting/reporting.dart';
13
import '../runner/flutter_command.dart';
14 15 16
import 'build.dart';

class BuildBundleCommand extends BuildSubCommand {
17
  BuildBundleCommand({
18
    required super.logger,
19
    bool verboseHelp = false,
20 21
    BundleBuilder? bundleBuilder,
  }) :  _bundleBuilder = bundleBuilder ?? BundleBuilder(), super(verboseHelp: verboseHelp) {
22
    usesTargetOption();
23
    usesFilesystemOptions(hide: !verboseHelp);
24
    usesBuildNumberOption();
25
    addBuildModeFlags(verboseHelp: verboseHelp, defaultToRelease: false);
26
    usesDartDefineOption();
27
    usesExtraDartFlagOptions(verboseHelp: verboseHelp);
28
    argParser
29 30 31 32 33
      ..addOption('depfile',
        defaultsTo: defaultDepfilePath,
        help: 'A file path where a depfile will be written. '
              'This contains all build inputs and outputs in a Make-style syntax.'
      )
34 35
      ..addOption('target-platform',
        defaultsTo: 'android-arm',
36 37 38 39 40 41
        allowed: const <String>[
          'android-arm',
          'android-arm64',
          'android-x86',
          'android-x64',
          'ios',
42
          'darwin',
43
          'linux-x64',
44
          'linux-arm64',
45 46
          'windows-x64',
        ],
47
        help: 'The architecture for which to build the application.',
48
      )
49 50
      ..addOption('asset-dir',
        defaultsTo: getAssetBuildDirectory(),
51
        help: 'The output directory for the kernel_blob.bin file, the native snapshot, the assets, etc. '
52
              'Can be used to redirect the output when driving the Flutter toolchain from another build system.',
53 54 55 56 57
      )
      ..addFlag(
        'tree-shake-icons',
        hide: !verboseHelp,
        help: '(deprecated) Icon font tree shaking is not supported by this command.',
58
      );
59
    usesPubOption();
60
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
61 62
  }

63
  final BundleBuilder _bundleBuilder;
64

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<CustomDimensions> get usageValues async {
78
    final String projectDir = globals.fs.file(targetFile).parent.parent.path;
79
    final FlutterProject flutterProject = FlutterProject.fromDirectory(globals.fs.directory(projectDir));
80
    return CustomDimensions(
81
      commandBuildBundleTargetPlatform: stringArg('target-platform'),
82 83
      commandBuildBundleIsModule: flutterProject.isModule,
    );
84 85
  }

86 87
  @override
  Future<void> validateCommand() async {
88
    if (boolArg('tree-shake-icons')) {
89 90 91 92 93
      throwToolExit('The "--tree-shake-icons" flag is deprecated for "build bundle" and will be removed in a future version of Flutter.');
    }
    return super.validateCommand();
  }

94
  @override
95
  Future<FlutterCommandResult> runCommand() async {
96
    final String targetPlatform = stringArg('target-platform')!;
97
    final TargetPlatform platform = getTargetPlatformForName(targetPlatform);
98
    // Check for target platforms that are only allowed via feature flags.
99
    switch (platform) {
100
      case TargetPlatform.darwin:
101 102 103
        if (!featureFlags.isMacOSEnabled) {
          throwToolExit('macOS is not a supported target platform.');
        }
104
      case TargetPlatform.windows_x64:
105 106 107
        if (!featureFlags.isWindowsEnabled) {
          throwToolExit('Windows is not a supported target platform.');
        }
108
      case TargetPlatform.linux_x64:
109
      case TargetPlatform.linux_arm64:
110 111
        if (!featureFlags.isLinuxEnabled) {
          throwToolExit('Linux is not a supported target platform.');
112
        }
113 114 115 116 117 118 119 120 121 122
      case TargetPlatform.android:
      case TargetPlatform.android_arm:
      case TargetPlatform.android_arm64:
      case TargetPlatform.android_x64:
      case TargetPlatform.android_x86:
      case TargetPlatform.fuchsia_arm64:
      case TargetPlatform.fuchsia_x64:
      case TargetPlatform.ios:
      case TargetPlatform.tester:
      case TargetPlatform.web_javascript:
123 124
        break;
    }
125

126 127 128
    final BuildInfo buildInfo = await getBuildInfo();
    displayNullSafetyMode(buildInfo);

129
    await _bundleBuilder.build(
130
      platform: platform,
131
      buildInfo: buildInfo,
132
      mainPath: targetFile,
133 134
      depfilePath: stringArg('depfile'),
      assetDirPath: stringArg('asset-dir'),
135
    );
136
    return FlutterCommandResult.success();
137 138
  }
}