Skip to content

Commit

Permalink
Merge pull request #3085 from brave/async-update-patches
Browse files Browse the repository at this point in the history
update_patches command becomes async
  • Loading branch information
petemill authored Jan 24, 2019
2 parents 537a288 + c254926 commit 66eab35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
33 changes: 15 additions & 18 deletions lib/updatePatches.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs-extra')
const config = require('../lib/config')
const util = require('../lib/util')

const updatePatches = (options) => {
const updatePatches = async (options) => {
config.update(options)

const patchDir = path.join(config.projects['brave-core'].dir, 'patches')
Expand All @@ -18,8 +18,7 @@ const updatePatches = (options) => {

// grab Modified (and later Deleted) files but not Created (since we copy those)
const modifiedDiffArgs = ['diff', '--diff-filter=M', '--name-only', '--ignore-space-at-eol']
const modifiedDiff = util.run('git', modifiedDiffArgs, runOptionsChrome)
const modifiedFileList = modifiedDiff.stdout.toString()
const modifiedFileList = (await util.runAsync('git', modifiedDiffArgs, runOptionsChrome))
.split('\n')
.filter(s => s.length > 0 &&
!s.startsWith('chrome/app/theme/default') &&
Expand All @@ -38,8 +37,8 @@ const updatePatches = (options) => {

// grab every existing patch file in the dir (at this point, patchfiles for now-unmodified files live on)
const existingFileArgs = ['ls-files', '--exclude-standard']
let existingFileOutput = util.run('git', existingFileArgs, runOptionsPatch)
let existingFileList = existingFileOutput.stdout.toString().split('\n').filter(s => s.length > 0)
let existingFileList = (await util.runAsync('git', existingFileArgs, runOptionsPatch))
.split('\n').filter(s => s.length > 0)

// Add files here we specifically want to keep around regardless
const exclusionList = []
Expand All @@ -61,22 +60,20 @@ const updatePatches = (options) => {
// appear, you can quickly patch this by changing the separator, even
// to something longer

let n = modifiedFileList.length

for (let i = 0; i < n; i++) {
const old = modifiedFileList[i]
const revised = substitutedFileList[i]

let writeOpsDoneCount = 0
let writePatchOps = modifiedFileList.map(async (old, i) => {
const singleDiffArgs = ['diff', '--src-prefix=a/', '--dst-prefix=b/', '--full-index', old]
let singleDiff = util.run('git', singleDiffArgs, runOptionsChrome)
const patchContents = await util.runAsync('git', singleDiffArgs, runOptionsChrome)
const patchFilename = substitutedFileList[i]
await fs.writeFile(path.join(patchDir, patchFilename), patchContents)

const contents = singleDiff.stdout.toString()
const filename = revised
writeOpsDoneCount++
console.log(
`updatePatches wrote ${writeOpsDoneCount} / ${modifiedFileList.length}: ${patchFilename}`
)
})

fs.writeFileSync(path.join(patchDir, filename), contents)

console.log('updatePatches wrote ' + (1 + i) + '/' + n + ': ' + filename)
}
await Promise.all(writePatchOps)

// regular rm patchfiles whose target is no longer modified
let m = cruftList.length
Expand Down
28 changes: 27 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path')
const spawnSync = require('child_process').spawnSync
const { spawn, spawnSync } = require('child_process')
const config = require('./config')
const fs = require('fs-extra')
const crypto = require('crypto')
Expand Down Expand Up @@ -34,6 +34,32 @@ const util = {
return prog
},

runAsync: (cmd, args = [], options = {}) => {
console.log(cmd, args.join(' '))
const continueOnFail = options.continueOnFail
delete options.continueOnFail
return new Promise((resolve, reject) => {
const prog = spawn(cmd, args, options)
let stderr = ''
let stdout = ''
prog.stderr.on('data', data => {
stderr += data
})
prog.stdout.on('data', data => {
stdout += data
})
prog.on('close', statusCode => {
const hasFailed = statusCode !== 0
if (hasFailed && !continueOnFail) {
console.log(stdout)
console.error(stderr)
process.exit(1)
}
resolve(stdout)
})
})
},

buildGClientConfig: () => {
function replacer(key, value) {
return value;
Expand Down

0 comments on commit 66eab35

Please sign in to comment.