Module: GitGem::Action

Defined in:
lib/gitgem/action.rb

Class Method Summary collapse

Class Method Details

.add(repo_alias, repo) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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? ([email protected]): "
    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) ⇒ Object



35
36
37
38
39
# 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) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object



41
42
43
44
45
# 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