Merge revisions 13674, 13676, 13677, 13678 to trunk
Review URL: https://codereview.chromium.org//11174004

git-svn-id: http://dart.googlecode.com/svn/trunk@13679 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/tools/VERSION b/tools/VERSION
index c3d743e..87db7a4 100644
--- a/tools/VERSION
+++ b/tools/VERSION
@@ -1,4 +1,4 @@
 MAJOR 0
 MINOR 1
 BUILD 6
-PATCH 3
+PATCH 4
diff --git a/utils/pub/hosted_source.dart b/utils/pub/hosted_source.dart
index 2d5c1f5..b3676b8 100644
--- a/utils/pub/hosted_source.dart
+++ b/utils/pub/hosted_source.dart
@@ -77,7 +77,8 @@
 
     // Download and extract the archive to a temp directory.
     var tempDir;
-    return Futures.wait([httpGet(fullUrl), createTempDir()]).chain((args) {
+    return Futures.wait([httpGet(fullUrl),
+                         systemCache.createTempDir()]).chain((args) {
       tempDir = args[1];
       return timeout(extractTarGz(args[0], tempDir), HTTP_TIMEOUT,
           'Timed out while fetching URL "$fullUrl".');
@@ -85,17 +86,16 @@
       // Now that the install has succeeded, move it to the real location in
       // the cache. This ensures that we don't leave half-busted ghost
       // directories in the user's pub cache if an install fails.
-      var rename = renameDir(tempDir, destPath);
 
-      // TODO(rnystrom): Awful hack. On Windows, we see cases where the extract
-      // has not finished by the time we get here, so the rename fails with a
-      // "directory in use" error. So, we will just wait a couple of seconds
-      // before we start.
       if (io.Platform.operatingSystem == "windows") {
-        rename = sleep(2000).chain((_) => rename);
+        // TODO(rnystrom): Awful hack. On Windows, we see cases where the
+        // extract has not finished by the time we get here, so the rename fails
+        // with a "directory in use" error. So, we will just wait a couple of
+        // seconds before we start.
+        return sleep(2000).chain((_) => renameDir(tempDir, destPath));
+      } else {
+        return renameDir(tempDir, destPath);
       }
-
-      return rename;
     }).transform((_) => true);
   }
 
diff --git a/utils/pub/io.dart b/utils/pub/io.dart
index c5f64c6..6981e28 100644
--- a/utils/pub/io.dart
+++ b/utils/pub/io.dart
@@ -21,6 +21,8 @@
 /** Gets the current working directory. */
 String get workingDir => new File('.').fullPathSync();
 
+const Pattern NEWLINE_PATTERN = const RegExp("\r\n?|\n\r?");
+
 /**
  * Prints the given string to `stderr` on its own line.
  */
@@ -499,50 +501,18 @@
   }
   options.environment = environment;
 
-  final process = Process.start(executable, args, options);
-
-  final outStream = new StringInputStream(process.stdout);
-  final processStdout = <String>[];
-
-  final errStream = new StringInputStream(process.stderr);
-  final processStderr = <String>[];
-
-  final completer = new Completer<PubProcessResult>();
-
-  checkComplete() {
-    // Wait until the process is done and its output streams are closed.
-    if (!pipeStdout && !outStream.closed) return;
-    if (!pipeStderr && !errStream.closed) return;
-    if (exitCode == null) return;
-
-    completer.complete(new PubProcessResult(
-        processStdout, processStderr, exitCode));
-  }
-
-  if (pipeStdout) {
-    process.stdout.pipe(stdout, close: false);
-  } else {
-    outStream.onLine   = () => processStdout.add(outStream.readLine());
-    outStream.onClosed = checkComplete;
-    outStream.onError  = (error) => completer.completeException(error);
-  }
-
-  if (pipeStderr) {
-    process.stderr.pipe(stderr, close: false);
-  } else {
-    errStream.onLine   = () => processStderr.add(errStream.readLine());
-    errStream.onClosed = checkComplete;
-    errStream.onError  = (error) => completer.completeException(error);
-  }
-
-  process.onExit = (actualExitCode) {
-    exitCode = actualExitCode;
-    checkComplete();
-  };
-
-  process.onError = (error) => completer.completeException(error);
-
-  return completer.future;
+  var future = Process.run(executable, args, options);
+  return future.transform((result) {
+    // TODO(rnystrom): Remove this and change to returning one string.
+    List<String> toLines(String output) {
+      var lines = output.split(NEWLINE_PATTERN);
+      if (!lines.isEmpty() && lines.last() == "") lines.removeLast();
+      return lines;
+    }
+    return new PubProcessResult(toLines(result.stdout),
+                                toLines(result.stderr),
+                                result.exitCode);
+  });
 }
 
 /**
diff --git a/utils/pub/pub.dart b/utils/pub/pub.dart
index e85b60e..a5d8340 100644
--- a/utils/pub/pub.dart
+++ b/utils/pub/pub.dart
@@ -102,7 +102,7 @@
   }
 
   var commandArgs =
-    globalOptions.rest.getRange(1, globalOptions.rest.length - 1);
+      globalOptions.rest.getRange(1, globalOptions.rest.length - 1);
   command.run(cache, globalOptions, commandArgs);
 }
 
@@ -221,6 +221,8 @@
       }
     });
 
+    future = future.chain((_) => cache_.deleteTempDir());
+
     future.handleException((e) {
       if (e is PubspecNotFoundException && e.name == null) {
         e = 'Could not find a file named "pubspec.yaml" in the directory '
@@ -232,6 +234,7 @@
 
       handleError(e, future.stackTrace);
     });
+
     // Explicitly exit on success to ensure that any dangling dart:io handles
     // don't cause the process to never terminate.
     future.then((_) => exit(0));
diff --git a/utils/pub/system_cache.dart b/utils/pub/system_cache.dart
index 1a4cdd0..0e4822c 100644
--- a/utils/pub/system_cache.dart
+++ b/utils/pub/system_cache.dart
@@ -4,7 +4,10 @@
 
 library system_cache;
 
+import 'dart:io';
+
 import 'io.dart';
+import 'io.dart' as io show createTempDir;
 import 'package.dart';
 import 'source.dart';
 import 'source_registry.dart';
@@ -23,6 +26,8 @@
    */
   final String rootDir;
 
+  String get tempDir => join(rootDir, '_temp');
+
   /**
    * Packages which are currently being asynchronously installed to the cache.
    */
@@ -70,4 +75,23 @@
     _pendingInstalls[id] = future;
     return future;
   }
+
+  /// Create a new temporary directory within the system cache. The system
+  /// cache maintains its own temporary directory that it uses to stage
+  /// packages into while installing. It uses this instead of the OS's system
+  /// temp directory to ensure that it's on the same volume as the pub system
+  /// cache so that it can move the directory from it.
+  Future<Directory> createTempDir() {
+    return ensureDir(tempDir).chain((temp) {
+      return io.createTempDir(join(temp, 'dir'));
+    });
+  }
+
+  /// Delete's the system cache's internal temp directory.
+  Future deleteTempDir() {
+    return dirExists(tempDir).chain((exists) {
+      if (!exists) return new Future.immediate(null);
+      return deleteDir(tempDir);
+    });
+  }
 }