Skip to content

Commit

Permalink
Automatically add .grdp files when adding .grd.
Browse files Browse the repository at this point in the history
Fixes #6907
  • Loading branch information
mkarolin committed Nov 15, 2019
1 parent 9e563cb commit 7f8bc62
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 68 deletions.
13 changes: 4 additions & 9 deletions lib/chromiumRebaseL10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ const util = require('../lib/util')
const {rebaseBraveStringFilesOnChromiumL10nFiles, braveAutoGeneratedPaths} = require('./l10nUtil')

const resetChromeStringFiles = () => {
// Revert to original before string replacement for android_chrome_strings.grd
// because original grd is overwritten with replaced one in ./src/brave.
// Revert to originals before string replacement because original grd(p)s are
// overwritten with modified versions from ./src/brave during build.
const srcDir = config.projects['chrome'].dir
const targetFilesForReset = []
targetFilesForReset.push(path.resolve(path.join(srcDir, 'chrome', 'android', 'java', 'strings', 'android_chrome_strings.grd')))
targetFilesForReset.push(path.resolve(path.join(srcDir, 'components', 'components_strings.grd')))
targetFilesForReset.push(path.resolve(path.join(srcDir, 'components', 'management_strings.grdp')))
targetFilesForReset.push(path.resolve(path.join(srcDir, 'components', 'password_manager_strings.grdp')))
targetFilesForReset.push(path.resolve(path.join(srcDir, 'components', 'bookmark_bar_strings.grdp')))
const targetFilesForReset = [ "*.grd", "*.grdp" ]
targetFilesForReset.forEach((targetFile) => {
util.run('git', ['checkout', targetFile], { cwd: srcDir })
util.run('git', ['checkout', '--', targetFile], { cwd: srcDir })
})
}

Expand Down
101 changes: 47 additions & 54 deletions lib/l10nUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

const path = require('path')
const fs = require('fs')
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const srcDir = path.resolve(path.join(__dirname, '..', 'src'))

Expand All @@ -31,46 +33,17 @@ const braveAndroidChromeStringsPath = path.resolve(path.join(srcDir, 'brave', 'a
const chromiumComponentsChromiumStringsPath = path.resolve(path.join(srcDir, 'components', 'components_chromium_strings.grd'))
const braveComponentsBraveStringsPath = path.resolve(path.join(srcDir, 'brave', 'components', 'components_brave_strings.grd'))

// component_strings.grd and any of its parts files that we track localization for in transifex
// These map to brave/app/strings/components_strings*.xtb
// components/component_strings.grd and any of its parts files that we track localization for in transifex
// These map to brave/components/component_strings*.xtb
const chromiumComponentsStringsPath = path.resolve(path.join(srcDir, 'components', 'components_strings.grd'))
const braveComponentsStringsPath = path.resolve(path.join(srcDir, 'brave', 'components', 'components_strings.grd'))
const chromiumBookmarksBarStringsPartPath = path.resolve(path.join(srcDir, 'components', 'bookmark_bar_strings.grdp'))
const braveBookmarksBarStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'components', 'bookmark_bar_strings.grdp'))

// src/components/components_strings.grd and any of its parts files.
const chromiumManagementStringsPartPath = path.resolve(path.join(srcDir, 'components', 'management_strings.grdp'))
const braveManagementStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'components', 'management_strings.grdp'))
const chromiumPasswordManagerStringsPartPath = path.resolve(path.join(srcDir, 'components', 'password_manager_strings.grdp'))
const bravePasswordManagerStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'components', 'password_manager_strings.grdp'))

