diff --git a/src/android-utils.ts b/src/android-utils.ts index f154562..a39cfb2 100644 --- a/src/android-utils.ts +++ b/src/android-utils.ts @@ -305,7 +305,7 @@ export const createPluginPackageFile = ( spinner.succeed(); }; -export const displayFinishStepsForAndroidApplicationPlugin = (pluginName: string) => { +const displayFinishStepsForAndroidApplicationPlugin = (pluginName: string) => { console.log(kleur.gray(`${kleur.yellow(`Finish Android setup with registering ${pluginName + 'PluginPackage'} in getPackages method, in your MainApplication.(java|kt)`)} // MainApplication.(java|kt) @@ -319,7 +319,7 @@ protected List getPackages() { }`.trim())); }; -export const displayFinishStepsForAndroidLibraryPlugin = () => { +const displayFinishStepsForAndroidLibraryPlugin = () => { console.log(kleur.gray(`${kleur.yellow(`Finish setup for your Android library with adding "VisionCamera" dependency in your library's "build.gradle" file:`)} // build.gradle @@ -332,3 +332,13 @@ dependencies { ${kleur.green('api project(":react-native-vision-camera") // <--- add this')} }`.trim())); }; + +export const printFinishSetupForAndroid = (manifestPath: string, pluginName: string) => { + if (isManifestForApplication(manifestPath)) { + displayFinishStepsForAndroidApplicationPlugin(pluginName); + } else { + displayFinishStepsForAndroidLibraryPlugin(); + } + + console.log('\n'); +}; diff --git a/src/android.ts b/src/android.ts index f495c92..f9ace1d 100644 --- a/src/android.ts +++ b/src/android.ts @@ -10,11 +10,9 @@ import { createPluginFile, createPluginPackageFile, displayExtractPackageNameErrorMessage, - displayFinishStepsForAndroidApplicationPlugin, - displayFinishStepsForAndroidLibraryPlugin, extractPackageName, getSourceSetDirectory, - isManifestForApplication, + printFinishSetupForAndroid, suggestAndroidManifest, } from './android-utils'; import { getPromptResponse, printFinishSetup, spinner } from './common'; @@ -138,12 +136,7 @@ export async function androidCommandHandler(argv: Arguments) { spinner.stop(); console.log('\n'); - if (isManifestForApplication(manifestPath)) { - displayFinishStepsForAndroidApplicationPlugin(pluginName); - } else { - displayFinishStepsForAndroidLibraryPlugin(); - } + printFinishSetupForAndroid(manifestPath, pluginName); - console.log('\n'); printFinishSetup(methodName); } diff --git a/test/android-utils.spec.ts b/test/android-utils.spec.ts new file mode 100644 index 0000000..c2f8a1e --- /dev/null +++ b/test/android-utils.spec.ts @@ -0,0 +1,100 @@ +import { afterEach, describe, expect, test } from '@jest/globals'; +import mockFS from 'mock-fs'; + +import { extractPackageName } from '../src/android-utils'; + +const PACKAGE_NAME = 'com.myawesomeapp'; +const PROJECT_FILE_PATH = '/path/to/project'; +const BUILD_GRADLE_FILE = ` +android { + ndkVersion rootProject.ext.ndkVersion + + compileSdk rootProject.ext.compileSdkVersion + + namespace "${PACKAGE_NAME}" + defaultConfig { + applicationId "com.helloworld" + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion + versionCode 1 + versionName "1.0" + } +} +`; +const BUILD_GRADLE_KTS_FILE = ` +android { + val safeExtGet: (prop: String, fallback: Any) -> Any? by project.extra + + compileSdk = safeExtGet("compileSdkVersion", 33) as Int? + + namespace = "${PACKAGE_NAME}" + + defaultConfig { + minSdk = safeExtGet("minSdkVersion", 21) as Int? + targetSdk = safeExtGet("targetSdkVersion", 33) as Int? + buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()) + } +} +`; +const ANDROID_MANIFEST_FILE = ` + + +`; + +afterEach(() => { + mockFS.restore(); +}); + +describe('extractPackageName', () => { + test('should retrieve package name from namespace value in build.gradle', () => { + mockFS({ + [PROJECT_FILE_PATH]: { + android: { + app: { + 'build.gradle': BUILD_GRADLE_FILE, + }, + }, + }, + }); + expect( + extractPackageName( + `${PROJECT_FILE_PATH}/android/app`, + `${PROJECT_FILE_PATH}/android/app/src/main/AndroidManifest.xml` + ) + ).toBe(PACKAGE_NAME); + }); + test('should retrieve package name from namespace value in build.gradle.kts', () => { + mockFS({ + [PROJECT_FILE_PATH]: { + android: { + app: { + 'build.gradle.kts': BUILD_GRADLE_KTS_FILE, + }, + }, + }, + }); + expect( + extractPackageName( + `${PROJECT_FILE_PATH}/android/app`, + `${PROJECT_FILE_PATH}/android/app/src/main/AndroidManifest.xml` + ) + ).toBe(PACKAGE_NAME); + }); + test('should retrieve package name from package attribute in AndroidManifest.xml', () => { + mockFS({ + [PROJECT_FILE_PATH]: { + android: { + app: { + src: { + main: { + 'AndroidManifest.xml': ANDROID_MANIFEST_FILE, + }, + }, + }, + }, + }, + }); + extractPackageName(`${PROJECT_FILE_PATH}/android/app`, `${PROJECT_FILE_PATH}/android/app/src/main/AndroidManifest.xml`); + }); +});