build_bundle.dart 4.57 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 6
// @dart = 2.8

7
import '../base/common.dart';
8 9
import '../build_info.dart';
import '../bundle.dart';
10
import '../bundle_builder.dart';
11
import '../features.dart';
12
import '../globals_null_migrated.dart' as globals;
13
import '../project.dart';
14
import '../reporting/reporting.dart';
15
import '../runner/flutter_command.dart';
16 17 18
import 'build.dart';

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

    bundleBuilder ??= BundleBuilder();
63 64
  }

65 66
  BundleBuilder bundleBuilder;

67 68 69 70 71 72 73 74 75 76 77
  @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.';

78
  @override
79
  Future<CustomDimensions> get usageValues async {
80
    final String projectDir = globals.fs.file(targetFile).parent.parent.path;
81
    final FlutterProject flutterProject = FlutterProject.fromDirectory(globals.fs.directory(projectDir));
82
    if (flutterProject == null) {
83
      return const CustomDimensions();
84
    }
85 86 87 88
    return CustomDimensions(
      commandBuildBundleTargetPlatform: stringArg('target-platform'),
      commandBuildBundleIsModule: flutterProject.isModule,
    );
89 90
  }

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

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

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

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