fuchsia_attach.dart 7.32 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 8 9 10 11 12 13 14 15 16
import 'package:args/args.dart';

import 'package:flutter_tools/runner.dart' as runner;
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/attach.dart';
import 'package:flutter_tools/src/commands/doctor.dart';
17
import 'package:flutter_tools/src/device.dart';
18
import 'package:flutter_tools/src/features.dart';
19
import 'package:flutter_tools/src/fuchsia/fuchsia_device.dart';
20
import 'package:flutter_tools/src/fuchsia/fuchsia_sdk.dart';
21
import 'package:flutter_tools/src/fuchsia/fuchsia_workflow.dart';
22
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
23
import 'package:flutter_tools/src/project.dart';
24 25 26 27 28 29
import 'package:flutter_tools/src/runner/flutter_command.dart';

final ArgParser parser = ArgParser()
  ..addOption('build-dir', help: 'The fuchsia build directory')
  ..addOption('dart-sdk', help: 'The prebuilt dart SDK')
  ..addOption('target', help: 'The GN target to attach to')
30 31
  ..addOption('entrypoint', defaultsTo: 'main.dart', help: 'The filename of the main method. Defaults to main.dart')
  ..addOption('device', help: 'The device id to attach to')
32
  ..addOption('dev-finder', help: 'The location of the device-finder binary')
33
  ..addOption('ffx', help: 'The location of the ffx binary')
34 35 36 37 38 39 40 41
  ..addFlag('verbose', negatable: true);

// Track the original working directory so that the tool can find the
// flutter repo in third_party.
String originalWorkingDirectory;

Future<void> main(List<String> args) async {
  final ArgResults argResults = parser.parse(args);
42 43
  final bool verbose = argResults['verbose'] as bool;
  final String target = argResults['target'] as String;
44 45 46
  final List<String> targetParts = _extractPathAndName(target);
  final String path = targetParts[0];
  final String name = targetParts[1];
47
  final File dartSdk = globals.fs.file(argResults['dart-sdk']);
48
  final String buildDirectory = argResults['build-dir'] as String;
49 50 51
  final File frontendServer = globals.fs.file('$buildDirectory/host_x64/gen/third_party/flutter/frontend_server/frontend_server_tool.snapshot');
  final File sshConfig = globals.fs.file('$buildDirectory/ssh-keys/ssh_config');
  final File devFinder = globals.fs.file(argResults['dev-finder']);
52
  final File ffx = globals.fs.file(argResults['ffx']);
53 54
  final File platformKernelDill = globals.fs.file('$buildDirectory/flutter_runner_patched_sdk/platform_strong.dill');
  final File flutterPatchedSdk = globals.fs.file('$buildDirectory/flutter_runner_patched_sdk');
55 56 57
  final String packages = '$buildDirectory/dartlang/gen/$path/${name}_dart_library.packages';
  final String outputDill = '$buildDirectory/${name}_tmp.dill';

58
  // Running from fuchsia root hangs hot reload for some reason.
59
  // switch to the project root directory and run from there.
60 61
  originalWorkingDirectory = globals.fs.currentDirectory.path;
  globals.fs.currentDirectory = path;
62

63
  if (!devFinder.existsSync()) {
64
    print('Error: device-finder not found at ${devFinder.path}.');
65 66
    return 1;
  }
67 68 69 70
  if (!ffx.existsSync()) {
    print('Error: ffx not found at ${ffx.path}.');
    return 1;
  }
71 72 73 74 75 76 77 78 79
  if (!frontendServer.existsSync()) {
    print(
      'Error: frontend_server not found at ${frontendServer.path}. This '
      'Usually means you ran fx set without specifying '
      '--args=flutter_profile=true.'
    );
    return 1;
  }

80
  // Check for a package with a lib directory.
81
  final String entrypoint = argResults['entrypoint'] as String;
82
  String targetFile = 'lib/$entrypoint';
83
  if (!globals.fs.file(targetFile).existsSync()) {
84
    // Otherwise assume the package is flat.
85
    targetFile = entrypoint;
86
  }
87
  final String deviceName = argResults['device'] as String;
88 89 90 91 92 93 94
  final List<String> command = <String>[
    'attach',
    '--module',
    name,
    '--target',
    targetFile,
    '--target-model',
95
    'flutter_runner',
96 97 98 99
    '--output-dill',
    outputDill,
    '--packages',
    packages,
100 101
    if (deviceName != null && deviceName.isNotEmpty) ...<String>['-d', deviceName],
    if (verbose) '--verbose',
102 103 104 105
  ];
  Cache.disableLocking(); // ignore: invalid_use_of_visible_for_testing_member
  await runner.run(
    command,
106
    () => <FlutterCommand>[
107 108 109 110 111 112 113
      _FuchsiaAttachCommand(),
      _FuchsiaDoctorCommand(), // If attach fails the tool will attempt to run doctor.
    ],
    verbose: verbose,
    muteCommandLogging: false,
    verboseHelp: false,
    overrides: <Type, Generator>{
114
      FeatureFlags: () => const _FuchsiaFeatureFlags(),
115
      DeviceManager: () => _FuchsiaDeviceManager(),
116 117
      FuchsiaArtifacts: () => FuchsiaArtifacts(
        sshConfig: sshConfig, devFinder: devFinder, ffx: ffx),
118
      Artifacts: () => OverrideArtifacts(
119 120 121 122
        parent: CachedArtifacts(
          fileSystem: globals.fs,
          cache: globals.cache,
          platform: globals.platform,
123
          operatingSystemUtils: globals.os,
124
        ),
125 126 127 128 129
        frontendServer: frontendServer,
        engineDartBinary: dartSdk,
        platformKernelDill: platformKernelDill,
        flutterPatchedSdk: flutterPatchedSdk,
      ),
130
    },
131 132 133
  );
}

