Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
| 2 | # |
| 3 | # Copyright 2023 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # This file generates a TypeScript-ified version of blink's "deprecation.json5". |
| 8 | # We do this in order to get tyep-saftey of the JSON's content vs the |
| 9 | # deprecations defined in CDP. |
| 10 | |
| 11 | import datetime |
| 12 | import json |
| 13 | import os |
| 14 | import sys |
| 15 | from os import path |
| 16 | |
| 17 | PYJSON5_DIR = path.join(os.path.dirname(__file__), '..', '..', 'third_party', |
| 18 | 'pyjson5', 'src') |
| 19 | sys.path.append(PYJSON5_DIR) |
| 20 | |
| 21 | import json5 # pylint: disable=import-error |
| 22 | |
| 23 | ROOT_DIRECTORY = path.join(path.dirname(__file__), '..', '..') |
| 24 | GENERATED_LOCATION = path.join(ROOT_DIRECTORY, 'front_end', 'generated', |
| 25 | 'Deprecation.ts') |
| 26 | READ_LOCATION = path.join(ROOT_DIRECTORY, 'third_party', 'blink', 'renderer', |
| 27 | 'core', 'frame', 'deprecation', 'deprecation.json5') |
| 28 | |
| 29 | |
| 30 | def deprecations_from_file(file_name): |
Ari Chivukula | 52c774a | 2023-02-10 18:14:09 | [diff] [blame] | 31 | with open(file_name) as json5_file: |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 32 | doc = json5.loads(json5_file.read()) |
| 33 | |
| 34 | # We turn the list of deprecations into two maps, both keyed by the deprecation name. |
| 35 | # One contains the message + translation note. |
| 36 | # The other contains the metadata such as milestone and chrome feature. |
| 37 | meta = {} |
| 38 | ui_strings = {} |
| 39 | for entry in doc["data"]: |
| 40 | name = entry["name"] |
| 41 | |
| 42 | meta_for_entry = {} |
| 43 | if "milestone" in entry: |
| 44 | meta_for_entry["milestone"] = entry["milestone"] |
| 45 | if "chrome_status_feature" in entry: |
| 46 | meta_for_entry["chromeStatusFeature"] = entry[ |
| 47 | "chrome_status_feature"] |
| 48 | if len(meta_for_entry): meta[name] = meta_for_entry |
| 49 | |
Ari Chivukula | 52c774a | 2023-02-10 18:14:09 | [diff] [blame] | 50 | # The PRESUBMIT script in chromium prevents unicode characters, |
| 51 | # but there are still a few special cases we need to handle. |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 52 | ui_strings[name] = { |
Ari Chivukula | 52c774a | 2023-02-10 18:14:09 | [diff] [blame] | 53 | "message": |
| 54 | entry["message"].replace("\\", "\\\\").replace("'", "\\'"), |
| 55 | "note": |
| 56 | entry["translation_note"].replace("\n", |
| 57 | "\\n").replace("\r", "\\r") |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return meta, ui_strings |
| 61 | |
| 62 | |
| 63 | meta, ui_strings = deprecations_from_file(READ_LOCATION) |
| 64 | now = datetime.datetime.now() |
Ari Chivukula | 52c774a | 2023-02-10 18:14:09 | [diff] [blame] | 65 | with open(GENERATED_LOCATION, mode="w+") as f: |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 66 | f.write("// Copyright %d The Chromium Authors. All rights reserved.\n" % |
| 67 | now.year) |
| 68 | f.write( |
| 69 | "// Use of this source code is governed by a BSD-style license that can be\n" |
| 70 | ) |
| 71 | f.write("// found in the LICENSE file.\n") |
| 72 | f.write("\n") |
| 73 | f.write("// This file is auto-generated, do not edit manually.\n") |
| 74 | f.write("// Re-generate with: npm run generate-protocol-resources\n") |
| 75 | f.write("\n") |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 76 | f.write("export const UIStrings = {\n") |
| 77 | for name, ui_string in ui_strings.items(): |
Ari Chivukula | 52c774a | 2023-02-10 18:14:09 | [diff] [blame] | 78 | message = ui_string["message"] |
| 79 | note = ui_string["note"] |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 80 | f.write(" /**\n") |
| 81 | f.write(" * @description %s\n" % note) |
| 82 | f.write(" */\n") |
| 83 | f.write(" %s: '%s',\n" % (name, message)) |
| 84 | f.write("};\n") |
| 85 | f.write("\n") |
| 86 | f.write("export interface DeprecationDescriptor {\n") |
| 87 | f.write(" milestone?: number;\n") |
| 88 | f.write(" chromeStatusFeature?: number;\n") |
| 89 | f.write("}\n") |
| 90 | f.write("\n") |
| 91 | f.write( |
Ari Chivukula | 6b47192 | 2023-02-08 17:21:27 | [diff] [blame] | 92 | "export const DEPRECATIONS_METADATA: Partial<Record<string, DeprecationDescriptor>> = %s;\n" |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 93 | % json.dumps(meta, sort_keys=True, indent=2)) |