# frozen_string_literal: true require 'cocoapods' require 'xcodeproj' require 'json' module GoNative module Plugins module IOS class ExtractExtensions extend DSL::Serviceable autoload :FileUtils, 'fileutils' attr_reader :proj_path def initialize(path) @proj_path = path end def call original_path = FileUtils.pwd FileUtils.cd(File.dirname(proj_path)) proj = Xcodeproj::Project.open(proj_path) extension_targets = proj.targets.select { |target| target.product_type == Xcodeproj::Constants::PRODUCT_TYPE_UTI[:app_extension] } extension_targets.each do |target| extension_name = target.name extension_path = "Extensions/#{target.name}" target_definition = {} target_definition[:name] = extension_name FileUtils.cd(extension_name) target_definition[:info] = `find . -name '*Info.plist'`.split("\n").first target_definition[:entitlements] = `find -E . -regex ".*\.(entitlements)"`.split("\n").first target_definition[:headers] = `find -E . -regex ".*\.(h)"`.split("\n") target_definition[:source_files] = `find -E . -regex ".*\.(m|swift)"`.split("\n") target_definition[:resources] = target .resources_build_phase .files .map(&:file_ref) .map(&:name) .map { |name| `find . -name #{name}`.strip } target_definition[:system_frameworks] = target .frameworks_build_phase .files .map(&:file_ref) .map(&:path) .select { |p| p.start_with? 'System' } .map { |p| File.basename(p) } target_definition[:dependencies] = podfile.target_definitions[extension_name].dependencies.map do |dependency| { name: dependency.name, requirement: dependency.requirement.requirements.map { |r| r.map(&:to_s).join(' ') }.join(', ') } end target_definition.slice(:info, :entitlements, :headers, :source_files, :resources).values.compact.flatten.each do |file_path| system('ditto', file_path, "#{original_path}/#{extension_path}/#{file_path}") end File.open("#{original_path}/#{extension_path}.json", "w") do |f| f.write(JSON.pretty_generate(target_definition)) end FileUtils.cd('..') end end def podfile @podfile ||= Pod::Podfile.from_file(Pathname.new(File.dirname(proj_path) + '/Podfile')) end end end end end # //:configuration = Debug # CODE_SIGN_ENTITLEMENTS = ShareExtension/ShareExtension.entitlements # # //:configuration = Release # CODE_SIGN_ENTITLEMENTS = ShareExtension/ShareExtension.entitlements # # //:completeSettings = some # CODE_SIGN_ENTITLEMENTS