build_ios.dart 3.33 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 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';

import '../application_package.dart';
8
import '../base/common.dart';
9
import '../base/utils.dart';
10
import '../build_info.dart';
11 12
import '../globals.dart';
import '../ios/mac.dart';
13
import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult;
14
import 'build.dart';
15

16
class BuildIOSCommand extends BuildSubCommand {
17
  BuildIOSCommand() {
18
    usesTargetOption();
19
    usesFlavorOption();
20
    usesPubOption();
21 22
    usesBuildNumberOption();
    usesBuildNameOption();
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    argParser
      ..addFlag('debug',
        negatable: false,
        help: 'Build a debug version of your app (default mode for iOS simulator builds).',
      )
      ..addFlag('profile',
        negatable: false,
        help: 'Build a version of your app specialized for performance profiling.',
      )
      ..addFlag('release',
        negatable: false,
        help: 'Build a release version of your app (default mode for device builds).',
      )
      ..addFlag('simulator',
        help: 'Build for the iOS simulator instead of the device.',
      )
      ..addFlag('codesign',
        defaultsTo: true,
        help: 'Codesign the application bundle (only available on device builds).',
42
      );
43 44 45 46 47 48
  }

  @override
  final String name = 'ios';

  @override
49
  final String description = 'Build an iOS application bundle (Mac OS X host only).';
50

51 52 53 54 55 56
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
    DevelopmentArtifact.universal,
    DevelopmentArtifact.iOS,
  };

57 58
  @override
  Future<FlutterCommandResult> runCommand() async {
59 60
    final bool forSimulator = argResults['simulator'];
    defaultBuildMode = forSimulator ? BuildMode.debug : BuildMode.release;
61

62
    if (getCurrentHostPlatform() != HostPlatform.darwin_x64) {
63
      throwToolExit('Building for iOS is only supported on the Mac.');
64
    }
65

66
    final BuildableIOSApp app = await applicationPackages.getPackageForPlatform(TargetPlatform.ios);
67

68
    if (app == null) {
69
      throwToolExit('Application not configured for iOS');
70
    }
71

72
    final bool shouldCodesign = argResults['codesign'];
73 74

    if (!forSimulator && !shouldCodesign) {
75 76
      printStatus('Warning: Building for device with codesigning disabled. You will '
        'have to manually codesign before deploying to device.');
77
    }
78
    final BuildInfo buildInfo = getBuildInfo();
79
    if (forSimulator && !buildInfo.supportsSimulator) {
80
      throwToolExit('${toTitleCase(buildInfo.friendlyModeName)} mode is not supported for simulators.');
81
    }
82

83
    final String logTarget = forSimulator ? 'simulator' : 'device';
84

85
    final String typeName = artifacts.getEngineType(TargetPlatform.ios, buildInfo.mode);
86
    printStatus('Building $app for $logTarget ($typeName)...');
87
    final XcodeBuildResult result = await buildXcodeProject(
88
      app: app,
89
      buildInfo: buildInfo,
90
      targetOverride: targetFile,
91
      buildForDevice: !forSimulator,
92
      codesign: shouldCodesign,
93
    );
94

95
    if (!result.success) {
xster's avatar
xster committed
96
      await diagnoseXcodeBuildFailure(result);
97
      throwToolExit('Encountered error while building for $logTarget.');
98 99
    }

100
    if (result.output != null) {
101
      printStatus('Built ${result.output}.');
102
    }
103 104

    return null;
105 106
  }
}