[sdk/lib] Better presubmit check for libraries.{json,yaml}

Logical follow-up to https://dart-review.googlesource.com/c/sdk/+/226681

Change-Id: Ie35edd7931b3d19d8ef4e8134179ab7e58c7609c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/226683
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
diff --git a/pkg/front_end/test/spell_checking_list_tests.txt b/pkg/front_end/test/spell_checking_list_tests.txt
index d9f5fdf..97ef414 100644
--- a/pkg/front_end/test/spell_checking_list_tests.txt
+++ b/pkg/front_end/test/spell_checking_list_tests.txt
@@ -1060,7 +1060,6 @@
 xxxxxxxxxxxx
 xyz
 y's
-yaml2json
 year
 yxxx
 yy
diff --git a/pkg/front_end/tool/smoke_test_quick.dart b/pkg/front_end/tool/smoke_test_quick.dart
index 2b7a200..12b77c3 100644
--- a/pkg/front_end/tool/smoke_test_quick.dart
+++ b/pkg/front_end/tool/smoke_test_quick.dart
@@ -29,9 +29,6 @@
   futures.add(run(
       "pkg/front_end/test/generated_files_up_to_date_git_test.dart", [],
       filter: false));
-  futures.add(run("tools/yaml2json.dart",
-      ["sdk/lib/libraries.yaml", "sdk/lib/libraries.json", '--check'],
-      filter: false));
   await Future.wait(futures);
   print("\n-----------------------\n");
   print("Done with exitcode $exitCode in ${stopwatch.elapsedMilliseconds} ms");
diff --git a/sdk/lib/PRESUBMIT.py b/sdk/lib/PRESUBMIT.py
new file mode 100644
index 0000000..21b779b
--- /dev/null
+++ b/sdk/lib/PRESUBMIT.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+# Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+"""sdk/lib specific presubmit script.
+
+See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
+for more details about the presubmit API built into gcl.
+"""
+
+import imp
+import os.path
+import subprocess
+
+USE_PYTHON3 = True
+
+
+def runSmokeTest(input_api, output_api):
+    hasChangedFiles = False
+    for git_file in input_api.AffectedTextFiles():
+        filename = git_file.AbsoluteLocalPath()
+        if filename.endswith('libraries.yaml') or filename.endswith(
+                'libraries.json'):
+            hasChangedFiles = True
+            break
+
+    if hasChangedFiles:
+        local_root = input_api.change.RepositoryRoot()
+        utils = imp.load_source('utils',
+                                os.path.join(local_root, 'tools', 'utils.py'))
+        dart = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart')
+        yaml2json = os.path.join(local_root, 'tools', 'yaml2json.dart')
+        libYaml = os.path.join(local_root, 'sdk', 'lib', 'libraries.yaml')
+        libJson = os.path.join(local_root, 'sdk', 'lib', 'libraries.json')
+
+        windows = utils.GuessOS() == 'win32'
+        if windows:
+            dart += '.exe'
+
+        if not os.path.isfile(dart):
+            print('WARNING: dart not found: %s' % dart)
+            return []
+
+        if not os.path.isfile(yaml2json):
+            print('WARNING: yaml2json not found: %s' % yaml2json)
+            return []
+
+        args = [
+            dart, yaml2json, libYaml, libJson, '--check',
+            '--relative=' + local_root + '/'
+        ]
+        process = subprocess.Popen(args,
+                                   stdout=subprocess.PIPE,
+                                   stdin=subprocess.PIPE)
+        outs, _ = process.communicate()
+
+        if process.returncode != 0:
+            return [
+                output_api.PresubmitError('lib/sdk smoketest failure(s)',
+                                          long_text=outs)
+            ]
+
+    return []
+
+
+def CheckChangeOnCommit(input_api, output_api):
+    return runSmokeTest(input_api, output_api)
+
+
+def CheckChangeOnUpload(input_api, output_api):
+    return runSmokeTest(input_api, output_api)
diff --git a/tools/yaml2json.dart b/tools/yaml2json.dart
index abf313d..50eba45 100644
--- a/tools/yaml2json.dart
+++ b/tools/yaml2json.dart
@@ -10,12 +10,19 @@
 
 import 'package:yaml/yaml.dart' show loadYaml;
 
-main(List<String> arguments) {
+main(List<String> rawArguments) {
   var port = new RawReceivePort();
   bool check = false;
-  if (arguments.contains('--check')) {
-    arguments = arguments.toList()..remove('--check');
-    check = true;
+  String? relative;
+  List<String> arguments = [];
+  for (String argument in rawArguments) {
+    if (argument == '--check') {
+      check = true;
+    } else if (argument.startsWith('--relative=')) {
+      relative = argument.substring('--relative='.length);
+    } else {
+      arguments.add(argument);
+    }
   }
   if (arguments.length != 2) {
     stderr.writeln("Usage: yaml2json.dart input.yaml output.json [--check]");
@@ -23,11 +30,22 @@
   }
   Uri input = Uri.base.resolve(arguments[0]);
   Uri output = Uri.base.resolve(arguments[1]);
+  String inputString = arguments[0];
+  String outputString = arguments[1];
+  if (relative != null) {
+    String relativeTo = Uri.base.resolve(relative).toString();
+    if (input.toString().startsWith(relativeTo)) {
+      inputString = input.toString().substring(relativeTo.length);
+    }
+    if (output.toString().startsWith(relativeTo)) {
+      outputString = output.toString().substring(relativeTo.length);
+    }
+  }
   Map yaml = loadYaml(new File.fromUri(input).readAsStringSync());
   Map<String, dynamic> result = new Map<String, dynamic>();
   result["comment:0"] = "NOTE: THIS FILE IS GENERATED. DO NOT EDIT.";
   result["comment:1"] =
-      "Instead modify '${arguments[0]}' and follow the instructions therein.";
+      "Instead modify '$inputString' and follow the instructions therein.";
   for (String key in yaml.keys) {
     result[key] = yaml[key];
   }
@@ -41,9 +59,9 @@
     }
     if (needsUpdate) {
       stderr.write('''
-The file ${arguments[1]} is not up to date. Regenerate using
+The file $outputString is not up to date. Regenerate using
 
-  dart tools/yaml2json.dart ${arguments[0]} ${arguments[1]}
+  dart tools/yaml2json.dart $inputString $outputString
 ''');
       exit(1);
     }