Skip to content

Commit a42701e

Browse files
committed
only copy theme resources when they actually change
fix #989
1 parent 25a69f6 commit a42701e

File tree

2 files changed

+68
-47
lines changed

2 files changed

+68
-47
lines changed

lib/build.js

Lines changed: 2 additions & 24 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

lib/util.js

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,47 +66,79 @@ const util = {
6666
const braveBrowserDir = path.join(config.projects['brave-core'].dir, 'browser')
6767
const braveAppVectorIconsDir = path.join(config.projects['brave-core'].dir, 'vector_icons', 'chrome', 'app')
6868

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')
69+
let fileMap = {}
70+
fileMap[path.join(braveAppDir, 'brave_strings.grd')] = path.join(chromeAppDir, 'brave_strings.grd')
71+
fileMap[path.join(braveAppDir, 'settings_brave_strings.grdp')] = path.join(chromeAppDir, 'settings_brave_strings.grdp')
72+
fileMap[path.join(braveAppDir, 'components_brave_strings.grd')] = path.join(config.srcDir, 'components', 'components_brave_strings.grd')
7373

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

7875
// Copy xtb files for:
7976
// brave/app/resources/chromium_strings*.xtb
8077
// brave/app/strings/components_chromium_strings*.xtb
8178
// 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'))
79+
fileMap[path.join(braveAppDir, 'resources')] = path.join(chromeAppDir, 'resources')
80+
fileMap[path.join(braveAppDir, 'strings')] = path.join(chromeComponentsDir, 'strings')
8481

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'))
82+
fileMap[path.join(braveAppDir, 'theme', 'brave')] = path.join(chromeAppDir, 'theme', 'brave')
83+
fileMap[path.join(braveAppDir, 'theme', 'default_100_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_100_percent', 'brave')
84+
fileMap[path.join(braveAppDir, 'theme', 'default_200_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_200_percent', 'brave')
8885
// 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'))
86+
fileMap[path.join(braveAppDir, 'theme', 'brave')] = path.join(chromeAppDir, 'theme', 'chromium')
87+
fileMap[path.join(braveAppDir, 'theme', 'default_100_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_100_percent', 'chromium')
88+
fileMap[path.join(braveAppDir, 'theme', 'default_200_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_200_percent', 'chromium')
89+
fileMap[path.join(braveComponentsDir, 'resources', 'default_100_percent', 'brave')] = path.join(chromeComponentsDir, 'resources', 'default_100_percent', 'chromium')
90+
fileMap[path.join(braveComponentsDir, 'resources', 'default_200_percent', 'brave')] = path.join(chromeComponentsDir, 'resources', 'default_200_percent', 'chromium')
91+
fileMap[path.join(braveAppVectorIconsDir, 'vector_icons', 'brave')] = path.join(chromeAppDir, 'vector_icons', 'brave')
92+
fileMap[path.join(braveResourcesDir, 'settings', 'brave_page_visibility.js')] = path.join(chromeResourcesDir, 'settings', 'brave_page_visibility.js')
93+
fileMap[path.join(braveResourcesDir, 'settings', 'brave_appearance_page')] = path.join(chromeResourcesDir, 'settings', 'brave_appearance_page')
9794

9895
if (process.platform === 'darwin') {
9996
// Copy proper mac app icon for channel to chrome/app/theme/mac/app.icns.
10097
// Each channel's app icons are stored in brave/app/theme/$channel/app.icns.
10198
// 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'))
99+
fileMap[path.join(braveAppDir, 'theme', 'brave', 'mac', config.channel, 'app.icns')] =
100+
path.join(chromeAppDir, 'theme', 'brave', 'mac', 'app.icns')
104101

105102
// Copy branding file
106103
let branding_file_name = 'BRANDING'
107104
if (config.channel)
108105
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'))
106+
fileMap[path.join(braveAppDir, 'theme', 'brave', branding_file_name)] =
107+
path.join(chromeAppDir, 'theme', 'brave', 'BRANDING')
108+
}
109+
110+
for (source in fileMap) {
111+
let sourceFiles = []
112+
const output = fileMap[source]
113+
114+
// get all the files if source if a directory
115+
if (fs.statSync(source).isDirectory()) {
116+
sourceFiles = util.walkSync(source)
117+
} else {
118+
sourceFiles = [source]
119+
}
120+
121+
for (var i in sourceFiles) {
122+
const sourceFile = sourceFiles[i]
123+
let destinationFile = output
124+
if (fs.statSync(destinationFile).isDirectory()) {
125+
destinationFile = path.join(destinationFile, path.basename(sourceFile))
126+
}
127+
128+
// The destination file might be newer when updating chromium so
129+
// we check for an exact match on the timestamp. We use seconds instead
130+
// of ms because utimesSync doesn't set the times with ms precision
131+
if (!fs.existsSync(destinationFile) ||
132+
Math.floor(new Date(fs.statSync(sourceFile).mtimeMs).getTime() / 1000) !=
133+
Math.floor(new Date(fs.statSync(destinationFile).mtimeMs).getTime() / 1000)) {
134+
fs.copySync(sourceFile, destinationFile)
135+
// can't set the date in the past so update the source file
136+
// to match the newly copied destionation file
137+
const date = fs.statSync(destinationFile).mtime
138+
fs.utimesSync(sourceFile, date, date)
139+
console.log(sourceFile + ' copied to ' + destinationFile)
140+
}
141+
}
110142
}
111143
},
112144

@@ -179,6 +211,17 @@ const util = {
179211
args += arg + '=' + val + ' '
180212
}
181213
return args.replace(/"/g,'\\"')
214+
},
215+
216+
walkSync: (dir, filter = null, filelist = []) => {
217+
fs.readdirSync(dir).forEach(file => {
218+
if (fs.statSync(path.join(dir, file)).isDirectory()) {
219+
filelist = util.walkSync(path.join(dir, file), filter, filelist)
220+
} else if (!filter || filter.call(null, file)) {
221+
filelist = filelist.concat(path.join(dir, file))
222+
}
223+
})
224+
return filelist
182225
}
183226
}
184227

0 commit comments

Comments
 (0)