blob: 5e806d9ee27386cfb96ee9c2f4f27a775622f5ff [file] [log] [blame]
Simon Zünd2ce67542023-02-07 10:15:141#!/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
11import datetime
12import json
13import os
14import sys
15from os import path
16
17PYJSON5_DIR = path.join(os.path.dirname(__file__), '..', '..', 'third_party',
18 'pyjson5', 'src')
19sys.path.append(PYJSON5_DIR)
20
21import json5 # pylint: disable=import-error
22
23ROOT_DIRECTORY = path.join(path.dirname(__file__), '..', '..')
24GENERATED_LOCATION = path.join(ROOT_DIRECTORY, 'front_end', 'generated',
25 'Deprecation.ts')
26READ_LOCATION = path.join(ROOT_DIRECTORY, 'third_party', 'blink', 'renderer',
27 'core', 'frame', 'deprecation', 'deprecation.json5')
28
29
30def deprecations_from_file(file_name):
Ari Chivukula52c774a2023-02-10 18:14:0931 with open(file_name) as json5_file:
Simon Zünd2ce67542023-02-07 10:15:1432 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 Chivukula52c774a2023-02-10 18:14:0950 # The PRESUBMIT script in chromium prevents unicode characters,
51 # but there are still a few special cases we need to handle.
Simon Zünd2ce67542023-02-07 10:15:1452 ui_strings[name] = {
Ari Chivukula52c774a2023-02-10 18:14:0953 "message":
54 entry["message"].replace("\\", "\\\\").replace("'", "\\'"),
55 "note":
56 entry["translation_note"].replace("\n",
57 "\\n").replace("\r", "\\r")
Simon Zünd2ce67542023-02-07 10:15:1458 }
59
60 return meta, ui_strings
61
62
63meta, ui_strings = deprecations_from_file(READ_LOCATION)
64now = datetime.datetime.now()
Ari Chivukula52c774a2023-02-10 18:14:0965with open(GENERATED_LOCATION, mode="w+") as f:
Simon Zünd2ce67542023-02-07 10:15:1466 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ünd2ce67542023-02-07 10:15:1476 f.write("export const UIStrings = {\n")
77 for name, ui_string in ui_strings.items():
Ari Chivukula52c774a2023-02-10 18:14:0978 message = ui_string["message"]
79 note = ui_string["note"]
Simon Zünd2ce67542023-02-07 10:15:1480 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 Chivukula6b471922023-02-08 17:21:2792 "export const DEPRECATIONS_METADATA: Partial<Record<string, DeprecationDescriptor>> = %s;\n"
Simon Zünd2ce67542023-02-07 10:15:1493 % json.dumps(meta, sort_keys=True, indent=2))