From 561de0d1fff9efdbb2c89e14d0c644fc5fa5e516 Mon Sep 17 00:00:00 2001
From: Benoit Lemaire <lemaireb@gmail.com>
Date: Mon, 3 Jun 2019 16:45:55 -0700
Subject: [PATCH] :sparkles: Add new setBuildSettings directive for iOS plugin
 configuration

---
 .../platform-parts/manifest/native-modules.md | 17 +++++++++++
 ern-core/src/Manifest.ts                      | 21 ++++++++++++++
 ern-core/src/iosUtil.ts                       | 29 +++++++++++++++++++
 3 files changed, 67 insertions(+)

diff --git a/docs/platform-parts/manifest/native-modules.md b/docs/platform-parts/manifest/native-modules.md
index 65080df2c..cedbc9ddb 100644
--- a/docs/platform-parts/manifest/native-modules.md
+++ b/docs/platform-parts/manifest/native-modules.md
@@ -232,4 +232,21 @@ Adds a source file from the plugin project to the container list of sources.
   "path": "ElectrodeReactNativeBridge/ElectrodeObject.swift",
   "group": "ElectrodeReactNativeBridge"
 }
+```
+
+- `setBuildSettings`
+
+Set one or more build setting(s) in one or more pbxproj(s) associated to the plugin.
+For example setting `ENABLE_BITCODE` to `NO` for `Debug` and `Release` configurations of `Foo` plugin project :
+
+```json
+"setBuildSettings": [
+  { 
+    "path": "{{{projectName}}}/Libraries/Foo/Foo.xcodeproj/project.pbxproj",
+    "buildSettings": {
+      "configurations": ["Debug", "Release"],
+      "settings": { "ENABLE_BITCODE": "NO" }
+    }
+  }
+]
 ```
\ No newline at end of file
diff --git a/ern-core/src/Manifest.ts b/ern-core/src/Manifest.ts
index 47f03e3a9..48bd86882 100644
--- a/ern-core/src/Manifest.ts
+++ b/ern-core/src/Manifest.ts
@@ -162,6 +162,10 @@ export interface IosPluginConfig extends CommonPluginConfig {
    * Directives specific to Container pbxproj patching
    */
   pbxproj: PbxProjDirectives
+  /**
+   * Set specific build settings in the plugin pbxproj
+   */
+  setBuildSettings?: IosPluginSetBuildSettingsDirective[]
 }
 
 /**
@@ -329,6 +333,23 @@ export interface IosPluginAddProjectDirective {
   group: string
 }
 
+export interface IosPluginBuildSettings {
+  /**
+   * Configuration(s) for which these build settings apply.
+   * For example [ 'Debug' , 'Release' ]
+   */
+  configurations: string[]
+
+  /**
+   * Build settings as key values pairs
+   */
+  settings: { [key: string]: string }
+}
+export interface IosPluginSetBuildSettingsDirective {
+  path: string
+  buildSettings: IosPluginBuildSettings[] | IosPluginBuildSettings
+}
+
 /**
  * iOS Static library representation
  */
diff --git a/ern-core/src/iosUtil.ts b/ern-core/src/iosUtil.ts
index 094c422db..a94a81660 100644
--- a/ern-core/src/iosUtil.ts
+++ b/ern-core/src/iosUtil.ts
@@ -128,6 +128,35 @@ export async function fillProjectHull(
             }
           }
 
+          if (pluginConfig.ios.setBuildSettings) {
+            for (const s of pluginConfig.ios.setBuildSettings) {
+              const pathToPbxProj = path.join(pathSpec.outputDir, s.path)
+              // Add any missing section in the target pbxproj
+              // This is necessary for proper parsing and modification of
+              // the pbxproj with the xcode-ern library
+              xcode
+                .pbxProjFileUtils()
+                .addMissingSectionsToPbxProj(pathToPbxProj)
+              const iosProj = await getIosProject(projectPath)
+              const buildSettings =
+                s.buildSettings instanceof Array
+                  ? s.buildSettings
+                  : [s.buildSettings]
+              for (const buildSettingsEntry of buildSettings) {
+                for (const buildType of buildSettingsEntry.configurations) {
+                  for (const key of Object.keys(buildSettingsEntry.settings)) {
+                    iosProj.updateBuildProperty(
+                      key,
+                      buildSettingsEntry.settings[key],
+                      buildType
+                    )
+                  }
+                }
+              }
+              fs.writeFileSync(pathToPbxProj, iosProj.writeSync())
+            }
+          }
+
           if (pluginConfig.ios.pbxproj) {
             if (pluginConfig.ios.pbxproj.addSource) {
               for (const source of pluginConfig.ios.pbxproj.addSource) {