class LZCodeInner

Public Instance Methods

begin(params) click to toggle source
# File lib/source/LZCodeInner.rb, line 21
def begin(params)
       if params.size < 1
              showInfo()
       else
               $OperationType=params[0]
               $Keyword=params[1]
               start()
       end
 end
checkExistDir(key) click to toggle source

Tools

# File lib/source/LZCodeInner.rb, line 32
def checkExistDir(key)
  isExist = false
  Dir.foreach("./") do |file|
    if File.directory?("#{file}") && file == key
        isExist = true
    end
  end
  return isExist
end
clear() click to toggle source

Clear

# File lib/source/LZCodeInner.rb, line 139
def clear()
  LZCodeGitLab.new.resetInfo()
end
cyan(s) click to toggle source
# File lib/source/LZCodeInner.rb, line 68
def cyan(s)
  return "\033[36m#{s}\033[0m"
end
download() click to toggle source
# File lib/source/LZCodeInner.rb, line 219
def download()
  if $Keyword == nil
    showInfo()
  end

  info = LZCodeGitLab.new.getRepoInfo($Keyword)
  
  if info.keys.count > 0

    $name = info["name"]
    $addr = info["ssh_url_to_repo"]
    $path = info["path"]
    $pid = info["id"]
    $forkInfo = info["forked_from_project"]

    #源仓库
    if $forkInfo
      $fork_addr = info["forked_from_project"]["ssh_url_to_repo"]
      run_download($path,$addr,$fork_addr)
    #fork仓库
    else
      result = ''
      while result != 'Y' && result != 'n'
        print "Do you want to fork the repository?(Y/n):"
        result = STDIN.gets.chomp
      end
      #需要fork
      if result == 'Y'
        result = LZCodeGitLab.new.getMatchRepo($pid,$name)
        #已经存在fork仓库
        if result.keys.size > 0
          puts 'The repository already forks!'
          #下载fork repo
          $ssh_url = result["ssh_url_to_repo"]
          $fork_url = result["forked_from_project"]["ssh_url_to_repo"]

          run_download($path,$ssh_url,$fork_url)

        else
          #fork repo
          puts 'Performing fork...'
          f_info = LZCodeGitLab.new.forkRepo($pid,$name)
          if f_info.keys.size > 0
            puts 'Operation completed!'
            $ssh_url = f_info["ssh_url_to_repo"]
            $fork_url = f_info["forked_from_project"]["ssh_url_to_repo"]
            run_download($path,$ssh_url,$fork_url)
          else
            puts 'Operation failure!'
          end
        end
      #不需要fork
      elsif result == 'n'
        run_download($path,$addr,'')
      end
    end

  else
    puts "No search result by the keyword."
  end
  
end
green(s) click to toggle source
# File lib/source/LZCodeInner.rb, line 56
def green(s)
  return "\033[32m#{s}\033[0m"
end
magenta(s) click to toggle source
# File lib/source/LZCodeInner.rb, line 60
def magenta(s)
  return "\033[35m#{s}\033[0m"
end
remove() click to toggle source

remove

# File lib/source/LZCodeInner.rb, line 144
def remove()
  info = LZCodeGitLab.new.removeRepo($Keyword)
end
run_download(path,addr,forkAddr) click to toggle source
# File lib/source/LZCodeInner.rb, line 184
def  run_download(path,addr,forkAddr)
  #检查本地是否已经存在
  t = checkExistDir(path)
  if t == false
    #执行下载操作
    run_s_download(path,addr,forkAddr)
  else
    #本地已经存在,是否覆盖?
    isCover = ''
    while isCover != 'Y' && isCover != 'n'
      print "Do you want to overwrite the local repository?(Y/n):"
      isCover = STDIN.gets.chomp
    end
    if isCover == 'Y'
      #删除目录,进行下载
      system("rm -rf #{path}")
      run_s_download(path,addr,forkAddr)
    elsif isCover == 'n'
      #是否进行更新操作?
      isUpdate = ''
      while isUpdate != 'Y' && isUpdate != 'n'
        print "Do you want to update the local repository?(Y/n):"
        isUpdate = STDIN.gets.chomp
      end
      if isUpdate == 'Y'
      #执行更新操作
        run_update(path)
      elsif isUpdate == 'n'
      #退出 say bye!
        sayBye()
      end
    end
  end
end
run_s_download(path,addr,forkAddr) click to toggle source

Download

