Class: Repositories

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/commands/plugin/repositories.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_file = File.join(find_home, ".rails", "plugin_source_cache.yml")) ⇒ Repositories

Returns a new instance of Repositories.



8
9
10
11
# File 'lib/commands/plugin/repositories.rb', line 8

def initialize(cache_file = File.join(find_home, ".rails", "plugin_source_cache.yml"))
  @cache_file = File.expand_path(cache_file)
  load!
end

Instance Attribute Details

#source_cacheObject

Returns the value of attribute source_cache.



6
7
8
# File 'lib/commands/plugin/repositories.rb', line 6

def source_cache
  @source_cache
end

Class Method Details

.each(&block) ⇒ Object



125
126
127
# File 'lib/commands/plugin/repositories.rb', line 125

def self.each(&block)
  self.instance.each(&block)
end

.instanceObject



120
121
122
123
# File 'lib/commands/plugin/repositories.rb', line 120

def self.instance
  # TODO Use test cache file if $testing
  @instance ||= Repositories.new
end

Instance Method Details

#add(uri) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/commands/plugin/repositories.rb', line 17

def add(uri)
  unless find { |repo| repo.uri == uri }
    repository = Repository.new(uri)
    refresh_repository!(repository)
    @repositories.push(repository).last
  end
end

#allObject



33
34
35
# File 'lib/commands/plugin/repositories.rb', line 33

def all
  @repositories
end

#cached_pluginsObject

List the known plugins from the sources cache.



52
53
54
55
56
# File 'lib/commands/plugin/repositories.rb', line 52

def cached_plugins
  @source_cache['plugins'].keys.sort.map do |plugin_name| 
    Plugin.new(@source_cache['plugins'][plugin_name]['plugin'], plugin_name)
  end
end

#defaultsObject



95
96
97
98
99
100
# File 'lib/commands/plugin/repositories.rb', line 95

def defaults
  {
    'repositories' => ['http://dev.rubyonrails.com/svn/rails/plugins/'],
    'plugins' => {}
    }
end

#each(&block) ⇒ Object



13
14
15
# File 'lib/commands/plugin/repositories.rb', line 13

def each(&block)
  @repositories.each(&block)
end

#exist?(uri) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/commands/plugin/repositories.rb', line 29

def exist?(uri)
  @repositories.detect{|repo| repo.uri == uri }
end

#find_homeObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/commands/plugin/repositories.rb', line 102

def find_home
  ['HOME', 'USERPROFILE'].each do |homekey|
    return ENV[homekey] if ENV[homekey]
  end
  if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
  end
  begin
    File.expand_path("~")
  rescue StandardError => ex
    if File::ALT_SEPARATOR
      "C:/"
    else
      "/"
    end
  end
end

#find_plugin(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/commands/plugin/repositories.rb', line 37

def find_plugin(name)
  # Try to get plugin from cache first
  if @source_cache['plugins'].has_key?(name) && @source_cache['plugins'][name].has_key?('plugin')
    return Plugin.new(@source_cache['plugins'][name]['plugin'], name)
  end
  
  @repositories.each do |repo|
    repo.each do |plugin|
      return plugin if plugin.name == name
    end
  end
  return nil
end

#load!Object



80
81
82
83
84
# File 'lib/commands/plugin/repositories.rb', line 80

def load!
  @source_cache = File.exist?(@cache_file) ? YAML.load(File.read(@cache_file)) : defaults
  @source_cache = defaults if @source_cache.keys.nil?
  @repositories = @source_cache['repositories'].map { |source| Repository.new(source.strip) }
end

#plugin_countObject

Returns the number of known plugins in the cache.



59
60
61
62
63
# File 'lib/commands/plugin/repositories.rb', line 59

def plugin_count
  source_cache['plugins'].keys.length
rescue
  0
end

#refresh!Object

Gets the updated list of plugins available at each repository.



66
67
68
# File 'lib/commands/plugin/repositories.rb', line 66

def refresh!
  @repositories.each { |repo| refresh_repository!(repo) }
end

#refresh_repository!(repository) ⇒ Object

Gets the list of plugins available at this repository.



71
72
73
74
75
76
77
# File 'lib/commands/plugin/repositories.rb', line 71

def refresh_repository!(repository)
  repository.plugins.each do |plugin|
    if about_hash = plugin.about(true)
      @source_cache['plugins'][plugin.name] = about_hash
    end
  end    
end

#remove(uri) ⇒ Object



25
26
27
# File 'lib/commands/plugin/repositories.rb', line 25

def remove(uri)
  @repositories.reject!{|repo| repo.uri == uri}
end

#saveObject



86
87
88
89
90
91
92
93
# File 'lib/commands/plugin/repositories.rb', line 86

def save
  mkdir_p(File.dirname(@cache_file)) unless File.exist?(File.dirname(@cache_file))
  File.open(@cache_file, 'w') do |f|
    @source_cache['repositories'] = @repositories.map {|r| r.uri }.uniq
    f.write(@source_cache.to_yaml)
    f.write("\n")
  end
end