Skip to content

Commit f90c6b4

Browse files
author
Maxim Lobanov
committed
Implement logic to parse podfile.lock
1 parent 79321a3 commit f90c6b4

File tree

10 files changed

+160
-20
lines changed

10 files changed

+160
-20
lines changed

.github/workflows/test.yml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,35 @@ jobs:
7272
version: latest
7373

7474
- name: Validate version
75-
run: pod --version
75+
run: pod --version
76+
77+
podfile:
78+
name: install version from podfile.lock
79+
runs-on: macos-latest
80+
steps:
81+
- name: Checkout
82+
uses: actions/checkout@v2
83+
84+
- name: setup-cocoapods
85+
uses: ./
86+
with:
87+
podfile-path: __tests__/podfile-example/Podfile.lock
88+
89+
- name: Validate version
90+
run: pod --version | grep "1.5.3"
91+
92+
- name: setup-cocoapods
93+
uses: ./
94+
with:
95+
podfile-path: __tests__/podfile-example/Podfile2.lock
96+
97+
- name: Validate version
98+
run: pod --version | grep "1.3.1"
99+
100+
- name: setup-cocoapods
101+
uses: ./
102+
with:
103+
podfile-path: __tests__/podfile-example/Podfile3.lock
104+
105+
- name: Validate version
106+
run: pod --version | grep "1.7.0"

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,34 @@
22
This action sets up specific version of Cocoapods in GitHub Actions workflow.
33

44
# Available parameters
5-
| Argument | Description | Supported format |
5+
| Parameter | Description | Supported format |
66
|---------------|-----------------------------------------|----------------------------|
77
| version | Specify version of Cocoapods to install | `latest`, `1.5.2`, `1.9.1` |
8+
| podfile-path | Specify path to `Podfile.lock` file to determine Cocoapods version dynamically | `myApp/Podfile.lock` |
9+
Only one parameter `version` or `podfile-path` should be specified at the same time.
810

911
# Usage
1012
```
1113
name: CI
1214
on: [push]
1315
jobs:
1416
build:
15-
name: Setup Cocoapods
17+
name: Setup Cocoapods based on provided version
1618
runs-on: macos-latest
1719
steps:
1820
- name: setup-cocoapods
1921
uses: maxim-lobanov/setup-cocoapods
2022
with:
21-
version: 1.9.1
23+
version: 1.9.0
24+
25+
build:
26+
name: Setup Cocoapods based on Podfile.lock
27+
runs-on: macos-latest
28+
steps:
29+
- name: setup-cocoapods
30+
uses: maxim-lobanov/setup-cocoapods
31+
with:
32+
podfile-path: myApp/Podfile.lock
2233
```
2334

2435
# License

__tests__/installer.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as path from "path";
12
import * as exec from "@actions/exec";
23
import { CocoapodsInstaller } from "../src/installer";
34

@@ -28,6 +29,21 @@ describe("CocoapodsInstaller", () => {
2829
await CocoapodsInstaller.install("1.9.1");
2930
expect(execCommandSpy).toHaveBeenCalledTimes(0);
3031
});
32+
});
3133

