module GitGem::Action

Public Class Methods

add(repo_alias, repo) click to toggle source
# File lib/gitgem/action.rb, line 10
def add(repo_alias, repo)
  repo_alias = repo if repo_alias.nil? || repo_alias.empty?

  abort("Alias could not start with '.'") if repo_alias.start_with?(".")

  should_add = true

  result = read_alias(repo_alias)

  unless result.nil?
    puts "#{repo_alias} is already exist: #{result}"
    printf "replace? (y/n): "
    input = STDIN.gets.chomp

    if input.start_with?("y")
      remove(repo_alias)
      should_add = true
    else
      should_add = false
    end
  end

  system("echo #{repo_alias}=#{repo} >> #{alias_path}") if should_add
end
install(repo_alias, gem_dir, bindir) click to toggle source
# File lib/gitgem/action.rb, line 47
def install(repo_alias, gem_dir, bindir)
  abort("Please specify alias like alias/gem") if repo_alias.nil? || repo_alias.empty?
  result = read_alias(repo_alias)
  repo = ""
  if result.nil?
    printf "Could not found repo named #{repo_alias}, please enter? (xxx@xxx.git): "
    repo = STDIN.gets.chomp
    add(repo_alias, repo)
  else
    repo = result.gsub("#{repo_alias}=", "")
  end
  abort("Error git repo format #{repo_alias}, please check again.") unless repo.end_with?(".git")


  alias_dir = File.join(base_dir, repo_alias)
  repo_dir = File.join(alias_dir, File.basename(repo, ".git"))
  pwd = Dir.pwd

  unless File.exist?(alias_dir)
    Dir.chdir(base_dir)
    FileUtils.mkdir_p(alias_dir)
    Dir.chdir(alias_dir)
    system("git clone -q #{repo}")
  end

  Dir.chdir(repo_dir)
  system("git checkout -- .")
  system("git pull -q")

  # 进入 gem 的目录,build gemspec
  Dir.chdir(gem_dir)

  gemspecs = Dir.glob("*.gemspec")
  abort("Could not find gemspec in #{File.join(Dir.pwd)}") if gemspecs.nil? || gemspecs.empty?
  abort("Mutiple gemspecs found in #{File.join(Dir.pwd)}") if gemspecs.count > 1

  Dir.glob("*.gem").each do |gem|
    FileUtils.rm_rf(gem)
  end

  result = system("sudo gem build #{gemspecs.first}")

  abort("Fail to build gemspec") unless result

  gems = Dir.glob("*.gem")
  abort("Could not find gem in #{File.join(Dir.pwd)}") if gemspecs.nil? || gemspecs.empty?

  # 获取最新 gem
  gem = gems.first
  bin = "-n #{bindir}" unless bindir.nil?

  system("sudo gem install #{gem} #{bin}")

  Dir.chdir(pwd)
end
remove(repo_alias) click to toggle source
# File lib/gitgem/action.rb, line 35
def remove(repo_alias)
  result = read_alias(repo_alias)
  FileUtils.rm_rf(File.join(base_dir, repo_alias)) unless result.nil?
  system("sed -i '' '/#{repo_alias}=/d' #{alias_path}")
end
uninstall(repo_alias, gem_name, bindir) click to toggle source
# File lib/gitgem/action.rb, line 103
def uninstall(repo_alias, gem_name, bindir)
  # abort("Please specify alias like alias/gem") if repo_alias.nil? || repo_alias.empty?
  # result = read_alias(repo_alias)
  # abort("Could not find alias named #{repo_alias}, please check again.") if result.nil?
  # repo = result.gsub("#{repo_alias}=", "")
  #
  # alias_dir = File.join(base_dir, repo_alias)
  # repo_dir = File.join(alias_dir, File.basename(repo, ".git"))
  #
  # gems = Dir.glob(File.join(repo_dir, gem_dir, "*.gem"))
  # abort("Could not find gem in #{File.join(repo_dir, gem_dir)}") if gems.nil? || gems.empty?
  #
  # bin = "-n #{bindir}" unless bindir.nil?
  # gem_name = File.basename(gems.first, ".gem").split("-")[0...-1].join("-")
  # system("sudo gem unisntall #{gem_name} #{bin}")
  bin = "-n #{bindir}" unless bindir.nil?
  system("sudo gem uninstall #{gem_name} #{bin}")
end
update(repo_alias, gem_name, bindir) click to toggle source
# File lib/gitgem/action.rb, line 41
def update(repo_alias, gem_name, bindir)
  # system("sudo gem unisntall #{gem_name} -n #{bindir}")
  uninstall(repo_alias, gem_name, bindir)
  install(repo_alias, gem_name, bindir)
end

Private Class Methods

alias_path() click to toggle source
# File lib/gitgem/action.rb, line 134
def alias_path
  path = File.join(base_dir, "alias")
  FileUtils.mkdir_p(base_dir)
  system("touch #{path}") unless File.exist?(path)
  path
end
base_dir() click to toggle source
# File lib/gitgem/action.rb, line 130
def base_dir
  File.join(ENV["HOME"], ".gitgem")
end
read_alias(repo_alias) click to toggle source
# File lib/gitgem/action.rb, line 124
def read_alias(repo_alias)
  result = `cat #{alias_path} | grep #{repo_alias}=`.chomp
  return nil if result.nil? || result.empty?
  result
end