|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import ini from "ini"; |
| 4 | + |
| 5 | +const [, , nextVersion] = process.argv; |
| 6 | + |
| 7 | +if (!nextVersion) { |
| 8 | + console.error("No version supplied to bump-version script."); |
| 9 | + process.exit(1); |
| 10 | +} |
| 11 | + |
| 12 | +const gradlePropsPath = path.join(__dirname, "..", "gradle.properties"); |
| 13 | +const gradleFilePath = path.join(__dirname, "..", "build.gradle.kts"); |
| 14 | + |
| 15 | +function readProperties(filePath: string): Record<string, string> { |
| 16 | + if (!fs.existsSync(filePath)) return {}; |
| 17 | + const content = fs.readFileSync(filePath, "utf8"); |
| 18 | + return ini.parse(content); |
| 19 | +} |
| 20 | + |
| 21 | +function writeProperties(filePath: string, props: Record<string, string>) { |
| 22 | + const serialized = ini.stringify(props); |
| 23 | + fs.writeFileSync(filePath, `${serialized}\n`, "utf8"); |
| 24 | +} |
| 25 | + |
| 26 | +function updateGradleProperties() { |
| 27 | + const props = readProperties(gradlePropsPath); |
| 28 | + props.version = nextVersion; |
| 29 | + writeProperties(gradlePropsPath, props); |
| 30 | +} |
| 31 | + |
| 32 | +function updateBuildGradleIfNeeded() { |
| 33 | + if (!fs.existsSync(gradleFilePath)) return; |
| 34 | + const content = fs.readFileSync(gradleFilePath, "utf8"); |
| 35 | + const versionPattern = /(^\s*version\s*=\s*")[^"]+(")/m; |
| 36 | + if (versionPattern.test(content)) { |
| 37 | + const updated = content.replace(versionPattern, `$1${nextVersion}$2`); |
| 38 | + fs.writeFileSync(gradleFilePath, updated, "utf8"); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +updateGradleProperties(); |
| 43 | +updateBuildGradleIfNeeded(); |
| 44 | + |
| 45 | +console.log(`Set project version to ${nextVersion}`); |
0 commit comments