Skip to content

Commit

Permalink
fix(cli): Move package to build.gradle in Capacitor plugins (#6569)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed May 9, 2023
1 parent 56b0037 commit 8cb26cd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cli/src/android/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from '../plugin';
import type { Plugin } from '../plugin';
import { copy as copyTask } from '../tasks/copy';
import { patchOldCapacitorPlugins } from '../tasks/migrate';
import { convertToUnixPath } from '../util/fs';
import { resolveNode } from '../util/node';
import { extractTemplate } from '../util/template';
Expand All @@ -54,6 +55,7 @@ export async function updateAndroid(config: Config): Promise<void> {
const cordovaPlugins = plugins.filter(
p => getPluginType(p, platform) === PluginType.Cordova,
);
await patchOldCapacitorPlugins(config);
if (cordovaPlugins.length > 0) {
await copyPluginsNativeFiles(config, cordovaPlugins);
}
Expand Down
58 changes: 49 additions & 9 deletions cli/src/tasks/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {
import { join } from 'path';
import rimraf from 'rimraf';

import { getAndroidPlugins } from '../android/common';
import c from '../colors';
import { getCoreVersion, runTask, checkJDKMajorVersion } from '../common';
import type { Config } from '../definitions';
import { fatal } from '../errors';
import { logger, logPrompt, logSuccess } from '../log';
import { getPlugins } from '../plugin';
import { deleteFolderRecursive } from '../util/fs';
import { runCommand, getCommandOutput } from '../util/subprocess';
import { extractTemplate } from '../util/template';
Expand Down Expand Up @@ -312,6 +314,13 @@ export async function migrateCommand(
});

rimraf.sync(join(config.android.appDirAbs, 'build'));

await runTask(
'Migrating package from Manifest to build.gradle in Capacitor plugins',
() => {
return patchOldCapacitorPlugins(config);
},
);
}

// Run Cap Sync
Expand Down Expand Up @@ -662,24 +671,19 @@ async function movePackageFromManifestToBuildGradle(
}

let packageName: string;
const manifestRegEx = new RegExp(/<manifest ([^>]*package="(.+)"[^>]*)>/);
const manifestRegEx = new RegExp(/package="(.+)"/);
const manifestResults = manifestRegEx.exec(manifestText);

if (manifestResults === null) {
logger.error(`Unable to update Android Manifest. Missing <activity> tag`);
logger.error(`Unable to update Android Manifest. Package not found.`);
return;
} else {
packageName = manifestResults[2];
packageName = manifestResults[1];
}

let manifestReplaced = manifestText;

manifestReplaced = setAllStringIn(
manifestText,
'<manifest xmlns:android="http://schemas.android.com/apk/res/android"',
'>',
``,
);
manifestReplaced = manifestReplaced.replace(manifestRegEx, '');

if (manifestText == manifestReplaced) {
logger.error(
Expand Down Expand Up @@ -827,3 +831,39 @@ function setAllStringIn(
}
return result;
}

export async function patchOldCapacitorPlugins(
config: Config,
): Promise<void[]> {
const allPlugins = await getPlugins(config, 'android');
const androidPlugins = await getAndroidPlugins(allPlugins);
return await Promise.all(
androidPlugins.map(async p => {
if (p.manifest?.android?.src) {
const buildGradlePath = join(
config.app.rootDir,
'node_modules',
p.id,
p.manifest.android.src,
'build.gradle',
);
const manifestPath = join(
config.app.rootDir,
'node_modules',
p.id,
p.manifest.android.src,
'src',
'main',
'AndroidManifest.xml',
);
const gradleContent = readFile(buildGradlePath);
if (!gradleContent?.includes('namespace')) {
logger.warn(
`${p.id} doesn't officially support Capacitor ${coreVersion} yet, doing our best moving it's package to build.gradle so it builds`,
);
movePackageFromManifestToBuildGradle(manifestPath, buildGradlePath);
}
}
}),
);
}

0 comments on commit 8cb26cd

Please sign in to comment.