# frozen_string_literal: true require 'cocoapods' require 'xcodeproj' require 'active_support/core_ext/string/inflections' module GoNative module Plugins module IOS class BuildFramework autoload :FileUtils, 'fileutils' extend DSL::Serviceable BUILD_TEMPLATE_DIRECTORY_PATH = File.expand_path(File.join(__dir__, '../../../..', 'templates', 'build', 'ios')) attr_reader :plugin_name def initialize @plugin_name = Utils::SanitizePluginName.call(FileUtils.pwd.split('/').last, 'ios').capitalize + 'Plugin' end def call setup_dirs create_framework_proj move_template_files run_pod_install chmod_frameworks_script! build_framework! move_framework_file ensure FileUtils.cd('..') FileUtils.rm_rf('build') end def setup_dirs build_dir = File.join(FileUtils.pwd, 'build') FileUtils.mkdir(build_dir) FileUtils.cd(build_dir) end def create_framework_proj proj = Xcodeproj::Project.new(FileUtils.pwd) target = proj.new_target(:framework, "#{plugin_name}", :ios, '10.0') main_group = proj.new_group(plugin_name, plugin_name) classes_group = main_group.new_group('Classes', 'Classes') references = Dir.glob("../#{plugin_name}/Classes/**").map{ |file| classes_group.new_file("../../#{file}") } target.add_file_references(references) target.build_configurations.each do |config| config.build_settings['GENERATE_INFOPLIST_FILE'] = 'YES' end proj.save("#{plugin_name}.xcodeproj") end def move_template_files spec = Pod::Specification.from_file("../#{plugin_name}.podspec") plugin_dependencies = spec.dependencies.map{|d| ["pod '#{d.name}'", "'#{d.requirement}'"].compact.join(', ') } * "\n\t" FileUtils.cp_r("#{BUILD_TEMPLATE_DIRECTORY_PATH}/.", '.') Utils::TemplateInflator.new(plugin_name: plugin_name, plugin_dependencies: plugin_dependencies).call end def run_pod_install system 'pod install' end def chmod_frameworks_script! FileUtils.chmod 0755, 'create-framework.sh' end def build_framework! Utils::UI.info 'Building framework' return if system('sh create-framework.sh >/dev/null 2>/dev/null') raise Error, "Error building framework. Please run the create-framework file manually to fix any errors" end def move_framework_file FileUtils.mv("XCFramework", '..', force: true) end end end end end