Module: GemTools

Extended by:
GemTools
Included in:
GemTools
Defined in:
lib/gem_tools/gem_tools.rb

Instance Method Summary collapse

Instance Method Details

#check_gem(command, name, version = '') ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/gem_tools/gem_tools.rb', line 82

def check_gem(command, name, version='')
  spec = YAML.load(`#{command} spec #{name} 2> /dev/null`)
  loaded = false
  begin
    loaded = require_gem name, version
    version = spec.version.version
  rescue Exception
  end
  [spec, loaded, version]
end

#commandsObject



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
# File 'lib/gem_tools/gem_tools.rb', line 47

def commands
  config = load_config
  gems = config['gems']
  commands = []

  unless gems.nil?
    docs = ''
    unless OPTIONS.has_key?(:docs)
      docs << '--no-rdoc ' unless (`rdoc -v`).nil?
      docs << '--no-ri ' unless (`ri -v`).nil?
    end

    gem_command = config['gem_command'] || 'gem'
    gem_dash_y = "1.0" > Gem::RubyGemsVersion ? '-y' : ''

    gems.each do |gem|
      spec, loaded, version = check_gem(gem_command, gem['name'], gem['version'])
      # if forced
      # or the spec version doesn't match the required version
      # or require_gem returns false
      #    (return false also happens if the gem has already been loaded)
      if OPTIONS.has_key?(:force) || !spec || (! loaded && version != gem['version'])
        gem_config = gem['config'] ? " -- #{gem['config']}" : ''
        source = gem['source'] || config['source'] || nil
        source = "--source #{source}" if source
        cmd = "#{gem_command} install #{gem['path'] || gem['name']} -v '#{gem['version']}' #{gem_dash_y} #{source} #{docs} #{gem_config}"
        commands << cmd
      else
        puts "#{gem['name']} #{gem['version']} already installed"
      end
    end
  end
  commands
end

#dryrunObject



43
44
45
# File 'lib/gem_tools/gem_tools.rb', line 43

def dryrun
  puts "\n#{commands.join("\n")}\n\n"
end

#find_configObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gem_tools/gem_tools.rb', line 120

def find_config
  dirs = []
  if Object.const_defined?('RAILS_ROOT')
    dirs << RAILS_ROOT
    dirs << File.join(RAILS_ROOT, 'config')
  end
  dirs << %w{. config}
  dirs.flatten!
  
  dirs.each do |dir|
    config_file = File.join(dir, 'gems.yml')
    return config_file if File.exist?(config_file)
  end
  nil
end

#helpObject



24
25
26
# File 'lib/gem_tools/gem_tools.rb', line 24

def help
  puts OPTIONS
end

#installObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gem_tools/gem_tools.rb', line 28

def install
  require 'rubygems'
  if RUBY_PLATFORM =~ /MSWIN/
    puts "gemtools install doesn't currently work in windows. The commands you need to install the gems will be printed out for you.\n"
    dryrun
    return
  else
    commands.each do |command|
      ret = system command
      # something bad happened, pass on the message
      p $? unless ret
    end
  end
end

#load_configObject



111
112
113
114
115
116
117
118
# File 'lib/gem_tools/gem_tools.rb', line 111

def load_config
  config_file = find_config
  unless config_file
    puts 'Could not find a gems.yml, checked . and ./config'
    exit 1
  end
  Yamler.load(config_file)
end

#load_gemsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gem_tools/gem_tools.rb', line 93

def load_gems
  config = load_config
  unless config['gems'].nil?
    gems = config['gems'].reject {|gem_info| ! gem_info['load'] }
    gems.each do |gem_info|
      if defined?(gem)
        gem gem_info['name'], gem_info['version']
      else
        require_gem gem_info['name'], gem_info['version']
      end
      reqs = [(gem_info['require_name'] || gem_info['name'])].flatten
      reqs.each do |req|
        require req
      end
    end
  end
end

#run(cmd) ⇒ Object



8
9
10
# File 'lib/gem_tools/gem_tools.rb', line 8

def run(cmd)
  send(cmd.to_sym)# rescue help
end

#setupObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gem_tools/gem_tools.rb', line 12

def setup
  require 'fileutils'
  dest_file = File.join(ARGV[1], '/config/gems.yml')
  if File.exist?(dest_file) && ! OPTIONS.has_key?(:force)
    puts "#{dest_file} already exists.\n\ngemtools install #{ARGV[1]} --force\n\nto overwrite"
    exit 1
  else
    FileUtils.copy(File.join(File.dirname(__FILE__), '../config/gems.template.yml'), dest_file)
    puts "#{dest_file} created"
  end
end