From f7db8d69bda9f3bacb32c091c25e0ad99a5ac434 Mon Sep 17 00:00:00 2001 From: bridiver Date: Thu, 6 Sep 2018 12:48:27 -0700 Subject: [PATCH] only copy theme resources when they actually change fix #989 --- lib/build.js | 26 ++--------------- lib/util.js | 79 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/lib/build.js b/lib/build.js index 190b376086386..74e7a43b84251 100644 --- a/lib/build.js +++ b/lib/build.js @@ -18,19 +18,8 @@ const touchOverriddenFiles = () => { return true } - const walkSync = (dir, filelist = []) => { - fs.readdirSync(dir).forEach(file => { - if (fs.statSync(path.join(dir, file)).isDirectory()) { - filelist = walkSync(path.join(dir, file), filelist) - } else if (applyFileFilter(file)) { - filelist = filelist.concat(path.join(dir, file)) - } - }) - return filelist - } - const chromiumSrcDir = path.join(config.srcDir, 'brave', 'chromium_src') - var sourceFiles = walkSync(chromiumSrcDir) + var sourceFiles = util.walkSync(chromiumSrcDir, applyFileFilter) // Touch original files by updating mtime. const chromiumSrcDirLen = chromiumSrcDir.length @@ -63,19 +52,8 @@ const touchOverriddenVectorIconFiles = () => { return true } - const walkSync = (dir, filelist = []) => { - fs.readdirSync(dir).forEach(file => { - if (fs.statSync(path.join(dir, file)).isDirectory()) { - filelist = walkSync(path.join(dir, file), filelist) - } else if (applyFileFilter(file)) { - filelist = filelist.concat(path.join(dir, file)) - } - }) - return filelist - } - const braveVectorIconsDir = path.join(config.srcDir, 'brave', 'vector_icons') - var braveVectorIconFiles = walkSync(braveVectorIconsDir) + var braveVectorIconFiles = util.walkSync(braveVectorIconsDir, applyFileFilter) // Touch original files by updating mtime. const braveVectorIconsDirLen = braveVectorIconsDir.length diff --git a/lib/util.js b/lib/util.js index c82a0b7d7f8f0..83e08ab10a1c5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -75,38 +75,74 @@ const util = { Object.entries(autoGeneratedBraveToChromiumMapping).forEach(([bravePath, chromiumPath]) => fs.copySync(bravePath, chromiumPath)) + let fileMap = {} // Copy xtb files for: // brave/app/resources/chromium_strings*.xtb // brave/app/strings/components_chromium_strings*.xtb // brave/app/resources/generated_resoruces*.xtb - fs.copySync(path.join(braveAppDir, 'resources'), path.join(chromeAppDir, 'resources')) - fs.copySync(path.join(braveAppDir, 'strings'), path.join(chromeComponentsDir, 'strings')) + fileMap[path.join(braveAppDir, 'resources')] = path.join(chromeAppDir, 'resources') + fileMap[path.join(braveAppDir, 'strings')] = path.join(chromeComponentsDir, 'strings') - fs.copySync(path.join(braveAppDir, 'theme', 'brave'), path.join(chromeAppDir, 'theme', 'brave')) - fs.copySync(path.join(braveAppDir, 'theme', 'default_100_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'brave')) - fs.copySync(path.join(braveAppDir, 'theme', 'default_200_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'brave')) + fileMap[path.join(braveAppDir, 'theme', 'brave')] = path.join(chromeAppDir, 'theme', 'brave') + fileMap[path.join(braveAppDir, 'theme', 'default_100_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_100_percent', 'brave') + fileMap[path.join(braveAppDir, 'theme', 'default_200_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_200_percent', 'brave') // By overwriting, we don't need to modify some grd files. - fs.copySync(path.join(braveAppDir, 'theme', 'brave'), path.join(chromeAppDir, 'theme', 'chromium')) - fs.copySync(path.join(braveAppDir, 'theme', 'default_100_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'chromium')) - fs.copySync(path.join(braveAppDir, 'theme', 'default_200_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'chromium')) - fs.copySync(path.join(braveComponentsDir, 'resources', 'default_100_percent', 'brave'), path.join(chromeComponentsDir, 'resources', 'default_100_percent', 'chromium')) - fs.copySync(path.join(braveComponentsDir, 'resources', 'default_200_percent', 'brave'), path.join(chromeComponentsDir, 'resources', 'default_200_percent', 'chromium')) - fs.copySync(path.join(braveAppVectorIconsDir, 'vector_icons', 'brave'), path.join(chromeAppDir, 'vector_icons', 'brave')) - fs.copySync(path.join(braveResourcesDir, 'settings', 'brave_page_visibility.js'), path.join(chromeResourcesDir, 'settings', 'brave_page_visibility.js')) - fs.copySync(path.join(braveResourcesDir, 'settings', 'brave_appearance_page'), path.join(chromeResourcesDir, 'settings', 'brave_appearance_page')) + fileMap[path.join(braveAppDir, 'theme', 'brave')] = path.join(chromeAppDir, 'theme', 'chromium') + fileMap[path.join(braveAppDir, 'theme', 'default_100_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_100_percent', 'chromium') + fileMap[path.join(braveAppDir, 'theme', 'default_200_percent', 'brave')] = path.join(chromeAppDir, 'theme', 'default_200_percent', 'chromium') + fileMap[path.join(braveComponentsDir, 'resources', 'default_100_percent', 'brave')] = path.join(chromeComponentsDir, 'resources', 'default_100_percent', 'chromium') + fileMap[path.join(braveComponentsDir, 'resources', 'default_200_percent', 'brave')] = path.join(chromeComponentsDir, 'resources', 'default_200_percent', 'chromium') + fileMap[path.join(braveAppVectorIconsDir, 'vector_icons', 'brave')] = path.join(chromeAppDir, 'vector_icons', 'brave') + fileMap[path.join(braveResourcesDir, 'settings', 'brave_page_visibility.js')] = path.join(chromeResourcesDir, 'settings', 'brave_page_visibility.js') + fileMap[path.join(braveResourcesDir, 'settings', 'brave_appearance_page')] = path.join(chromeResourcesDir, 'settings', 'brave_appearance_page') if (process.platform === 'darwin') { // Copy proper mac app icon for channel to chrome/app/theme/mac/app.icns. // Each channel's app icons are stored in brave/app/theme/$channel/app.icns. // With this copying, we don't need to modify chrome/BUILD.gn for this. - fs.copySync(path.join(braveAppDir, 'theme', 'brave', 'mac', config.channel, 'app.icns'), - path.join(chromeAppDir, 'theme', 'brave', 'mac', 'app.icns')) + fileMap[path.join(braveAppDir, 'theme', 'brave', 'mac', config.channel, 'app.icns')] = + path.join(chromeAppDir, 'theme', 'brave', 'mac', 'app.icns') // Copy branding file let branding_file_name = 'BRANDING' if (config.channel) branding_file_name = branding_file_name + '.' + config.channel - fs.copySync(path.join(braveAppDir, 'theme', 'brave', branding_file_name), path.join(chromeAppDir, 'theme', 'brave', 'BRANDING')) + fileMap[path.join(braveAppDir, 'theme', 'brave', branding_file_name)] = + path.join(chromeAppDir, 'theme', 'brave', 'BRANDING') + } + + for (source in fileMap) { + let sourceFiles = [] + const output = fileMap[source] + + // get all the files if source if a directory + if (fs.statSync(source).isDirectory()) { + sourceFiles = util.walkSync(source) + } else { + sourceFiles = [source] + } + + for (var i in sourceFiles) { + const sourceFile = sourceFiles[i] + let destinationFile = output + if (fs.statSync(destinationFile).isDirectory()) { + destinationFile = path.join(destinationFile, path.basename(sourceFile)) + } + + // The destination file might be newer when updating chromium so + // we check for an exact match on the timestamp. We use seconds instead + // of ms because utimesSync doesn't set the times with ms precision + if (!fs.existsSync(destinationFile) || + Math.floor(new Date(fs.statSync(sourceFile).mtimeMs).getTime() / 1000) != + Math.floor(new Date(fs.statSync(destinationFile).mtimeMs).getTime() / 1000)) { + fs.copySync(sourceFile, destinationFile) + // can't set the date in the past so update the source file + // to match the newly copied destionation file + const date = fs.statSync(destinationFile).mtime + fs.utimesSync(sourceFile, date, date) + console.log(sourceFile + ' copied to ' + destinationFile) + } + } } }, @@ -179,6 +215,17 @@ const util = { args += arg + '=' + val + ' ' } return args.replace(/"/g,'\\"') + }, + + walkSync: (dir, filter = null, filelist = []) => { + fs.readdirSync(dir).forEach(file => { + if (fs.statSync(path.join(dir, file)).isDirectory()) { + filelist = util.walkSync(path.join(dir, file), filter, filelist) + } else if (!filter || filter.call(null, file)) { + filelist = filelist.concat(path.join(dir, file)) + } + }) + return filelist } }