root/lib/retrospectiva/extension_manager/extension_installer.rb

Download in other formats: Raw | Text
Revisions
Dimitrij Denissenko
Dimitrij Denissenko
Sep 19 2009 * 20:30
(12 months ago)

Revision 6111c302945b401c85bbd861f7181d536f6b8c2d

Updated GRIT's request cache Minor updates to core

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
require 'tempfile'
require 'yaml'

module Retrospectiva
  module ExtensionManager
    module ExtensionInstaller
      include ActiveSupport::Memoizable
      extend self
      
      CONFIG_FILE = File.join(RAILS_ROOT, 'config', 'runtime', 'extensions.yml')
            
      def install(extension)
        @installed_extension_names << extension.name
        returning write_extension_table do
          dump_schema
        end
      end

      def uninstall(extension)
        @installed_extension_names.delete(extension.name)        
        returning write_extension_table do
          dump_schema
        end
      end
      
      def download(uri)
        name = File.basename(uri, '.git').split('.').last
        if system("git clone --depth 1 #{uri} #{extension_path(name)}")
          FileUtils.rm_rf File.join(extension_path(name), ".git")
          true
        else
          false
        end
      end

      def installed_extension_names
        @installed_extension_names ||= if RAILS_ENV == 'test'
          ENV['RETRO_EXT'].to_s.split(/[\s,]+/).reject(&:blank?) 
        else
          YAML.load_configuration(CONFIG_FILE, [])
        end
      end

      private

        def write_extension_table
          sanitize!
          File.open(CONFIG_FILE, 'w') do |f| 
            YAML.dump(installed_extension_names, f )
          end            
        end

        def sanitize!
          valid_names = ExtensionManager.available_extensions.map(&:name)
          @installed_extension_names = (installed_extension_names & valid_names).uniq
        end
        
        def dump_schema
          require 'active_record/schema_dumper'
          File.open("#{RAILS_ROOT}/db/schema.rb", "w") do |file|
            ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
          end        
        end

        def extension_path(name = nil)
          ExtensionManager.extension_path(name)
        end   

    end
  end
end