public
Description: Webrat - Ruby Acceptance Testing for Web applications
Homepage: http://gitrdoc.com/brynary/webrat/tree/master/
Clone URL: git://github.com/brynary/webrat.git
webrat / spec / integration / merb / tasks / merb.thor / main.thor
100644 150 lines (123 sloc) 4.385 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require "rubygems"
require "rubygems/source_index"
require "rubygems/dependency_installer"
require "rubygems/uninstaller"
require "fileutils"
require File.join(File.dirname(__FILE__), "utils")
require File.join(File.dirname(__FILE__), "gem_ext")
require File.join(File.dirname(__FILE__), "ops")
 
$INSTALLING = []
 
module Merb
  
  class Gem < Thor
    extend ColorfulMessages
    
    def initialize
      dirs = [Dir.pwd, File.dirname(__FILE__) / ".."]
      root = dirs.find {|d| File.file?(d / "config" / "dependencies.rb")}
      
      if root
        @depsrb = root / "config" / "dependencies.rb"
      else
        self.class.error "dependencies.rb was not found"
        exit!
      end
      
      FileUtils.mkdir_p(Dir.pwd / "gems")
      
      @list = Collector.collect(File.read(@depsrb))
      @idx = ::Gem::SourceIndex.new.load_gems_in("gems/specifications")
    end
    
    def list
      require "pp"
      pp @list
    end
    
    desc "redeploy", "Syncs up gems/cache with gems/gems. All gems in the cache " \
                     "that are not already installed will be installed from the " \
                     "cache. All installed gems that are not in the cache will " \
                     "be uninstalled."
    def redeploy
      gem_dir = Dir.pwd / "gems" / "gems"
      cache_dir = Dir.pwd / "gems" / "cache"
      
      gems = Dir[gem_dir / "*"].map! {|n| File.basename(n)}
      cache = Dir[cache_dir / "*.gem"].map! {|n| File.basename(n, ".gem")}
      new_gems = cache - gems
      outdated = gems - cache
      idx = ::Gem::SourceIndex.new
      idx.load_gems_in(Dir.pwd / "gems" / "specifications")
 
      new_gems.each do |g|
        installer = ::Gem::Installer.new(cache_dir / "#{g}.gem",
          :bin_dir => Dir.pwd / "bin",
          :install_dir => Dir.pwd / "gems",
          :ignore_dependencies => true,
          :user_install => false,
          :wrappers => true,
          :source_index => idx)
            
        installer.install
      end
      
      outdated.each do |g|
        /(.*)\-(.*)/ =~ g
        name, version = $1, $2
        uninstaller = ::Gem::Uninstaller.new(name,
          :version => version,
          :bin_dir => Dir.pwd / "bin",
          :install_dir => Dir.pwd / "gems",
          :ignore => true,
          :executables => true
        )
        uninstaller.uninstall
      end
    end
    
    desc "confirm", "Confirm the current setup. merb:gem:install will " \
                    "automatically run this task before committing the " \
                    "changes it makes."
    def confirm(gems = @list)
      ::Gem.path.replace([Dir.pwd / "gems"])
      ::Gem.source_index.load_gems_in(Dir.pwd / "gems" / "specifications")
      
      self.class.info "Confirming configuration..."
      
      ::Gem.loaded_specs.clear
      
      begin
        gems.each do |name, versions|
          versions ||= []
          ::Gem.activate name, *versions
        end
      rescue ::Gem::LoadError => e
        self.class.error "Configuration could not be confirmed: #{e.message}"
        self.class.rollback_trans
      end
      self.class.info "Confirmed"
    end
    
    desc 'install', 'Sync up your bundled gems with the list in config/dependencies.rb'
    def install(*gems)
      if gems.empty?
        gems = @list
      else
        gems = gems.map {|desc| name, *versions = desc.split(" ") }
      end
      
      $GEMS = gems
      
      self.class.begin_trans
      
      gems.each do |name, versions|
        dep = ::Gem::Dependency.new(name, versions || [])
        unless @idx.search(dep).empty?
          next
        end
        
        rescue_failures do
          $INSTALLING = []
          _install(dep)
        end
      end
 
      gem_dir = Dir.pwd / "gems" / "gems"
      installed_gems = Dir[gem_dir / "*"].map! {|n| File.basename(n)}
      
      list = full_list.map {|x| x.full_name}.compact
      
      (installed_gems - list).each do |g|
        /^(.*)\-(.*)$/ =~ g
        name, version = $1, $2
        uninstaller = ::Gem::Uninstaller.new(name,
          :version => version,
          :bin_dir => (Dir.pwd / "bin").to_s,
          :install_dir => (Dir.pwd / "gems").to_s,
          :ignore => true,
          :executables => true
        )
        uninstaller.uninstall
      end
      
      confirm(gems)
      
      self.class.commit_trans
    end
  end
end