From 6cb7eacefb7143bff6e8c8b24ee475ccfbcbcc56 Mon Sep 17 00:00:00 2001
From: bielikb <h3sperian@gmail.com>
Date: Mon, 21 Sep 2020 16:40:16 +0200
Subject: [PATCH] v1.1.0

---
 README.md                                     |  6 +-
 .../actions/create_xcframework_action.rb      | 55 +++++++++++++++----
 .../helper/create_xcframework_helper.rb       | 10 ++--
 .../plugin/create_xcframework/version.rb      |  2 +-
 4 files changed, 53 insertions(+), 20 deletions(-)

diff --git a/README.md b/README.md
index 08b90a0..8378791 100644
--- a/README.md
+++ b/README.md
@@ -26,13 +26,12 @@ create_xcframework(
     workspace: 'path/to/your.xcworkspace',
     scheme: 'framework scheme',
     product_name: 'Sample', # optional if scheme doesnt match the name of your framework
-    include_bitcode: true,
-    destinations: ['iOS', 'maccatalyst'], # 
+    destinations: ['iOS', 'maccatalyst'],
     xcframework_output_directory: 'path/to/your/output dir'
 )
 ```
 
-Run 
+Run
 ```bash
 $ fastlane actions create_xcframework
 ```
@@ -48,7 +47,6 @@ to learn more about the plugin.
 * carPlayOS
 * macOS
 
-
 ## Output
 
 #### Files:
diff --git a/lib/fastlane/plugin/create_xcframework/actions/create_xcframework_action.rb b/lib/fastlane/plugin/create_xcframework/actions/create_xcframework_action.rb
index a4d2be1..91896ed 100644
--- a/lib/fastlane/plugin/create_xcframework/actions/create_xcframework_action.rb
+++ b/lib/fastlane/plugin/create_xcframework/actions/create_xcframework_action.rb
@@ -25,7 +25,7 @@ def self.run(params)
             XcarchiveAction.run(params)
           end
 
-          create_xcframework
+          create_xcframework(params)
 
           copy_dSYMs(params)
 
@@ -52,17 +52,41 @@ def self.clean
         @xchelper.xcarchive_frameworks_path.each { |framework| FileUtils.rm_rf(framework.split('/').first) }
       end
 
-      def self.create_xcframework
+      def self.create_xcframework(params)
         xcframework = @xchelper.xcframework_path
         begin
           FileUtils.rm_rf(xcframework) if File.exist?(xcframework)
-          framework_links = @xchelper.xcarchive_frameworks_path.map { |path| "-framework #{path}" }.join(' ')
-          Actions.sh("set -o pipefail && xcodebuild -create-xcframework #{framework_links} -output #{xcframework}")
+          framework_links = params[:destinations].each_with_index.map do |_, index| 
+            "-framework #{@xchelper.xcarchive_framework_path(index)} #{debug_symbols(index: index, params: params)}"
+          end
+
+          Actions.sh("set -o pipefail && xcodebuild -create-xcframework #{framework_links.join(' ')} -output #{xcframework}")
         rescue => err
           UI.user_error!(err)
         end
       end
 
+      def self.debug_symbols(index:, params:)
+        return "" unless Helper.xcode_at_least?('12.0.0')
+
+        debug_symbols = []
+
+        # Include dSYMs in xcframework
+        if params[:include_dSYMs] != false
+          debug_symbols << "-debug-symbols #{@xchelper.xcarchive_dSYMs_path(index)}/#{@xchelper.framework}.dSYM"
+        end
+
+        # Include BCSymbols in xcframework
+        if params[:include_BCSymbolMaps] != false && params[:include_bitcode] != false
+          bc_symbols_dir = @xchelper.xcarchive_BCSymbolMaps_path(index)
+          if Dir.exist?(bc_symbols_dir) 
+            debug_symbols << Dir.children(bc_symbols_dir).map { |path| "-debug-symbols #{File.expand_path("#{bc_symbols_dir}/#{path}")}" }.join(' ') 
+          end
+        end
+
+        debug_symbols.join(' ')
+      end
+
       def self.copy_dSYMs(params)
         dSYMs_output_dir = @xchelper.xcframework_dSYMs_path
         FileUtils.mkdir_p(dSYMs_output_dir)
@@ -79,7 +103,7 @@ def self.copy_dSYMs(params)
       end
 
       def self.copy_BCSymbolMaps(params)
-        return unless params[:include_bitcode]
+        return if params[:include_bitcode] == false
 
         symbols_output_dir = @xchelper.xcframework_BCSymbolMaps_path
         FileUtils.mkdir_p(symbols_output_dir)
@@ -109,7 +133,7 @@ def self.update_xcargs(params)
         end
         xcargs = ['SKIP_INSTALL=NO', 'BUILD_LIBRARY_FOR_DISTRIBUTION=YES']
 
-        if params[:include_bitcode]
+        if params[:include_bitcode] != false
           params[:xcargs].gsub!(/ENABLE_BITCODE(=|\s+)(YES|NO)/, '') if params[:xcargs]
           xcargs << ['OTHER_CFLAGS="-fembed-bitcode"', 'BITCODE_GENERATION_MODE="bitcode"', 'ENABLE_BITCODE=YES']
         end
@@ -153,7 +177,6 @@ def self.example_code
           create_xcframework(
             workspace: 'path/to/your.xcworkspace',
             scheme: 'framework scheme',
-            include_bitcode: true,
             destinations: ['iOS'],
             xcframework_output_directory: 'output_directory'
           )
@@ -173,7 +196,7 @@ def self.authors
       end
 
       def self.details
-        "Create xcframework plugin generates xcframework for specified destinations. The output of this action consists of the xcframework itself, dSYM and BCSymbolMaps, if bitcode is enabled."
+        "Create xcframework plugin generates xcframework for specified destinations. The output of this action consists of the xcframework itself, which contains dSYM and BCSymbolMaps, if bitcode is enabled."
       end
 
       def self.available_options
@@ -185,10 +208,10 @@ def self.available_options
           ),
           FastlaneCore::ConfigItem.new(
             key: :include_bitcode,
-            description: "Should the xcframework include bitcode?",
+            description: "Should the project be built with bitcode enabled?",
             optional: true,
             is_string: false,
-            default_value: false
+            default_value: true
           ),
           FastlaneCore::ConfigItem.new(
             key: :destinations,
@@ -202,6 +225,18 @@ def self.available_options
             description: "The directory in which the xcframework should be stored in",
             optional: true
           ),
+          FastlaneCore::ConfigItem.new(
+            key: :include_dSYMs,
+            description: "Includes dSYM files in the xcframework",
+            optional: true,
+            default_value: true
+          ),
+          FastlaneCore::ConfigItem.new(
+            key: :include_BCSymbolMaps,
+            description: "Includes BCSymbolMap files in the xcframework",
+            optional: true,
+            default_value: true
+          ),
           FastlaneCore::ConfigItem.new(
             key: :product_name,
             description: "The name of your module. Optional if equals to :scheme. Equivalent to CFBundleName",
diff --git a/lib/fastlane/plugin/create_xcframework/helper/create_xcframework_helper.rb b/lib/fastlane/plugin/create_xcframework/helper/create_xcframework_helper.rb
index 04a26cb..52a783d 100644
--- a/lib/fastlane/plugin/create_xcframework/helper/create_xcframework_helper.rb
+++ b/lib/fastlane/plugin/create_xcframework/helper/create_xcframework_helper.rb
@@ -33,23 +33,23 @@ def xcarchive_frameworks_path
       end
 
       def xcarchive_dSYMs_path(framework_index)
-        "#{xcarchive_path(framework_index)}/dSYMS"
+        File.expand_path("#{xcarchive_path(framework_index)}/dSYMS")
       end
 
       def xcframework_dSYMs_path
-        "#{output_directory}/#{product_name}.dSYMs"
+        File.expand_path("#{output_directory}/#{product_name}.dSYMs")
       end
 
       def xcarchive_BCSymbolMaps_path(framework_index)
-        "#{xcarchive_path(framework_index)}/BCSymbolMaps"
+        File.expand_path("#{xcarchive_path(framework_index)}/BCSymbolMaps")
       end
 
       def xcframework_BCSymbolMaps_path
-        "#{output_directory}/#{product_name}.BCSymbolMaps"
+        File.expand_path("#{output_directory}/#{product_name}.BCSymbolMaps")
       end
 
       def xcframework_path
-        "#{output_directory}/#{xcframework}"
+        File.expand_path("#{output_directory}/#{xcframework}")
       end
 
       def output_directory
diff --git a/lib/fastlane/plugin/create_xcframework/version.rb b/lib/fastlane/plugin/create_xcframework/version.rb
index 6369422..6309cc2 100644
--- a/lib/fastlane/plugin/create_xcframework/version.rb
+++ b/lib/fastlane/plugin/create_xcframework/version.rb
@@ -1,5 +1,5 @@
 module Fastlane
   module CreateXcframework
-    VERSION = "1.0.0"
+    VERSION = "1.1.0"
   end
 end