Revert "Revert "Make dart_test generate a script to run the tests.""

The Flutter shell can now be successfully built on bots.

TO-341

This reverts commit 3b81183978b4e533aee3d2725fe3070b7fefc6cb.

Change-Id: If5dd2901e52613aa4b6e76fd8bf933796c1218a1
diff --git a/dart/dart_package.gni b/dart/dart_package.gni
index 45b913b..0bdc79b 100644
--- a/dart/dart_package.gni
+++ b/dart/dart_package.gni
@@ -159,8 +159,6 @@
         rebase_path(source_dir),
         "--dot-packages",
         rebase_path(dot_packages_file),
-        "--root-build-dir",
-        rebase_path(root_build_dir),
         "--dartanalyzer",
         rebase_path(dart_analyzer_binary),
         "--package-name",
diff --git a/dart/dart_test.gni b/dart/dart_test.gni
index 90f2b23..f8a2e05 100644
--- a/dart/dart_test.gni
+++ b/dart/dart_test.gni
@@ -33,9 +33,15 @@
 #   }
 template("dart_test") {
 
-  template_name = target_name
+  main_target_name = target_name
+  package_target_name = "${target_name}_package"
 
-  dart_package(template_name) {
+  sources_dir = "test"
+  if (defined(invoker.source_dir)) {
+    sources_dir = invoker.source_dir
+  }
+
+  dart_package(package_target_name) {
     forward_variables_from(invoker, [
       "analysis_options",
       "deps",
@@ -44,9 +50,45 @@
 
     infer_package_name = true
 
-    source_dir = "test"
-    if (defined(invoker.source_dir)) {
-      source_dir = invoker.source_dir
-    }
+    source_dir = sources_dir
+  }
+
+  dot_packages_file = "$target_gen_dir/$package_target_name.packages"
+
+  fuchsia_tester_label = "//lib/flutter/packages/flutter_tools:fuchsia_tester($host_toolchain)"
+  fuchsia_tester_out_dir = get_label_info(fuchsia_tester_label, "root_out_dir")
+  fuchsia_tester_bin = "$fuchsia_tester_out_dir/dart-tools/fuchsia_tester"
+
+  flutter_shell_label = "//flutter/shell($host_toolchain)"
+  flutter_shell_out_dir = get_label_info(flutter_shell_label, "root_out_dir")
+  flutter_shell_bin = "$flutter_shell_out_dir/flutter_tester"
+
+  invocation_file = "$target_gen_dir/$target_name.sh"
+
+  action(main_target_name) {
+    script = "//build/dart/gen_test_invocation.py"
+
+    outputs = [
+      invocation_file,
+    ]
+
+    args = [
+      "--out",
+      rebase_path(invocation_file),
+      "--source-dir",
+      rebase_path(sources_dir),
+      "--dot-packages",
+      rebase_path(dot_packages_file),
+      "--test-runner",
+      rebase_path(fuchsia_tester_bin),
+      "--flutter-shell",
+      rebase_path(flutter_shell_bin),
+    ]
+
+    deps = [
+      ":$package_target_name",
+      flutter_shell_label,
+      fuchsia_tester_label,
+    ]
   }
 }
diff --git a/dart/gen_analyzer_invocation.py b/dart/gen_analyzer_invocation.py
index deddd4f..6359dfa 100755
--- a/dart/gen_analyzer_invocation.py
+++ b/dart/gen_analyzer_invocation.py
@@ -18,8 +18,6 @@
                       required=True)
   parser.add_argument('--dot-packages', help='Path to the .packages file',
                       required=True)
-  parser.add_argument('--root-build-dir',
-                      help='Path to root of the build directory', required=True)
   parser.add_argument('--dartanalyzer',
                       help='Path to the Dart analyzer executable',
                       required=True)
@@ -30,7 +28,7 @@
   parser.add_argument('--options', help='Path to analysis options')
   args = parser.parse_args()
 
-  analyzer_file = os.path.join(args.root_build_dir, args.out)
+  analyzer_file = args.out
   analyzer_path = os.path.dirname(analyzer_file)
   if not os.path.exists(analyzer_path):
     os.makedirs(analyzer_path)
diff --git a/dart/gen_test_invocation.py b/dart/gen_test_invocation.py
new file mode 100755
index 0000000..5bd4063
--- /dev/null
+++ b/dart/gen_test_invocation.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# Copyright 2017 The Fuchsia 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 argparse
+import os
+import stat
+import string
+import sys
+
+
+def main():
+  parser = argparse.ArgumentParser(
+      'Generate a script that invokes the Dart analyzer')
+  parser.add_argument('--out',
+                      help='Path to the invocation file to generate',
+                      required=True)
+  parser.add_argument('--source-dir',
+                      help='Path to test sources',
+                      required=True)
+  parser.add_argument('--dot-packages',
+                      help='Path to the .packages file',
+                      required=True)
+  parser.add_argument('--test-runner',
+                      help='Path to the test runner',
+                      required=True)
+  parser.add_argument('--flutter-shell',
+                      help='Path to the Flutter shell',
+                      required=True)
+  args = parser.parse_args()
+
+  test_file = args.out
+  test_path = os.path.dirname(test_file)
+  if not os.path.exists(test_path):
+    os.makedirs(test_path)
+
+  script_template = string.Template('''#!/bin/sh
+
+$test_runner \\
+  --packages=$dot_packages \\
+  --shell=$flutter_shell \\
+  --test-directory=$source_dir
+''')
+  with open(test_file, 'w') as file:
+      file.write(script_template.substitute(args.__dict__))
+  permissions = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
+                 stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP |
+                 stat.S_IROTH)
+  os.chmod(test_file, permissions)
+
+
+if __name__ == '__main__':
+  sys.exit(main())