build_bundle.dart 4.14 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 '../features.dart';
11
import '../globals_null_migrated.dart' as globals;
12
import '../project.dart';
13
import '../reporting/reporting.dart';
14
import '../runner/flutter_command.dart';
15 16 17
import 'build.dart';

class BuildBundleCommand extends BuildSubCommand {
18
  BuildBundleCommand({bool verboseHelp = false, this.bundleBuilder}) {
19
    addTreeShakeIconsFlag();
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 40 41
        allowed: const <String>[
          'android-arm',
          'android-arm64',
          'android-x86',
          'android-x64',
          'ios',
          'darwin-x64',
          'linux-x64',
42
          'linux-arm64',
43 44
          'windows-x64',
        ],
45
        help: 'The architecture for which to build the application.',
46
      )
47 48 49 50 51
      ..addOption('asset-dir',
        defaultsTo: getAssetBuildDirectory(),
        help: 'The output directory for the kernel_blob.bin file, the native snapshet, the assets, etc. '
              'Can be used to redirect the output when driving the Flutter toolchain from another build system.',
      );
52
    usesPubOption();
53
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
54 55

    bundleBuilder ??= BundleBuilder();
56 57
  }

58 59
  BundleBuilder bundleBuilder;

60 61 62 63 64 65 66 67 68 69 70
  @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.';

71
  @override
72
  Future<Map<CustomDimensions, String>> get usageValues async {
73
    final String projectDir = globals.fs.file(targetFile).parent.parent.path;
74
    final FlutterProject flutterProject = FlutterProject.fromDirectory(globals.fs.directory(projectDir));
75
    if (flutterProject == null) {
76
      return const <CustomDimensions, String>{};
77
    }
78
    return <CustomDimensions, String>{
79
      CustomDimensions.commandBuildBundleTargetPlatform: stringArg('target-platform'),
80
      CustomDimensions.commandBuildBundleIsModule: '${flutterProject.isModule}',
81 82 83
    };
  }

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

112
    final BuildInfo buildInfo = await getBuildInfo();
113
    displayNullSafetyMode(buildInfo);
114

115
    await bundleBuilder.build(
116
      platform: platform,
117
      buildInfo: buildInfo,
118
      mainPath: targetFile,
119
      manifestPath: defaultManifestPath,
120 121
      depfilePath: stringArg('depfile'),
      assetDirPath: stringArg('asset-dir'),
122
    );
123
    return FlutterCommandResult.success();
124 125
  }
}