Class: Babushka::SourcePool

Inherits:
Object show all
Includes:
LogHelpers, Singleton
Defined in:
lib/babushka/source_pool.rb

Constant Summary collapse

SEPARATOR =
':'

Instance Method Summary collapse

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Instance Method Details

#all_presentObject



22
23
24
# File 'lib/babushka/source_pool.rb', line 22

def all_present
  current.concat(Source.present)
end

#anonymousObject



26
27
28
# File 'lib/babushka/source_pool.rb', line 26

def anonymous
  @anonymous ||= Source.new(nil, :name => 'anonymous')
end

#coreObject



30
31
32
# File 'lib/babushka/source_pool.rb', line 30

def core
  @core ||= Source.new(Path.path / 'deps', :name => 'core')
end

#currentObject



9
10
11
12
# File 'lib/babushka/source_pool.rb', line 9

def current
  @current ||= default.concat(standard)
  @current.dup
end

#current_dirObject



34
35
36
# File 'lib/babushka/source_pool.rb', line 34

def current_dir
  @current_dir ||= Source.new('./babushka-deps', :name => 'current dir')
end

#current_load_optsObject



142
143
144
# File 'lib/babushka/source_pool.rb', line 142

def current_load_opts
  current_load_context[:opts] || {}
end

#current_load_pathObject



138
139
140
# File 'lib/babushka/source_pool.rb', line 138

def current_load_path
  current_load_context[:path].try(:p)
end

#current_load_sourceObject



134
135
136
# File 'lib/babushka/source_pool.rb', line 134

def current_load_source
  current_real_load_source || Base.sources.anonymous
end

#current_namesObject



18
19
20
# File 'lib/babushka/source_pool.rb', line 18

def current_names
  current.map {|source| source.deps.names }.flatten.uniq
end

#current_real_load_sourceObject



130
131
132
# File 'lib/babushka/source_pool.rb', line 130

def current_real_load_source
  current_load_context[:source]
end

#defaultObject



14
15
16
# File 'lib/babushka/source_pool.rb', line 14

def default
  [anonymous, core].dup
end

#dep_for(dep_spec, opts = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/babushka/source_pool.rb', line 55

def dep_for dep_spec, opts = {}
  if dep_spec.is_a?(Dep)
    dep_spec
  elsif dep_spec[/#{SEPARATOR}/] # If a source was specified, that's where we load from.
    source_name, dep_name = dep_spec.split(SEPARATOR, 2)
    source_for(source_name).find(dep_name)
  elsif opts[:from] # Next, try opts[:from], the requiring dep's source.
    opts[:from].find(dep_spec) || dep_for(dep_spec)
  else # Otherwise, try the standard set.
    matches = Base.sources.current.map {|source| source.find(dep_spec) }.flatten.compact
    if matches.length > 1
      log "Multiple sources (#{matches.map(&:dep_source).map(&:name).join(',')}) contain a dep called '#{dep_spec}'."
    else
      matches.first
    end
  end
end

#list!Object



101
102
103
104
105
106
107
108
109
# File 'lib/babushka/source_pool.rb', line 101

def list!
  Logging.log_table(
    ['Name', 'Source path', 'Type', 'Last updated'],
    Source.present.tap {|sources|
      log "There #{sources.length == 1 ? 'is' : 'are'} #{sources.length} source#{'s' unless sources.length == 1} in #{Source.source_prefix}:"
      log ''
    }.map(&:description_pieces)
  )
end

#load_context(context, &block) ⇒ Object



123
124
125
126
127
128
# File 'lib/babushka/source_pool.rb', line 123

def load_context context, &block
  (@load_contexts ||= []).push context
  yield
ensure
  @load_contexts.pop
end

#local_only(&block) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/babushka/source_pool.rb', line 115

def local_only &block
  previously = @local_only
  @local_only = true
  yield
ensure
  @local_only = previously
end

#local_only?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/babushka/source_pool.rb', line 111

def local_only?
  @local_only
end

#personalObject



38
39
40
# File 'lib/babushka/source_pool.rb', line 38

def personal
  @personal ||= Source.new('~/.babushka/deps', :name => 'personal')
end

#source_for(name) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/babushka/source_pool.rb', line 46

def source_for name
  (
    all_present.detect {|source| source.name == name } ||
    Source.for_remote(name)
  ).tap {|source|
    source.load!(Base.task.opt(:update))
  }
end

#standardObject



42
43
44
# File 'lib/babushka/source_pool.rb', line 42

def standard
  [current_dir, personal]
end

#template_for(template_spec, opts = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/babushka/source_pool.rb', line 73

def template_for template_spec, opts = {}
  if template_spec.nil?
    nil
  elsif template_spec[/#{SEPARATOR}/] # If a source was specified, that's where we load from.
    source_name, template_name = template_spec.split(SEPARATOR, 2)
    source_for(source_name).find_template(template_name)
  elsif opts[:from]
    opts[:from].find_template(template_spec) || template_for(template_spec)
  else
    matches = Base.sources.default.map {|source| source.find_template(template_spec) }.flatten.compact
    if matches.length > 1
      log "Multiple sources (#{matches.map(&:source).map(&:name).join(',')}) contain a template called '#{template_name}'."
    else
      matches.first
    end
  end
end

#update!Object



91
92
93
94
95
96
97
98
99
# File 'lib/babushka/source_pool.rb', line 91

def update!
  all_present.select {|source|
    source.cloneable?
  }.tap {|sources|
    log "Updating #{sources.length} source#{'s' unless sources.length == 1}."
  }.map {|source|
    source.update!
  }.all?
end