require 'rake' require 'optparse' require 'pathname' require "rake" require 'git' require_relative "manifest.rb" require_relative 'gitlab.rb' module QCloudHive module Project def Project.init(cmd , opts) L.debug "#{opts}" name = GetOptValue(cmd, opts, "name") project = CodeOA.createProject(name) if CodeOA.empty?(project) == true puts "工程#{name}已经在CodeOA上存在,但是没有任何提交记录,还是个空工程,将对其进行初始化" Project.initLocalProject(project) else if File.exist?(name) Error "ERROR! the project local dirctory exist, can not cover it. please check" end workingPath = Pathname.new(name).to_path+"/" if Dir.mkdir(workingPath) !=0 Error "创建目录#{name}失败" end Project.repoInit(workingPath, project.http_url_to_repo) end end def Project.repoInit(workingPath, url) Rake::sh "cd #{workingPath} && repo init -u #{url} \n repo sync" manifestPath = workingPath + ".repo/manifests/default.xml" L.debug "#{manifestPath}" if File.exist?(manifestPath) == false Error "初始化失败,没有manifest配置文件" end manifest = Manifest.new(manifestPath) Rake::sh "cd #{workingPath} && repo start #{manifest.default.revision} --all" end def Project.initLocalProject(project) name = project.name if File.exist?(name) Error "ERROR! the project local dirctory exist, can not cover it. please check" end workingPath = Pathname.new(name).to_path+"/" if Dir.mkdir(workingPath) !=0 Error "创建目录#{name}失败" end L.debug "#{project}" git = Git.init(workingPath) git.add_remote("origin",project.http_url_to_repo) DZCopyFile(Config.templateManifestPath+".", workingPath, true) git.add(:all=>true) git.commit_all("project init") git.push Rake::sh("rm -rf #{workingPath}") if Dir.mkdir(workingPath) !=0 Error "创建目录#{name}失败" end Project.repoInit(workingPath, project.http_url_to_repo) end end def QCloudHive.addCMD(origin , cmd) return origin + "\n" + cmd + "\n" end def InitEmptyProject(cmd, opts) name = opts[:i_name] url = opts[:i_url] if name == nil puts "ERROR! no project name, please input one" puts cmd.help exit(1) end if url == nil puts "ERROR! no git url" puts cmd.help exit(1) end if File.exist?(name) puts "ERROR! the project exist, can not cover it. please check" exit(1) end emptyProjectPath = CURRENT_PATH+name+"/" templatePath = CDM_RES_PATH + "manifests/" DZCopyFile(templatePath, emptyProjectPath, true) Rake::sh "cd #{emptyProjectPath} \n" +''' git init . git add . git commit -m "init project" ''' + "git remote add origin #{url} \n " + " git push -u origin master \n" + "cd .. \n" + "rm -rf #{name}\n" + "mkdir #{name}\n" + "cd #{name}\n" + ''' echo "产品初始化完成,将使用下述命令从您常用的源码目录,checkout该项目:" ''' + "repo init -u #{url}" end end