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

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

10
import '../android/android_device.dart';
11
import '../globals.dart';
12 13 14
import '../runner/flutter_command.dart';

class RefreshCommand extends FlutterCommand {
15
  @override
16
  final String name = 'refresh';
17 18

  @override
19 20 21
  final String description = 'Build and deploy the Dart code in a Flutter app (Android only).';

  RefreshCommand() {
22
    usesTargetOption();
23 24 25 26

    argParser.addOption('activity',
      help: 'The Android activity that will be told to reload the Flutter code.'
    );
27 28
  }

29
  @override
30 31
  bool get androidOnly => true;

32
  @override
33 34
  bool get requiresDevice => true;

35 36 37 38 39 40
  @override
  Future<int> runInProject() async {
    Directory tempDir = await Directory.systemTemp.createTemp('flutter_tools');
    try {
      String snapshotPath = path.join(tempDir.path, 'snapshot_blob.bin');

41
      int result = await toolchain.compiler.createSnapshot(
42 43 44 45 46 47 48
          mainPath: argResults['target'], snapshotPath: snapshotPath
      );
      if (result != 0) {
        printError('Failed to run the Flutter compiler. Exit code: $result');
        return result;
      }

49 50 51 52 53 54 55 56 57 58
      String activity = argResults['activity'];
      if (activity == null) {
        if (applicationPackages.android != null) {
          activity = applicationPackages.android.launchActivity;
        } else {
          printError('Unable to find the activity to be refreshed.');
          return 1;
        }
      }

59 60
      AndroidDevice device = deviceForCommand;

61
      bool success = await device.refreshSnapshot(activity, snapshotPath);
62
      if (!success) {
63
        printError('Error refreshing snapshot on $device.');
64 65 66 67 68 69 70 71 72
        return 1;
      }

      return 0;
    } finally {
      tempDir.deleteSync(recursive: true);
    }
  }
}