Skip to content

Commit 31230ca

Browse files
author
Maxim Lobanov
committed
minor refactoring
1 parent 087685b commit 31230ca

File tree

5 files changed

+110
-69
lines changed

5 files changed

+110
-69
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
task:
11-
name: task
11+
name: install
1212
strategy:
1313
matrix:
1414
operating-system: [ubuntu-latest, windows-latest, macos-latest]
@@ -45,7 +45,7 @@ jobs:
4545
cache: ${{ matrix.cache }}
4646

4747
already-installed:
48-
name: 'Check that installed tools are not re-installed'
48+
name: pre-installed
4949
strategy:
5050
matrix:
5151
operating-system: [ubuntu-latest, windows-latest, macos-latest]
@@ -54,10 +54,12 @@ jobs:
5454
steps:
5555
- name: Checkout
5656
uses: actions/checkout@v2
57-
- name: setup platforms;android-30
57+
- name: setup platforms
5858
uses: ./
5959
with:
60-
packages: platforms;android-30
60+
packages: |
61+
platforms;android-29
62+
platforms;android-30
6163
- name: setup ndk-bundle
6264
uses: ./
6365
with:

dist/index.js

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36236,7 +36236,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
3623636236
return result;
3623736237
};
3623836238
Object.defineProperty(exports, "__esModule", { value: true });
36239-
exports.getBooleanInput = exports.getListInput = exports.splitByEOL = void 0;
36239+
exports.getPackageCacheKey = exports.getBooleanInput = exports.getListInput = exports.splitByEOL = void 0;
3624036240
const core = __importStar(__webpack_require__(470));
3624136241
exports.splitByEOL = (stdout) => {
3624236242
return stdout.split(/[\r\n]/);
@@ -36248,6 +36248,9 @@ exports.getListInput = (inputName) => {
3624836248
exports.getBooleanInput = (inputName) => {
3624936249
return (core.getInput(inputName) || "false").toUpperCase() === "TRUE";
3625036250
};
36251+
exports.getPackageCacheKey = (packageInfo) => {
36252+
return `${packageInfo.name} ${packageInfo.remoteVersion}/1`;
36253+
};
3625136254

3625236255

3625336256
/***/ }),
@@ -42055,7 +42058,13 @@ class SDKManager {
4205542058
this.sdkManagerPath = path_1.default.join(androidHome, "tools", "bin", "sdkmanager");
4205642059
}
4205742060
async install(packageInfo) {
42061+
core.startGroup("Trying to download package via sdkmanager...");
4205842062
await this.run([packageInfo.name], true);
42063+
core.endGroup();
42064+
if (!this.isPackageInstalled(packageInfo)) {
42065+
const localPackagePath = this.getPackagePath(packageInfo);
42066+
throw new Error(`Package '${packageInfo.name}' was not installed properly. '${localPackagePath}' folder is empty and doesn't exist`);
42067+
}
4205942068
}
4206042069
async getAllPackagesInfo() {
4206142070
const stdout = await this.run(["--list"], false);
@@ -47550,65 +47559,71 @@ const os = __importStar(__webpack_require__(87));
4755047559
const sdk_manager_1 = __webpack_require__(857);
4755147560
const utils_1 = __webpack_require__(611);
4755247561
const patchUbuntuPermissions = async (androidHome) => {
47553-
core.info("Patch permissions for $ANDROID_HOME on Ubuntu");
47562+
core.startGroup("Patch permissions for $ANDROID_HOME on Ubuntu");
4755447563
await exec.exec("sudo", ["chmod", "-R", "a+rwx", androidHome]);
47564+
core.endGroup();
47565+
};
47566+
const restoreCache = async (sdkmanager, foundPackage) => {
47567+
core.startGroup("Trying to restore package from cache...");
47568+
const cacheKey = utils_1.getPackageCacheKey(foundPackage);
47569+
const localPackagePath = sdkmanager.getPackagePath(foundPackage);
47570+
const cacheHitKey = await cache.restoreCache([localPackagePath], cacheKey);
47571+
let cacheHit = Boolean(cacheHitKey);
47572+
if (cacheHit && !sdkmanager.isPackageInstalled(foundPackage)) {
47573+
core.debug(" [WARNING] Cache is invalid and contains empty folder. ");
47574+
cacheHit = false;
47575+
}
47576+
core.endGroup();
47577+
return cacheHit;
47578+
};
47579+
const saveCache = async (sdkmanager, packageInfo) => {
47580+
core.startGroup("Saving package to cache...");
47581+
const cacheKey = utils_1.getPackageCacheKey(packageInfo);
47582+
const localPackagePath = sdkmanager.getPackagePath(packageInfo);
47583+
await cache.saveCache([localPackagePath], cacheKey);
47584+
core.endGroup();
4755547585
};
4755647586
const run = async () => {
4755747587
try {
47588+
const enableCache = utils_1.getBooleanInput("cache");
47589+
const packages = utils_1.getListInput("packages");
4755847590
const androidHome = process.env.ANDROID_HOME;
4755947591
if (!androidHome) {
4756047592
throw new Error("ANDROID_HOME env variable is not defined");
4756147593
}
47594+
const sdkmanager = new sdk_manager_1.SDKManager(androidHome);
4756247595
if (os.platform() === "linux") {
4756347596
await patchUbuntuPermissions(androidHome);
4756447597
}
47565-
const sdkmanager = new sdk_manager_1.SDKManager(androidHome);
47598+
core.startGroup("Getting list of available components");
4756647599
const allPackages = await sdkmanager.getAllPackagesInfo();
47567-
const enableCache = utils_1.getBooleanInput("cache");
47568-
const packages = utils_1.getListInput("packages");
47600+
core.endGroup();
4756947601
for (const packageName of packages) {
4757047602
core.info(`Installing '${packageName}'...`);
4757147603
const foundPackage = allPackages.find(p => p.name === packageName);
4757247604
if (!foundPackage) {
47573-
throw new Error(`Package '${packageName}' is not available. Enable debug output for more details.`);
47605+
throw new Error(`Package '${packageName}' is not available. Enable debug output for more details`);
4757447606
}
4757547607
if (foundPackage.installed && !foundPackage.update) {
4757647608
core.info(` Package '${foundPackage.name}' is already installed and update is not required`);
4757747609
continue;
4757847610
}
47579-
const cacheKey = `${foundPackage.name} ${foundPackage.remoteVersion}/1`;
47580-
const localPackagePath = sdkmanager.getPackagePath(foundPackage);
47581-
let cacheHit = false;
4758247611
if (enableCache) {
47583-
core.startGroup("Trying to restore package from cache...");
47584-
const cacheHitKey = await cache.restoreCache([localPackagePath], cacheKey);
47585-
cacheHit = Boolean(cacheHitKey);
47586-
if (cacheHit && !sdkmanager.isPackageInstalled(foundPackage)) {
47587-
core.debug(" [WARNING] Cache is invalid and contains empty folder. ");
47588-
cacheHit = false;
47612+
if (await restoreCache(sdkmanager, foundPackage)) {
47613+
core.info(` Package '${foundPackage.name}' is restored from cache`);
47614+
continue;
47615+
}
47616+
else {
47617+
core.info(" No cache found");
4758947618
}
47590-
core.endGroup();
47591-
}
47592-
if (cacheHit) {
47593-
core.info(` Package '${foundPackage.name}' is restored from cache`);
47594-
continue;
47595-
}
47596-
else {
47597-
core.info(" No cache found");
4759847619
}
47599-
core.startGroup("Trying to download package via sdkmanager...");
4760047620
await sdkmanager.install(foundPackage);
47601-
core.endGroup();
4760247621
core.info(` Package '${foundPackage.name}' is downloaded and installed`);
47603-
if (!sdkmanager.isPackageInstalled(foundPackage)) {
47604-
throw new Error(`Package '${packageName}' was not installed properly. '${localPackagePath}' folder is empty and doesn't exist`);
47605-
}
4760647622
if (enableCache) {
47607-
core.startGroup("Saving package to cache...");
47608-
await cache.saveCache([localPackagePath], cacheKey);
47609-
core.endGroup();
47623+
await saveCache(sdkmanager, foundPackage);
4761047624
core.info(` Package '${foundPackage.name}' is saved to cache`);
4761147625
}
47626+
core.info("");
4761247627
}
4761347628
}
4761447629
catch (error) {

src/sdk-manager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ export class SDKManager {
1313
}
1414

1515
public async install(packageInfo: AndroidPackageInfo): Promise<void> {
16+
core.startGroup("Trying to download package via sdkmanager...");
1617
await this.run([packageInfo.name], true);
18+
core.endGroup();
19+
20+
if (!this.isPackageInstalled(packageInfo)) {
21+
const localPackagePath = this.getPackagePath(packageInfo);
22+
throw new Error(`Package '${packageInfo.name}' was not installed properly. '${localPackagePath}' folder is empty and doesn't exist`);
23+
}
1724
}
1825

1926
public async getAllPackagesInfo(): Promise<AndroidPackageInfo[]> {

src/setup-android-tools.ts

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,88 @@ import * as core from "@actions/core";
33
import * as exec from "@actions/exec";
44
import * as os from "os";
55
import { SDKManager } from "./sdk-manager";
6-
import { getListInput, getBooleanInput } from "./utils";
6+
import { getListInput, getBooleanInput, getPackageCacheKey } from "./utils";
7+
import { AndroidPackageInfo } from "./sdk-manager-parser";
78

89
const patchUbuntuPermissions = async(androidHome: string): Promise<void> => {
9-
core.info("Patch permissions for $ANDROID_HOME on Ubuntu");
10+
core.startGroup("Patch permissions for $ANDROID_HOME on Ubuntu");
1011
await exec.exec("sudo", ["chmod", "-R", "a+rwx", androidHome]);
12+
core.endGroup();
13+
};
14+
15+
const restoreCache = async(sdkmanager: SDKManager, foundPackage: AndroidPackageInfo): Promise<boolean> => {
16+
core.startGroup("Trying to restore package from cache...");
17+
18+
const cacheKey = getPackageCacheKey(foundPackage);
19+
const localPackagePath = sdkmanager.getPackagePath(foundPackage);
20+
const cacheHitKey = await cache.restoreCache([localPackagePath], cacheKey);
21+
22+
let cacheHit = Boolean(cacheHitKey);
23+
if (cacheHit && !sdkmanager.isPackageInstalled(foundPackage)) {
24+
core.debug(" [WARNING] Cache is invalid and contains empty folder. ");
25+
cacheHit = false;
26+
}
27+
core.endGroup();
28+
29+
return cacheHit;
30+
};
31+
32+
const saveCache = async(sdkmanager: SDKManager, packageInfo: AndroidPackageInfo): Promise<void> => {
33+
core.startGroup("Saving package to cache...");
34+
35+
const cacheKey = getPackageCacheKey(packageInfo);
36+
const localPackagePath = sdkmanager.getPackagePath(packageInfo);
37+
38+
await cache.saveCache([localPackagePath], cacheKey);
39+
core.endGroup();
1140
};
1241

1342
const run = async(): Promise<void> => {
1443
try {
44+
const enableCache = getBooleanInput("cache");
45+
const packages = getListInput("packages");
46+
1547
const androidHome = process.env.ANDROID_HOME;
1648
if (!androidHome) { throw new Error("ANDROID_HOME env variable is not defined"); }
49+
const sdkmanager = new SDKManager(androidHome);
1750

1851
if (os.platform() === "linux") {
1952
await patchUbuntuPermissions(androidHome);
2053
}
2154

22-
const sdkmanager = new SDKManager(androidHome);
55+
core.startGroup("Getting list of available components");
2356
const allPackages = await sdkmanager.getAllPackagesInfo();
57+
core.endGroup();
2458

25-
const enableCache = getBooleanInput("cache");
26-
const packages = getListInput("packages");
2759
for (const packageName of packages) {
2860
core.info(`Installing '${packageName}'...`);
2961
const foundPackage = allPackages.find(p => p.name === packageName);
3062
if (!foundPackage) {
31-
throw new Error(`Package '${packageName}' is not available. Enable debug output for more details.`);
63+
throw new Error(`Package '${packageName}' is not available. Enable debug output for more details`);
3264
}
3365

3466
if (foundPackage.installed && !foundPackage.update) {
3567
core.info(` Package '${foundPackage.name}' is already installed and update is not required`);
3668
continue;
3769
}
3870

39-
const cacheKey = `${foundPackage.name} ${foundPackage.remoteVersion}/1`;
40-
const localPackagePath = sdkmanager.getPackagePath(foundPackage);
41-
42-
let cacheHit = false;
4371
if (enableCache) {
44-
core.startGroup("Trying to restore package from cache...");
45-
const cacheHitKey = await cache.restoreCache([localPackagePath], cacheKey);
46-
cacheHit = Boolean(cacheHitKey);
47-
if (cacheHit && !sdkmanager.isPackageInstalled(foundPackage)) {
48-
core.debug(" [WARNING] Cache is invalid and contains empty folder. ");
49-
cacheHit = false;
72+
if (await restoreCache(sdkmanager, foundPackage)) {
73+
core.info(` Package '${foundPackage.name}' is restored from cache`);
74+
continue;
75+
} else {
76+
core.info(" No cache found");
5077
}
51-
core.endGroup();
5278
}
5379

54-
if (cacheHit) {
55-
core.info(` Package '${foundPackage.name}' is restored from cache`);
56-
continue;
57-
} else {
58-
core.info(" No cache found");
59-
}
60-
61-
core.startGroup("Trying to download package via sdkmanager...");
6280
await sdkmanager.install(foundPackage);
63-
core.endGroup();
6481
core.info(` Package '${foundPackage.name}' is downloaded and installed`);
6582

66-
if (!sdkmanager.isPackageInstalled(foundPackage)) {
67-
throw new Error(`Package '${packageName}' was not installed properly. '${localPackagePath}' folder is empty and doesn't exist`);
68-
}
69-
7083
if (enableCache) {
71-
core.startGroup("Saving package to cache...");
72-
await cache.saveCache([localPackagePath], cacheKey);
73-
core.endGroup();
84+
await saveCache(sdkmanager, foundPackage);
7485
core.info(` Package '${foundPackage.name}' is saved to cache`);
7586
}
87+
core.info("");
7688
}
7789
} catch (error) {
7890
core.setFailed(error.message);

src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as core from "@actions/core";
2+
import { AndroidPackageInfo } from "./sdk-manager-parser";
23

34
export const splitByEOL = (stdout: string): string[] => {
45
return stdout.split(/[\r\n]/);
@@ -11,4 +12,8 @@ export const getListInput = (inputName: string): string[] => {
1112

1213
export const getBooleanInput = (inputName: string): boolean => {
1314
return (core.getInput(inputName) || "false").toUpperCase() === "TRUE";
14-
};
15+
};
16+
17+
export const getPackageCacheKey = (packageInfo: AndroidPackageInfo): string => {
18+
return `${packageInfo.name} ${packageInfo.remoteVersion}/1`;
19+
}

0 commit comments

Comments
 (0)