From 6392b00064cb881fbf3db29646100b36edbd449c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Chmiela?= Date: Fri, 29 Nov 2019 11:24:45 +0100 Subject: [PATCH 01/10] feat: replace {{ packageName }} in deps' packageImportPath --- packages/platform-android/native_modules.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index b82f32541..83a348d96 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -126,9 +126,9 @@ class ReactNativeModules { String packageClassInstances = "" if (packages.size() > 0) { - packageImports = "import ${packageName}.BuildConfig;\nimport ${packageName}.R;\n\n" packageImports = packageImports + packages.collect { - "// ${it.name}\n${it.packageImportPath}" + String interpolatedPackageImportPath = it.packageImportPath.replace("{{ packageName }}", packageName) + "// ${it.name}\n${interpolatedPackageImportPath}" }.join('\n') packageClassInstances = ",\n " + packages.collect { it.packageInstance }.join(",\n ") } From 4ff2319414af31fb7340c16e97a93a662d6dc979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Chmiela?= Date: Fri, 29 Nov 2019 11:39:50 +0100 Subject: [PATCH 02/10] chore: extract interpolating to a separate function --- packages/platform-android/native_modules.gradle | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index 83a348d96..6f611d616 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -126,9 +126,11 @@ class ReactNativeModules { String packageClassInstances = "" if (packages.size() > 0) { + def interpolateDynamicValues = { + it.replace("{{ packageName }}", packageName) + } packageImports = packageImports + packages.collect { - String interpolatedPackageImportPath = it.packageImportPath.replace("{{ packageName }}", packageName) - "// ${it.name}\n${interpolatedPackageImportPath}" + "// ${it.name}\n${interpolateDynamicValues(it.packageImportPath)}" }.join('\n') packageClassInstances = ",\n " + packages.collect { it.packageInstance }.join(",\n ") } From 2c64a99aae5598f7e395bca7308409284d25645a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Chmiela?= Date: Fri, 29 Nov 2019 11:39:58 +0100 Subject: [PATCH 03/10] feat: add support for templating in packageInstance too --- packages/platform-android/native_modules.gradle | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index 6f611d616..3e51d5bb9 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -132,7 +132,9 @@ class ReactNativeModules { packageImports = packageImports + packages.collect { "// ${it.name}\n${interpolateDynamicValues(it.packageImportPath)}" }.join('\n') - packageClassInstances = ",\n " + packages.collect { it.packageInstance }.join(",\n ") + packageClassInstances = ",\n " + packages.collect { + interpolateDynamicValues(it.packageInstance) + }.join(",\n ") } String generatedFileContents = generatedFileContentsTemplate From 5aa2c0cea9c130679d0d6c6a1688ab241d1da7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Chmiela?= Date: Fri, 29 Nov 2019 12:30:43 +0100 Subject: [PATCH 04/10] docs: add paragraphs about interpolation --- docs/dependencies.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/dependencies.md b/docs/dependencies.md index 4bd2d35f6..7119683e7 100644 --- a/docs/dependencies.md +++ b/docs/dependencies.md @@ -91,7 +91,7 @@ An array of iOS script phases to add to the project. Specifying a `path` propert module.exports = { dependency: { platforms: { - ios: { + ios: { scriptPhases: [ { name: '[MY DEPENDENCY] My Script', @@ -119,10 +119,16 @@ Path to a custom `AndroidManifest.xml` Custom package import. For example: `import com.acme.AwesomePackage;`. +`{{ packageName }}` will be replaced with the package of `MainApplication`, so you can eg. +import application's `BuildConfig` class by writing `import {{ packageName }}.BuildConfig;`. + #### platforms.android.packageInstance Custom syntax to instantiate a package. By default, it's a `new AwesomePackage()`. It can be useful when your package requires additional arguments while initializing. +`{{ packageName }}` will be replaced with the package of `MainApplication`, so you can eg. +import application's `R` class by writing `new AwesomePackage({{ packageName }}.R.string.key)`. + For settings applicable on other platforms, please consult their respective documentation. ### assets From ba749c4b346b2269e7a06bc0ef60907594d9f879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Chmiela?= Date: Fri, 29 Nov 2019 15:41:47 +0100 Subject: [PATCH 05/10] Update packages/platform-android/native_modules.gradle --- packages/platform-android/native_modules.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index 3e51d5bb9..7545d3e7b 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -129,7 +129,7 @@ class ReactNativeModules { def interpolateDynamicValues = { it.replace("{{ packageName }}", packageName) } - packageImports = packageImports + packages.collect { + packageImports = packages.collect { "// ${it.name}\n${interpolateDynamicValues(it.packageImportPath)}" }.join('\n') packageClassInstances = ",\n " + packages.collect { From 7c85b648aa546a1e7f472490800cc9acdc2d3e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Chmiela?= Date: Fri, 29 Nov 2019 15:42:56 +0100 Subject: [PATCH 06/10] Fix GH whitespace changes --- packages/platform-android/native_modules.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index 7545d3e7b..db11cbe10 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -129,7 +129,7 @@ class ReactNativeModules { def interpolateDynamicValues = { it.replace("{{ packageName }}", packageName) } - packageImports = packages.collect { + packageImports = packages.collect { "// ${it.name}\n${interpolateDynamicValues(it.packageImportPath)}" }.join('\n') packageClassInstances = ",\n " + packages.collect { From d433e87791efbd2abbe8ca558d8f5d417e094892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Chmiela?= Date: Mon, 3 Feb 2020 11:45:24 +0100 Subject: [PATCH 07/10] Make the change backwards-compatible Libraries already expect to be able to use R and BuildConfig classes without having to worry about their packages. To match the expectations the script will replace un-fully-qualified references to fully-qualified. --- .../platform-android/native_modules.gradle | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index db11cbe10..674503124 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -127,7 +127,29 @@ class ReactNativeModules { if (packages.size() > 0) { def interpolateDynamicValues = { - it.replace("{{ packageName }}", packageName) + it + // Before adding the {{ packageName }} replacement mechanism, + // BuildConfig and R classes were imported automatically + // into the scope of the file. In order to be backwards-compatible + // let's prefix all non-FQDN references to those classes with + // the package name. + // + // We want to match: + // - new Package(R.string…), + // - Module.configure(BuildConfig); + // ^ hence including (BuildConfig|R) + // but we don't want to match: + // - new Package(getResources…), + // - new PackageR…, + // - new Royal…, + // ^ hence excluding \w before and after matches + // - Module.configure({{ packageName }}.BuildConfig); + // ^ hence excluding . before the match. + .replaceAll(~/([^.\w])(BuildConfig|R)([^\w])/, { + wholeString, prefix, className, suffix -> + "${prefix}{{ packageName }}.${className}${suffix}" + }) + .replace("{{ packageName }}", packageName) } packageImports = packages.collect { "// ${it.name}\n${interpolateDynamicValues(it.packageImportPath)}" From 9553fd3066b517a878921a0ad6e0c300b05021a3 Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Thu, 19 Mar 2020 17:46:29 +0100 Subject: [PATCH 08/10] Update dependencies.md --- docs/dependencies.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/docs/dependencies.md b/docs/dependencies.md index 7119683e7..ef4b9d98a 100644 --- a/docs/dependencies.md +++ b/docs/dependencies.md @@ -119,16 +119,10 @@ Path to a custom `AndroidManifest.xml` Custom package import. For example: `import com.acme.AwesomePackage;`. -`{{ packageName }}` will be replaced with the package of `MainApplication`, so you can eg. -import application's `BuildConfig` class by writing `import {{ packageName }}.BuildConfig;`. - #### platforms.android.packageInstance Custom syntax to instantiate a package. By default, it's a `new AwesomePackage()`. It can be useful when your package requires additional arguments while initializing. -`{{ packageName }}` will be replaced with the package of `MainApplication`, so you can eg. -import application's `R` class by writing `new AwesomePackage({{ packageName }}.R.string.key)`. - For settings applicable on other platforms, please consult their respective documentation. ### assets From b8594327871e50070d12f36d5f53537c46c29971 Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Thu, 19 Mar 2020 17:48:25 +0100 Subject: [PATCH 09/10] Update native_modules.gradle --- packages/platform-android/native_modules.gradle | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index 674503124..fd0a279aa 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -134,22 +134,22 @@ class ReactNativeModules { // let's prefix all non-FQDN references to those classes with // the package name. // - // We want to match: + // We want to match "R" or "BuildConfig": // - new Package(R.string…), // - Module.configure(BuildConfig); // ^ hence including (BuildConfig|R) - // but we don't want to match: + // but we don't want to match "R": // - new Package(getResources…), // - new PackageR…, // - new Royal…, // ^ hence excluding \w before and after matches - // - Module.configure({{ packageName }}.BuildConfig); + // and "BuildConfig" that has FQDN reference: + // - Module.configure(com.acme.BuildConfig); // ^ hence excluding . before the match. .replaceAll(~/([^.\w])(BuildConfig|R)([^\w])/, { wholeString, prefix, className, suffix -> - "${prefix}{{ packageName }}.${className}${suffix}" + "${prefix}packageName.${className}${suffix}" }) - .replace("{{ packageName }}", packageName) } packageImports = packages.collect { "// ${it.name}\n${interpolateDynamicValues(it.packageImportPath)}" From ef34d8212ff686e8398b6093e82500f40ba1abc6 Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Thu, 19 Mar 2020 17:51:13 +0100 Subject: [PATCH 10/10] Update native_modules.gradle --- packages/platform-android/native_modules.gradle | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index fd0a279aa..97ef1d9dd 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -128,11 +128,11 @@ class ReactNativeModules { if (packages.size() > 0) { def interpolateDynamicValues = { it - // Before adding the {{ packageName }} replacement mechanism, + // Before adding the package replacement mechanism, // BuildConfig and R classes were imported automatically - // into the scope of the file. In order to be backwards-compatible - // let's prefix all non-FQDN references to those classes with - // the package name. + // into the scope of the file. We want to replace all + // non-FQDN references to those classes with the package name + // of the MainApplication. // // We want to match "R" or "BuildConfig": // - new Package(R.string…), @@ -148,7 +148,7 @@ class ReactNativeModules { // ^ hence excluding . before the match. .replaceAll(~/([^.\w])(BuildConfig|R)([^\w])/, { wholeString, prefix, className, suffix -> - "${prefix}packageName.${className}${suffix}" + "${prefix}${packageName}.${className}${suffix}" }) } packageImports = packages.collect {