# frozen_string_literal: true require 'xcodeproj' require 'active_support/core_ext/string/inflections' module GoNative module Plugins module IOS class Create autoload :FileUtils, 'fileutils' extend DSL::Serviceable TEMPLATE_DIRECTORY_PATH = File.expand_path(File.join(__dir__, '../../../..', 'templates', 'plugins', 'ios')) COMMON_TEMPLATES_PATH = File.join(TEMPLATE_DIRECTORY_PATH, 'common') LANGUAGES = %w[objc swift].freeze attr_reader :plugin_name, :language def initialize(plugin_name, language) @plugin_name = plugin_name @language = language end def call assert_not_exists! set_working_dir! cp_template_files! inflate_templates! chmod_frameworks_script! create_framework_proj! run_pod_install! end def assert_not_exists! return unless File.directory?(plugin_name) raise Error, "Directory #{plugin_name} already exists" end def set_working_dir! FileUtils.mkdir(plugin_name) FileUtils.cd(plugin_name) end def cp_template_files! FileUtils.cp_r("#{COMMON_TEMPLATES_PATH}/.", '.') system('ditto', File.join(TEMPLATE_DIRECTORY_PATH, 'language-specific', language), '.') end def inflate_templates! Dir.glob('*/').each do |dir| FileUtils.cd(dir) inflate_templates! FileUtils.cd('..') dir_name = dir.delete_suffix('/') normalized_name = normalized_name(dir_name) FileUtils.mv(dir_name, normalized_name) if dir_name != normalized_name end inflate_files end def chmod_frameworks_script! FileUtils.chmod 0755, 'create-framework.sh' end def create_framework_proj! proj = Xcodeproj::Project.new(FileUtils.pwd) target = proj.new_target(:framework, "#{plugin_name}Framework", :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.split('/').last) } main_group.new_file('Info.plist') target.add_file_references(references) target.build_configurations.each do |config| config.build_settings['INFOPLIST_FILE'] = "$(SRCROOT)/#{plugin_name}/Info.plist" end proj.save("#{plugin_name}.xcodeproj") end def run_pod_install! system 'pod install' end def inflate_files Dir.glob("*#{TEMPLATE_FILES_EXTENSION}").each do |file| File.write(normalized_name(file), contents(file)) FileUtils.rm(file) end end def normalized_name(file) file.gsub('PLUGIN_NAME', plugin_name).delete_suffix(TEMPLATE_FILES_EXTENSION) end def contents(file) Utils::TemplateInflator.new(File.read(file)).call(PLUGIN_NAME: plugin_name, REPO_NAME: repo_name) end def repo_name plugin_name.underscore.dasherize.prepend 'ios-' end end end end end