# File lib/source/LZCodeInner.rb, line 174
def run_s_download(path,addr,forkAddr)
  puts 'Performing download...'
  log = system("#{$ScriptPath}/z_download.sh #{path} #{addr} #{forkAddr} #{ForkName}")
  if log == true
    puts 'Operation completed!'
  else
    puts 'Operation failure!'
  end
end
run_update(path) click to toggle source

Update

# File lib/source/LZCodeInner.rb, line 149
def run_update(path)
  #检查本地是否已经存在
  t = checkExistDir(path)
  if t == false
    puts 'Not found the in repository current directory!'
    sayBye()
  else
    puts 'Performing update...'
    log = system("#{$ScriptPath}/z_update.sh #{path} #{ForkName}")
    if log == true
          puts 'Operation completed!'
        else
          puts 'Operation failure!'
        end
  end
end
sayBye() click to toggle source

SayBye

# File lib/source/LZCodeInner.rb, line 43
def sayBye
  puts 'Have a nice day!'
  exit
end
showInfo() click to toggle source

end

# File lib/source/LZCodeInner.rb, line 82
def showInfo()
    puts ' '
    puts white_("Usage:")
    puts ' '
    puts green("    lzcode ")+cyan("option ")+magenta("keyword")
    puts ' '
    puts white("    An automatic tool of GitLab.")
    puts ' '
    puts white_("Option:")
    puts ' '
    puts cyan("     -d ")+white("   Download a repository.(Fork & Clone)")
    puts ' '
    puts cyan("     -u ")+white("   Update a repository.(Fetch & Merge & Stash)")
    puts ' '
    puts cyan("     -s ")+white("   Submit a repository.(Commit & Push & Create Merge Request)")
    puts ' '
    puts cyan("     -r ")+white("   Remove a fork repository on a server.")
    puts ' '
    puts cyan("     -q ")+white("   Query a repository information.")
    puts ' '
    puts cyan("     -c ")+white("   Clear user information.")
    puts ' '
    puts white_("Tips:")
    puts ' '
    puts yellow("  Please use 'Tab' to auto complete!") 
    exit
end
start() click to toggle source
# File lib/source/LZCodeInner.rb, line 110
def start()
  $ScriptPath = File.dirname(Pathname.new(__FILE__).realpath)
  case $OperationType
  when '-d'
    download()
  when '-u'
    update()
  when '-s'
    submit()
  when '-r'
    remove()
  when '-q'
    search()
  when '-c'
    clear()
  when '--help'
    showInfo()
  else
     puts 'Operation failed!'
     showInfo()
  end
end
submit() click to toggle source

Submit

# File lib/source/LZCodeInner.rb, line 283
def submit
  t = checkExistDir($Keyword)
  if t == false
    puts 'Not found the in repository current directory!'
    sayBye()
  else
    puts 'Performing commit...'
    log = system("#{$ScriptPath}/z_submit.sh #{$Keyword} #{$ForkName}")
    if log == true

      puts 'Operation completed!'

      isFork = `cd #{$Keyword};git remote -v | grep #{ForkName} | head -1`

      if isFork.chomp.include?ForkName

        last_msg = `cd #{$Keyword};git log --pretty=format:"%s" head -1`
        s = `cd #{$Keyword};git remote -v | head -1`
        addr = s.split(" ")[1]
        branch = `cd #{$Keyword};git branch | awk  '$1 == "*"{print $2}'`

        if last_msg.length > 0 && addr.length > 0 && branch.length > 0
          puts 'Performing create merge request...'
          info = LZCodeGitLab.new.creatMR(addr,branch.chomp,$Keyword,last_msg.chomp)
          if info == true
            puts 'Operation completed!'
          else
            puts 'Operation failure!'
          end
        end
      end
    else
      puts 'Operation failure!'
    end
  end
end
update() click to toggle source
# File lib/source/LZCodeInner.rb, line 166
def update()
  if $Keyword == nil
    showInfo()
  end
  run_update($Keyword)
end
white(s) click to toggle source
# File lib/source/LZCodeInner.rb, line 52
def white(s)
  return "\033[37m#{s}\033[0m"
end
white_(s) click to toggle source
# File lib/source/LZCodeInner.rb, line 48
def white_(s)
  return "\033[37;4;1m#{s}\033[0m"
end
yellow(s) click to toggle source
# File lib/source/LZCodeInner.rb, line 64
def yellow(s)
  return "\033[33m#{s}\033[0m"
end