Skip to content

Commit

Permalink
fix(#48): extracting package name regex (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz1913 authored Oct 3, 2023
1 parent 3e87112 commit fe6ba90
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
34 changes: 33 additions & 1 deletion src/android-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ANDROID_GLOB_OPTIONS = {
nodir: false,
};

const NAMESPACE_VALUE_REGEX = /namespace (= ){0,1}"([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*"/;
const NAMESPACE_VALUE_REGEX = /namespace (= ){0,1}("|')([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*("|')/;
const PACKAGE_NAME_REGEX = /([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*/;
const PACKAGE_ATTRIBUTE_REGEX = /package="(.+?)"/;

Expand Down Expand Up @@ -69,6 +69,38 @@ export const extractPackageName = (workingDir: string, manifestPath: string) =>
return packageNameMatchArray[1] as string;
};

export const displayExtractPackageNameErrorMessage = () => {
console.error(kleur.red(`
Cannot extract package from gradle scripts or manifest
Make sure either:
1. your application/library's "build.gradle" file has "namespace" value assigned e.g.
android {
ndkVersion rootProject.ext.ndkVersion
compileSdkVersion rootProject.ext.compileSdkVersion
// in Groovy build.gradle you can use single or double quoted strings
namespace "com.myawesomeapp" // <--- add this
// namespace 'com.myawesomeapp' // <--- or this
// in Kotlin build.gradle.kts you must use double quotes string and "=" char
// namespace = "com.myawesomeapp" // <--- add this
// ...
}
2. your application/library's "AndroidManifest.xml" has a package attribute assigned e.g.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myawesomeapp"> // <--- add this
</manifest>
`));
};

/**
* Helper that suggests absolute path to project's/library's android manifest file
*/
Expand Down
7 changes: 5 additions & 2 deletions src/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createAndroidPluginDirectory,
createPluginFile,
createPluginPackageFile,
displayExtractPackageNameErrorMessage,
displayFinishStepsForAndroidApplicationPlugin,
displayFinishStepsForAndroidLibraryPlugin,
extractPackageName,
Expand Down Expand Up @@ -93,10 +94,11 @@ export async function androidCommandHandler(argv: Arguments<unknown>) {
spinner.text = 'Extracting android package name';
spinner.start();

const packageName = extractPackageName(process.cwd(), manifestPath);
const packageName = extractPackageName(path.resolve(manifestPath, '..', '..'), manifestPath);

if (!packageName) {
console.error(kleur.red('\nCannot extract package from gradle scripts or manifest\n'));
spinner.fail();
displayExtractPackageNameErrorMessage();
return;
}

Expand All @@ -108,6 +110,7 @@ export async function androidCommandHandler(argv: Arguments<unknown>) {
const sourceDir = getSourceSetDirectory(manifestPath, packageName);

if (!sourceDir) {
spinner.fail();
console.error(kleur.red(`\nCannot find main source set at ${sourceDir}\n`));
return;
}
Expand Down

0 comments on commit fe6ba90

Please sign in to comment.