MCPcopy
hub / github.com/ionic-team/capacitor / findAndroidPluginClassesInPlugin

Function findAndroidPluginClassesInPlugin

cli/src/android/update.ts:84–131  ·  view source on GitHub ↗
(plugin: Plugin)

Source from the content-addressed store, hash-verified

82}
83
84async function findAndroidPluginClassesInPlugin(plugin: Plugin): Promise<PluginsJsonEntry[]> {
85 if (!plugin.android || getPluginType(plugin, platform) !== PluginType.Core) {
86 return [];
87 }
88
89 const srcPath = resolve(plugin.rootPath, plugin.android.path, 'src/main');
90 const srcFiles = await readdirp(srcPath, {
91 filter: (entry) => !entry.stats.isDirectory() && ['.java', '.kt'].includes(extname(entry.path)),
92 });
93
94 const classRegex = /^@(?:CapacitorPlugin|NativePlugin)[\s\S]+?class ([\w]+)/gm;
95 const packageRegex = /^package ([\w.]+);?$/gm;
96
97 debug('Searching %O source files in %O by %O regex', srcFiles.length, srcPath, classRegex);
98
99 const entries = await Promise.all(
100 srcFiles.map(async (srcFile): Promise<PluginsJsonEntry | undefined> => {
101 const srcFileContents = await readFile(srcFile, { encoding: 'utf-8' });
102 classRegex.lastIndex = 0;
103 const classMatch = classRegex.exec(srcFileContents);
104
105 if (classMatch) {
106 const className = classMatch[1];
107
108 debug('Searching %O for package by %O regex', srcFile, packageRegex);
109
110 packageRegex.lastIndex = 0;
111 const packageMatch = packageRegex.exec(srcFileContents.substring(0, classMatch.index));
112
113 if (!packageMatch) {
114 fatal(`Package could not be parsed from Android plugin.\n` + `Location: ${c.strong(srcFile)}`);
115 }
116
117 const packageName = packageMatch[1];
118 const classpath = `${packageName}.${className}`;
119
120 debug('%O is a suitable plugin class', classpath);
121
122 return {
123 pkg: plugin.id,
124 classpath,
125 };
126 }
127 }),
128 );
129
130 return entries.filter((entry): entry is PluginsJsonEntry => !!entry);
131}
132
133export async function installGradlePlugins(
134 config: Config,

Callers 1

findAndroidPluginClassesFunction · 0.85

Calls 4

getPluginTypeFunction · 0.90
readdirpFunction · 0.90
fatalFunction · 0.90
readFileFunction · 0.85

Tested by

no test coverage detected