Search code examples
rubyxcodeflutteronesignalpodfile

Removing linked Pods_framework in post_install Podfile hook


I currently have the following situation in my flutter iOS/Android development process:

Every time flutter build runs it executes pod install which installs the regular Flutter Podfile

# Uncomment this line to define a global platform for your project
platform :ios, '10.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'false'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def parse_KV_file(file, separator='=')
  file_abs_path = File.expand_path(file)
  if !File.exists? file_abs_path
    return [];
  end
  generated_key_values = {}
  skip_line_start_symbols = ["#", "/"]
  File.foreach(file_abs_path) do |line|
    next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
    plugin = line.split(pattern=separator)
    if plugin.length == 2
      podname = plugin[0].strip()
      path = plugin[1].strip()
      podpath = File.expand_path("#{path}", file_abs_path)
      generated_key_values[podname] = podpath
    else
      puts "Invalid plugin specification: #{line}"
    end
  end
  generated_key_values
end

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  # Flutter Pod

  copied_flutter_dir = File.join(__dir__, 'Flutter')
  copied_framework_path = File.join(copied_flutter_dir, 'Flutter.framework')
  copied_podspec_path = File.join(copied_flutter_dir, 'Flutter.podspec')
  unless File.exist?(copied_framework_path) && File.exist?(copied_podspec_path)
    # Copy Flutter.framework and Flutter.podspec to Flutter/ to have something to link against if the xcode backend script has not run yet.
    # That script will copy the correct debug/profile/release version of the framework based on the currently selected Xcode configuration.
    # CocoaPods will not embed the framework on pod install (before any build phases can generate) if the dylib does not exist.

    generated_xcode_build_settings_path = File.join(copied_flutter_dir, 'Generated.xcconfig')
    unless File.exist?(generated_xcode_build_settings_path)
      raise "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter pub get is executed first"
    end
    generated_xcode_build_settings = parse_KV_file(generated_xcode_build_settings_path)
    cached_framework_dir = generated_xcode_build_settings['FLUTTER_FRAMEWORK_DIR'];

    unless File.exist?(copied_framework_path)
      FileUtils.cp_r(File.join(cached_framework_dir, 'Flutter.framework'), copied_flutter_dir)
    end
    unless File.exist?(copied_podspec_path)
      FileUtils.cp(File.join(cached_framework_dir, 'Flutter.podspec'), copied_flutter_dir)
    end
  end

  # Keep pod path relative so it can be checked into Podfile.lock.
  pod 'Flutter', :path => 'Flutter'

  # Plugin Pods

  # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
  # referring to absolute paths on developers' machines.
  system('rm -rf .symlinks')
  system('mkdir -p .symlinks/plugins')
  plugin_pods = parse_KV_file('../.flutter-plugins')
  plugin_pods.each do |name, path|
    symlink = File.join('.symlinks', 'plugins', name)
    File.symlink(path, symlink)
    pod name, :path => File.join(symlink, 'ios')
  end
end

target 'OneSignalNotificationServiceExtension' do
  use_frameworks!
  pod 'OneSignal', '>= 2.9.3', '< 3.0'
end

as seen at the end to enable OneSignal push notifications in my app, I've added the OneSignalNotificationServiceExtension. Since the Flutter Runner needs use_frameworks!, I have to add this line to the OneSignal Extension target as well.

This leads to the following file being included unter "General" > "Framework and Libraries" on my OneSignal Target ("Pods_OneSignalNotificationServiceExtension.framework"): wrongly linked file

But this file probably doesn't exist so the build fails.

If I manually remove this file from XCode, the build works.

But since running Flutter in debug mode from my IDE runs pod install again, I can't remove this link, so my idea was to automate the removing in the post_install hook inside the Podfile.

But since I'm neither really familiar with Ruby nor seem to be able to find good documentation for methods/properties in this callback, I'm not getting it to work.

