Allow running pub with --preview-dart-2

- Make a snapshot in Dart 2 mode
- Check for the VM argument `--preview-dart-2` and run the correct
  snapshot

Towards fixing https://github.com/dart-lang/sdk/issues/32188

Change-Id: I56d5e7f268ff40b80783fae571981705536280f2
Reviewed-on: https://dart-review.googlesource.com/54743
Commit-Queue: Nate Bosch <nbosch@google.com>
Reviewed-by: Keerti Parthasarathy <keertip@google.com>
Reviewed-by: Kevin Moore <kevmoo@google.com>
diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn
index 5a3cf37..62ea806 100644
--- a/sdk/BUILD.gn
+++ b/sdk/BUILD.gn
@@ -46,6 +46,7 @@
 # ........dartdevk.dart.snapshot
 # ........kernel_summary_worker.dart.snapshot
 # ........pub.dart.snapshot
+# ........pub2.dart.snapshot
 #.........resources/
 #...........dartdoc/
 #..............packages
@@ -133,6 +134,10 @@
     "pub",
     "../utils/pub",
   ],
+  [
+    "pub2",
+    "../utils/pub:pub2",
+  ],
 ]
 
 _full_sdk_snapshots = [
@@ -173,6 +178,10 @@
     "../utils/pub",
   ],
   [
+    "pub2",
+    "../utils/pub:pub2",
+  ],
+  [
     "kernel-service",
     "../utils/kernel-service",
   ],
diff --git a/sdk/bin/pub_sdk b/sdk/bin/pub_sdk
index f42f609..74a86ca 100755
--- a/sdk/bin/pub_sdk
+++ b/sdk/bin/pub_sdk
@@ -15,13 +15,20 @@
   echo "$file"
 }
 
+function array_contains() {
+  local needle="$1"
+  local element
+  shift
+  for element; do [ "$element" = "$needle" ] && return 0; done
+  return 1
+}
+
 # Unlike $0, $BASH_SOURCE points to the absolute path of this file.
 PROG_NAME="$(follow_links "$BASH_SOURCE")"
 
 # Handle the case where dart-sdk/bin has been symlinked to.
 BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
 
-SNAPSHOT="$BIN_DIR/snapshots/pub.dart.snapshot"
 
 unset VM_OPTIONS
 declare -a VM_OPTIONS
@@ -34,4 +41,10 @@
 
 # Run the pub snapshot.
 DART="$BIN_DIR/dart"
-exec "$DART" "${VM_OPTIONS[@]}" "$SNAPSHOT" "$@"
+if array_contains "--preview-dart-2" "${VM_OPTIONS[@]}"; then
+  SNAPSHOT="$BIN_DIR/snapshots/pub2.dart.snapshot"
+  exec "$DART" "${VM_OPTIONS[@]}" "$SNAPSHOT" "$@"
+else
+  SNAPSHOT="$BIN_DIR/snapshots/pub.dart.snapshot"
+  exec "$DART" "${VM_OPTIONS[@]}" "$SNAPSHOT" "$@"
+fi
diff --git a/sdk/bin/pub_sdk.bat b/sdk/bin/pub_sdk.bat
index 3ea8f13..1c400bd 100644
--- a/sdk/bin/pub_sdk.bat
+++ b/sdk/bin/pub_sdk.bat
@@ -18,13 +18,21 @@
 IF %SDK_DIR:~-1%==\ set SDK_DIR=%SDK_DIR:~0,-1%
 
 set VM_OPTIONS=
+set USING_DART_2=
 
 rem We allow extra vm options to be passed in through an environment variable.
 if not "_%DART_VM_OPTIONS%_" == "__" (
   set VM_OPTIONS=%VM_OPTIONS% %DART_VM_OPTIONS%
+  for %%o in (%DART_VM_OPTIONS%) do (
+    if "%%o" equ "--preview-dart-2" set USING_DART_2=y
+  )
 )
 
-"%BIN_DIR%\dart" %VM_OPTIONS% "%BIN_DIR%\snapshots\pub.dart.snapshot" %*
+if defined USING_DART_2 (
+  "%BIN_DIR%\dart" %VM_OPTIONS% "%BIN_DIR%\snapshots\pub2.dart.snapshot" %*
+) else (
+  "%BIN_DIR%\dart" %VM_OPTIONS% "%BIN_DIR%\snapshots\pub.dart.snapshot" %*
+)
 
 endlocal
 
@@ -42,4 +50,4 @@
 endlocal & set %~2=%result%
 goto :eof
 
-:end
\ No newline at end of file
+:end
diff --git a/utils/pub/BUILD.gn b/utils/pub/BUILD.gn
index b2f724c..f34a69a 100644
--- a/utils/pub/BUILD.gn
+++ b/utils/pub/BUILD.gn
@@ -8,3 +8,11 @@
   main_dart = "../../third_party/pkg/pub/bin/pub.dart"
   training_args = [ "--help" ]
 }
+
+application_snapshot("pub2") {
+  main_dart = "../../third_party/pkg/pub/bin/pub.dart"
+  training_args = [ "--help" ]
+  vm_args = [
+    "--preview-dart-2",
+  ]
+}