build_bundle.dart 4.98 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
    if (flutterProject == null) {
81
      return const CustomDimensions();
82
    }
83
    return CustomDimensions(
84
      commandBuildBundleTargetPlatform: stringArgDeprecated('target-platform'),
85 86
      commandBuildBundleIsModule: flutterProject.isModule,
    );
87 88
  }

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

97
  @override
98
  Future<FlutterCommandResult> runCommand() async {
99
    final String targetPlatform = stringArgDeprecated('target-platform')!;
100
    final TargetPlatform platform = getTargetPlatformForName(targetPlatform);
101
    if (platform == null) {
102
      throwToolExit('Unknown platform: $targetPlatform');
103
    }
104
    // Check for target platforms that are only allowed via feature flags.
105
    switch (platform) {
106
      case TargetPlatform.darwin:
107 108 109 110
        if (!featureFlags.isMacOSEnabled) {
          throwToolExit('macOS is not a supported target platform.');
        }
        break;
111
      case TargetPlatform.windows_x64:
112 113 114 115
        if (!featureFlags.isWindowsEnabled) {
          throwToolExit('Windows is not a supported target platform.');
        }
        break;
116
      case TargetPlatform.linux_x64:
117
      case TargetPlatform.linux_arm64:
118 119
        if (!featureFlags.isLinuxEnabled) {
          throwToolExit('Linux is not a supported target platform.');
120 121
        }
        break;
122 123 124 125 126 127 128 129 130 131
      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:
132 133
        break;
    }
134

135
    final BuildInfo buildInfo = await getBuildInfo();
136
    displayNullSafetyMode(buildInfo);
137

138
    await _bundleBuilder.build(
139
      platform: platform,
140
      buildInfo: buildInfo,
141
      mainPath: targetFile,
142 143
      depfilePath: stringArgDeprecated('depfile'),
      assetDirPath: stringArgDeprecated('asset-dir'),
144
    );
145
    return FlutterCommandResult.success();
146 147
  }
}