134 135 136 137
// An implementation of [DeviceManager] that only supports fuchsia devices.
class _FuchsiaDeviceManager extends DeviceManager {
  @override
  List<DeviceDiscovery> get deviceDiscoverers => List<DeviceDiscovery>.unmodifiable(<DeviceDiscovery>[
138 139 140 141 142 143
    FuchsiaDevices(
      logger: globals.logger,
      platform: globals.platform,
      fuchsiaWorkflow: fuchsiaWorkflow,
      fuchsiaSdk: fuchsiaSdk,
    ),
144 145 146 147 148 149 150 151
  ]);

  @override
  bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) {
    return true;
  }
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
List<String> _extractPathAndName(String gnTarget) {
  // Separate strings like //path/to/target:app into [path/to/target, app]
  final int lastColon = gnTarget.lastIndexOf(':');
  if (lastColon < 0) {
    throwToolExit('invalid path: $gnTarget');
  }
  final String name = gnTarget.substring(lastColon + 1);
  // Skip '//' and chop off after :
  if ((gnTarget.length < 3) || (gnTarget[0] != '/') || (gnTarget[1] != '/')) {
    throwToolExit('invalid path: $gnTarget');
  }
  final String path = gnTarget.substring(2, lastColon);
  return <String>[path, name];
}

class _FuchsiaDoctorCommand extends DoctorCommand {
  @override
  Future<FlutterCommandResult> runCommand() async {
    Cache.flutterRoot = '$originalWorkingDirectory/third_party/dart-pkg/git/flutter';
    return super.runCommand();
  }
}

class _FuchsiaAttachCommand extends AttachCommand {
  @override
  Future<FlutterCommandResult> runCommand() async {
    Cache.flutterRoot = '$originalWorkingDirectory/third_party/dart-pkg/git/flutter';
    return super.runCommand();
  }
}
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

class _FuchsiaFeatureFlags extends FeatureFlags {
  const _FuchsiaFeatureFlags();

  @override
  bool get isLinuxEnabled => false;

  @override
  bool get isMacOSEnabled => false;

  @override
  bool get isWebEnabled => false;

  @override
  bool get isWindowsEnabled => false;

  @override
  bool get isAndroidEnabled => false;

  @override
  bool get isIOSEnabled => false;

  @override
  bool get isFuchsiaEnabled => true;

  @override
  bool get isSingleWidgetReloadEnabled => false;
}