require 'cocoapods' require 'zip' require_relative '../util/sh_util' require_relative '../util/download_util' module OrmDev class CreateHelper # ORMDEV_TEMPLATE_URL = 'https://gitee.com/mvn/pod-template.git'.freeze ORMDEV_GIT_TEMPLATE_URL = 'https://gitee.com/mvn/pod-fast-template.git'.freeze ORMDEV_ZIP_TEMPLATE_URL = 'http://112.65.142.83:7788/ceshi/tmpl/pod-fast-template.zip'.freeze attr_reader :fast_build attr_reader :project_name attr_reader :template_url def initialize(name, fast = false, template_url = nil) @project_name = name @fast_build = fast if @fast_build then @template_url = template_url || ORMDEV_ZIP_TEMPLATE_URL else @template_url = template_url || ORMDEV_GIT_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 if @template_url.downcase.end_with?('.zip') then unzip_dir = File.basename(@template_url, 'zip') abort('插件目录本地已存在。') if Dir.exist?(unzip_dir) zip_path = File.join('./', File.basename(@template_url)) if (@template_url.downcase.start_with?('http://') || @template_url.downcase.start_with?('https://')) then OrmDev::DownloadUtil.download( @template_url, zip_path )#Pathname.pwd OrmDev::LogUtil.info "【创建插件工程】下载#{@template_url}到#{zip_path}" elsif File.exist?(@template_url) then OrmDev::LogUtil.info "【创建插件工程】复制#{@template_url}到#{zip_path}" FileUtils.cp( @template_url, zip_path ) end abort('插件模板zip下载失败') unless File.exist? zip_path # 解压模板 result = OrmDev::SHUtil.run_command("unzip #{zip_path}",'解压模板工程', false) File.rename('CIPPROJECT', @project_name) FileUtils.rm zip_path abort('插件模板zip解压失败') unless result else Pod::UI.section("Cloning `#{template_repo_url}` into `#{@project_name}`.") do git! ['clone', template_repo_url, @project_name] end 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, "cipPROJECT" => @project_name.gsub(/^CIP/, 'cip'),#模板替换,插件名->js对象 } 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/asset/www/V2/data/CIPPROJECT.js", "#{project_folder}/Example/CIPPROJECT/asset/www/V2/data/#{@project_name}.js") safe_rename_file("#{project_folder}/Example/CIPPROJECT/asset/www/V2/html/CIPPROJECT.html", "#{project_folder}/Example/CIPPROJECT/asset/www/V2/html/#{@project_name}.html") 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` `rm -rf .git` 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