Skip to content

Commit

Permalink
fix: check for main manifest if multiple are present (#2174)
Browse files Browse the repository at this point in the history
* fix: check for main manifest if multiple are present

* chore: add test case

---------

Co-authored-by: Jakub Piasecki <jakub.piasecki@swmansion.com>
  • Loading branch information
WoLewicki and j-piasecki authored Nov 24, 2023
1 parent f24b09d commit ebde072
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
19 changes: 19 additions & 0 deletions packages/cli-platform-android/src/config/__fixtures__/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ const classNameManifest = fs.readFileSync(
path.join(__dirname, './files/AndroidManifest-className.xml'),
);

const customFlavorManifest = fs.readFileSync(
path.join(__dirname, './files/AndroidManifest-custom-flavor.xml'),
);

const mainManifest = fs.readFileSync(
path.join(__dirname, './files/AndroidManifest.xml'),
);

function generateValidFileStructureForLib(classFileName: string) {
return {
'build.gradle': buildGradle,
Expand Down Expand Up @@ -298,3 +306,14 @@ export const className = {
'AndroidManifest.xml': classNameManifest,
},
};

export const customFlavor = {
src: {
e2e: {
'AndroidManifest.xml': customFlavorManifest,
},
main: {
'AndroidManifest.xml': mainManifest,
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ describe('android::getMainActivity', () => {
app: mocks.fewActivities,
},
},
customFlavor: {
android: {
app: mocks.customFlavor,
},
},
});
});

Expand Down Expand Up @@ -53,6 +58,14 @@ describe('android::getMainActivity', () => {
expect(mainActivity).toBe('com.example.ExampleAppActivity');
});

it('returns main activity from main manifest when custom flavors are defined', () => {
const manifestPath = findManifest('/customFlavor');
const mainActivity = getMainActivity(manifestPath || '');
expect(mainActivity).not.toBeNull();
expect(typeof mainActivity).toBe('string');
expect(mainActivity).toBe('.MainActivity');
});

it('returns null if file do not exist', () => {
const fakeManifestPath = findManifest('/empty');
expect(getMainActivity(fakeManifestPath || '')).toBeNull();
Expand Down
15 changes: 12 additions & 3 deletions packages/cli-platform-android/src/config/findManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import glob from 'glob';
import path from 'path';

export default function findManifest(folder: string) {
const manifestPath = glob.sync(path.join('**', 'AndroidManifest.xml'), {
let manifestPaths = glob.sync(path.join('**', 'AndroidManifest.xml'), {
cwd: folder,
ignore: [
'node_modules/**',
Expand All @@ -23,7 +23,16 @@ export default function findManifest(folder: string) {
'**/src/androidTest/**',
'**/src/test/**',
],
})[0];
});
if (manifestPaths.length > 1) {
// if we have more than one manifest, pick the one in the main folder if present
const mainManifest = manifestPaths.filter((manifestPath) =>
manifestPath.includes('src/main/'),
);
if (mainManifest.length === 1) {
manifestPaths = mainManifest;
}
}

return manifestPath ? path.join(folder, manifestPath) : null;
return manifestPaths[0] ? path.join(folder, manifestPaths[0]) : null;
}

0 comments on commit ebde072

Please sign in to comment.