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

Function handleCordovaPluginsGradle

cli/src/android/update.ts:224–304  ·  view source on GitHub ↗
(config: Config, cordovaPlugins: Plugin[])

Source from the content-addressed store, hash-verified

222}
223
224export async function handleCordovaPluginsGradle(config: Config, cordovaPlugins: Plugin[]): Promise<void> {
225 const pluginsGradlePath = join(config.android.cordovaPluginsDirAbs, 'build.gradle');
226 const kotlinNeeded = await kotlinNeededCheck(config, cordovaPlugins);
227 const kotlinVersionString = config.app.extConfig.cordova?.preferences?.GradlePluginKotlinVersion ?? '2.2.20';
228 const frameworksArray: any[] = [];
229 let prefsArray: any[] = [];
230 const applyArray: any[] = [];
231 applyArray.push(`apply from: "cordova.variables.gradle"`);
232 cordovaPlugins.map((p) => {
233 const relativePluginPath = convertToUnixPath(relative(config.android.cordovaPluginsDirAbs, p.rootPath));
234 const frameworks = getPlatformElement(p, platform, 'framework');
235 frameworks.map((framework: any) => {
236 if (!framework.$.type && !framework.$.custom) {
237 frameworksArray.push(framework.$.src);
238 } else if (
239 framework.$.custom &&
240 framework.$.custom === 'true' &&
241 framework.$.type &&
242 framework.$.type === 'gradleReference'
243 ) {
244 applyArray.push(`apply from: "${relativePluginPath}/${framework.$.src}"`);
245 }
246 });
247 prefsArray = prefsArray.concat(getAllElements(p, platform, 'preference'));
248 });
249 let frameworkString = frameworksArray
250 .map((f) => {
251 if (f.startsWith('platform(')) {
252 return ` implementation ${f}`;
253 } else {
254 return ` implementation "${f}"`;
255 }
256 })
257 .join('\n');
258 frameworkString = await replaceFrameworkVariables(config, prefsArray, frameworkString);
259 if (kotlinNeeded) {
260 frameworkString += `\n implementation "androidx.core:core-ktx:$androidxCoreKTXVersion"`;
261 frameworkString += `\n implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"`;
262 }
263 const applyString = applyArray.join('\n');
264 let buildGradle = await readFile(pluginsGradlePath, { encoding: 'utf-8' });
265 buildGradle = buildGradle.replace(
266 /(SUB-PROJECT DEPENDENCIES START)[\s\S]*(\/\/ SUB-PROJECT DEPENDENCIES END)/,
267 '$1\n' + frameworkString.concat('\n') + ' $2',
268 );
269 buildGradle = buildGradle.replace(
270 /(PLUGIN GRADLE EXTENSIONS START)[\s\S]*(\/\/ PLUGIN GRADLE EXTENSIONS END)/,
271 '$1\n' + applyString.concat('\n') + '$2',
272 );
273 if (kotlinNeeded) {
274 buildGradle = buildGradle.replace(
275 /(buildscript\s{\n(\t|\s{4})repositories\s{\n((\t{2}|\s{8}).+\n)+(\t|\s{4})}\n(\t|\s{4})dependencies\s{\n(\t{2}|\s{8}).+)\n((\t|\s{4})}\n}\n)/,
276 `$1\n classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"\n$8`,
277 );
278 buildGradle = buildGradle.replace(
279 /(ext\s{)/,
280 `$1\n androidxCoreKTXVersion = project.hasProperty('androidxCoreKTXVersion') ? rootProject.ext.androidxCoreKTXVersion : '1.8.0'`,
281 );

Callers 1

updateAndroidFunction · 0.85

Calls 6

convertToUnixPathFunction · 0.90
getPlatformElementFunction · 0.90
getAllElementsFunction · 0.90
kotlinNeededCheckFunction · 0.85
readFileFunction · 0.85

Tested by

no test coverage detected