require 'zip' module Pod class Uploader def initialize(spec, upload_spec_name, upload_local_path, spec_sources) @spec = spec @work_dir = Dir.pwd @upload_spec_name = upload_spec_name @upload_local_path = upload_local_path @spec_sources = spec_sources build git_push push_repo end def build filename = "#{@spec.name}-#{@spec.version}" add_to_zip_file("#{Dir.pwd}/#{filename}/#{filename}.zip", "#{Dir.pwd}/#{filename}") end def git_push filename = "#{@spec.name}-#{@spec.version}" framworks_path = @upload_local_path zip_path = "#{framworks_path}/#{@spec.name}" FileUtils.mkdir_p(zip_path) unless File.exists?(zip_path) FileUtils.cp("#{Dir.pwd}/#{filename}/#{filename}.zip", "#{zip_path}") Dir.chdir(framworks_path) git_add_command = "git add ." git_output = `#{git_add_command}`.lines.to_a commit_add_command = "git commit -s -m 'add #{filename} framework'" commit_output = `#{commit_add_command}`.lines.to_a push_add_command = "git push origin master" push_commit_output = `#{push_add_command}`.lines.to_a end def push_repo filename = "#{@spec.name}-#{@spec.version}" UI.puts("准备发布 #{@work_dir}/#{filename}") Dir.chdir("#{@work_dir}/#{filename}") source = @spec_sources.join(",") command = "pod repo push #{@upload_spec_name} #{@work_dir}/#{filename}/#{@spec.name}.podspec --allow-warnings --use-libraries --use-modular-headers --verbose --skip-import-validation --sources=#{source}" output = `#{command}`.lines.to_a end # 压缩文件方法 def add_to_zip_file(zip_file_name,file_path) def add_file(start_path,file_path,zip) if File.directory?(file_path) zip.mkdir(file_path) Dir.foreach(file_path) do |filename| add_file("#{start_path}/#{filename}","#{file_path}/#{filename}",zip) unless filename=="." or filename==".." end else zip.add(start_path,file_path) end end if File.exist?(zip_file_name) File.delete(zip_file_name) end chdir,tardir = File.split(file_path) Dir.chdir(chdir) do Zip::File.open(zip_file_name,Zip::File::CREATE) do |zipfile| add_file(tardir,tardir,zipfile) end end end end end