Use a custom skylark rule to generate io tests (#139)

The current approach depends on internal details of dart_codegen that
won't be supported going forward. Since this is a very simple string
replacement use the skylark template_action instead of a Dart script.

- Add _generate_io_tests.bzl with a single rule that can do the test
  rewriting
- Drop transform_tests.dart and the related dart_vm_binary
- Use the new _generate_io_tests rather than dart_codegen
diff --git a/BUILD b/BUILD
index 007c81e..7b04a60 100644
--- a/BUILD
+++ b/BUILD
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 load("@io_bazel_rules_dart//dart/build_rules:core.bzl", "dart_library")
-load("@io_bazel_rules_dart//dart/build_rules:vm.bzl", "dart_vm_binary", "dart_vm_test")
+load("@io_bazel_rules_dart//dart/build_rules:vm.bzl", "dart_vm_test")
 
 licenses(["notice"])  # Apache (Google-authored with external contributions)
 
@@ -35,12 +35,5 @@
     ],
 )
 
-dart_vm_binary(
-    name = "transform_tests",
-    srcs = ["bin/transform_tests.dart"],
-    script_file = "bin/transform_tests.dart",
-    visibility = ["//test:__subpackages__"],
-)
-
 # Test BUILD rules are defined test/BUILD instead of here to prevent cyclic
 # dependency between this and rules_webtesting.
diff --git a/_generate_io_tests.bzl b/_generate_io_tests.bzl
new file mode 100644
index 0000000..90745d9
--- /dev/null
+++ b/_generate_io_tests.bzl
@@ -0,0 +1,39 @@
+IN_EXTENSION = ".dart"
+OUT_EXTENSION = ".io.dart"
+
+def _change_extension(path):
+  return "%s%s" % (path[:-1 * len(IN_EXTENSION)], OUT_EXTENSION)
+
+def _compute_outs(srcs):
+  outs = {}
+  for label in srcs:
+    if label.name.endswith(IN_EXTENSION):
+      out_name = _change_extension(label.name)
+      outs[out_name] = out_name
+  return outs
+
+def _generate_io_tests_impl(ctx):
+  """Rewrites tests to replace dart_util imports with io_dart_util"""
+  outs = []
+  for src in ctx.files.srcs:
+    path = src.basename
+    if path.endswith(IN_EXTENSION):
+      out_name = _change_extension(path)
+      out_file = ctx.new_file(src, out_name)
+      outs.append(out_file)
+      ctx.template_action(
+          output = out_file,
+          template = src,
+          substitutions = {
+              "test_util.dart": "io_test_util.dart",
+          }
+      )
+  return struct(files=set(outs))
+
+generate_io_tests = rule(
+    attrs = {
+        "srcs": attr.label_list(allow_files=True),
+    },
+    outputs = _compute_outs,
+    implementation = _generate_io_tests_impl,
+)
diff --git a/bin/transform_tests.dart b/bin/transform_tests.dart
deleted file mode 100644
index f624129..0000000
--- a/bin/transform_tests.dart
+++ /dev/null
@@ -1,65 +0,0 @@
-// Copyright 2017 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-library webdriver.transform_tests;
-
-import 'dart:io';
-
-void main(List<String> args) {
-  String inExtension;
-  String outExtension;
-  String outDirectory;
-  String srcsFile;
-
-  // Stupid simple arg parsing.
-  for (var arg in args.takeWhile((arg) => arg != '--')) {
-    if (arg.startsWith('--in-extension=')) {
-      inExtension = arg.split('=')[1];
-    } else if (arg.startsWith('--out-extension=')) {
-      outExtension = arg.split('=')[1];
-    } else if (arg.startsWith('--out=')) {
-      outDirectory = arg.split('=')[1];
-    } else if (arg.startsWith('--srcs-file=')) {
-      srcsFile = arg.split('=')[1];
-    }
-  }
-
-  print('Parsed  --in-extension $inExtension');
-  print('Parsed --out-extension $outExtension');
-  print('Parsed           --out $outDirectory');
-  print('Parsed     --srcs-file $srcsFile');
-
-  String testUtilImport;
-  for (var arg in args.skipWhile((arg) => arg != '--')) {
-    if (arg.startsWith('--test_util_import=')) {
-      testUtilImport = arg.split('=')[1];
-    }
-  }
-
-  print('Parsed        --test_util_import $testUtilImport');
-
-  var srcsList = new File(srcsFile).readAsLinesSync();
-  var year = new DateTime.now().year;
-  for (var srcFile in srcsList) {
-    if (!srcFile.endsWith(inExtension)) {
-      continue;
-    }
-    var outFile = '$outDirectory/$srcFile'
-        .replaceFirst(new RegExp('$inExtension\$'), outExtension);
-
-    var srcContents = new File(srcFile).readAsStringSync();
-    srcContents = srcContents.replaceFirst('test_util.dart', '$testUtilImport');
-    new File(outFile).writeAsString(srcContents);
-  }
-}
diff --git a/test/BUILD b/test/BUILD
index 38d35bb..e00d3c2 100644
--- a/test/BUILD
+++ b/test/BUILD
@@ -13,8 +13,8 @@
 # limitations under the License.
 
 load("@io_bazel_rules_dart//dart/build_rules:vm.bzl", "dart_vm_test")
-load("@io_bazel_rules_dart//dart/build_rules:code_gen.bzl", "dart_code_gen")
 load("@io_bazel_rules_webtesting//web:dart.bzl", "dart_web_test_suite")
+load("//:_generate_io_tests.bzl", "generate_io_tests")
 
 licenses(["notice"])  # Apache (Google-authored with external contributions)
 
@@ -22,12 +22,9 @@
 
 _TESTS_TO_RUN = glob(["*_test.dart"])
 
-dart_code_gen(
+generate_io_tests(
     name = "generate_io_tests",
     srcs = _TESTS_TO_RUN,
-    generator = "//:transform_tests",
-    generator_args = ["--test_util_import=io_test_util.dart"],
-    out_extensions = [".io.dart"],
 )
 
 [