require 'xcodeproj' require 'uri' require 'cocoapods' require 'git' require 'rake' require 'optparse' require 'pathname' require_relative 'project.rb' require_relative "manifest.rb" require 'fileutils' require_relative 'utils.rb' require_relative 'gitlab.rb' require_relative 'pod_helper.rb' module QCloudHive module HiveModule def HiveModule.add(cmd, opts) name = GetOptValue(cmd, opts, :c_name) rootPath = Config.projectRootDirectory pwd = Dir.pwd projectPath = opts[:i_path] if projectPath == nil if pwd.start_with? rootPath projectPath = pwd[rootPath.length..-1] L.warn "您没有给定具体的目录 将使用当前目录#{projectPath}" else Error "您没有给定具体的目录,并且当前不在Hive工程中!!!!" end end addModuleByName(name, projectPath) end def HiveModule.addModuleByName(name, projectPath) L.debug "#{Config.manifest}" if Config.manifest.existModule?(name) L.warn "#{name} 已经在项目中,请不要重复添加" end if CodeOA.exist?(name) gitProject = CodeOA.existProjectByName?(name) if CodeOA.empty?(gitProject) addModuleByCreate(name, projectPath) return end Config.manifest.addModule(gitProject.http_url_to_repo, projectPath) Config.manifest.flushStorage L.debug "模块已经在git.code.oa.com上存在" puts "已经添加模块#{name}" podspec = HivePod.search(name) if podspec == nil L.warn"#{name} 已经存在于git.code.oa.com上,但是没有发布到cocoapods私有仓库,无法解析其依赖关系,将不进行依赖添加" else dependencies = podspec.specification.dependencies L.debug "#{name}依赖以下库 #{dependencies}" dependencies.each { |dependency| addModuleByName(dependency.name, projectPath) } end else L.warn "#{name} 不再git.code.oa.com上面" addModuleByCreate(name, projectPath) end end def HiveModule.addModuleByCreate(name, projectPath) gitProject = createModuleByName(name, projectPath) Config.manifest.addModule(gitProject.http_url_to_repo, projectPath) Config.manifest.flushStorage end def HiveModule.createLocalModule(name, path) aimPodspecPath = "#{path}/#{name}.podspec" podspecTempPath = Config.templatesPath+"template.podspec" if Pathname(aimPodspecPath).exist? L.info "#{aimPodspecPath} exsit" return end tempFile= File.new(podspecTempPath, "r") if not Pathname(path).exist? FileUtils.mkdir_p(path) end if tempFile content = tempFile.read content = content.gsub(//,"#{name}") File.open(aimPodspecPath, "w") { |f| f.write content } classPath = "#{path}/Pod/Classes" FileUtils.mkdir_p(classPath) File.open(classPath+"/replace.m", "w") { |f| f.write "//replace me !!!" } File.open("#{path}/README.md", "w") { |f| f.write "README" } g = Git.init(path) g.add(:all=>true) g.commit_all("mudule init") else Error "模版文件不存在 #{podspecTempPath}" end end def HiveModule.createModuleByName(name, projectPath) if File.exist?(name) L.warn "#{name} 已经存在于当前目录下,生成新模块将会覆盖他" end g = Git.init(name) oaProject = CodeOA.createProject(name) originRemote = nil g.remotes.each { |r| if r.name == "origin" originRemote = r end } if originRemote != nil if originRemote.url == oaProject.http_url_to_repo L.warn "当前Git仓库已经有 remote origin 为 #{oaProject.http_url_to_repo}" else Error "当前仓库已经有remote 但是不是#{oaProject.http_url_to_repo}" end else g.add_remote("origin",oaProject.http_url_to_repo) end createLocalModule(name, name) g.push return oaProject end def HiveModule.release(cmd, opts) rootPath = Dir.pwd path = Pathname(rootPath) podspec = nil path.entries.each { |entry| if (entry.extname == '.podspec' ) podspec = entry end } if podspec == nil puts "当前目录下没有Podspec文件,请在有Podspec文件的目录下进行该操作" exit(1) end spec = Pod::Specification.from_file(podspec) version = spec.version.version git = Git.open(rootPath) versionExsit = false git.tags.each { |t| if t.name == version versionExsit = true break end } if versionExsit git.delete_tag(version) git.push("origin", ":refs/tags/"+version) end git.add_tag(version) git.push("origin",version) specStorage = "oa-qcloud-terminal-qcloudpodspecs " Rake::sh " pod repo push #{specStorage} #{podspec} --allow-warnings" Rake::sh "cd ~/.cocoapods/repos/#{specStorage} \n"+ ''' git add . ''' + "git commit -m #{podspec}" + ''' git pull git push ''' end def HiveModule.tryRmGit?(gitPath) git = Git.open(gitPath) L.info "git is #{git}" if git == nil puts "您给的目录不是一个Git目录#{path}" exit(1) end remoteUrl = nil git.remotes.each { |r| if r.name == "origin" remoteUrl = r.url end } if remoteUrl == nil puts "您输入的Git目录没有设置远端地址#{path},您可能没没有提交的修改,暂不执行删除操作" exit(1) end if git.status.changed.count > 0 puts "您输入的Git目录存在未提交的修改,暂不执行删除操作" exit(1) end if git.status.deleted.count > 0 puts "您输入的Git目录存在未提交的修改,暂不执行删除操作" exit(1) end if git.status.added.count > 0 puts "您输入的Git目录存在未提交的修改,暂不执行删除操作" exit(1) end if git.status.untracked.count > 0 puts "您输入的Git目录存在未提交的修改,暂不执行删除操作" exit(1) end return true end def HiveModule.dislink(cmd, opts) name = GetOptValue(cmd, opts, :i_name) manifestProjcet = Config.manifest.moduleByName(name) if manifestProjcet == nil Error "您要dislink的模块#{name}不在改项目中" end realetivePath = manifestProjcet.path gitPath = Pathname(Config.projectRootDirectory).join(realetivePath).to_path if File.exist?(gitPath) tryRmGit?(gitPath) end Config.manifest.deleteModuleByName(name) Config.manifest.flushStorage FileUtils.rm_rf(gitPath) end end end