Here's something I've tried so far:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['ENABLE_BITCODE'] = 'NO'
    end
    
    if target.name == 'Pods-OneSignalNotificationServiceExtension'
      all_filerefs = installer.pods_project.files
      all_filerefs.each do |fileref|
        #puts(fileref.path)
        if fileref.path.end_with? "Pods_OneSignalNotificationServiceExtension.framework"
         puts("Found Pods_OneSignalNotificationServiceExtension.framework fileref.")
         build_phase = target.frameworks_build_phase
         #puts("Determining if build phase needs correction.")
         
         #all_filerefs.delete(fileref)
         build_phase.remove_file_reference(fileref)
         puts("Removing Pods_OneSignalNotificationServiceExtension.framework from OneSignalNotificationServiceExtension target")
        end
      end
    end
  end
end

But neither removing it from all_filerefs nor build_phase.remove_file_reference is quite working. Does anybody know how I can access the linked files from the "Framework and Libraries" section in XCode and how to remove said .framework-file?

Thanks a lot!


Solution

  • With the right google search I've managed to find a single result regarding this topic: https://titanwolf.org/Network/Articles/Article?AID=18711f19-4d55-49b9-a49e-8c4652dc0262#gsc.tab=0

    I've took his function from "Fourth, add or remove framework to target"

    def updateCustomFramework(project,target,path,name,command)
    
        # Get useful variables
        frameworks_group = project.groups.find { |group| group.display_name == 'Frameworks' }
        frameworks_build_phase = target.build_phases.find { |build_phase| build_phase.to_s == 'FrameworksBuildPhase' }
        embed_frameworks_build_phase = nil
        target.build_phases.each do |phase|
            if phase.display_name == 'Embed Frameworks'
                embed_frameworks_build_phase = phase
            end
            # puts phase
        end
    
        if command == :add
            # Add new "Embed Frameworks" build phase to target
            if embed_frameworks_build_phase.nil?
                embed_frameworks_build_phase = project.new(Xcodeproj::Project::Object::PBXCopyFilesBuildPhase)
                embed_frameworks_build_phase.name = 'Embed Frameworks'
                embed_frameworks_build_phase.symbol_dst_subfolder_spec = :frameworks
                target.build_phases << embed_frameworks_build_phase
            end
    
            # Add framework search path to target
            ['Debug', 'Release'].each do |config|
                paths = ['$(inherited)', path]
                orgpath = target.build_settings(config)['FRAMEWORK_SEARCH_PATHS']
                if orgpath.nil?
                    puts "path is empty----------"
                    target.build_settings(config)['FRAMEWORK_SEARCH_PATHS'] = paths
            else
                    puts "path is not empty----------"
                    paths.each do |p|
                        if !orgpath.include?(p)
                            orgpath << p
                        end
                    end
                end
            end
    
            # Add framework to target as "Embedded Frameworks"
            framework_ref = frameworks_group.new_file("#{path}#{name}")
            exsit = false
            embed_frameworks_build_phase.files_references.each do |ref|
                if ref.name = name
                    exsit = true
                end
            end
            if !exsit
                build_file = embed_frameworks_build_phase.add_file_reference(framework_ref)
                frameworks_build_phase.add_file_reference(framework_ref)
                build_file.settings = { 'ATTRIBUTES' => ['CodeSignOnCopy'] }
            end
        else
            frameworks_build_phase.files_references.each do |ref|
                #puts(ref.path)
                if ref.path == "#{name}"
                    puts "delete #{name}"
                    frameworks_build_phase.remove_file_reference(ref)
                    #embed_frameworks_build_phase.remove_file_reference(ref)
                    break
                end
            end
            
        end
    
    end
    

    And only changed if ref.name == "#{name}" to if ref.path == "#{name}". I also realized that I can't use the pods_projects from the installer but need to open the xcodeproj-file myself. Here is my final post_install script:

    post_install do |installer|
      installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
      end
      
      project_path = 'Runner.xcodeproj'
      project = Xcodeproj::Project.open(project_path) # Opening the Runner.xcodeproj in the same folder as the Podfile
      project.targets.each do |target|
        if target.name == "OneSignalNotificationServiceExtension" # Find the OneSignal Target
          updateCustomFramework(project, target, '', 'Pods_OneSignalNotificationServiceExtension.framework', ':remove') # Run the function on the target with the framework name
        end
      end
      project.save() # Don't forget to save the project, or the changes won't stay
    end
    

    after that a flutter clean was necessary and afterwards the flutter build works like a charm. Hopefully in the future this will save someone 6 hours of time.