// generated_resources.grd and any of its parts files that we track localization for in transifex
// There is also chromeos_strings.grdp but we don't need to track it here
// There is also chromeos_strings.grdp, but we don't need to track it here because it is explicitly skipped in transifex.py
// These map to brave/app/resources/generated_resoruces*.xtb
const chromiumGeneratedResourcesPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'generated_resources.grd'))
const braveGeneratedResourcesPath = path.resolve(path.join(srcDir, 'brave', 'app', 'generated_resources.grd'))
const chromiumBookmarksPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'bookmarks_strings.grdp'))
const braveBookmarksPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'bookmarks_strings.grdp'))
const chromiumMediaRouterPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'media_router_strings.grdp'))
const braveMediaRouterPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'media_router_strings.grdp'))
const chromiumSettingsStringsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'settings_strings.grdp'))
const braveSettingsStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'settings_strings.grdp'))
const chromiumExtensionsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'extensions_strings.grdp'))
const braveExtensionsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'extensions_strings.grdp'))
const chromiumPrintingStringsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'printing_strings.grdp'))
const bravePrintingStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'printing_strings.grdp'))
const chromiumProfileSettingsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'profiles_strings.grdp'))
const braveProfileSettingsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'profiles_strings.grdp'))
const chromiumVRStringsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'vr_strings.grdp'))
const braveVRStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'vr_strings.grdp'))
const chromiumWelcomeStringsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'welcome_strings.grdp'))
const braveWelcomeStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'welcome_strings.grdp'))
const chromiumAppManagementStringsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'app_management_strings.grdp'))
const braveAppManagementStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'app_management_strings.grdp'))
const chromiumGlobalMediaControlsStringsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'global_media_controls_strings.grdp'))
const braveGlobalMediaControlsStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'global_media_controls_strings.grdp'))
const chromiumSupervisedUserErrorPageStringsPartPath = path.resolve(path.join(srcDir, 'chrome', 'app', 'supervised_user_error_page_strings.grdp'))
const braveSupervisedUserErrorPageStringsPartPath = path.resolve(path.join(srcDir, 'brave', 'app', 'supervised_user_error_page_strings.grdp'))
const chromiumGeneratedResourcesExcludes = new Set(["chromeos_strings.grdp"])

