Write binstubs using the system encoding.

Closes #1304

R=rnystrom@google.com

Review URL: https://codereview.chromium.org//1248483003 .
diff --git a/lib/src/global_packages.dart b/lib/src/global_packages.dart
index be7bad9..60eef15 100644
--- a/lib/src/global_packages.dart
+++ b/lib/src/global_packages.dart
@@ -5,6 +5,7 @@
 library pub.global_packages;
 
 import 'dart:async';
+import 'dart:convert';
 import 'dart:io';
 
 import 'package:path/path.dart' as p;
@@ -694,7 +695,9 @@
 """;
       }
 
-      writeTextFile(binStubPath, bash);
+      // Write this as the system encoding since the system is going to execute
+      // it and it might contain non-ASCII caharacters in the pathnames.
+      writeTextFile(binStubPath, bash, encoding: const SystemEncoding());
 
       // Make it executable.
       var result = Process.runSync('chmod', ['+x', binStubPath]);
diff --git a/lib/src/io.dart b/lib/src/io.dart
index 2ec22ff..8b7fc17 100644
--- a/lib/src/io.dart
+++ b/lib/src/io.dart
@@ -165,14 +165,16 @@
 ///
 /// If [dontLogContents] is true, the contents of the file will never be logged.
 String writeTextFile(String file, String contents,
-  {bool dontLogContents: false}) {
+    {bool dontLogContents: false, Encoding encoding}) {
+  if (encoding == null) encoding = UTF8;
+
   // Sanity check: don't spew a huge file.
   log.io("Writing ${contents.length} characters to text file $file.");
   if (!dontLogContents && contents.length < 1024 * 1024) {
     log.fine("Contents:\n$contents");
   }
 
-  new File(file).writeAsStringSync(contents);
+  new File(file).writeAsStringSync(contents, encoding: encoding);
   return file;
 }