Skip to content

Commit c2c4e76

Browse files
authored
Merge pull request brave#990 from brave/theme-copy
only copy theme resources when they actually change
2 parents 25a69f6 + 43bb783 commit c2c4e76

File tree

4 files changed

+100
-57
lines changed

4 files changed

+100
-57
lines changed

lib/build.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,8 @@ const touchOverriddenFiles = () => {
1818
return true
1919
}
2020

21-
const walkSync = (dir, filelist = []) => {
22-
fs.readdirSync(dir).forEach(file => {
23-
if (fs.statSync(path.join(dir, file)).isDirectory()) {
24-
filelist = walkSync(path.join(dir, file), filelist)
25-
} else if (applyFileFilter(file)) {
26-
filelist = filelist.concat(path.join(dir, file))
27-
}
28-
})
29-
return filelist
30-
}
31-
3221
const chromiumSrcDir = path.join(config.srcDir, 'brave', 'chromium_src')
33-
var sourceFiles = walkSync(chromiumSrcDir)
22+
var sourceFiles = util.walkSync(chromiumSrcDir, applyFileFilter)
3423

3524
// Touch original files by updating mtime.
3625
const chromiumSrcDirLen = chromiumSrcDir.length
@@ -63,19 +52,8 @@ const touchOverriddenVectorIconFiles = () => {
6352
return true
6453
}
6554

66-
const walkSync = (dir, filelist = []) => {
67-
fs.readdirSync(dir).forEach(file => {
68-
if (fs.statSync(path.join(dir, file)).isDirectory()) {
69-
filelist = walkSync(path.join(dir, file), filelist)
70-
} else if (applyFileFilter(file)) {
71-
filelist = filelist.concat(path.join(dir, file))
72-
}
73-
})
74-
return filelist
75-
}
76-
7755
const braveVectorIconsDir = path.join(config.srcDir, 'brave', 'vector_icons')
78-
var braveVectorIconFiles = walkSync(braveVectorIconsDir)
56+
var braveVectorIconFiles = util.walkSync(braveVectorIconsDir, applyFileFilter)
7957

8058
// Touch original files by updating mtime.
8159
const braveVectorIconsDirLen = braveVectorIconsDir.length
@@ -116,10 +94,7 @@ const build = (buildConfig = config.defaultBuildConfig, options) => {
11694

11795
touchOverriddenFiles()
11896
touchOverriddenVectorIconFiles()
119-
120-
if (!options.no_branding_update) {
121-
util.updateBranding()
122-
}
97+
util.updateBranding()
12398

12499
util.buildTarget()
125100
}

lib/createDist.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ const createDist = (buildConfig = config.defaultBuildConfig, options) => {
77
config.buildConfig = buildConfig
88
config.update(options)
99

10-
if (!options.no_branding_update) {
11-
util.updateBranding()
12-
}
13-
10+
util.updateBranding()
1411
fs.removeSync(path.join(config.outputDir, 'dist'))
1512
config.buildTarget = 'create_dist'
1613
util.buildTarget()

lib/util.js

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const spawnSync = require('child_process').spawnSync
33
const config = require('./config')
44
const fs = require('fs-extra')
5+
const crypto = require('crypto')
56
const autoGeneratedBraveToChromiumMapping = Object.assign({}, require('./l10nUtil').autoGeneratedBraveToChromiumMapping)
67

78
const runGClient = (args, options = {}) => {
@@ -54,6 +55,26 @@ const util = {
5455
fs.writeFileSync(config.defaultGClientFile, out)
5556
},
5657

58+
calculateFileChecksum: (filename) => {
59+
// adapted from https://github.com/roryrjb/md5-file
60+
const BUFFER_SIZE = 8192
61+
const fd = fs.openSync(filename, 'r')
62+
const buffer = Buffer.alloc(BUFFER_SIZE)
63+
const md5 = crypto.createHash('md5')
64+
65+
try {
66+
let bytesRead
67+
do {
68+
bytesRead = fs.readSync(fd, buffer, 0, BUFFER_SIZE)
69+
md5.update(buffer.slice(0, bytesRead))
70+
} while (bytesRead === BUFFER_SIZE)
71+
} finally {
72+
fs.closeSync(fd)
73+
}
74+
75+
return md5.digest('hex')
76+
},
77+
5778
updateBranding: () => {
5879
console.log('update branding...')
5980
const chromeComponentsDir = path.join(config.srcDir, 'components')
@@ -66,47 +87,88 @@ const util = {
6687
const braveBrowserDir = path.join(config.projects['brave-core'].dir, 'browser')
6788
const braveAppVectorIconsDir = path.join(config.projects['brave-core'].dir, 'vector_icons', 'chrome', 'app')
6889

69-
// The following 3 entries we map to the same name, not the chromium equivalent name for copying back
70-
autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'brave_strings.grd')] = path.join(chromeAppDir, 'brave_strings.grd')
71-
autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'settings_brave_strings.grdp')] = path.join(chromeAppDir, 'settings_brave_strings.grdp')
72-
autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'components_brave_strings.grd')] = path.join(config.srcDir, 'components', 'components_brave_strings.grd')
90+
let fileMap = {}
91+
fileMap[path.join(braveAppDir, 'brave_strings.grd')] = path.join(chromeAppDir, 'brave_strings.grd')
92+
fileMap[path.join(braveAppDir, 'settings_brave_strings.grdp')] = path.join(chromeAppDir, 'settings_brave_strings.grdp')
93+
fileMap[path.join(braveAppDir, 'components_brave_strings.grd')] = path.join(config.srcDir, 'components', 'components_brave_strings.grd')
7394

