Class: Coherent::Repositories

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_file = File.join(find_home, ".coherent-plugin-sources")) ⇒ Repositories

Returns a new instance of Repositories.



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

def initialize(cache_file = File.join(find_home, ".coherent-plugin-sources"))
  @cache_file = File.expand_path(cache_file)
  load!
end

Class Method Details

.each(&block) ⇒ Object



87
88
89
# File 'lib/plugin/repositories.rb', line 87

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

.instanceObject



83
84
85
# File 'lib/plugin/repositories.rb', line 83

def self.instance
  @instance ||= Repositories.new
end

Instance Method Details

#add(uri) ⇒ Object



15
16
17
18
19
# File 'lib/plugin/repositories.rb', line 15

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

#allObject



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

def all
  @repositories
end

#defaultsObject



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

def defaults
  <<-DEFAULTS
  http://coherent.googlecode.com/svn/plugins
  DEFAULTS
end

#each(&block) ⇒ Object



11
12
13
# File 'lib/plugin/repositories.rb', line 11

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

#exist?(uri) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#find_homeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/plugin/repositories.rb', line 65

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



33
34
35
36
37
38
39
40
# File 'lib/plugin/repositories.rb', line 33

def find_plugin(name)
  @repositories.each do |repo|
    repo.each do |plugin|
      return plugin if plugin.name == name
    end
  end
  return nil
end

#load!Object



42
43
44
45
46
47
48
# File 'lib/plugin/repositories.rb', line 42

def load!
  contents = File.exist?(@cache_file) ? File.read(@cache_file) : defaults
  contents = defaults if contents.empty?
  @repositories = contents.split(/\n/).reject do |line|
    line =~ /^\s*#/ or line =~ /^\s*$/
  end.map { |source| Repository.new(source.strip) }
end

#remove(uri) ⇒ Object



21
22
23
# File 'lib/plugin/repositories.rb', line 21

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

#saveObject



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

def save
  File.open(@cache_file, 'w') do |f|
    each do |repo|
      f.write(repo.uri)
      f.write("\n")
    end
  end
end