run_mojo.dart 5.92 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2015 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 'dart:io';

import 'package:path/path.dart' as path;

10
import '../artifacts.dart';
11
import '../base/process.dart';
12
import '../build_configuration.dart';
13
import '../flx.dart' as flx;
14
import '../globals.dart';
15
import '../runner/flutter_command.dart';
16
import 'run.dart';
17 18

const String _kDefaultBundlePath = 'build/app.flx';
19

20
class RunMojoCommand extends FlutterCommand {
21
  RunMojoCommand({ this.hidden: false }) {
22
    argParser.addFlag('android', negatable: false, help: 'Run on an Android device');
23
    argParser.addFlag('checked', negatable: false, help: 'Run Flutter in checked mode');
24 25 26
    argParser.addFlag('mojo-debug', negatable: false, help: 'Use Debug build of mojo');
    argParser.addFlag('mojo-release', negatable: false, help: 'Use Release build of mojo (default)');

27 28 29 30 31
    argParser.addOption('target',
        defaultsTo: '',
        abbr: 't',
        help: 'Target app path or filename to start.');
    argParser.addOption('app', help: 'Run this Flutter app instead of building the target.');
32 33
    argParser.addOption('mojo-path', help: 'Path to directory containing mojo_shell and services.');
    argParser.addOption('devtools-path', help: 'Path to mojo devtools\' mojo_run command.');
34 35
  }

36 37 38 39 40 41 42 43 44
  @override
  final String name = 'run_mojo';

  @override
  final String description = 'Run a Flutter app in mojo (from github.com/domokit/mojo).';

  @override
  final bool hidden;

45
  @override
Kris Giesing's avatar
Kris Giesing committed
46
  bool get requiresProjectRoot => false;
47

48
  // TODO(abarth): Why not use path.absolute?
49
  String _makePathAbsolute(String relativePath) {
50
    File file = new File(relativePath);
51
    if (!file.existsSync()) {
Hixie's avatar
Hixie committed
52
      throw new Exception('Path "$relativePath" does not exist');
53 54 55 56
    }
    return file.absolute.path;
  }

57 58 59 60 61 62 63 64 65 66
  bool _useDevtools() {
    if (argResults['android'] || argResults['devtools-path'] != null) {
      return true;
    }
    return false;
  }

  String _getDevtoolsPath() {
    if (argResults['devtools-path'] != null) {
      return _makePathAbsolute(argResults['devtools-path']);
67
    }
68 69 70 71
    return _makePathAbsolute(path.join(argResults['mojo-path'], 'mojo', 'devtools', 'common', 'mojo_run'));
  }

  String _getMojoShellPath() {
Ian Hickson's avatar
Ian Hickson committed
72
    final String mojoBuildType = argResults['mojo-debug']  ? 'Debug' : 'Release';
73
    return _makePathAbsolute(path.join(argResults['mojo-path'], 'out', mojoBuildType, 'mojo_shell'));
74 75
  }

Kris Giesing's avatar
Kris Giesing committed
76
  BuildConfiguration _getCurrentHostConfig() {
77
    BuildConfiguration result;
78
    TargetPlatform target = argResults['android'] ?
79
      TargetPlatform.android_arm : getCurrentHostPlatformAsTarget();
80 81 82 83 84 85 86 87 88
    for (BuildConfiguration config in buildConfigurations) {
      if (config.targetPlatform == target) {
        result = config;
        break;
      }
    }
    return result;
  }

89
  Future<List<String>> _getShellConfig(String targetApp) async {
90
    List<String> args = <String>[];
91

92 93
    final bool useDevtools = _useDevtools();
    final String command = useDevtools ? _getDevtoolsPath() : _getMojoShellPath();
94 95
    args.add(command);

96
    BuildConfiguration config = _getCurrentHostConfig();
97 98 99

    String flutterPath;
    if (config == null || config.type == BuildType.prebuilt) {
100
      TargetPlatform targetPlatform = argResults['android'] ? TargetPlatform.android_arm : TargetPlatform.linux_x64;
101
      Artifact artifact = ArtifactStore.getArtifact(type: ArtifactType.mojo, targetPlatform: targetPlatform);
Devon Carew's avatar
Devon Carew committed
102
      flutterPath = _makePathAbsolute(ArtifactStore.getPath(artifact));
103 104 105 106
    } else {
      String localPath = path.join(config.buildDir, 'flutter.mojo');
      flutterPath = _makePathAbsolute(localPath);
    }
107

108
    if (argResults['android'])
109
      args.add('--android');
110

111
    final Uri appUri = Uri.parse(targetApp);
112
    if (appUri.scheme.isEmpty || appUri.scheme == 'file') {
113
      final String appPath = _makePathAbsolute(targetApp);
114
      if (argResults['android']) {
115 116 117 118
        final String appName = path.basename(appPath);
        final String appDir = path.dirname(appPath);
        args.add('mojo:launcher http://app/$appName');
        args.add('--map-origin=http://app/=$appDir');
119 120 121
      } else {
        args.add('mojo:launcher file://$appPath');
      }
122
    } else {
123
      args.add('mojo:launcher $targetApp');
124 125 126 127
    }

    // Add url-mapping for mojo:flutter.
    if (argResults['android']) {
128 129 130 131
      final String flutterName = path.basename(flutterPath);
      final String flutterDir = path.dirname(flutterPath);
      args.add('--map-origin=http://flutter/=$flutterDir');
      args.add('--url-mappings=mojo:flutter=http://flutter/$flutterName');
132
    } else {
133
      args.add('--url-mappings=mojo:flutter=file://$flutterPath');
134 135 136
    }

    if (useDevtools) {
137
      final String buildFlag = argResults['mojo-debug'] ? '--debug' : '--release';
138
      args.add(buildFlag);
Devon Carew's avatar
Devon Carew committed
139
      if (logger.isVerbose)
140 141 142
        args.add('--verbose');
    }

143
    if (argResults['checked'])
144
      args.add('--args-for=mojo:flutter --enable-checked-mode');
145 146

    args.addAll(argResults.rest);
147
    printStatus('$args');
148
    return args;
149 150
  }

151
  @override
Kris Giesing's avatar
Kris Giesing committed
152
  Future<int> runInProject() async {
153
    if ((argResults['mojo-path'] == null && argResults['devtools-path'] == null) || (argResults['mojo-path'] != null && argResults['devtools-path'] != null)) {
154
      printError('Must specify either --mojo-path or --devtools-path.');
155 156 157
      return 1;
    }

158
    if (argResults['mojo-debug'] && argResults['mojo-release']) {
159
      printError('Cannot specify both --mojo-debug and --mojo-release');
160 161
      return 1;
    }
162

163 164 165
    String targetApp = argResults['app'];
    if (targetApp == null) {
      targetApp = _kDefaultBundlePath;
166

167
      String mainPath = findMainDartFile(argResults['target']);
Kris Giesing's avatar
Kris Giesing committed
168

169 170 171
      int result = await flx.build(
        toolchain,
        mainPath: mainPath,
172
        outputPath: targetApp
173
      );
174 175 176 177
      if (result != 0)
        return result;
    }

178
    return await runCommandAndStreamOutput(await _getShellConfig(targetApp));
179
  }
180
}