Skip to content

Commit

Permalink
Add auto release code
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinhenderson committed Mar 5, 2024
1 parent 655a2ed commit cd5948e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -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]"
68 changes: 68 additions & 0 deletions version-handling.js
Original file line number Diff line number Diff line change
@@ -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);
}
};

0 comments on commit cd5948e

Please sign in to comment.