require 'cocoapods' require_relative '../util/sh_util' module OrmDev class CreateHelper ORMDEV_TEMPLATE_URL = 'https://gitee.com/mvn/pod-template.git'.freeze ORMDEV_FAST_TEMPLATE_URL = 'https://gitee.com/mvn/pod-fast-template.git'.freeze # ORMDEV_PLUGIN_TEMPLATE_URL = 'https://gitee.com/mvn/pod-plugin-template.git'.freeze # ORMDEV_PLUGIN_FAST_TEMPLATE_URL = 'https://gitee.com/mvn/pod-plugin-fast-template.git'.freeze attr_reader :fast_build attr_reader :project_name attr_reader :class_prefix attr_reader :template_url def initialize(name, fast = false, prefix = '', template_url = nil) @project_name = name @class_prefix = prefix @fast_build = fast if @fast_build then @template_url = template_url || ORMDEV_FAST_TEMPLATE_URL else @template_url = template_url || ORMDEV_TEMPLATE_URL end end def setup(skip= false) OrmDev::SHUtil.run_command('pod repo update orm','更验证Pod',false) abort('插件名已存在'.red) if system("pod search #{@project_name} --no-pager --ios") if @fast_build then clone_template configure_template(skip) print_info else create_command = "pod lib create #{@project_name} --template-url=#{@template_url}" OrmDev::SHUtil.run_command(create_command,'创建ORM插件工程') end end private #----------------------------------------# # !@group Private helpers extend Pod::Executable executable :git # Clones the template from the remote in the working directory using # the name of the Pod. # # @return [void] # def clone_template Pod::UI.section("Cloning `#{template_repo_url}` into `#{@project_name}`.") do git! ['clone', template_repo_url, @project_name] end end # Runs the template configuration utilities. # # @return [void] # def configure_template(skip_open = false) string_replacements = { # "PROJECT_OWNER" => @configurator.user_name, "TODAYS_DATE" => Time.now.strftime("%m/%d/%Y"), "TODAYS_YEAR" => Time.now.year.to_s, "CIPPROJECT" => @project_name } Dir.glob(project_folder + "/**/**/**/**").each do |name| next if Dir.exists? name text = File.read(name) for find, replace in string_replacements text = text.gsub(find, replace) end File.open(name, "w") { |file| file.puts text } end # shared schemes have project specific names scheme_path = project_folder + "/Example/CIPPROJECT.xcodeproj/xcshareddata/xcschemes/" raise Pod::Informative, 'No Xcode project shared schemes found, please specify one' unless Dir.exists? scheme_path File.rename(scheme_path + "CIPPROJECT-Example.xcscheme", scheme_path + @project_name + "-Example.xcscheme") # rename xcproject File.rename(project_folder + "/Example/CIPPROJECT.xcodeproj", File.join(project_folder, "Example/#{@project_name}.xcodeproj")) # # change app file prefixes # ["OrmProAppDelegate.h", "OrmProAppDelegate.m"].each do |file| # before = project_folder + "/PROJECT/" + file # next unless File.exists? before # # after = project_folder + "/PROJECT/" + file.gsub("OrmPro", @class_prefix) # File.rename before, after # end # rename project related files safe_rename_file("#{project_folder}/Example/CIPPROJECT.xcworkspace", "#{project_folder}/Example/#{@project_name}.xcworkspace") safe_rename_file("#{project_folder}/Example/CIPPROJECT/CIPPROJECT-Info.plist", "#{project_folder}/Example/CIPPROJECT/#{@project_name}-Info.plist") safe_rename_file("#{project_folder}/Example/CIPPROJECT/CIPPROJECT-Prefix.pch", "#{project_folder}/Example/CIPPROJECT/#{@project_name}-Prefix.pch") safe_rename_file("#{project_folder}/Example/CIPPROJECT", "#{project_folder}/Example/#{@project_name}") safe_rename_file("#{project_folder}/CIPPROJECT/Classes/CIPPROJECT.h", "#{project_folder}/CIPPROJECT/Classes/#{@project_name}.h") safe_rename_file("#{project_folder}/CIPPROJECT/Classes/CIPPROJECT.m", "#{project_folder}/CIPPROJECT/Classes/#{@project_name}.m") safe_rename_file("#{project_folder}/CIPPROJECT/Classes/CIPPROJECTAPI.h", "#{project_folder}/CIPPROJECT/Classes/#{@project_name}API.h") safe_rename_file("#{project_folder}/CIPPROJECT/Classes/CIPPROJECTConst.h", "#{project_folder}/CIPPROJECT/Classes/#{@project_name}Const.h") safe_rename_file("#{project_folder}/CIPPROJECT/Classes/CIPPROJECTConst.m", "#{project_folder}/CIPPROJECT/Classes/#{@project_name}Const.m") safe_rename_file("#{project_folder}/CIPPROJECT/Classes/CIPPROJECTMacros.h", "#{project_folder}/CIPPROJECT/Classes/#{@project_name}Macros.h") safe_rename_file("#{project_folder}/CIPPROJECT", "#{project_folder}/#{@project_name}") safe_rename_file("#{project_folder}/CIPPROJECT.podspec", "#{project_folder}/#{@project_name}.podspec") reinitialize_svn_repo OrmDev::LogUtil.info "\nRunning " + "pod install".magenta + " on your new library." Dir.chdir("#{project_folder}/Example") do system "pod install" OrmDev::LogUtil.info " open '#{project_folder}/Example/#{@project_name}.xcworkspace'".magenta system "open '#{@project_name}.xcworkspace'" unless skip_open end project_folder end private #----------------------------------------# # !@group Private helpers def project_folder @project_name end def reinitialize_svn_repo `rm -rf .svn` end def safe_rename_dir(old, dir_name) if Dir.exist? old File.rename(old, dir_name) end end def safe_rename_file(old, file_name) if File.exist? old File.rename(old, file_name) end end # Runs the template configuration utilities. # # @return [void] # def print_info Pod::UI.puts "\nTo learn more about the template see `#{template_repo_url}`." end # Checks if a template URL is given else returns the TEMPLATE_REPO URL # # @return String # def template_repo_url @template_url || ORM_PLUGIN_TEMPLATE_URL end end end