Skip to content

Commit d3f2e70

Browse files
authored
feat: interactive typings generation for android (#5798)
1 parent 1bb41ca commit d3f2e70

File tree

1 file changed

+100
-4
lines changed

1 file changed

+100
-4
lines changed

lib/commands/typings.ts

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { IOptions, IStaticConfig } from "../declarations";
1+
import { glob } from "glob";
2+
import { homedir } from "os";
3+
import * as path from "path";
4+
import { PromptObject } from "prompts";
5+
import { color } from "../color";
26
import { IChildProcess, IFileSystem, IHostInfo } from "../common/declarations";
37
import { ICommand, ICommandParameter } from "../common/definitions/commands";
48
import { injector } from "../common/yok";
9+
import { IOptions, IStaticConfig } from "../declarations";
510
import { IProjectData } from "../definitions/project";
6-
import * as path from "path";
711

812
export class TypingsCommand implements ICommand {
913
public allowedParameters: ICommandParameter[] = [];
@@ -15,7 +19,8 @@ export class TypingsCommand implements ICommand {
1519
private $mobileHelper: Mobile.IMobileHelper,
1620
private $childProcess: IChildProcess,
1721
private $hostInfo: IHostInfo,
18-
private $staticConfig: IStaticConfig
22+
private $staticConfig: IStaticConfig,
23+
private $prompter: IPrompter
1924
) {}
2025

2126
public async execute(args: string[]): Promise<void> {
@@ -49,8 +54,98 @@ export class TypingsCommand implements ICommand {
4954
return true;
5055
}
5156

57+
private async resolveGradleDependencies(target: string) {
58+
const gradleHome = path.resolve(
59+
process.env.GRADLE_USER_HOME ?? path.join(homedir(), `/.gradle`)
60+
);
61+
const gradleFiles = path.resolve(gradleHome, "caches/modules-2/files-2.1/");
62+
63+
if (!this.$fs.exists(gradleFiles)) {
64+
this.$logger.warn("No gradle files found");
65+
return;
66+
}
67+
68+
const pattern = `${target.replaceAll(":", "/")}/**/*.{jar,aar}`;
69+
70+
const res = await glob(pattern, {
71+
cwd: gradleFiles,
72+
});
73+
74+
if (!res || res.length === 0) {
75+
this.$logger.warn("No files found");
76+
return [];
77+
}
78+
79+
const items = res.map((item) => {
80+
const [group, artifact, version, sha1, file] = item.split("/");
81+
return {
82+
id: sha1 + version,
83+
group,
84+
artifact,
85+
version,
86+
sha1,
87+
file,
88+
path: path.resolve(gradleFiles, item),
89+
};
90+
});
91+
92+
this.$logger.clearScreen();
93+
94+
const choices = await this.$prompter.promptForChoice(
95+
`Select dependencies to generate typings for (${color.greenBright(
96+
target
97+
)})`,
98+
items
99+
.sort((a, b) => {
100+
if (a.artifact < b.artifact) return -1;
101+
if (a.artifact > b.artifact) return 1;
102+
103+
return a.version.localeCompare(b.version, undefined, {
104+
numeric: true,
105+
sensitivity: "base",
106+
});
107+
})
108+
.map((item) => {
109+
return {
110+
title: `${color.white(item.group)}:${color.greenBright(
111+
item.artifact
112+
)}:${color.yellow(item.version)} - ${color.cyanBright.bold(
113+
item.file
114+
)}`,
115+
value: item.id,
116+
};
117+
}),
118+
true,
119+
{
120+
optionsPerPage: process.stdout.rows - 6, // 6 lines are taken up by the instructions
121+
} as Partial<PromptObject>
122+
);
123+
124+
this.$logger.clearScreen();
125+
126+
return items
127+
.filter((item) => choices.includes(item.id))
128+
.map((item) => item.path);
129+
}
130+
52131
private async handleAndroidTypings() {
53-
if (!(this.$options.jar || this.$options.aar)) {
132+
const targets = this.$options.argv._.slice(2) ?? [];
133+
const paths: string[] = [];
134+
135+
if (targets.length) {
136+
for (const target of targets) {
137+
try {
138+
paths.push(...(await this.resolveGradleDependencies(target)));
139+
} catch (err) {
140+
this.$logger.trace(
141+
`Failed to resolve gradle dependencies for target "${target}"`,
142+
err
143+
);
144+
}
145+
}
146+
}
147+
148+
if (!paths.length && !(this.$options.jar || this.$options.aar)) {
54149
this.$logger.warn(
55150
[
56151
"No .jar or .aar file specified. Please specify at least one of the following:",
@@ -97,6 +192,7 @@ export class TypingsCommand implements ICommand {
97192
const inputs: string[] = [
98193
...asArray(this.$options.jar),
99194
...asArray(this.$options.aar),
195+
...paths,
100196
];
101197

102198
await this.$childProcess.spawnFromEvent(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy