Skip to content

Commit fa7b77d

Browse files
committed
Search for all version occurences and replace in all build files (better multi-project support
1 parent 258eeb3 commit fa7b77d

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

ReplaceVersion.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,63 @@ module.exports = {
33
const oldVersion = dependency.oldVersion
44
const newVersion = dependency.version
55

6-
let modifiedBody = body
6+
const replaceActions = []
77

88
const regexVersionVariable = new RegExp(dependency.group + ":" + dependency.name + ":\\${?(\\w+)}?", "ig")
99

1010
// 'de.kevcodez:pubg-api-wrapper:$myVar'
1111
// 'de.kevcodez:pubg-api-wrapper:${myVar}'
12-
const versionWithVariableMatches = regexVersionVariable.exec(modifiedBody)
12+
const versionWithVariableMatches = regexVersionVariable.exec(body)
1313
if (versionWithVariableMatches && versionWithVariableMatches.length === 2) {
1414
const variableName = versionWithVariableMatches[1]
1515

1616
const regexVariableDefinition = new RegExp(`(${variableName}(\\s+)?=(\\s+)?('|")${oldVersion}('|"))`, "ig")
17-
const regexVariableDefinitionMatches = regexVariableDefinition.exec(modifiedBody)
17+
const regexVariableDefinitionMatches = regexVariableDefinition.exec(body)
1818

1919
if (regexVariableDefinitionMatches && regexVariableDefinitionMatches.length) {
2020
regexVariableDefinitionMatches.filter(it => it.includes(dependency.oldVersion)).forEach(match => {
21-
modifiedBody = modifiedBody.replace(match, match.replace(dependency.oldVersion, dependency.version))
21+
replaceActions.push({
22+
searchValue: match,
23+
replaceValue: match.replace(dependency.oldVersion, dependency.version)
24+
})
2225
})
2326
}
2427
}
2528

2629
// compile 'de.kevcodez:pubg-api-wrapper:1.0.0'
2730
const regexVersionInline = new RegExp(`${dependency.group}:${dependency.name}:${dependency.oldVersion}`, "g")
28-
if (regexVersionInline.exec(modifiedBody)) {
29-
modifiedBody = modifiedBody.replace(regexVersionInline, `${dependency.group}:${dependency.name}:${dependency.version}`)
31+
if (regexVersionInline.exec(body)) {
32+
replaceActions.push({
33+
searchValue: regexVersionInline,
34+
replaceValue: `${dependency.group}:${dependency.name}:${dependency.version}`
35+
})
3036
}
3137

3238
// id 'com.github.ben-manes.versions' version "0.21.0"
3339
// id("com.github.ben-manes.versions") version "0.22.0"
3440
const regexPluginVersionWithPrefix = new RegExp(`${dependency.group}("|')\\)?(\\s+)?version(\\s+)?("|')${oldVersion}("|')`)
35-
const regexVersionWithPrefixMatches = regexPluginVersionWithPrefix.exec(modifiedBody)
41+
const regexVersionWithPrefixMatches = regexPluginVersionWithPrefix.exec(body)
3642
if (regexVersionWithPrefixMatches && regexVersionWithPrefixMatches.length) {
3743
regexVersionWithPrefixMatches.filter(it => it.includes(oldVersion)).forEach(match => {
38-
modifiedBody = modifiedBody.replace(match, match.replace(oldVersion, newVersion))
44+
replaceActions.push({
45+
searchValue: match,
46+
replaceValue: match.replace(oldVersion, newVersion)
47+
})
3948
})
4049
}
4150

4251
// compile group: 'de.kevcodez.pubg', name: 'pubg-api-wrapper', version: '0.8.1'
4352
const regexDependencyWithVersionPrefix = new RegExp(`${dependency.name}('|"),(\\s+)?version:(\\s+)('|")${dependency.oldVersion}('|")`)
44-
const regexDependencyWithVersionPrefixMatches = regexDependencyWithVersionPrefix.exec(modifiedBody)
53+
const regexDependencyWithVersionPrefixMatches = regexDependencyWithVersionPrefix.exec(body)
4554
if (regexDependencyWithVersionPrefixMatches && regexDependencyWithVersionPrefixMatches.length) {
4655
regexDependencyWithVersionPrefixMatches.filter(it => it.includes(oldVersion)).forEach(match => {
47-
modifiedBody = modifiedBody.replace(match, match.replace(oldVersion, newVersion))
56+
replaceActions.push({
57+
searchValue: match,
58+
replaceValue: match.replace(oldVersion, newVersion)
59+
})
4860
})
4961
}
5062

51-
return modifiedBody
63+
return replaceActions
5264
}
5365
}

index.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ function debugLog(message) {
1616
const prompts = require('prompts');
1717
const {
1818
existsSync,
19-
readFile,
2019
readFileSync,
21-
writeFile
20+
writeFileSync
2221
} = require('fs');
2322
const {
2423
subDirectories
@@ -63,10 +62,15 @@ async function executeCommandAndWaitForExitCode(command, args) {
6362

6463
const child = spawn(command, args);
6564
child.stdout.setEncoding('utf8');
66-
child.stdout.on('data', function (data) {
65+
child.stdout.on('data', (data) => {
6766
debugLog(data);
6867
})
6968

69+
child.stderr.setEncoding('utf8');
70+
child.stderr.on('data', (data) => {
71+
console.error(data);
72+
})
73+
7074
child.on('close', (code) => {
7175
commandExitCode = code
7276
})
@@ -147,25 +151,50 @@ async function executeCommandAndWaitForExitCode(command, args) {
147151
}
148152
}
149153

154+
const allReplacements = []
155+
const buildFileContentMap = new Map()
156+
157+
150158
buildFiles.forEach(buildFile => {
151159
debugLog(`Reading Gradle build file ${buildFile}\n`)
152160

153-
readFile(buildFile, function (err, buf) {
154-
let buildFileAsString = buf.toString()
161+
const fileDataBuffer = readFileSync(buildFile)
155162

156-
response.upgrades.filter(it => it !== 'gradle').forEach(it => {
157-
debugLog(`Replacing version\n${JSON.stringify(it)}\n`)
158-
buildFileAsString = ReplaceVersion.replace(buildFileAsString, it)
159-
})
163+
let buildFileAsString = fileDataBuffer.toString()
160164

161-
debugLog(`Writing Gradle build file ${buildFile}\n`)
162-
writeFile(buildFile, buildFileAsString, 'utf8', function (err) {
163-
if (err) return console.log(`Unable to write gradle build file.\n${err}`.bgRed);
164-
});
165+
response.upgrades.filter(it => it !== 'gradle').forEach(dependency => {
166+
debugLog(`Replacing version\n${JSON.stringify(dependency)}\n`)
167+
const replaceVersionActions = ReplaceVersion.replace(buildFileAsString, dependency)
165168

166-
});
169+
replaceVersionActions.forEach(action => {
170+
if (!allReplacements.some(it => it.searchValue === action.searchValue && it.replaceValue === action.replaceValue)) {
171+
allReplacements.push(action)
172+
173+
debugLog(`${action.searchValue} => ${action.replaceValue}`)
174+
}
175+
})
176+
})
177+
178+
buildFileContentMap.set(buildFile, buildFileAsString)
167179
})
168180

181+
buildFileContentMap.forEach((content, buildFile) => {
182+
183+
let modifiedContent = content
184+
185+
allReplacements.forEach(replaceAction => {
186+
modifiedContent = modifiedContent.replace(replaceAction.searchValue, replaceAction.replaceValue)
187+
})
188+
189+
debugLog(`Writing Gradle build file: ${buildFile}\n`)
190+
try {
191+
writeFileSync(buildFile, modifiedContent, 'utf8');
192+
} catch (err) {
193+
console.log(`Unable to write gradle build file.\n${err}`.bgRed);
194+
return
195+
}
196+
197+
}, buildFileContentMap)
169198

170199
})();
171200

0 commit comments

Comments
 (0)