diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..09eaea8 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,27 @@ +name: "Release: Manage Versions" + +on: + push: + branches: + - main + +jobs: + HandleVersionChange: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Run script + uses: actions/github-script@v6 + with: + script: | + const script = require('./version-handling') + script({ github, context, core }) + + - name: Commit changes + id: commit_changes + uses: EndBug/add-and-commit@v9.1.1 + with: + add: "./Echo.xcodeproj/project.pbxproj" + message: "[AUTO RELEASE]" diff --git a/version-handling.js b/version-handling.js new file mode 100644 index 0000000..855334b --- /dev/null +++ b/version-handling.js @@ -0,0 +1,68 @@ +const fs = require("fs"); +const path = require("path"); + +const getNewVersion = (currentVersion, commit) => { + const [major, minor, patch] = currentVersion.split("."); + + if (commit.includes("[PATCH]")) { + return `${major}.${minor}.${parseInt(patch) + 1}`; + } else if (commit.includes("[MINOR]")) { + return `${major}.${parseInt(minor) + 1}.${0}`; + } else if (commit.includes("[MAJOR]")) { + return `${parseInt(major) + 1}.${0}.${0}`; + } else { + return null; + } +}; + +module.exports = ({ github, context, core }) => { + const commit = context.payload.commits.map((x) => x.message).join(" = "); + + console.log("Commit message:", commit); + + const projectFilePath = path.join( + __dirname, + "./DragToSpeak.xcodeproj/project.pbxproj" + ); + + console.log(`Getting project file: ${projectFilePath}`); + + const contents = fs + .readFileSync(projectFilePath, { + encoding: "utf8", + }) + .toString(); + + const regex = new RegExp(/MARKETING_VERSION = (\d+\.\d+\.\d+);/, "g"); + + const results = contents.matchAll(regex); + + let count = 0; + let currentVersion = null; + + for (const result of results) { + if (count == 0) { + currentVersion = result[1]; + } else { + if (result[1] !== currentVersion) + throw new Error("All versions didn't match"); + } + count++; + } + + console.log(`Changing ${count} version numbers`); + + console.log("Current version is: ", currentVersion); + + const newVersion = getNewVersion(currentVersion, commit); + + if (newVersion === null) { + console.log("You didn't ask for a new version"); + } else { + console.log("New version", newVersion); + + const newContents = contents.replaceAll(currentVersion, newVersion); + + fs.writeFileSync(projectFilePath, newContents); + } +};