Skip to content

Commit

Permalink
release: v2 with new param and handle fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-chillios committed Jul 7, 2023
1 parent 2a3c09c commit 971bc48
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 56 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ults-io/mobile-version-bump-action@v1
- uses: ults-io/mobile-version-bump-action@v2
with:
version-code: 1 # optional param for Android versionCode - if not provided, will be incremented by 1
build-number: 1 # optional param for iOS build number - if not provided, will be incremented by 1
android-project-path: ./android # required if you want to trigger Android version bump
ios-project-path: ./ios # required if you want to trigger iOS version bump
bump-type: patch # optional param for version bump type - can be one of: major, minor, patch
app-version: 1.0.0 # optional param for Android & iOS version
```
## Inputs
| Name | Description | Required |
| ---------------------- | ----------------------- | -------- |
| `version-code` | Android versionCode | No |
| `build-number` | iOS build number | No |
| `android-project-path` | Path to Android project | No |
| `ios-project-path` | Path to iOS project | No |
| `bump-type` | Version bump type | No |
| Name | Description | Required |
| ---------------------- | ----------------------------- | -------- |
| `version-code` | Android versionCode | No |
| `build-number` | iOS build number | No |
| `android-project-path` | Path to Android project | No |
| `ios-project-path` | Path to iOS project | No |
| `bump-type` | Version bump type | No |
| `app-version` | App for Android & iOS version | No |

## Outputs

Expand Down
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.

87 changes: 59 additions & 28 deletions src/android.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,75 @@
import { bumpedVersion } from "./helpers";

import { setOutput } from '@actions/core'
import { readFile, writeFile } from 'fs'
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 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
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}`
}
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}"`
}
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`
export function bumpAndroidValues({
version,
androidPath,
versionCode,
bumpType,
}: {
version?: string;
androidPath: string;
versionCode: string;
bumpType: string;
}) {
const gradlePath = `${androidPath}/app/build.gradle`;

readFile(gradlePath, 'utf8', (_, data) => {
const fileLines = data.split("\n")
const versionCodeLineIndex = fileLines.findIndex(line => line.match(versionCodeRegex))
const versionNameLineIndex = fileLines.findIndex(line => line.match(versionNameRegex))
readFile(gradlePath, "utf8", (_, data) => {
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)
if (versionCodeLineIndex > 0) {
fileLines[versionCodeLineIndex] = getVersionCodeLine(
fileLines[versionCodeLineIndex],
versionCode
);
}
if (versionNameLineIndex > 0 || version) {
fileLines[versionNameLineIndex] = version || getVersionNameLine(
fileLines[versionNameLineIndex],
bumpType
);
}

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

14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ const run = () => {
const bumpType = getInput(Input.BumpType)
const versionCode = getInput(Input.VersionCode)
const buildNumber = getInput(Input.BuildNumber)
const appVersion = getInput(Input.AppVersion)

if (androidPath) bumpAndroidValues({ androidPath, versionCode, bumpType })
if (iosPath) bumpIosValues({ iosPath, buildNumber, bumpType })
if (androidPath) {
bumpAndroidValues({ version: appVersion, androidPath, versionCode, bumpType })
} else {
console.log("No android path provided. Skipping...")
}

if (iosPath) {
bumpIosValues({ version: appVersion, iosPath, buildNumber, bumpType })
} else {
console.log("No ios path provided. Skipping...")
}
}

try {
Expand Down
58 changes: 42 additions & 16 deletions src/ios.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
import { setOutput } from "@actions/core"
import { getExecOutput } from "@actions/exec"
import { bumpedVersion } from "./helpers"
import { Output } from "./types"
import { setOutput } from "@actions/core";
import { getExecOutput } from "@actions/exec";
import { bumpedVersion } from "./helpers";
import { Output } from "./types";

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

const { stdout: iosVersion } = await getExecOutput("agvtool", ["new-marketing-version", version], options)
setOutput(Output.IosVersion, iosVersion.toString().trim())
if (newVersion) {
const { stdout: iosVersion } = await getExecOutput(
"agvtool",
["new-marketing-version", newVersion],
options
);
setOutput(Output.IosVersion, iosVersion.toString().trim());
} else {
console.log("No version found for path:", path);
}
}

async function bumpBuildNumber(path: string, buildNumber?: string) {
const params = buildNumber ? ["new-version", "-all", buildNumber] : ["next-version", "-all"]
const { stdout: iosBuildNumber } = await getExecOutput("agvtool", params, { 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())
setOutput(Output.IosBuildNumber, iosBuildNumber.toString().trim());
}

export function bumpIosValues({ iosPath, buildNumber, bumpType }: { iosPath: string; buildNumber?: string; bumpType: string }) {
bumpIosVersion(iosPath, bumpType)
bumpBuildNumber(iosPath, buildNumber)
export function bumpIosValues({
version,
iosPath,
buildNumber,
bumpType,
}: {
version?: string;
iosPath: string;
buildNumber?: string;
bumpType: string;
}) {
bumpIosVersion(iosPath, bumpType, version);
bumpBuildNumber(iosPath, buildNumber);
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum Input {
BumpType = "bump-type",
VersionCode = "version-code",
BuildNumber = "build-number",
AppVersion = "app-version",
}


Expand Down

0 comments on commit 971bc48

Please sign in to comment.