Skip to content

Commit a4ac897

Browse files
author
Capacitor+ Bot
committed
chore: sync upstream PR ionic-team#8274 from @White-D-coder
2 parents 3852542 + 6c34446 commit a4ac897

File tree

3 files changed

+120
-15
lines changed

3 files changed

+120
-15
lines changed

cli/src/android/update.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export async function updateAndroid(config: Config): Promise<void> {
4141
if (cordovaPlugins.length > 0) {
4242
await copyPluginsNativeFiles(config, cordovaPlugins);
4343
}
44+
await copyPluginsNativeFiles(config, capacitorPlugins);
4445
if (!(await pathExists(config.android.webDirAbs))) {
4546
await copyTask(config, platform);
4647
}
@@ -139,7 +140,7 @@ export async function installGradlePlugins(
139140
if (!capacitorAndroidPackagePath) {
140141
fatal(
141142
`Unable to find ${c.strong('node_modules/@capacitor/android')}.\n` +
142-
`Are you sure ${c.strong('@capacitor/android')} is installed?`,
143+
`Are you sure ${c.strong('@capacitor/android')} is installed?`,
143144
);
144145
}
145146

@@ -152,19 +153,19 @@ export async function installGradlePlugins(
152153
include ':capacitor-android'
153154
project(':capacitor-android').projectDir = new File('${relativeCapcitorAndroidPath}')
154155
${capacitorPlugins
155-
.map((p) => {
156-
if (!p.android) {
157-
return '';
158-
}
156+
.map((p) => {
157+
if (!p.android) {
158+
return '';
159+
}
159160
160-
const relativePluginPath = convertToUnixPath(relative(settingsPath, p.rootPath));
161+
const relativePluginPath = convertToUnixPath(relative(settingsPath, p.rootPath));
161162
162-
return `
163+
return `
163164
include ':${getGradlePackageName(p.id)}'
164165
project(':${getGradlePackageName(p.id)}').projectDir = new File('${relativePluginPath}/${p.android.path}')
165166
`;
166-
})
167-
.join('')}`;
167+
})
168+
.join('')}`;
168169

169170
const applyArray: any[] = [];
170171
const frameworksArray: any[] = [];
@@ -204,10 +205,10 @@ android {
204205
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
205206
dependencies {
206207
${capacitorPlugins
207-
.map((p) => {
208-
return ` implementation project(':${getGradlePackageName(p.id)}')`;
209-
})
210-
.join('\n')}
208+
.map((p) => {
209+
return ` implementation project(':${getGradlePackageName(p.id)}')`;
210+
})
211+
.join('\n')}
211212
${frameworkString}
212213
}
213214
${applyArray.join('\n')}

cli/src/plugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export async function resolvePlugin(config: Config, name: string): Promise<Plugi
7272
if (!meta) {
7373
return null;
7474
}
75+
const pluginXMLPath = join(rootPath, 'plugin.xml');
76+
const xmlMeta = await readXML(pluginXMLPath);
77+
7578
if (meta.capacitor) {
7679
return {
7780
id: name,
@@ -80,10 +83,9 @@ export async function resolvePlugin(config: Config, name: string): Promise<Plugi
8083
rootPath,
8184
repository: meta.repository,
8285
manifest: meta.capacitor,
86+
xml: xmlMeta ? xmlMeta.plugin : undefined,
8387
};
8488
}
85-
const pluginXMLPath = join(rootPath, 'plugin.xml');
86-
const xmlMeta = await readXML(pluginXMLPath);
8789
return {
8890
id: name,
8991
name: fixName(name),
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { APP_ID, APP_NAME, MappedFS, makeAppDir, run, installPlatform } from './util';
2+
import { join } from 'path';
3+
import { mkdirp, writeFile } from 'fs-extra';
4+
import { runCommand } from '../src/util/subprocess';
5+
6+
const CAPACITOR_PLUGIN_ID = 'capacitor-resource-plugin';
7+
const CAPACITOR_PLUGIN_JS = `
8+
module.exports = {
9+
echo: function(options) {
10+
return Promise.resolve(options);
11+
}
12+
};
13+
`;
14+
15+
const CAPACITOR_PLUGIN_XML = `
16+
<?xml version="1.0" encoding="UTF-8"?>
17+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
18+
id="${CAPACITOR_PLUGIN_ID}"
19+
version="1.0.0">
20+
<name>Capacitor Resource Plugin</name>
21+
<platform name="android">
22+
<resource-file src="android/src/main/res/xml/automotive_app_desc.xml" target="res/xml/automotive_app_desc.xml" />
23+
</platform>
24+
</plugin>
25+
`;
26+
27+
const CAPACITOR_PLUGIN_PACKAGE = `
28+
{
29+
"name": "${CAPACITOR_PLUGIN_ID}",
30+
"version": "1.0.0",
31+
"description": "Capacitor Resource Plugin",
32+
"capacitor": {
33+
"android": {
34+
"src": "android"
35+
}
36+
},
37+
"main": "plugin.js"
38+
}
39+
`;
40+
41+
const AUTOMOTIVE_APP_DESC = `
42+
<?xml version="1.0" encoding="utf-8"?>
43+
<automotiveApp>
44+
<uses name="media"/>
45+
</automotiveApp>
46+
`;
47+
48+
async function makeCapacitorPlugin(pluginPath: string) {
49+
const androidPath = join(pluginPath, 'android/src/main');
50+
const resPath = join(androidPath, 'res/xml');
51+
await mkdirp(resPath);
52+
await writeFile(join(pluginPath, 'plugin.js'), CAPACITOR_PLUGIN_JS);
53+
await writeFile(join(pluginPath, 'plugin.xml'), CAPACITOR_PLUGIN_XML);
54+
await writeFile(join(pluginPath, 'package.json'), CAPACITOR_PLUGIN_PACKAGE);
55+
await writeFile(join(resPath, 'automotive_app_desc.xml'), AUTOMOTIVE_APP_DESC);
56+
57+
// Create dummy android project structure for the plugin so it looks valid
58+
await writeFile(join(androidPath, 'AndroidManifest.xml'), '<manifest></manifest>');
59+
}
60+
61+
describe('Update: Android Resources', () => {
62+
let appDirObj: any;
63+
let appDir: string;
64+
let FS: MappedFS;
65+
66+
beforeAll(async () => {
67+
jest.setTimeout(150000);
68+
appDirObj = await makeAppDir();
69+
appDir = appDirObj.appDir;
70+
71+
// Create and install custom capacitor plugin
72+
const pluginPath = join(appDirObj.path, CAPACITOR_PLUGIN_ID);
73+
await makeCapacitorPlugin(pluginPath);
74+
75+
await runCommand('npm', ['install', '--save', pluginPath], {
76+
cwd: appDir,
77+
});
78+
79+
await run(appDir, `init "${APP_NAME}" "${APP_ID}"`);
80+
await installPlatform(appDir, 'android');
81+
await run(appDir, `add android`);
82+
FS = new MappedFS(appDir);
83+
});
84+
85+
afterAll(() => {
86+
//appDirObj.cleanupCallback();
87+
});
88+
89+
it('Should copy resource-file from Capacitor plugin', async () => {
90+
// Run update to trigger the copy
91+
await run(appDir, `update android`);
92+
93+
const resourcePath = 'android/capacitor-cordova-android-plugins/src/main/res/xml/automotive_app_desc.xml';
94+
const exists = await FS.exists(resourcePath);
95+
expect(exists).toBe(true);
96+
97+
if (exists) {
98+
const content = await FS.read(resourcePath);
99+
expect(content).toContain('<uses name="media"/>');
100+
}
101+
});
102+
});

0 commit comments

Comments
 (0)