forked from ionic-team/capacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoctor.ts
More file actions
200 lines (158 loc) · 6.52 KB
/
doctor.ts
File metadata and controls
200 lines (158 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { pathExists, readFile } from 'fs-extra';
import { join, extname, parse } from 'path';
import c from '../colors.js';
import { check } from '../common.js';
import type { Config } from '../definitions.js';
import { fatal, isFatal } from '../errors.js';
import { logSuccess } from '../log.js';
import { readdirp } from '../util/fs.js';
import { readXML } from '../util/xml.js';
export async function doctorAndroid(config: Config): Promise<void> {
try {
await check([checkAndroidInstalled, () => checkGradlew(config), () => checkAppSrcDirs(config)]);
logSuccess('Android looking great! 👌');
} catch (e: any) {
if (!isFatal(e)) {
fatal(e.stack ?? e);
}
throw e;
}
}
async function checkAppSrcDirs(config: Config): Promise<string | null> {
if (!(await pathExists(config.android.appDirAbs))) {
return `${c.strong(config.android.appDir)} directory is missing in ${c.strong(config.android.platformDir)}`;
}
if (!(await pathExists(config.android.srcMainDirAbs))) {
return `${c.strong(config.android.srcMainDir)} directory is missing in ${c.strong(config.android.platformDir)}`;
}
if (!(await pathExists(config.android.assetsDirAbs))) {
return `${c.strong(config.android.assetsDir)} directory is missing in ${c.strong(config.android.platformDir)}`;
}
if (!(await pathExists(config.android.webDirAbs))) {
return `${c.strong(config.android.webDir)} directory is missing in ${c.strong(config.android.platformDir)}`;
}
const appSrcMainAssetsWwwIndexHtmlDir = join(config.android.webDirAbs, 'index.html');
if (!(await pathExists(appSrcMainAssetsWwwIndexHtmlDir))) {
return `${c.strong('index.html')} file is missing in ${c.strong(config.android.webDirAbs)}`;
}
return checkAndroidManifestFile(config);
}
async function checkAndroidManifestFile(config: Config): Promise<string | null> {
const manifestFileName = 'AndroidManifest.xml';
const manifestFilePath = join(config.android.srcMainDirAbs, manifestFileName);
if (!(await pathExists(manifestFilePath))) {
return `${c.strong(manifestFileName)} is missing in ${c.strong(config.android.srcMainDir)}`;
}
try {
const xmlData = await readXML(manifestFilePath);
return checkAndroidManifestData(config, xmlData);
} catch (e: any) {
return e;
}
}
async function checkAndroidManifestData(config: Config, xmlData: any): Promise<string | null> {
const manifestNode: any = xmlData.manifest;
if (!manifestNode) {
return `Missing ${c.input('<manifest>')} XML node in ${c.strong(config.android.srcMainDir)}`;
}
const applicationChildNodes: any[] = manifestNode.application;
if (!Array.isArray(manifestNode.application)) {
return `Missing ${c.input('<application>')} XML node as a child node of ${c.input('<manifest>')} in ${c.strong(
config.android.srcMainDir,
)}`;
}
let mainActivityClassPath = '';
const mainApplicationNode = applicationChildNodes.find((applicationChildNode) => {
const activityChildNodes: any[] = applicationChildNode.activity;
if (!Array.isArray(activityChildNodes)) {
return false;
}
const mainActivityNode = activityChildNodes.find((activityChildNode) => {
const intentFilterChildNodes: any[] = activityChildNode['intent-filter'];
if (!Array.isArray(intentFilterChildNodes)) {
return false;
}
return intentFilterChildNodes.find((intentFilterChildNode) => {
const actionChildNodes: any[] = intentFilterChildNode.action;
if (!Array.isArray(actionChildNodes)) {
return false;
}
const mainActionChildNode = actionChildNodes.find((actionChildNode) => {
const androidName = actionChildNode.$['android:name'];
return androidName === 'android.intent.action.MAIN';
});
if (!mainActionChildNode) {
return false;
}
const categoryChildNodes: any[] = intentFilterChildNode.category;
if (!Array.isArray(categoryChildNodes)) {
return false;
}
return categoryChildNodes.find((categoryChildNode) => {
const androidName = categoryChildNode.$['android:name'];
return androidName === 'android.intent.category.LAUNCHER';
});
});
});
if (mainActivityNode) {
mainActivityClassPath = mainActivityNode.$['android:name'];
}
return mainActivityNode;
});
if (!mainApplicationNode) {
return `Missing main ${c.input('<activity>')} XML node in ${c.strong(config.android.srcMainDir)}`;
}
if (!mainActivityClassPath) {
return `Missing ${c.input('<activity android:name="">')} attribute for MainActivity class in ${c.strong(
config.android.srcMainDir,
)}`;
}
return checkPackage(config, mainActivityClassPath);
}
async function checkPackage(config: Config, mainActivityClassPath: string) {
const appSrcMainJavaDir = join(config.android.srcMainDirAbs, 'java');
if (!(await pathExists(appSrcMainJavaDir))) {
return `${c.strong('java')} directory is missing in ${c.strong(appSrcMainJavaDir)}`;
}
const mainActivityClassName: any = mainActivityClassPath.split('.').pop();
const srcFiles = await readdirp(appSrcMainJavaDir, {
filter: (entry) =>
!entry.stats.isDirectory() &&
['.java', '.kt'].includes(extname(entry.path)) &&
mainActivityClassName === parse(entry.path).name,
});
if (srcFiles.length == 0) {
return `Main activity file (${mainActivityClassName}) is missing`;
}
return checkBuildGradle(config);
}
async function checkBuildGradle(config: Config) {
const fileName = 'build.gradle';
const filePath = join(config.android.appDirAbs, fileName);
if (!(await pathExists(filePath))) {
return `${c.strong(fileName)} file is missing in ${c.strong(config.android.appDir)}`;
}
let fileContent = await readFile(filePath, { encoding: 'utf-8' });
fileContent = fileContent.replace(/'|"/g, '').replace(/\s+/g, ' ');
const searchFor = `applicationId`;
if (fileContent.indexOf(searchFor) === -1) {
return `${c.strong('build.gradle')} file missing ${c.input(`applicationId`)} config in ${filePath}`;
}
return null;
}
async function checkGradlew(config: Config) {
const fileName = 'gradlew';
const filePath = join(config.android.platformDirAbs, fileName);
if (!(await pathExists(filePath))) {
return `${c.strong(fileName)} file is missing in ${c.strong(config.android.platformDir)}`;
}
return null;
}
async function checkAndroidInstalled() {
/*
if (!await isInstalled('android')) {
return 'Android is not installed. For information: https://developer.android.com/studio/index.html';
}
*/
return null;
}