Class: Dispatch

Inherits:
Object
  • Object
show all
Defined in:
lib/templates/dispatch.rb

Constant Summary collapse

DIR =
"<%= @dir %>"

Class Method Summary collapse

Class Method Details

.install(args) ⇒ Object



11
12
13
# File 'lib/templates/dispatch.rb', line 11

def install(args)
  system("gem --config-file #{DIR}/.gemrc install --bindir #{DIR}/usr/bin #{ARGV.join(' ')}")
end

.load_config(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/templates/dispatch.rb', line 15

def load_config(args)
  cfg_path =  "#{ENV['HOME']}/.gembox/config.yml"
  if File.exists?(cfg_path)
    data = YAML.load(File.open(cfg_path).read)
    if data['symlink']
      data['symlink'].each do |symlink|
        if `gem query -i -n #{symlink}`.chomp == 'false'
          system("ruby #{DIR}/dispatch.rb symlink #{symlink}")
        end
      end
    end
  end
end

.method_missing(method, *args) ⇒ Object



107
108
109
110
# File 'lib/templates/dispatch.rb', line 107

def method_missing(method, *args)
  # must be a gem commands
  system("gem --config-file #{DIR}/.gemrc #{method} #{args.join(' ')}")
end


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
102
103
104
105
# File 'lib/templates/dispatch.rb', line 53

def symlink(args)
  data = YAML.load(File.open("#{DIR}/gems.yaml").read)
  args.each do |gem_name|
    if data[gem_name] && data[gem_name].keys.length > 0
      FileUtils.mkdir_p("#{DIR}/gems/")
      FileUtils.mkdir_p("#{DIR}/specifications/")
      versions = []
      if data[gem_name].keys.length == 1
        versions << data[gem_name].keys[0]
      elsif data[gem_name].keys.length > 1
        puts 'Select gem to symlink:'
        index = 1
        data[gem_name].keys.sort{|a,b| b <=> a}.each do |key|
          puts " #{index}. #{gem_name}-#{key}"
          index += 1
        end
        puts " #{index}. All versions"
        print "> "
        choice = STDIN.gets
        if choice == index
          versions = data[gem_name].keys
        else
          versions << data[gem_name].keys[choice.to_i - 1]
        end
      end

      versions.each do |version|
        gemdir = data[gem_name][version]
        gemname = gem_name+'-'+version
        gem_file = gemdir + '/gems/' + gemname
        spec_file = gemdir + '/specifications/' + gemname + '.gemspec'
        unless File.exists?("#{DIR}/gems/#{gemname}")
          `ln -s #{gem_file} #{DIR}/gems/#{gemname} && ln -s #{spec_file} #{DIR}/specifications/#{gemname}.gemspec`
          puts "Successfully symlinked #{gemname}"
        else
          puts "#{gemname} already exists"
        end
    
        # check dependencies
        data = `gem dependency #{gem_name} -v #{version}`.chomp.split("\n")
        data.shift
        data.each do |dependency|
          name = dependency.match(/\s+(.*?)\s\(/).captures[0]
          if `gem query -i -n #{name}`.strip != 'true'
            symlink([name])
          end
        end
      end
    else
      puts "Could not find gem #{gem_name}"
    end
  end
end


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/templates/dispatch.rb', line 29

def unsymlink(args)
  gem_name = args.shift
  data = `gem query -d -n #{gem_name}`.strip.split("\n")
  versions = []
  data.each do |line|
    if line =~ /^[a-z]/i
      versions << line.match(/\((.*?)\)/).captures[0]
    end
  end
  if versions.length == 1
    version = versions[0]
    if `ls -la #{DIR}/gems/ | grep #{gem_name}-#{version}` =~ /^l/
      `rm #{DIR}/gems/#{gem_name}-#{version} && rm #{DIR}/specifications/#{gem_name}-#{version}.gemspec`
      puts "Gem #{gem_name} is been unsymlinked"
    else
      puts "Gem #{gem_name} is installed, but not symlinked"
    end
  elsif versions.length > 1
    puts "more"
  else
    puts "Gem #{gem_name} is not installed"
  end
end