34+
describe("getVersionFromPodfile", () => {
35+
it.each([
36+
["Podfile.lock", "1.5.3"],
37+
["Podfile2.lock", "1.3.1"],
38+
["Podfile3.lock", "1.7.0"],
39+
["Podfile4.lock", null]
40+
])("test case %#", (input: string, expected: string | null) => {
41+
const testCasePath = path.resolve(path.join(__dirname, "podfile-example", input));
42+
if (expected) {
43+
expect(CocoapodsInstaller.getVersionFromPodfile(testCasePath)).toBe(expected);
44+
} else {
45+
expect(() => CocoapodsInstaller.getVersionFromPodfile(testCasePath)).toThrow();
46+
}
47+
});
3248
});
3349
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PODS:
2+
- SPLog (0.0.1)
3+
4+
DEPENDENCIES:
5+
- SPLog (from `../`)
6+
7+
EXTERNAL SOURCES:
8+
SPLog:
9+
:path: "../"
10+
11+
SPEC CHECKSUMS:
12+
SPLog: e9e29ef2a0f04bf28cc62015f795e4a7cef63007
13+
14+
PODFILE CHECKSUM: 2e83ae529882f792020265f1a930efc0af0614b7
15+
16+
COCOAPODS: 1.5.3
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
PODS:
2+
- Bond (6.2.10):
3+
- Diff (~> 0.4)
4+
- ReactiveKit (~> 3.6.0)
5+
- Diff (0.5.3)
6+
- ReactiveKit (3.6.0)
7+
8+
DEPENDENCIES:
9+
- Bond
10+
11+
SPEC CHECKSUMS:
12+
Bond: 8bafdefda68c4f525c9a751b60ef827b0548ef3c
13+
Diff: befa1d19c32726b2b36ce915d98793e2fbde8dcc
14+
ReactiveKit: b3037cb797aa79b58964d309cd724611bea033e6
15+
16+
PODFILE CHECKSUM: f0549efd571fb0d6e3b9828ef306c31ce0bfa0d0
17+
18+
COCOAPODS: 1.3.1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PODS:
2+
- Alamofire (4.8.2)
3+
4+
DEPENDENCIES:
5+
- Alamofire
6+
7+
SPEC REPOS:
8+
https://github.com/cocoapods/specs.git:
9+
- Alamofire
10+
11+
SPEC CHECKSUMS:
12+
Alamofire: ae5c501addb7afdbb13687d7f2f722c78734c2d3
13+
14+
PODFILE CHECKSUM: 6e25dc1d1d6302e538a09a3e0120a572a5fbd5d1
15+
16+
COCOAPODS: 1.7.0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
PODS:
2+
- Alamofire (4.8.2)
3+
4+
DEPENDENCIES:
5+
- Alamofire
6+
7+
SPEC REPOS:
8+
https://github.com/cocoapods/specs.git:
9+
- Alamofire
10+
11+
SPEC CHECKSUMS:
12+
Alamofire: ae5c501addb7afdbb13687d7f2f722c78734c2d3
13+
14+
PODFILE CHECKSUM: 6e25dc1d1d6302e538a09a3e0120a572a5fbd5d1

dist/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,11 +1522,17 @@ class CocoapodsInstaller {
15221522
static getVersionFromPodfile(podfilePath) {
15231523
const absolutePath = path.resolve(podfilePath);
15241524
if (!fs.existsSync(absolutePath)) {
1525-
throw new Error(`podfile is not found on path '${absolutePath}'`);
1525+
throw new Error(`Podfile is not found on path '${absolutePath}'`);
15261526
}
15271527
const fileContent = fs.readFileSync(absolutePath);
1528-
const fileLines = fileContent.toString().split(os_1.EOL);
1529-
return fileLines[0];
1528+
const podLines = fileContent.toString().split(os_1.EOL);
1529+
for (const podLine of podLines) {
1530+
const match = podLine.match(this.podVersionRegex);
1531+
if (match && match.length >= 2) {
1532+
return match[1].trim();
1533+
}
1534+
}
1535+
throw new Error(`Podfile '${absolutePath}' doesn't contain COCOAPODS version.`);
15301536
}
15311537
static async getInstalledVersion() {
15321538
let stdOutput = "";
@@ -1545,6 +1551,7 @@ class CocoapodsInstaller {
15451551
}
15461552
}
15471553
exports.CocoapodsInstaller = CocoapodsInstaller;
1554+
CocoapodsInstaller.podVersionRegex = /^COCOAPODS: ([\d.]+)$/i;
15481555

15491556

15501557
/***/ }),
@@ -1569,14 +1576,15 @@ const run = async () => {
15691576
if (process.platform !== "darwin") {
15701577
throw new Error(`This task is intended only for macOS platform. It can't be run on '${process.platform}' platform`);
15711578
}
1572-
const versionInput = core.getInput("version", { required: false });
1573-
const podfilePathInput = core.getInput("podfile-path", { required: false });
1574-
if (!!versionInput === !!podfilePathInput) {
1579+
let versionSpec = core.getInput("version", { required: false });
1580+
const podfilePath = core.getInput("podfile-path", { required: false });
1581+
if (!!versionSpec === !!podfilePath) {
15751582
throw new Error("Invalid input parameters usage. Only 'version' or 'podfile-path' should be defined");
15761583
}
1577-
const versionSpec = versionInput; // || CocoapodsInstaller.getVersionFromPodfile(podfilePathInput);
15781584
if (!versionSpec) {
1579-
throw new Error(`Invalid version format '${versionSpec}'`);
1585+
core.debug("Reading Podfile to determine the version of Cocoapods...");
1586+
versionSpec = installer_1.CocoapodsInstaller.getVersionFromPodfile(podfilePath);
1587+
core.info(`Podfile points to the Cocoapods ${versionSpec}`);
15801588
}
15811589
await installer_1.CocoapodsInstaller.install(versionSpec);
15821590
}

src/installer.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as core from "@actions/core";
66
import { ExecOptions } from "@actions/exec/lib/interfaces";
77

88
export class CocoapodsInstaller {
9+
private static podVersionRegex = /^COCOAPODS: ([\d.]+)$/i;
910
public static async install(versionSpec: string): Promise<void> {
1011
// Checking pre-installed version of Cocoapods
1112
const installedVersion = await this.getInstalledVersion();
@@ -28,12 +29,20 @@ export class CocoapodsInstaller {
2829
const absolutePath = path.resolve(podfilePath);
2930

3031
if (!fs.existsSync(absolutePath)) {
31-
throw new Error(`podfile is not found on path '${absolutePath}'`);
32+
throw new Error(`Podfile is not found on path '${absolutePath}'`);
3233
}
3334

3435
const fileContent = fs.readFileSync(absolutePath);
35-
const fileLines = fileContent.toString().split(EOL);
36-
return fileLines[0];
36+
const podLines = fileContent.toString().split(EOL);
37+
38+
for (const podLine of podLines) {
39+
const match = podLine.match(this.podVersionRegex);
40+
if (match && match.length >= 2) {
41+
return match[1].trim();
42+
}
43+
}
44+
45+
throw new Error(`Podfile '${absolutePath}' doesn't contain COCOAPODS version.`);
3746
}
3847

3948
private static async getInstalledVersion(): Promise<string | null> {

src/setup-cocoapods.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ const run = async (): Promise<void> => {
77
throw new Error(`This task is intended only for macOS platform. It can't be run on '${process.platform}' platform`);
88
}
99

10-
const versionInput = core.getInput("version", { required: false });
11-
const podfilePathInput = core.getInput("podfile-path", { required: false });
10+
let versionSpec = core.getInput("version", { required: false });
11+
const podfilePath = core.getInput("podfile-path", { required: false });
1212

13-
if (!!versionInput === !!podfilePathInput) {
13+
if (!!versionSpec === !!podfilePath) {
1414
throw new Error("Invalid input parameters usage. Only 'version' or 'podfile-path' should be defined");
1515
}
1616

17-
const versionSpec = versionInput;// || CocoapodsInstaller.getVersionFromPodfile(podfilePathInput);
1817
if (!versionSpec) {
19-
throw new Error(`Invalid version format '${versionSpec}'`);
18+
core.debug("Reading Podfile to determine the version of Cocoapods...");
19+
versionSpec = CocoapodsInstaller.getVersionFromPodfile(podfilePath);
20+
core.info(`Podfile points to the Cocoapods ${versionSpec}`);
2021
}
2122

2223
await CocoapodsInstaller.install(versionSpec);

0 commit comments

Comments
 (0)