Skip to content

Commit

Permalink
fix: android & ios commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-chillios committed Mar 18, 2023
1 parent 6d61fa7 commit 2a3c09c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 36 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
"description": "Github Action to bump Android & iOS versions and build numbers",
"main": "dist/index.js",
"scripts": {
"build": "ncc build ./src/index.ts -s -m -o dist"
"build": "ncc build ./src/index.ts -s -m -o dist",
"publish": ""
},
"dependencies": {
"@actions/core": "1.10.0",
"@actions/exec": "1.1.1"
},
"devDependencies": {
"@types/node": "18.13.0",
"@types/node": "18.15.3",
"@vercel/ncc": "0.36.1",
"typescript": "4.9.5"
"typescript": "5.0.2"
},
"keywords": [
"github",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 31 additions & 17 deletions src/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,41 @@ import { setOutput } from '@actions/core'
import { readFile, writeFile } from 'fs'
import { Output } from "./types";

const versionCodeRegex = new RegExp(/^(\s*versionCode(?:\s|=)\s*)(\d+.*)/, "g")
const versionNameRegex = new RegExp(/(versionName(?:\s|=)*)(.*)/, "g")
const versionCodeRegex = new RegExp(/^(\s*versionCode(?:\s|=)\s*)(\d+.*)/, "g");
const versionNameRegex = new RegExp(/(versionName(?:\s|=)*)(.*)/, "g");

const getVersionCodeLine = (versionCodeLine: string, versionCode: string | string[]) => {
const forceVersionCode = parseInt(typeof versionCode === 'string' ? versionCode : versionCode?.[0]?.toString())
const versionCodeLineArray = versionCodeLine.split(' ')
const currentVersionCode = parseInt(versionCodeLineArray.pop()) + 1
const nextVersionCode = forceVersionCode || currentVersionCode

setOutput(Output.AndroidVersionCode, nextVersionCode)
return `${versionCodeLineArray.join(' ')} ${nextVersionCode}`
}

const getVersionNameLine = (versionNameLine: string, bumpType: string) => {
const versionNameLineArray = versionNameLine.split(' ')
const currentVersionName = versionNameLineArray.pop().replace(new RegExp('"', 'g'), '')
const nextVersionName = bumpedVersion(currentVersionName, bumpType)

setOutput(Output.AndroidVersion, nextVersionName)
return `${versionNameLineArray.join(' ')} "${nextVersionName}"`
}


export function bumpAndroidValues({ androidPath, versionCode, bumpType }: { androidPath: string; versionCode: string; bumpType: string }) {
const gradlePath = `${androidPath}/app/build.gradle`

readFile(gradlePath, 'utf8', (_, data) => {
const currentVersionName = data.match(versionNameRegex)[0].split("=")[1].trim();
const currentVersionCode = data.match(versionCodeRegex)[0].split("=")[1].trim();
const nextVersionCode = parseInt(versionCode || currentVersionCode) + 1
const nextVersionName = bumpedVersion(currentVersionName, bumpType)

const nextData = data
.replace(versionCodeRegex, `$1${nextVersionCode}`)
.replace(versionNameRegex, `$1\"${nextVersionName}\"`)

writeFile(gradlePath, nextData, function (err) {
if (err) throw err;
setOutput(Output.AndroidVersion, nextVersionName)
setOutput(Output.AndroidVersionCode, nextVersionCode)
});
});
const fileLines = data.split("\n")
const versionCodeLineIndex = fileLines.findIndex(line => line.match(versionCodeRegex))
const versionNameLineIndex = fileLines.findIndex(line => line.match(versionNameRegex))

fileLines[versionCodeLineIndex] = getVersionCodeLine(fileLines[versionCodeLineIndex], versionCode)
fileLines[versionNameLineIndex] = getVersionNameLine(fileLines[versionNameLineIndex], bumpType)

writeFile(gradlePath, fileLines.join("\n"), (error) => { if (error) throw error })
})
}

2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const bumpedVersion = (version: string, bumpType: string) => {
const [major, minor, patch] = version.split(".")
const [major, minor, patch] = version.replace(new RegExp('"', 'g'), '').split(".")

switch (bumpType) {
case "major":
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const run = () => {
const versionCode = getInput(Input.VersionCode)
const buildNumber = getInput(Input.BuildNumber)

if (iosPath) bumpIosValues({ iosPath, buildNumber, bumpType })
if (androidPath) bumpAndroidValues({ androidPath, versionCode, bumpType })
if (iosPath) bumpIosValues({ iosPath, buildNumber, bumpType })
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { Output } from "./types"

async function bumpIosVersion(path: string, bumpType: string) {
const options = { cwd: path }
const { stdout: currentIosVersion } = await getExecOutput("agvtool", ["what-marketing-version"], options)
const { stdout: currentIosVersion } = await getExecOutput("agvtool", ["what-marketing-version", "-terse1"], options)
const version = bumpedVersion(currentIosVersion.toString().trim(), bumpType)

const { stdout: iosVersion } = await getExecOutput("agvtool", ["new-marketing-version", version], options)
setOutput(Output.IosVersion, iosVersion.toString().trim())
}

async function bumpBuildNumber(path: string, buildNumber?: string) {
const command = buildNumber ? "new-version" : "next-version"
const { stdout: iosBuildNumber } = await getExecOutput("agvtool", [command, buildNumber], { cwd: path })
const params = buildNumber ? ["new-version", "-all", buildNumber] : ["next-version", "-all"]
const { stdout: iosBuildNumber } = await getExecOutput("agvtool", params, { cwd: path })

setOutput(Output.IosBuildNumber, iosBuildNumber.toString().trim())
}
Expand Down

0 comments on commit 2a3c09c

Please sign in to comment.