require 'nokogiri' require 'uri' require_relative "config.rb" module QCloudHive class ManifestProject attr_accessor :remote attr_accessor :path attr_accessor :name attr_accessor :moduleName def initialize(remote, name ,path) @remote = remote @name = name @path = path @moduleName = name.split("/").last #private values @serverProject = nil @podspec = nil @git = nil @podStorageSpec = nil @localPath end def localPath if @localPath == nil root = Pathname(Config.projectRootDirectory) @localPath = root.join(self.path).to_path end @localPath end def serverProject if @serverProject.nil? @serverProject = CodeOA.existProjectByName?(moduleName) end @serverProject end def podStorageSpec if @podStorageSpec.nil? L.info("search module #{moduleName}") @podStorageSpec = HivePod.search(moduleName) end @podStorageSpec end def podspec if @podspec.nil? HivePod.podspecs.each { |pspec| if pspec.name == self.moduleName @podspec = pspec.podspec end } if @podspec.nil? @podspec = self.podStorageSpec end end @podspec end def gitRepo if @git.nil? root = Pathname(Config.projectRootDirectory) gitPath = root.join(self.path) begin @git = Git.open(gitPath) rescue => err puts "创建Git工程失败 #{gitPath} #{err}" end end @git end def exsitLocal? return self.gitRepo.nil? == false end def exsitPodRepo? return self.podStorageSpec.nil? == false end def exsitServer? return self.serverProject.nil? == false end def needReleasePod? if exsitLocal? return false end version = self.podspec.version.first tag = gitRepo.tags.select { |e| e.name == version.to_s }.first if tag.nil? return true end master = gitRepo.branches["master"] if tag.sha != master.gcommit.sha return true end return false end end class ProjectFeature def initialize(project, branch) @project = project @branch = branch # @remoteBranch = nil end def remoteBranch if @remoteBranch.nil? @remoteBranch = CodeOA.branch(@project.serverProject.id, @branch.name) end @remoteBranch end def local? if @branch.nil? return false end localBranch.local? end def compareRemote localSha = @branch.gcommit.sha localDate = DateTime.parse(@branch.gcommit.committer_date.to_s) remtoeDate = DateTime.parse(self.remoteBranch.commit.committed_date) if self.remoteBranch.commit.id != localSha return localDate <=> remtoeDate end return 0 end def compareMaster if @branch.name == "master" return 0 end master = @project.gitRepo.branches["master"] localDate = DateTime.parse(@branch.gcommit.committer_date.to_s) masterLocalDate = master.gcommit.committer_date masterDate = DateTime.parse(masterLocalDate.to_s) localDate <=> masterDate end end class ManifestRemote attr_accessor :name attr_accessor :fetch def initialize(name, fetch) @name = name @fetch = fetch end end class ManifestDefault attr_accessor :revision attr_accessor :remote attr_accessor :sync_j def initialize @revision = "master" @remote = "origin" @sync_j = "16" end end class Manifest attr_accessor :projects attr_accessor :remotes attr_accessor :default def initialize(filePath) @manifestPath = filePath @doc = Nokogiri::XML(File.open(filePath)) @default = ManifestDefault.new @projects = [] @doc.xpath("//project").each { |p| remote = p.attribute("remote").value name = p.attribute("name").value if name.start_with?("/") name= name[1..name.length] end path = p.attribute("path").value project = ManifestProject.new(remote, name, path) @projects << project L.debug project } @remotes = [] @doc.xpath("//remote").each { |r| name = r.attribute("name").value fetch = r.attribute("fetch").value if not fetch.end_with?("/") fetch = fetch+"/" end remote = ManifestRemote.new(name, fetch) @remotes << remote } end def addModule(url, path) uri = URI(url) host = uri.host scheme = uri.scheme remote = nil uriPath = uri.path.split(".").first if uriPath.start_with?("/") uriPath= uriPath[1..uriPath.length] end name = uriPath.split("/").last @projects.each { |p| if p.name == uriPath return @projects end } @remotes.each { |r| rfetch = URI(r.fetch) if rfetch.host == host and rfetch.scheme == scheme remote = r.name end } projectPath = "#{name}" if path != nil if path.start_with?("/") path = path[1..-1] end if path.end_with?("/") projectPath = "#{path}#{name}" else if path.length > 0 projectPath = "#{path}/#{name}" else projectPath = "#{name}" end end end remote = ManifestProject.new(remote, uriPath ,projectPath) @projects << remote end def existModule?(name) @projects.select{ |p| if p.name.start_with?(Config.team) p.name == Config.team+"/#{name}" else p.name == name end }.count > 0 end def moduleByName(name) @projects.each { |p| L.info p.name } @projects.select{ |p| if p.name.start_with?(Config.team) p.name == Config.team+"/#{name}" else p.name == name end }.first end def deleteModule(url) uri = URI(url) uriPath = uri.path.split(".").first if uriPath.start_with?("/") uriPath= uriPath[1..uriPath.length] end L.debug "#{@projects}" @projects=@projects.select{ |e| e.name != uriPath } L.debug "#{@projects}" end def deleteModuleByName(name) L.debug "#{@projects}" @projects=@projects.select{ |e| e.moduleName != name } L.debug "#{@projects}" end def toXML doc = Nokogiri::XML('''''') manifest = doc.xpath("//manifest").first @remotes.each { |r| ele = Nokogiri::XML::Element.new("remote", doc) ele.set_attribute("name", r.name) ele.set_attribute("fetch", r.fetch) manifest.add_child(ele) } default = Nokogiri::XML::Element.new("default", doc) default.set_attribute("revision", @default.revision) default.set_attribute("remote", @default.remote) default.set_attribute("sync-j", @default.sync_j) manifest.add_child(default) @projects.each { |r| ele =Nokogiri::XML::Element.new("project", doc) ele.set_attribute("name",r.name) ele.set_attribute("remote" ,r.remote ) ele.set_attribute("path" ,r.path) manifest.add_child(ele) } return doc.to_s end def flushStorage File.open(@manifestPath, "w") { |f| f.write toXML } end def remoteByName(name) @remotes.select { |e| e.name == name }.first end def sourceURLByModuleName(name) L.debug "#{@projects}" moduleSpec = @projects.select{ |e| e.moduleName != name }.first if moduleSpec.nil? return nil end remote = remoteByName(moduleSpec.remote) return "#{remote.fetch}#{moduleSpec.name}" end end def QCloudHive.syncManifest repoRootPath = Config.repoRoot manifestsPath=repoRootPath+"./manifests/" cmd = "" cmd = addCMD(cmd,"cd #{manifestsPath}") cmd = addCMD(cmd, "git add .") cmd = addCMD(cmd, "git commit -m \"Project Edited\"") cmd = addCMD(cmd, "git checkout default") cmd = addCMD(cmd, "git push origin HEAD:default") cmd = addCMD(cmd, "git checkout master") cmd = addCMD(cmd, "git merge default") cmd = addCMD(cmd, "git push origin HEAD:master") cmd = addCMD(cmd, "git checkout default") L.debug cmd Rake::sh cmd end end