74-
// Copy each grd back to Chromium src dir
75-
Object.entries(autoGeneratedBraveToChromiumMapping).forEach(([bravePath, chromiumPath]) =>
76-
fs.copySync(bravePath, chromiumPath))
7795

7896
// Copy xtb files for:
7997
// brave/app/resources/chromium_strings*.xtb
8098
// brave/app/strings/components_chromium_strings*.xtb
8199
// brave/app/resources/generated_resoruces*.xtb
82-
fs.copySync(path.join(braveAppDir, 'resources'), path.join(chromeAppDir, 'resources'))
83-
fs.copySync(path.join(braveAppDir, 'strings'), path.join(chromeComponentsDir, 'strings'))
100+
fileMap[path.join(braveAppDir, 'resources')] = path.join(chromeAppDir, 'resources')
101+
fileMap[path.join(braveAppDir, 'strings')] = path.join(chromeComponentsDir, 'strings')
84102

85-
fs.copySync(path.join(braveAppDir, 'theme', 'brave'), path.join(chromeAppDir, 'theme', 'brave'))
86-
fs.copySync(path.join(braveAppDir, 'theme', 'default_100_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'brave'))
87-
fs.copySync(path.join(braveAppDir, 'theme', 'default_200_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'brave'))
103+
fileMap[path.join(braveAppDir, 'theme', 'brave')] = path.join(chromeAppDir, 'theme', 'brave')
104+
fileMap[path.join(braveAppDir, 'theme', 'default_100_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_100_percent', 'brave')
105+
fileMap[path.join(braveAppDir, 'theme', 'default_200_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_200_percent', 'brave')
88106
// By overwriting, we don't need to modify some grd files.
89-
fs.copySync(path.join(braveAppDir, 'theme', 'brave'), path.join(chromeAppDir, 'theme', 'chromium'))
90-
fs.copySync(path.join(braveAppDir, 'theme', 'default_100_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'chromium'))
91-
fs.copySync(path.join(braveAppDir, 'theme', 'default_200_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'chromium'))
92-
fs.copySync(path.join(braveComponentsDir, 'resources', 'default_100_percent', 'brave'), path.join(chromeComponentsDir, 'resources', 'default_100_percent', 'chromium'))
93-
fs.copySync(path.join(braveComponentsDir, 'resources', 'default_200_percent', 'brave'), path.join(chromeComponentsDir, 'resources', 'default_200_percent', 'chromium'))
94-
fs.copySync(path.join(braveAppVectorIconsDir, 'vector_icons', 'brave'), path.join(chromeAppDir, 'vector_icons', 'brave'))
95-
fs.copySync(path.join(braveResourcesDir, 'settings', 'brave_page_visibility.js'), path.join(chromeResourcesDir, 'settings', 'brave_page_visibility.js'))
96-
fs.copySync(path.join(braveResourcesDir, 'settings', 'brave_appearance_page'), path.join(chromeResourcesDir, 'settings', 'brave_appearance_page'))
107+
fileMap[path.join(braveAppDir, 'theme', 'brave')] = path.join(chromeAppDir, 'theme', 'chromium')
108+
fileMap[path.join(braveAppDir, 'theme', 'default_100_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_100_percent', 'chromium')
109+
fileMap[path.join(braveAppDir, 'theme', 'default_200_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_200_percent', 'chromium')
110+
fileMap[path.join(braveComponentsDir, 'resources', 'default_100_percent', 'brave')] = path.join(chromeComponentsDir, 'resources', 'default_100_percent', 'chromium')
111+
fileMap[path.join(braveComponentsDir, 'resources', 'default_200_percent', 'brave')] = path.join(chromeComponentsDir, 'resources', 'default_200_percent', 'chromium')
112+
fileMap[path.join(braveAppVectorIconsDir, 'vector_icons', 'brave')] = path.join(chromeAppDir, 'vector_icons', 'brave')
113+
fileMap[path.join(braveResourcesDir, 'settings', 'brave_page_visibility.js')] = path.join(chromeResourcesDir, 'settings', 'brave_page_visibility.js')
114+
fileMap[path.join(braveResourcesDir, 'settings', 'brave_appearance_page')] = path.join(chromeResourcesDir, 'settings', 'brave_appearance_page')
97115

98116
if (process.platform === 'darwin') {
99117
// Copy proper mac app icon for channel to chrome/app/theme/mac/app.icns.
100118
// Each channel's app icons are stored in brave/app/theme/$channel/app.icns.
101119
// With this copying, we don't need to modify chrome/BUILD.gn for this.
102-
fs.copySync(path.join(braveAppDir, 'theme', 'brave', 'mac', config.channel, 'app.icns'),
103-
path.join(chromeAppDir, 'theme', 'brave', 'mac', 'app.icns'))
120+
const iconSource = path.join(braveAppDir, 'theme', 'brave', 'mac', config.channel, 'app.icns')
121+
const iconDest = path.join(chromeAppDir, 'theme', 'brave', 'mac', 'app.icns')
122+
if (util.calculateFileChecksum(iconSource) != util.calculateFileChecksum(iconDest)) {
123+
console.log('copy app icon')
124+
fs.copySync(iconSource, iconDest)
125+
}
104126

105127
// Copy branding file
106128
let branding_file_name = 'BRANDING'
107129
if (config.channel)
108130
branding_file_name = branding_file_name + '.' + config.channel
109-
fs.copySync(path.join(braveAppDir, 'theme', 'brave', branding_file_name), path.join(chromeAppDir, 'theme', 'brave', 'BRANDING'))
131+
132+
const brandingSource = path.join(braveAppDir, 'theme', 'brave', branding_file_name)
133+
const brandingDest = path.join(chromeAppDir, 'theme', 'brave', 'BRANDING')
134+
if (util.calculateFileChecksum(brandingSource) != util.calculateFileChecksum(brandingDest)) {
135+
console.log('copy branding file')
136+
fs.copySync(brandingSource, brandingDest)
137+
}
138+
}
139+
140+
for (source in fileMap) {
141+
let sourceFiles = []
142+
const output = fileMap[source]
143+
144+
// get all the files if source if a directory
145+
if (fs.statSync(source).isDirectory()) {
146+
sourceFiles = util.walkSync(source)
147+
} else {
148+
sourceFiles = [source]
149+
}
150+
151+
for (var i in sourceFiles) {
152+
const sourceFile = sourceFiles[i]
153+
let destinationFile = output
154+
if (fs.statSync(destinationFile).isDirectory()) {
155+
destinationFile = path.join(destinationFile, path.basename(sourceFile))
156+
}
157+
158+
// The destination file might be newer when updating chromium so
159+
// we check for an exact match on the timestamp. We use seconds instead
160+
// of ms because utimesSync doesn't set the times with ms precision
161+
if (!fs.existsSync(destinationFile) ||
162+
Math.floor(new Date(fs.statSync(sourceFile).mtimeMs).getTime() / 1000) !=
163+
Math.floor(new Date(fs.statSync(destinationFile).mtimeMs).getTime() / 1000)) {
164+
fs.copySync(sourceFile, destinationFile)
165+
// can't set the date in the past so update the source file
166+
// to match the newly copied destionation file
167+
const date = fs.statSync(destinationFile).mtime
168+
fs.utimesSync(sourceFile, date, date)
169+
console.log(sourceFile + ' copied to ' + destinationFile)
170+
}
171+
}
110172
}
111173
},
112174

@@ -179,6 +241,17 @@ const util = {
179241
args += arg + '=' + val + ' '
180242
}
181243
return args.replace(/"/g,'\\"')
244+
},
245+
246+
walkSync: (dir, filter = null, filelist = []) => {
247+
fs.readdirSync(dir).forEach(file => {
248+
if (fs.statSync(path.join(dir, file)).isDirectory()) {
249+
filelist = util.walkSync(path.join(dir, file), filter, filelist)
250+
} else if (!filter || filter.call(null, file)) {
251+
filelist = filelist.concat(path.join(dir, file))
252+
}
253+
})
254+
return filelist
182255
}
183256
}
184257

scripts/commands.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ program
3535
.option('--official_build <official_build>', 'force official build settings')
3636
.option('--brave_google_api_key <brave_google_api_key>')
3737
.option('--brave_google_api_endpoint <brave_google_api_endpoint>')
38-
.option('--no_branding_update', 'don\'t copy BRANDING to the chrome theme dir')
3938
.option('--channel <target_chanel>', 'target channel to build', /^(beta|dev|nightly|release)$/i, 'release')
4039
.arguments('[build_config]')
4140
.action(build)
@@ -50,7 +49,6 @@ program
5049
.option('--official_build <official_build>', 'force official build settings')
5150
.option('--brave_google_api_key <brave_google_api_key>')
5251
.option('--brave_google_api_endpoint <brave_google_api_endpoint>')
53-
.option('--no_branding_update', 'don\'t copy BRANDING to the chrome theme dir')
5452
.option('--channel <target_chanel>', 'target channel to build', /^(beta|dev|nightly|release)$/i, 'release')
5553
.arguments('[build_config]')
5654
.action(createDist)

0 commit comments

Comments
 (0)