@@ -2,6 +2,7 @@ const path = require('path')
2
2
const spawnSync = require ( 'child_process' ) . spawnSync
3
3
const config = require ( './config' )
4
4
const fs = require ( 'fs-extra' )
5
+ const crypto = require ( 'crypto' )
5
6
const autoGeneratedBraveToChromiumMapping = Object . assign ( { } , require ( './l10nUtil' ) . autoGeneratedBraveToChromiumMapping )
6
7
7
8
const runGClient = ( args , options = { } ) => {
@@ -54,6 +55,26 @@ const util = {
54
55
fs . writeFileSync ( config . defaultGClientFile , out )
55
56
} ,
56
57
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
+
57
78
updateBranding : ( ) => {
58
79
console . log ( 'update branding...' )
59
80
const chromeComponentsDir = path . join ( config . srcDir , 'components' )
@@ -66,47 +87,88 @@ const util = {
66
87
const braveBrowserDir = path . join ( config . projects [ 'brave-core' ] . dir , 'browser' )
67
88
const braveAppVectorIconsDir = path . join ( config . projects [ 'brave-core' ] . dir , 'vector_icons' , 'chrome' , 'app' )
68
89
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' )
73
94
74
- // Copy each grd back to Chromium src dir
75
- Object . entries ( autoGeneratedBraveToChromiumMapping ) . forEach ( ( [ bravePath , chromiumPath ] ) =>
76
- fs . copySync ( bravePath , chromiumPath ) )
77
95
78
96
// Copy xtb files for:
79
97
// brave/app/resources/chromium_strings*.xtb
80
98
// brave/app/strings/components_chromium_strings*.xtb
81
99
// 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' )
84
102
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' )
88
106
// 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' )
97
115
98
116
if ( process . platform === 'darwin' ) {
99
117
// Copy proper mac app icon for channel to chrome/app/theme/mac/app.icns.
100
118
// Each channel's app icons are stored in brave/app/theme/$channel/app.icns.
101
119
// 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
+ }
104
126
105
127
// Copy branding file
106
128
let branding_file_name = 'BRANDING'
107
129
if ( config . channel )
108
130
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
+ }
110
172
}
111
173
} ,
112
174
@@ -179,6 +241,17 @@ const util = {
179
241
args += arg + '=' + val + ' '
180
242
}
181
243
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
182
255
}
183
256
}
184
257
0 commit comments