Commit 3927e549 authored by Mandy Chen's avatar Mandy Chen Committed by Commit Bot

DevTools: Add scripts to generate id mapping for localizable strings

The backend changes[1] for localizing devtools leverage the existing
webui functionalities, which consume a list of id mappings from the id
of a string used in the frontend (i.e. the English string) to the IDS_
key of the string in the corresponding grdp file. Example:

const LocalizedString kLocalizedStrings[] = {
  ...
  {"Editable", IDS_DEVTOOLS_2c09e798bf8c5a82d211331c82893a0f},
  ...
}

This patch adds scripts that generate the structure. The output will be
two files, devtools_ui_strings_map.h and .cc, under
<generated files root directory>/chrome/browser/ui/webui
(e.g. D:/chromium/src/out/Release/gen/chrome/browser/ui/webui).

The design is to create a build step that calls generate_devtools_ui_strings.py,
which then calls generate_devtools_ui_strings.js. Integration into the build
system will come as a another patch, because it depends on the first set of
changes[2].

Example arguments to generate_devtools_ui_strings.js to generate output
files (invoke under '--root_gen_dir', e.g. D:/chromium/src/out/release/gen
in the example below):

['D:/chromium/src/third_party/node/win/node.exe',
'D:/chromium/src/third_party/blink/renderer/devtools/scripts/build/generate_devtools_ui_strings.js',
'--root_gen_dir', 'D:/chromium/src/out/release/gen',
'--output_header', 'chrome/browser/ui/webui/devtools_ui_strings_map.h',
'--output_cc', 'chrome/browser/ui/webui/devtools_ui_strings_map.cc']

[1]: https://crrev.com/c/1672628
[2]: https://crrev.com/c/1628927

Bug: 941561
Change-Id: Ic03dae24cd074219790c6eec5df3183c24bf15a0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1671848Reviewed-by: default avatarYang Guo <yangguo@chromium.org>
Commit-Queue: Mandy Chen <mandy.chen@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#699370}
parent fe11d8d5
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* This script is called by generate_devtools_ui_strings.py as part of the build process. It
* parses DevTools frontend .js and module.json files, collects localizable strings, checks
* if frontend strings are in .grd/.grdp files and reports error if present, and generates
* {jsonKey, IDS_KEY} mappings if there is no error.
*
* Usage:
* --root_gen_dir The root directory of the output .h and .cc files
* --output_header Absolute path of the output .h file for the id mappings
* --output_cc Absolute path of the output .cc file for the id mappings
*/
const checkLocalizedStrings = require('../localization_utils/check_localized_strings');
const localizationUtils = require('../localization_utils/localization_utils');
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const writeFileAsync = promisify(fs.writeFile);
class Arguments {
constructor(rootGenDir, outputHeaderFilePath, outputCCFilePath) {
this.rootGenDir = rootGenDir;
this.outputHeaderFilePath = outputHeaderFilePath;
this.outputCCFilePath = outputCCFilePath;
}
}
function parseArguments(args) {
const rootGenDirIndex = args.indexOf('--root_gen_dir');
const outputHeaderIndex = args.indexOf('--output_header');
const outputCCIndex = args.indexOf('--output_cc');
return new Arguments(args[rootGenDirIndex + 1], args[outputHeaderIndex + 1], args[outputCCIndex + 1]);
}
async function main() {
const args = parseArguments(process.argv);
let frontendStrings;
try {
[frontendStrings, _] = await checkLocalizedStrings.parseLocalizableResourceMaps();
} catch (e) {
console.log(e);
process.exit(1);
}
const toAddError = checkLocalizedStrings.getAndReportResourcesToAdd();
const toModifyError = checkLocalizedStrings.getAndReportIDSKeysToModify();
const toRemoveError = checkLocalizedStrings.getAndReportResourcesToRemove();
let error = `${toAddError ? `${toAddError}\n` : ''}${toModifyError ? `${toModifyError}\n` : ''}${
toRemoveError ? `${toRemoveError}\n` : ''}`;
if (error !== '') {
error +=
'\nThe errors are potentially fixable with `node third_party/blink/renderer/devtools/scripts/check_localizable_resources.js --autofix`'
console.log(error);
}
// Since it's part of the build system, only fail if there are strings to be added to GRD/GRDP files
// or if there are wrong IDS_ keys.
if (toAddError || toModifyError)
process.exit(1);
try {
await generateDevToolsLocalizedStrings(args, frontendStrings);
} catch (e) {
console.log('Error generating id map files:');
console.log(e.stack);
process.exit(1);
}
}
// Generates {jsonKey, IDS_KEY} mappings according to frontendStrings
async function generateDevToolsLocalizedStrings(args, frontendStrings) {
const promises = [];
const outputAbsoluteHeaderFilePath = path.join(args.rootGenDir, args.outputHeaderFilePath);
const outputAbsoluteCCFilePath = path.join(args.rootGenDir, args.outputCCFilePath);
const doNotEditStr =
`// This file is automatically generated by //third_party/blink/rendered/devtools/build/generate_devtools_ui_strings.js. Do not edit.`;
const outputHeaderFileContent = `${doNotEditStr}
#ifndef CHROME_BROWSER_UI_WEBUI_DEVTOOLS_UI_STRINGS_H_
#define CHROME_BROWSER_UI_WEBUI_DEVTOOLS_UI_STRINGS_H_
#include "chrome/browser/ui/webui/localized_string.h"
namespace devtools {
constexpr unsigned int kLocalizedStringsSize = ${frontendStrings.size};
extern const LocalizedString kLocalizedStrings[kLocalizedStringsSize];
} // namespace devtools
#endif // CHROME_BROWSER_UI_WEBUI_DEVTOOLS_UI_STRINGS_H_
`;
promises.push(writeFileAsync(outputAbsoluteHeaderFilePath, outputHeaderFileContent));
let mappingsStr = '';
frontendStrings.forEach((frontendString, idsKey) => {
mappingsStr += ` {"${localizationUtils.sanitizeStringIntoCppFormat(frontendString.string)}", ${idsKey}},\n`;
});
const outputCCFileContent = `${doNotEditStr}
#include "${args.outputHeaderFilePath}"
#include "third_party/blink/renderer/devtools/front_end/langpacks/devtools_ui_strings.h"
namespace devtools {
const LocalizedString kLocalizedStrings[] = {
${mappingsStr}};
} // namespace devtools
`;
promises.push(writeFileAsync(outputAbsoluteCCFilePath, outputCCFileContent));
return Promise.all(promises);
}
main();
# Copyright 2019 The Chromium 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 optparse
import os
import subprocess
import sys
_HERE_PATH = os.path.abspath(os.path.dirname(__file__))
_JS_SCRIPT_PATH = os.path.join(_HERE_PATH, 'generate_devtools_ui_strings.js')
_SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..', '..', '..', '..'))
sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node'))
# pylint: disable=wrong-import-position
import node # pylint: disable=import-error
_NODE_PATH = node.GetBinaryPath()
def main():
parser = optparse.OptionParser(description=__doc__)
parser.add_option(
'--root_gen_dir',
action='store',
metavar='ROOT_GEN_DIR',
help='The root directory where the header and cc will be generated.')
parser.add_option('--output_header', action='store', metavar='OUTPUT_HEADER', help='Generated output .h file for pairs of IDs')
parser.add_option(
'--output_cc',
action='store',
metavar='OUTPUT_CC',
help='Generated output .cc file that contains pairs of {front-end string key, IDS_ key}')
options, _ = parser.parse_args()
if not options.root_gen_dir:
parser.error('--root_gen_dir was not specified.')
if not options.output_header:
parser.error('--output_header was not specified.')
if not options.output_header:
parser.error('--output_cc was not specified.')
cmd_parts = [
_NODE_PATH, _JS_SCRIPT_PATH, '--root_gen_dir',
os.path.abspath(options.root_gen_dir), '--output_header', options.output_header, '--output_cc', options.output_cc
]
process = subprocess.Popen(cmd_parts, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, _ = process.communicate()
if process.returncode != 0:
return out
if __name__ == '__main__':
sys.exit(main())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment