Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | const fs = require('fs'); |
| 6 | const md5 = require('./md5'); |
| 7 | const {promisify} = require('util'); |
| 8 | const path = require('path'); |
| 9 | const readFileAsync = promisify(fs.readFile); |
| 10 | const readDirAsync = promisify(fs.readdir); |
| 11 | const statAsync = promisify(fs.stat); |
Mandy Chen | 5128cc6 | 2019-09-23 16:46:00 +0000 | [diff] [blame] | 12 | const writeFileAsync = promisify(fs.writeFile); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 13 | |
| 14 | const esprimaTypes = { |
| 15 | BI_EXPR: 'BinaryExpression', |
| 16 | CALL_EXPR: 'CallExpression', |
| 17 | COND_EXPR: 'ConditionalExpression', |
| 18 | IDENTIFIER: 'Identifier', |
| 19 | LITERAL: 'Literal', |
| 20 | MEMBER_EXPR: 'MemberExpression', |
Mandy Chen | 7a8829b | 2019-06-25 22:13:07 +0000 | [diff] [blame] | 21 | NEW_EXPR: 'NewExpression', |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 22 | TAGGED_TEMP_EXPR: 'TaggedTemplateExpression', |
| 23 | TEMP_LITERAL: 'TemplateLiteral' |
| 24 | }; |
| 25 | |
Paul Irish | e7b977e | 2019-09-25 12:23:38 +0000 | [diff] [blame] | 26 | const excludeFiles = ['Tests.js']; |
| 27 | const excludeDirs = ['test_runner', 'Images', 'langpacks', 'node_modules', 'lighthouse']; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 28 | const cppSpecialCharactersMap = { |
| 29 | '"': '\\"', |
| 30 | '\\': '\\\\', |
| 31 | '\n': '\\n' |
| 32 | }; |
| 33 | const IDSPrefix = 'IDS_DEVTOOLS_'; |
| 34 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 35 | const SRC_PATH = path.resolve(__dirname, '..', '..'); |
| 36 | const GRD_PATH = path.resolve(SRC_PATH, 'front_end', 'langpacks', 'devtools_ui_strings.grd'); |
Mandy Chen | 1e9d87b | 2019-09-18 17:18:15 +0000 | [diff] [blame] | 37 | const SHARED_STRINGS_PATH = path.resolve(__dirname, '..', '..', 'front_end', 'langpacks', 'shared_strings.grdp'); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 38 | const NODE_MODULES_PATH = path.resolve(SRC_PATH, 'node_modules'); |
| 39 | const escodegen = require(path.resolve(NODE_MODULES_PATH, 'escodegen')); |
| 40 | const esprima = require(path.resolve(NODE_MODULES_PATH, 'esprima')); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 41 | |
| 42 | function getRelativeFilePathFromSrc(filePath) { |
| 43 | return path.relative(SRC_PATH, filePath); |
| 44 | } |
| 45 | |
| 46 | function shouldParseDirectory(directoryName) { |
| 47 | return !excludeDirs.some(dir => directoryName.includes(dir)); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @filepath can be partial path or full path, as long as it contains the file name. |
| 52 | */ |
| 53 | function shouldParseFile(filepath) { |
| 54 | return !excludeFiles.includes(path.basename(filepath)); |
| 55 | } |
| 56 | |
| 57 | async function parseFileContent(filePath) { |
Mandy Chen | 167e7ad | 2019-11-01 15:50:37 -0700 | [diff] [blame^] | 58 | let fileContent = await readFileAsync(filePath); |
| 59 | fileContent = fileContent.toString(); |
| 60 | // normalize line ending to LF |
| 61 | fileContent = fileContent.replace(/\r\n/g, '\n'); |
| 62 | return fileContent; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | function isNodeCallOnObject(node, objectName, propertyName) { |
| 66 | return node !== undefined && node.type === esprimaTypes.CALL_EXPR && |
| 67 | verifyCallExpressionCallee(node.callee, objectName, propertyName); |
| 68 | } |
| 69 | |
| 70 | function isNodeCommonUIStringCall(node) { |
| 71 | return isNodeCallOnObject(node, 'Common', 'UIString'); |
| 72 | } |
| 73 | |
Mandy Chen | 7a8829b | 2019-06-25 22:13:07 +0000 | [diff] [blame] | 74 | function isNodeCommonUIStringFormat(node) { |
| 75 | return node && node.type === esprimaTypes.NEW_EXPR && |
| 76 | verifyCallExpressionCallee(node.callee, 'Common', 'UIStringFormat'); |
| 77 | } |
| 78 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 79 | function isNodeUIformatLocalized(node) { |
| 80 | return isNodeCallOnObject(node, 'UI', 'formatLocalized'); |
| 81 | } |
| 82 | |
| 83 | function isNodelsTaggedTemplateExpression(node) { |
| 84 | return node !== undefined && node.type === esprimaTypes.TAGGED_TEMP_EXPR && verifyIdentifier(node.tag, 'ls') && |
| 85 | node.quasi !== undefined && node.quasi.type !== undefined && node.quasi.type === esprimaTypes.TEMP_LITERAL; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Verify callee of objectName.propertyName(), e.g. Common.UIString(). |
| 90 | */ |
| 91 | function verifyCallExpressionCallee(callee, objectName, propertyName) { |
| 92 | return callee !== undefined && callee.type === esprimaTypes.MEMBER_EXPR && callee.computed === false && |
| 93 | verifyIdentifier(callee.object, objectName) && verifyIdentifier(callee.property, propertyName); |
| 94 | } |
| 95 | |
| 96 | function verifyIdentifier(node, name) { |
| 97 | return node !== undefined && node.type === esprimaTypes.IDENTIFIER && node.name === name; |
| 98 | } |
| 99 | |
| 100 | function getLocalizationCase(node) { |
| 101 | if (isNodeCommonUIStringCall(node)) |
| 102 | return 'Common.UIString'; |
Mandy Chen | 7a8829b | 2019-06-25 22:13:07 +0000 | [diff] [blame] | 103 | else if (isNodeCommonUIStringFormat(node)) |
| 104 | return 'Common.UIStringFormat'; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 105 | else if (isNodelsTaggedTemplateExpression(node)) |
| 106 | return 'Tagged Template'; |
| 107 | else if (isNodeUIformatLocalized(node)) |
| 108 | return 'UI.formatLocalized'; |
| 109 | else |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | function isLocalizationCall(node) { |
| 114 | return isNodeCommonUIStringCall(node) || isNodelsTaggedTemplateExpression(node) || isNodeUIformatLocalized(node); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Verify if callee is functionName() or object.functionName(). |
| 119 | */ |
| 120 | function verifyFunctionCallee(callee, functionName) { |
| 121 | return callee !== undefined && |
| 122 | ((callee.type === esprimaTypes.IDENTIFIER && callee.name === functionName) || |
| 123 | (callee.type === esprimaTypes.MEMBER_EXPR && verifyIdentifier(callee.property, functionName))); |
| 124 | } |
| 125 | |
| 126 | function getLocationMessage(location) { |
| 127 | if (location !== undefined && location.start !== undefined && location.end !== undefined && |
| 128 | location.start.line !== undefined && location.end.line !== undefined) { |
| 129 | const startLine = location.start.line; |
| 130 | const endLine = location.end.line; |
| 131 | if (startLine === endLine) |
| 132 | return ` Line ${startLine}`; |
| 133 | else |
| 134 | return ` Line ${location.start.line}-${location.end.line}`; |
| 135 | } |
| 136 | return ''; |
| 137 | } |
| 138 | |
| 139 | function sanitizeStringIntoGRDFormat(str) { |
| 140 | return str.replace(/&/g, '&') |
| 141 | .replace(/</g, '<') |
| 142 | .replace(/>/g, '>') |
| 143 | .replace(/"/g, '"') |
| 144 | .replace(/'/g, ''') |
| 145 | } |
| 146 | |
| 147 | function sanitizeStringIntoFrontendFormat(str) { |
| 148 | return str.replace(/'/g, '\'') |
| 149 | .replace(/"/g, '"') |
| 150 | .replace(/>/g, '>') |
| 151 | .replace(/</g, '<') |
| 152 | .replace(/&/g, '&'); |
| 153 | } |
| 154 | |
| 155 | function sanitizeString(str, specialCharactersMap) { |
| 156 | let sanitizedStr = ''; |
| 157 | for (let i = 0; i < str.length; i++) { |
| 158 | let currChar = str.charAt(i); |
| 159 | if (specialCharactersMap[currChar] !== undefined) |
| 160 | currChar = specialCharactersMap[currChar]; |
| 161 | |
| 162 | sanitizedStr += currChar; |
| 163 | } |
| 164 | return sanitizedStr; |
| 165 | } |
| 166 | |
| 167 | function sanitizeStringIntoCppFormat(str) { |
| 168 | return sanitizeString(str, cppSpecialCharactersMap); |
| 169 | } |
| 170 | |
| 171 | async function getFilesFromItem(itemPath, filePaths, acceptedFileEndings) { |
| 172 | const stat = await statAsync(itemPath); |
| 173 | if (stat.isDirectory() && shouldParseDirectory(itemPath)) |
| 174 | return await getFilesFromDirectory(itemPath, filePaths, acceptedFileEndings); |
| 175 | |
| 176 | const hasAcceptedEnding = |
| 177 | acceptedFileEndings.some(acceptedEnding => itemPath.toLowerCase().endsWith(acceptedEnding.toLowerCase())); |
| 178 | if (hasAcceptedEnding && shouldParseFile(itemPath)) |
| 179 | filePaths.push(itemPath); |
| 180 | } |
| 181 | |
| 182 | async function getFilesFromDirectory(directoryPath, filePaths, acceptedFileEndings) { |
| 183 | const itemNames = await readDirAsync(directoryPath); |
| 184 | const promises = []; |
| 185 | for (const itemName of itemNames) { |
| 186 | const itemPath = path.resolve(directoryPath, itemName); |
| 187 | promises.push(getFilesFromItem(itemPath, filePaths, acceptedFileEndings)); |
| 188 | } |
| 189 | return Promise.all(promises); |
| 190 | } |
| 191 | |
| 192 | async function getChildDirectoriesFromDirectory(directoryPath) { |
| 193 | const dirPaths = []; |
| 194 | const itemNames = await readDirAsync(directoryPath); |
| 195 | for (const itemName of itemNames) { |
| 196 | const itemPath = path.resolve(directoryPath, itemName); |
| 197 | const stat = await statAsync(itemPath); |
| 198 | if (stat.isDirectory() && shouldParseDirectory(itemName)) |
| 199 | dirPaths.push(itemPath); |
| 200 | } |
| 201 | return dirPaths; |
| 202 | } |
| 203 | |
Mandy Chen | 7855263 | 2019-06-12 00:55:43 +0000 | [diff] [blame] | 204 | /** |
| 205 | * Pad leading / trailing whitespace with ''' so that the whitespace is preserved. See |
| 206 | * https://www.chromium.org/developers/tools-we-use-in-chromium/grit/grit-users-guide. |
| 207 | */ |
| 208 | function padWhitespace(str) { |
| 209 | if (str.match(/^\s+/)) |
| 210 | str = `'''${str}`; |
| 211 | if (str.match(/\s+$/)) |
| 212 | str = `${str}'''`; |
| 213 | return str; |
| 214 | } |
| 215 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 216 | function modifyStringIntoGRDFormat(str, args) { |
| 217 | let sanitizedStr = sanitizeStringIntoGRDFormat(str); |
Mandy Chen | 7855263 | 2019-06-12 00:55:43 +0000 | [diff] [blame] | 218 | sanitizedStr = padWhitespace(sanitizedStr); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 219 | |
| 220 | const phRegex = /%d|%f|%s|%.[0-9]f/gm; |
| 221 | if (!str.match(phRegex)) |
| 222 | return sanitizedStr; |
| 223 | |
| 224 | let phNames; |
| 225 | if (args !== undefined) |
| 226 | phNames = args.map(arg => arg.replace(/[^a-zA-Z]/gm, '_').toUpperCase()); |
| 227 | else |
| 228 | phNames = ['PH1', 'PH2', 'PH3', 'PH4', 'PH5', 'PH6', 'PH7', 'PH8', 'PH9']; |
| 229 | |
| 230 | // It replaces all placeholders with <ph> tags. |
| 231 | let match; |
| 232 | let count = 1; |
| 233 | while ((match = phRegex.exec(sanitizedStr)) !== null) { |
| 234 | // This is necessary to avoid infinite loops with zero-width matches |
| 235 | if (match.index === phRegex.lastIndex) |
| 236 | phRegex.lastIndex++; |
| 237 | |
| 238 | // match[0]: the placeholder (e.g. %d, %s, %.2f, etc.) |
| 239 | const ph = match[0]; |
| 240 | // e.g. $1s, $1d, $1.2f |
| 241 | const newPh = `$${count}` + ph.substr(1); |
| 242 | |
| 243 | const i = sanitizedStr.indexOf(ph); |
| 244 | sanitizedStr = `${sanitizedStr.substring(0, i)}<ph name="${phNames[count - 1]}">${newPh}</ph>${ |
| 245 | sanitizedStr.substring(i + ph.length)}`; |
| 246 | count++; |
| 247 | } |
| 248 | return sanitizedStr; |
| 249 | } |
| 250 | |
| 251 | function createGrdpMessage(ids, stringObj) { |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 252 | let message = ` <message name="${ids}" desc="${stringObj.description || ''}">\n`; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 253 | message += ` ${modifyStringIntoGRDFormat(stringObj.string, stringObj.arguments)}\n`; |
| 254 | message += ' </message>\n'; |
| 255 | return message; |
| 256 | } |
| 257 | |
| 258 | function getIDSKey(str) { |
Mandy Chen | 5128cc6 | 2019-09-23 16:46:00 +0000 | [diff] [blame] | 259 | return `${IDSPrefix}${md5(str)}`; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Mandy Chen | d97200b | 2019-07-29 21:13:39 +0000 | [diff] [blame] | 262 | // Get line number in the file of a character at given index |
| 263 | function lineNumberOfIndex(str, index) { |
| 264 | const stringToIndex = str.substr(0, index); |
| 265 | return stringToIndex.split('\n').length; |
| 266 | } |
| 267 | |
Mandy Chen | 5128cc6 | 2019-09-23 16:46:00 +0000 | [diff] [blame] | 268 | // Relative file path from grdp file with back slash replaced with forward slash |
| 269 | function getRelativeGrdpPath(grdpPath) { |
| 270 | return path.relative(path.dirname(GRD_PATH), grdpPath).split(path.sep).join('/'); |
| 271 | } |
| 272 | |
| 273 | function getAbsoluteGrdpPath(relativeGrdpFilePath) { |
| 274 | return path.resolve(path.dirname(GRD_PATH), relativeGrdpFilePath); |
| 275 | } |
| 276 | |
| 277 | // Create a <part> entry, given absolute path of a grdp file |
| 278 | function createPartFileEntry(grdpFilePath) { |
| 279 | const relativeGrdpFilePath = getRelativeGrdpPath(grdpFilePath); |
| 280 | return ` <part file="${relativeGrdpFilePath}" />\n`; |
| 281 | } |
| 282 | |
| 283 | // grdpFilePaths are sorted and are absolute file paths |
| 284 | async function addChildGRDPFilePathsToGRD(grdpFilePaths) { |
| 285 | const grdFileContent = await parseFileContent(GRD_PATH); |
| 286 | const grdLines = grdFileContent.split('\n'); |
| 287 | |
| 288 | let newGrdFileContent = ''; |
| 289 | for (let i = 0; i < grdLines.length; i++) { |
| 290 | const grdLine = grdLines[i]; |
| 291 | // match[0]: full match |
| 292 | // match[1]: relative grdp file path |
| 293 | const match = grdLine.match(/<part file="(.*?)"/); |
| 294 | if (match) { |
| 295 | const grdpFilePathsRemaining = []; |
| 296 | for (const grdpFilePath of grdpFilePaths) { |
| 297 | if (grdpFilePath < getAbsoluteGrdpPath(match[1])) |
| 298 | newGrdFileContent += createPartFileEntry(grdpFilePath); |
| 299 | else |
| 300 | grdpFilePathsRemaining.push(grdpFilePath); |
| 301 | } |
| 302 | grdpFilePaths = grdpFilePathsRemaining; |
| 303 | } else if (grdLine.includes('</messages>')) { |
| 304 | for (const grdpFilePath of grdpFilePaths) |
| 305 | newGrdFileContent += createPartFileEntry(grdpFilePath); |
| 306 | } |
| 307 | newGrdFileContent += grdLine; |
| 308 | if (i < grdLines.length - 1) |
| 309 | newGrdFileContent += '\n'; |
| 310 | } |
| 311 | return writeFileAsync(GRD_PATH, newGrdFileContent); |
| 312 | } |
| 313 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 314 | module.exports = { |
Mandy Chen | 5128cc6 | 2019-09-23 16:46:00 +0000 | [diff] [blame] | 315 | addChildGRDPFilePathsToGRD, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 316 | createGrdpMessage, |
Mandy Chen | 5128cc6 | 2019-09-23 16:46:00 +0000 | [diff] [blame] | 317 | createPartFileEntry, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 318 | escodegen, |
| 319 | esprima, |
| 320 | esprimaTypes, |
Mandy Chen | 5128cc6 | 2019-09-23 16:46:00 +0000 | [diff] [blame] | 321 | getAbsoluteGrdpPath, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 322 | getChildDirectoriesFromDirectory, |
| 323 | getFilesFromDirectory, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 324 | getIDSKey, |
| 325 | getLocalizationCase, |
| 326 | getLocationMessage, |
| 327 | getRelativeFilePathFromSrc, |
Mandy Chen | 5128cc6 | 2019-09-23 16:46:00 +0000 | [diff] [blame] | 328 | getRelativeGrdpPath, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 329 | GRD_PATH, |
| 330 | IDSPrefix, |
| 331 | isLocalizationCall, |
Mandy Chen | d97200b | 2019-07-29 21:13:39 +0000 | [diff] [blame] | 332 | lineNumberOfIndex, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 333 | modifyStringIntoGRDFormat, |
| 334 | parseFileContent, |
Mandy Chen | 1e9d87b | 2019-09-18 17:18:15 +0000 | [diff] [blame] | 335 | SHARED_STRINGS_PATH, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 336 | sanitizeStringIntoCppFormat, |
| 337 | sanitizeStringIntoFrontendFormat, |
Paul Irish | e7b977e | 2019-09-25 12:23:38 +0000 | [diff] [blame] | 338 | shouldParseDirectory, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 339 | verifyFunctionCallee |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 340 | }; |