# frozen_string_literal: true require 'json' require 'xcodeproj' module GoNative module Plugins module IOS class EmbedExtensions extend DSL::Serviceable autoload :FileUtils, 'fileutils' attr_reader :extensions def call prepare_extensions embed_in_project modify_podfile end def prepare_extensions extension_paths = `find ./Pods -iname Extensions -not -empty -type d`.split("\n") extension_paths .map { |path| Pathname(path).children.select(&:directory?) } .flatten .each { |path| FileUtils.cp_r(path, '.') } @extensions = extension_paths .map { |path| Pathname(path).children.select { |p| p.extname == '.json' } } .flatten .map { |p| JSON.parse(File.read(p)) } end def embed_in_project proj = Xcodeproj::Project.open('MedianIOS.xcodeproj') app_target = proj.native_targets.first extensions.each do |extension| extension_target = proj.new_target(:app_extension, extension['name'], :ios, '13.0') extension_group = proj.new_group(extension['name'], extension['name']) extension['headers'].each { |path| extension_group.new_file(path) } extension_target.add_file_references(extension['source_files'].map do |path| extension_group.new_file(path) end) extension_target.add_resources(extension['resources'].map { |path| extension_group.new_file(path) }) extension_target.add_system_frameworks(extension['system_frameworks']) extension_group.new_file(extension['info']) extension_group.new_file(extension['entitlements']) if extension['entitlements'] extension_target.build_configurations.each do |config| config.build_settings['PRODUCT_NAME'] = '$(TARGET_NAME)' config.build_settings['INFOPLIST_FILE'] = "#{extension["name"]}/#{extension["info"]}" if extension['entitlements'] config.build_settings['CODE_SIGN_ENTITLEMENTS'] = "#{extension["name"]}/#{extension["entitlements"]}" end end app_target.add_dependency(extension_target) extension_file = proj.files.find { |f| f.path == "#{extension["name"]}.appex" } embed_phase = app_target.build_phases.find do |phase| phase.class.method_defined?(:name) && phase.name == 'Embed Foundation Extensions' end embed_phase.add_file_reference(extension_file, true) app_config = proj.files.find { |f| f.path == 'appConfig.json' } extension_target.resources_build_phase.add_file_reference(app_config, true) end proj.save end def modify_podfile extensions.each do |extension| open('Podfile', 'a') do |f| f.puts "target '#{extension["name"]}' do" f.puts "\tuse_frameworks!" extension['dependencies'].each do |dep| f.puts "\tpod '#{dep["name"]}', '#{dep["requirement"]}'" end f.puts 'end' end end end end end end end