forked from ionic-team/capacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
106 lines (89 loc) · 4.14 KB
/
common.ts
File metadata and controls
106 lines (89 loc) · 4.14 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
import { copy, remove, mkdirp, readFile, pathExists, writeFile } from 'fs-extra';
import { join, resolve } from 'path';
import { checkCapacitorPlatform } from '../common.js';
import { getIncompatibleCordovaPlugins } from '../cordova.js';
import type { Config } from '../definitions.js';
import { PluginType, getPluginPlatform } from '../plugin.js';
import type { Plugin } from '../plugin.js';
import { convertToUnixPath } from '../util/fs.js';
export async function checkAndroidPackage(config: Config): Promise<string | null> {
return checkCapacitorPlatform(config, 'android');
}
export async function getAndroidPlugins(allPlugins: Plugin[]): Promise<Plugin[]> {
const resolved = await Promise.all(allPlugins.map(async (plugin) => await resolvePlugin(plugin)));
return resolved.filter((plugin): plugin is Plugin => !!plugin);
}
export async function resolvePlugin(plugin: Plugin): Promise<Plugin | null> {
const platform = 'android';
if (plugin.manifest?.android) {
let pluginFilesPath = plugin.manifest.android.src ? plugin.manifest.android.src : platform;
const absolutePath = join(plugin.rootPath, pluginFilesPath, plugin.id);
// Android folder shouldn't have subfolders, but they used to, so search for them for compatibility reasons
if (await pathExists(absolutePath)) {
pluginFilesPath = join(platform, plugin.id);
}
plugin.android = {
type: PluginType.Core,
path: convertToUnixPath(pluginFilesPath),
};
} else if (plugin.xml) {
plugin.android = {
type: PluginType.Cordova,
path: 'src/' + platform,
};
if (getIncompatibleCordovaPlugins(platform).includes(plugin.id) || !getPluginPlatform(plugin, platform)) {
plugin.android.type = PluginType.Incompatible;
}
} else {
return null;
}
return plugin;
}
/**
* Update an Android project with the desired app name and appId.
* This is a little trickier for Android because the appId becomes
* the package name.
*/
export async function editProjectSettingsAndroid(config: Config): Promise<void> {
const appId = config.app.appId;
const appName = config.app.appName
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/"/g, '\\"')
.replace(/'/g, "\\'");
const buildGradlePath = resolve(config.android.appDirAbs, 'build.gradle');
const domainPath = appId.split('.').join('/');
// Make the package source path to the new plugin Java file
const newJavaPath = resolve(config.android.srcMainDirAbs, `java/${domainPath}`);
if (!(await pathExists(newJavaPath))) {
await mkdirp(newJavaPath);
}
await copy(
resolve(config.android.srcMainDirAbs, 'java/com/getcapacitor/myapp/MainActivity.java'),
resolve(newJavaPath, 'MainActivity.java'),
);
if (appId.split('.')[1] !== 'getcapacitor') {
await remove(resolve(config.android.srcMainDirAbs, 'java/com/getcapacitor'));
}
// Remove our template 'com' folder if their ID doesn't have it
if (appId.split('.')[0] !== 'com') {
await remove(resolve(config.android.srcMainDirAbs, 'java/com/'));
}
// Update the package in the MainActivity java file
const activityPath = resolve(newJavaPath, 'MainActivity.java');
let activityContent = await readFile(activityPath, { encoding: 'utf-8' });
activityContent = activityContent.replace(/package ([^;]*)/, `package ${appId}`);
await writeFile(activityPath, activityContent, { encoding: 'utf-8' });
// Update the applicationId in build.gradle
let gradleContent = await readFile(buildGradlePath, { encoding: 'utf-8' });
gradleContent = gradleContent.replace(/applicationId "[^"]+"/, `applicationId "${appId}"`);
// Update the namespace in build.gradle
gradleContent = gradleContent.replace(/namespace = "[^"]+"/, `namespace = "${appId}"`);
await writeFile(buildGradlePath, gradleContent, { encoding: 'utf-8' });
// Update the settings in res/values/strings.xml
const stringsPath = resolve(config.android.resDirAbs, 'values/strings.xml');
let stringsContent = await readFile(stringsPath, { encoding: 'utf-8' });
stringsContent = stringsContent.replace(/com.getcapacitor.myapp/g, appId);
stringsContent = stringsContent.replace(/My App/g, appName);
await writeFile(stringsPath, stringsContent);
}