diff --git a/bin/cache/update_material_fonts.sh b/bin/cache/update_material_fonts.sh deleted file mode 100755 index a1c8874c24..0000000000 --- a/bin/cache/update_material_fonts.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash -# 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. - -set -e - -FLUTTER_ROOT=$(dirname $(dirname $(dirname "${BASH_SOURCE[0]}"))) - -MATERIAL_FONTS_STAMP_PATH="$FLUTTER_ROOT/bin/cache/material_fonts.stamp" -MATERIAL_FONTS_VERSION=`cat "$FLUTTER_ROOT/bin/cache/material_fonts.version"` - -if [ ! -f "$MATERIAL_FONTS_STAMP_PATH" ] || [ "$MATERIAL_FONTS_VERSION" != `cat "$MATERIAL_FONTS_STAMP_PATH"` ]; then - echo "Downloading Material Design fonts..." - - MATERIAL_FONTS_URL="$MATERIAL_FONTS_VERSION" - MATERIAL_FONTS_PATH="$FLUTTER_ROOT/bin/cache/artifacts/material_fonts" - MATERIAL_FONTS_ZIP="$FLUTTER_ROOT/bin/cache/material_fonts.zip" - - mkdir -p -- "$MATERIAL_FONTS_PATH" - - curl --progress-bar -continue-at=- --location --output "$MATERIAL_FONTS_ZIP" "$MATERIAL_FONTS_URL" - rm -rf -- "$MATERIAL_FONTS_PATH" - unzip -o -q "$MATERIAL_FONTS_ZIP" -d "$MATERIAL_FONTS_PATH" - rm -f -- "$MATERIAL_FONTS_ZIP" - - echo "$MATERIAL_FONTS_VERSION" > "$MATERIAL_FONTS_STAMP_PATH" -fi diff --git a/bin/flutter b/bin/flutter index 42f5c83ae4..e4b92e925c 100755 --- a/bin/flutter +++ b/bin/flutter @@ -30,7 +30,6 @@ REVISION=`(cd "$FLUTTER_ROOT"; git rev-parse HEAD)` if [ ! -f "$SNAPSHOT_PATH" ] || [ ! -f "$STAMP_PATH" ] || [ `cat "$STAMP_PATH"` != "$REVISION" ] || [ "$FLUTTER_TOOLS_DIR/pubspec.yaml" -nt "$FLUTTER_TOOLS_DIR/pubspec.lock" ]; then "$FLUTTER_ROOT/bin/cache/update_dart_sdk.sh" "$FLUTTER_ROOT/bin/cache/update_engine.sh" - "$FLUTTER_ROOT/bin/cache/update_material_fonts.sh" echo Building flutter tool... FLUTTER_DIR="$FLUTTER_ROOT/packages/flutter" diff --git a/packages/flutter_tools/lib/src/base/logger.dart b/packages/flutter_tools/lib/src/base/logger.dart index 7f2842ca9d..a8ac7d700f 100644 --- a/packages/flutter_tools/lib/src/base/logger.dart +++ b/packages/flutter_tools/lib/src/base/logger.dart @@ -2,6 +2,7 @@ // 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'; final _AnsiTerminal _terminal = new _AnsiTerminal(); @@ -21,27 +22,59 @@ abstract class Logger { /// to help diagnose issues with the toolchain or with their setup. void printTrace(String message); + /// Start an indeterminate progress display. + Status startProgress(String message); + /// Flush any buffered output. void flush() { } } +class Status { + void stop({ bool showElapsedTime: false }) { } + void cancel() { } +} + class StdoutLogger implements Logger { + Status _status; + @override bool get isVerbose => false; @override void printError(String message, [StackTrace stackTrace]) { + _status?.cancel(); + _status = null; + stderr.writeln(message); if (stackTrace != null) stderr.writeln(stackTrace); } @override - void printStatus(String message) => print(message); + void printStatus(String message) { + _status?.cancel(); + _status = null; + + print(message); + } @override void printTrace(String message) { } + @override + Status startProgress(String message) { + _status?.cancel(); + _status = null; + + if (_terminal.supportsColor) { + _status = new _AnsiStatus(message); + return _status; + } else { + printStatus(message); + return new Status(); + } + } + @override void flush() { } } @@ -67,6 +100,12 @@ class BufferLogger implements Logger { @override void printTrace(String message) => _trace.writeln(message); + @override + Status startProgress(String message) { + printStatus(message); + return new Status(); + } + @override void flush() { } } @@ -95,6 +134,12 @@ class VerboseLogger implements Logger { lastMessage = new _LogMessage(_LogType.trace, message); } + @override + Status startProgress(String message) { + printStatus(message); + return new Status(); + } + @override void flush() => _emit(); @@ -157,3 +202,53 @@ class _AnsiTerminal { String writeBold(String str) => supportsColor ? '$_bold$str$_reset' : str; } + +class _AnsiStatus extends Status { + _AnsiStatus(this.message) { + stopwatch = new Stopwatch()..start(); + + stdout.write('${message.padRight(40)} '); + stdout.write('${_progress[0]}'); + + timer = new Timer.periodic(new Duration(milliseconds: 100), _callback); + } + + static final List _progress = ['-', r'\', '|', r'/', '-', r'\', '|', '/']; + + final String message; + Stopwatch stopwatch; + Timer timer; + int index = 1; + bool live = true; + + void _callback(Timer timer) { + stdout.write('\b${_progress[index]}'); + index = ++index % _progress.length; + } + + @override + void stop({ bool showElapsedTime: false }) { + if (!live) + return; + live = false; + + if (showElapsedTime) { + double seconds = stopwatch.elapsedMilliseconds / 1000.0; + print('\b\b\b\b${seconds.toStringAsFixed(1)}s'); + } else { + print('\b'); + } + + timer.cancel(); + } + + @override + void cancel() { + if (!live) + return; + live = false; + + print('\b'); + timer.cancel(); + } +} diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart new file mode 100644 index 0000000000..476b048dd2 --- /dev/null +++ b/packages/flutter_tools/lib/src/cache.dart @@ -0,0 +1,122 @@ +// 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 'artifacts.dart'; +import 'base/context.dart'; +import 'base/logger.dart'; +import 'base/process.dart'; +import 'globals.dart'; + +// update_material_fonts + +class Cache { + static Cache get instance => context[Cache] ?? (context[Cache] = new Cache()); + + /// Return the top-level directory in the cache; this is `bin/cache`. + Directory getRoot() => new Directory(path.join(ArtifactStore.flutterRoot, 'bin', 'cache')); + + /// Return the top-level mutable directory in the cache; this is `bin/cache/artifacts`. + Directory getCacheArtifacts() { + Directory artifacts = new Directory(path.join(getRoot().path, 'artifacts')); + if (!artifacts.existsSync()) + artifacts.createSync(); + return artifacts; + } + + /// Get a named directory from with the cache's artifact directory; for example, + /// `material_fonts` would return `bin/cache/artifacts/material_fonts`. + Directory getArtifactDirectory(String name) { + return new Directory(path.join(getCacheArtifacts().path, name)); + } + + String getVersionFor(String kArtifactName) { + File versionFile = new File(path.join(getRoot().path, '$kArtifactName.version')); + return versionFile.existsSync() ? versionFile.readAsStringSync().trim() : null; + } + + String getStampFor(String kArtifactName) { + File stampFile = new File(path.join(getRoot().path, '$kArtifactName.stamp')); + return stampFile.existsSync() ? stampFile.readAsStringSync().trim() : null; + } + + void setStampFor(String kArtifactName, String version) { + File stampFile = new File(path.join(getRoot().path, '$kArtifactName.stamp')); + stampFile.writeAsStringSync(version); + } + + /// Download a file from the given URL and return the bytes. + static Future> _downloadFile(Uri url) async { + printTrace('Downloading $url.'); + + HttpClient httpClient = new HttpClient(); + HttpClientRequest request = await httpClient.getUrl(url); + HttpClientResponse response = await request.close(); + + printTrace('Received response statusCode=${response.statusCode}'); + if (response.statusCode != 200) + throw new Exception(response.reasonPhrase); + + BytesBuilder responseBody = new BytesBuilder(copy: false); + await for (List chunk in response) + responseBody.add(chunk); + + return responseBody.takeBytes(); + } + + /// Download a file from the given url and write it to the cache. + /// If [unzip] is true, treat the url as a zip file, and unzip it to the + /// directory given. + static Future _downloadFileToCache(Uri url, FileSystemEntity location, bool unzip) async { + if (!location.parent.existsSync()) + location.parent.createSync(recursive: true); + + List fileBytes = await _downloadFile(url); + if (unzip) { + if (location is Directory && !location.existsSync()) + location.createSync(recursive: true); + + File tempFile = new File(path.join(Directory.systemTemp.path, '${url.toString().hashCode}.zip')); + tempFile.writeAsBytesSync(fileBytes, flush: true); + // unzip -o -q zipfile -d dest + runSync(['unzip', '-o', '-q', tempFile.path, '-d', location.path]); + tempFile.deleteSync(); + } else { + (location as File).writeAsBytesSync(fileBytes, flush: true); + } + } +} + +class MaterialFonts { + MaterialFonts(this.cache); + + static const String kName = 'material_fonts'; + + final Cache cache; + + bool isUpToDate() { + if (!cache.getArtifactDirectory(kName).existsSync()) + return false; + return cache.getVersionFor(kName) == cache.getStampFor(kName); + } + + Future download() { + Status status = logger.startProgress('Downloading Material fonts...'); + + return Cache._downloadFileToCache( + Uri.parse(cache.getVersionFor(kName)), + cache.getArtifactDirectory(kName), + true + ).then((_) { + cache.setStampFor(kName, cache.getVersionFor(kName)); + status.stop(showElapsedTime: true); + }).whenComplete(() { + status.cancel(); + }); + } +} diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index 12582f8d18..3946d05e78 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -412,6 +412,12 @@ class NotifyingLogger extends Logger { void printTrace(String message) { // This is a lot of traffic to send over the wire. } + + @override + Status startProgress(String message) { + printStatus(message); + return new Status(); + } } class LogMessage { diff --git a/packages/flutter_tools/lib/src/globals.dart b/packages/flutter_tools/lib/src/globals.dart index 3ce1c1b3a5..877c276378 100644 --- a/packages/flutter_tools/lib/src/globals.dart +++ b/packages/flutter_tools/lib/src/globals.dart @@ -5,6 +5,7 @@ import 'android/android_sdk.dart'; import 'base/context.dart'; import 'base/logger.dart'; +import 'cache.dart'; import 'device.dart'; import 'doctor.dart'; @@ -12,6 +13,7 @@ DeviceManager get deviceManager => context[DeviceManager]; Logger get logger => context[Logger]; AndroidSdk get androidSdk => context[AndroidSdk]; Doctor get doctor => context[Doctor]; +Cache get cache => Cache.instance; /// Display an error level message to the user. Commands should use this if they /// fail in some way. diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index f604bb093a..e324451b24 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -9,6 +9,7 @@ import 'package:args/command_runner.dart'; import '../application_package.dart'; import '../build_configuration.dart'; +import '../cache.dart'; import '../dart/pub.dart'; import '../device.dart'; import '../flx.dart' as flx; @@ -126,6 +127,11 @@ abstract class FlutterCommand extends Command { return exitCode; } + // Populate the cache. + MaterialFonts materialFonts = new MaterialFonts(cache); + if (!materialFonts.isUpToDate()) + await materialFonts.download(); + _setupToolchain(); _setupApplicationPackages();