From cf8116368d88f5a99371d08267838008d1f9aa30 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 16 Feb 2016 11:02:41 -0800 Subject: [PATCH] flutter run should run pub get automatically This removes a step that can cause trouble. Fixes #1904 --- .../lib/src/commands/create.dart | 35 +------------- .../flutter_tools/lib/src/commands/run.dart | 11 +++++ packages/flutter_tools/lib/src/dart/pub.dart | 47 +++++++++++++++++++ 3 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 packages/flutter_tools/lib/src/dart/pub.dart diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 6b439eb2f8..f233911f40 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -12,7 +12,7 @@ import 'package:path/path.dart' as path; import '../android/android.dart' as android; import '../artifacts.dart'; import '../base/globals.dart'; -import '../base/process.dart'; +import '../dart/pub.dart'; import 'ios.dart'; class CreateCommand extends Command { @@ -70,39 +70,6 @@ All done! To run your application: printStatus(message); return 0; } - - Future pubGet({ - String directory: '', - bool skipIfAbsent: false - }) async { - File pubSpecYaml = new File(path.join(directory, 'pubspec.yaml')); - File pubSpecLock = new File(path.join(directory, 'pubspec.lock')); - File dotPackages = new File(path.join(directory, '.packages')); - - if (!pubSpecYaml.existsSync()) { - if (skipIfAbsent) - return 0; - printError('$directory: no pubspec.yaml found'); - return 1; - } - - if (!pubSpecLock.existsSync() || pubSpecYaml.lastModifiedSync().isAfter(pubSpecLock.lastModifiedSync())) { - printStatus("Running 'pub get' in '$directory'..."); - int code = await runCommandAndStreamOutput( - [sdkBinaryName('pub'), '--verbosity=warning', 'get'], - workingDirectory: directory - ); - if (code != 0) - return code; - } - - if ((pubSpecLock.existsSync() && pubSpecLock.lastModifiedSync().isAfter(pubSpecYaml.lastModifiedSync())) && - (dotPackages.existsSync() && dotPackages.lastModifiedSync().isAfter(pubSpecYaml.lastModifiedSync()))) - return 0; - - printError('$directory: pubspec.yaml, pubspec.lock, and .packages are in an inconsistent state'); - return 1; - } } abstract class Template { diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 7759cd67c8..1ae20a57cd 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -11,6 +11,7 @@ import '../application_package.dart'; import '../base/common.dart'; import '../base/globals.dart'; import '../build_configuration.dart'; +import '../dart/pub.dart'; import '../device.dart'; import '../flx.dart'; import '../runner/flutter_command.dart'; @@ -68,11 +69,21 @@ class RunCommand extends RunCommandBase { defaultsTo: false, negatable: false, help: 'Start in a paused mode and wait for a debugger to connect.'); + argParser.addFlag('pub', + defaultsTo: true, + help: 'Whether to run "pub get" before running the app.'); argParser.addOption('debug-port', defaultsTo: observatoryDefaultPort.toString(), help: 'Listen to the given port for a debug connection.'); } + @override + Future run() async { + if (argResults['pub']) + await pubGet(); + return await super.run(); + } + @override Future runInProject() async { printTrace('Downloading toolchain.'); diff --git a/packages/flutter_tools/lib/src/dart/pub.dart b/packages/flutter_tools/lib/src/dart/pub.dart new file mode 100644 index 0000000000..1856561c16 --- /dev/null +++ b/packages/flutter_tools/lib/src/dart/pub.dart @@ -0,0 +1,47 @@ +// 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; + +import '../base/globals.dart'; +import '../base/process.dart'; + +Future pubGet({ + String directory, + bool skipIfAbsent: false +}) async { + if (directory == null) + directory = Directory.current.path; + + File pubSpecYaml = new File(path.join(directory, 'pubspec.yaml')); + File pubSpecLock = new File(path.join(directory, 'pubspec.lock')); + File dotPackages = new File(path.join(directory, '.packages')); + + if (!pubSpecYaml.existsSync()) { + if (skipIfAbsent) + return 0; + printError('$directory: no pubspec.yaml found'); + return 1; + } + + if (!pubSpecLock.existsSync() || pubSpecYaml.lastModifiedSync().isAfter(pubSpecLock.lastModifiedSync())) { + printStatus("Running 'pub get' in '$directory'..."); + int code = await runCommandAndStreamOutput( + [sdkBinaryName('pub'), '--verbosity=warning', 'get'], + workingDirectory: directory + ); + if (code != 0) + return code; + } + + if ((pubSpecLock.existsSync() && pubSpecLock.lastModifiedSync().isAfter(pubSpecYaml.lastModifiedSync())) && + (dotPackages.existsSync() && dotPackages.lastModifiedSync().isAfter(pubSpecYaml.lastModifiedSync()))) + return 0; + + printError('$directory: pubspec.yaml, pubspec.lock, and .packages are in an inconsistent state'); + return 1; +}