forked from intro-skipper/intro-skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-version.js
58 lines (48 loc) · 2.23 KB
/
update-version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const fs = require('fs');
// Read csproj
const csprojPath = './ConfusedPolarBear.Plugin.IntroSkipper/ConfusedPolarBear.Plugin.IntroSkipper.csproj';
if (!fs.existsSync(csprojPath)) {
console.error('ConfusedPolarBear.Plugin.IntroSkipper.csproj file not found');
process.exit(1);
}
function updateCsprojVersion() {
const newVersion = process.env.VERSION
const csprojContent = fs.readFileSync(csprojPath, 'utf8');
const updatedContent = csprojContent
.replace(/<AssemblyVersion>.*<\/AssemblyVersion>/, `<AssemblyVersion>${newVersion}</AssemblyVersion>`)
.replace(/<FileVersion>.*<\/FileVersion>/, `<FileVersion>${newVersion}</FileVersion>`);
fs.writeFileSync(csprojPath, updatedContent);
console.log('Updated .csproj file with new version.');
}
// Function to increment version string
function incrementVersion(version) {
const parts = version.split('.').map(Number);
parts[parts.length - 1] += 1; // Increment the last part of the version
return parts.join('.');
}
// Read the .csproj file
fs.readFile(csprojPath, 'utf8', (err, data) => {
if (err) {
return console.error('Failed to read .csproj file:', err);
}
let newAssemblyVersion = null;
let newFileVersion = null;
// Use regex to find and increment versions
const updatedData = data.replace(/<AssemblyVersion>(.*?)<\/AssemblyVersion>/, (match, version) => {
newAssemblyVersion = incrementVersion(version);
return `<AssemblyVersion>${newAssemblyVersion}</AssemblyVersion>`;
}).replace(/<FileVersion>(.*?)<\/FileVersion>/, (match, version) => {
newFileVersion = incrementVersion(version);
return `<FileVersion>${newFileVersion}</FileVersion>`;
});
// Write the updated XML back to the .csproj file
fs.writeFile(csprojPath, updatedData, 'utf8', (err) => {
if (err) {
return console.error('Failed to write .csproj file:', err);
}
console.log('Version incremented successfully!');
// Write the new versions to GitHub Actions environment files
fs.appendFileSync(process.env.GITHUB_ENV, `NEW_ASSEMBLY_VERSION=${newAssemblyVersion}\n`);
fs.appendFileSync(process.env.GITHUB_ENV, `NEW_FILE_VERSION=${newFileVersion}\n`);
});
});