// The following are not generated files but still need to be tracked so they get sent to transifex
// These xtb files don't need to be copied anywhere.
Expand All @@ -82,32 +55,52 @@ const braveResourcesComponentsStringsPath = path.resolve(path.join(srcDir, 'brav
const braveExtensionMessagesPath = path.resolve(path.join(srcDir, 'brave', 'components', 'brave_extension', 'extension', 'brave_extension', '_locales', 'en_US', 'messages.json'))
const braveRewardsExtensionMessagesPath = path.resolve(path.join(srcDir, 'brave', 'components', 'brave_rewards', 'resources', 'extension', 'brave_rewards', '_locales', 'en_US', 'messages.json'))

// When adding new grd or grd files, never add a grdp part path without a parent grd path.
// Group them with a leading and trailing newline to keep this file organized.
// Helper function to find all grdp parts in a grd.
function getGrdPartsFromGrd(path) {
const grd = new JSDOM(fs.readFileSync(path, 'utf8'))
const partTags = grd.window.document.getElementsByTagName("part")
let parts = new Array()
for (i = 0; i < partTags.length; i++) {
parts.push(partTags[i].getAttribute('file'));
}
return parts
}

// Add all mappings here whether it is a GRD or a GRDP.
// Brave specific only grd and grdp files should not be added here.
// Helper function to create a mapping for grd and all of its grdp parts.
function AddGrd(chromiumPath, bravePath, exclude = new Set()) {
console.log("Adding mappings for GRD: " + chromiumPath)
let mapping = {
[chromiumPath]: bravePath
}
const grdps = getGrdPartsFromGrd(chromiumPath)
if (grdps.length) {
const chromiumDir = path.dirname(chromiumPath)
const braveDir = path.dirname(bravePath)
for (i = 0; i < grdps.length; i++) {
if (exclude.has(grdps[i])) {
continue
}
mapping[path.resolve(path.join(chromiumDir, grdps[i]))] = path.resolve(path.join(braveDir, grdps[i]))
}
console.log(" - Added " + (Object.keys(mapping).length - 1) + " GRDP.")
}
return mapping
}

// Add all GRD mappings here.
// Brave specific only grd and grdp files should NOT be added.
// When adding new grd or grdp files, never add a grdp part path without a parent grd path.
// Group them with a leading and trailing newline to keep this file organized.
// The first 3 are added explicitly because we change the file names.
const chromiumToAutoGeneratedBraveMapping = {
[chromiumStringsPath]: braveStringsPath,
[chromiumSettingsPartPath]: braveSettingsPartPath,

[chromiumComponentsChromiumStringsPath]: braveComponentsBraveStringsPath,
[chromiumComponentsStringsPath]: braveComponentsStringsPath,
[chromiumBookmarksBarStringsPartPath]: braveBookmarksBarStringsPartPath,
[chromiumGeneratedResourcesPath]: braveGeneratedResourcesPath,
[chromiumBookmarksPartPath]: braveBookmarksPartPath,
[chromiumMediaRouterPartPath]: braveMediaRouterPartPath,
[chromiumSettingsStringsPartPath]: braveSettingsStringsPartPath,
[chromiumExtensionsPartPath]: braveExtensionsPartPath,
[chromiumPrintingStringsPartPath]: bravePrintingStringsPartPath,
[chromiumProfileSettingsPartPath]: braveProfileSettingsPartPath,
[chromiumVRStringsPartPath]: braveVRStringsPartPath,
[chromiumWelcomeStringsPartPath]: braveWelcomeStringsPartPath,
[chromiumAppManagementStringsPartPath]: braveAppManagementStringsPartPath,
[chromiumGlobalMediaControlsStringsPartPath]: braveGlobalMediaControlsStringsPartPath,
[chromiumSupervisedUserErrorPageStringsPartPath]: braveSupervisedUserErrorPageStringsPartPath,
[androidChromeStringsPath]: braveAndroidChromeStringsPath,
[chromiumManagementStringsPartPath]: braveManagementStringsPartPath,
[chromiumPasswordManagerStringsPartPath]: bravePasswordManagerStringsPartPath

...AddGrd(chromiumComponentsStringsPath, braveComponentsStringsPath),
...AddGrd(chromiumGeneratedResourcesPath, braveGeneratedResourcesPath, chromiumGeneratedResourcesExcludes),
...AddGrd(androidChromeStringsPath, braveAndroidChromeStringsPath)
}

// Same as with chromiumToAutoGeneratedBraveMapping but maps in the opposite direction
Expand Down
6 changes: 1 addition & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ const util = {
autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'brave_strings.grd')] = path.join(chromeAppDir, 'brave_strings.grd')
autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'settings_brave_strings.grdp')] = path.join(chromeAppDir, 'settings_brave_strings.grdp')
autoGeneratedBraveToChromiumMapping[path.join(braveComponentsDir, 'components_brave_strings.grd')] = path.join(chromeComponentsDir, 'components_brave_strings.grd')
autoGeneratedBraveToChromiumMapping[path.join(braveComponentsDir, 'components_strings.grd')] = path.join(chromeComponentsDir, 'components_strings.grdp')
autoGeneratedBraveToChromiumMapping[path.join(braveComponentsDir, 'management_strings.grdp')] = path.join(chromeComponentsDir, 'management_strings.grdp')
autoGeneratedBraveToChromiumMapping[path.join(braveComponentsDir, 'password_manager_strings.grdp')] = path.join(chromeComponentsDir, 'password_manager_strings.grdp')
autoGeneratedBraveToChromiumMapping[path.join(braveComponentsDir, 'bookmark_bar_strings.grdp')] = path.join(chromeComponentsDir, 'bookmark_bar_strings.grdp')
autoGeneratedBraveToChromiumMapping[path.join(braveAndroidDir, 'java', 'strings', 'android_chrome_strings.grd')] = path.join(chromeAndroidDir, 'java', 'strings', 'android_chrome_strings.grd')

Object.entries(autoGeneratedBraveToChromiumMapping).forEach(mapping => fileMap.add(mapping))

// Copy xtb files for:
Expand Down

0 comments on commit 7f8bc62

Please sign in to comment.