Class: Babushka::Source

Inherits:
Object show all
Extended by:
LogHelpers, PathHelpers, ShellHelpers
Includes:
LogHelpers, PathHelpers
Defined in:
lib/babushka/source.rb

Direct Known Subclasses

ImplicitSource

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from PathHelpers

cd, in_build_dir, in_download_dir

Methods included from ShellHelpers

cmd_dir, current_username, log_shell, login_shell, raw_shell, shell, shell!, shell?, shell_cmd, sudo, which

Methods included from LogHelpers

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

Constructor Details

- (Source) initialize(path, name = nil, uri = nil)

Returns a new instance of Source

Raises:

  • (ArgumentError)


40
41
42
43
44
45
# File 'lib/babushka/source.rb', line 40

def initialize path, name = nil, uri = nil
  raise ArgumentError, "Sources with nil paths require a name (as the second argument)." if path.nil? && name.nil?
  raise ArgumentError, "The source URI can only be supplied if the source doesn't exist already." if !uri.nil? && !path.nil? && path.p.exists?
  init
  @path, @name, @uri = path.try(:p), name, uri
end

Instance Attribute Details

- (Object) deps (readonly)

Returns the value of attribute deps



13
14
15
# File 'lib/babushka/source.rb', line 13

def deps
  @deps
end

- (Object) name (readonly)

Returns the value of attribute name



13
14
15
# File 'lib/babushka/source.rb', line 13

def name
  @name
end

- (Object) path (readonly)

Returns the value of attribute path



13
14
15
# File 'lib/babushka/source.rb', line 13

def path
  @path
end

- (Object) templates (readonly)

Returns the value of attribute templates



13
14
15
# File 'lib/babushka/source.rb', line 13

def templates
  @templates
end

- (Object) uri (readonly)

Returns the value of attribute uri



13
14
15
# File 'lib/babushka/source.rb', line 13

def uri
  @uri
end

Class Method Details

+ (Object) default_remote_for(name)



31
32
33
34
35
36
37
38
# File 'lib/babushka/source.rb', line 31

def self.default_remote_for name
  if name == 'common'
    # This is a special case.
    "https://github.com/benhoskings/common-babushka-deps.git"
  else
    "https://github.com/#{name}/babushka-deps.git"
  end
end

+ (Object) for_path(path)



21
22
23
24
25
# File 'lib/babushka/source.rb', line 21

def self.for_path path
  source = Source.new(path)
  @sources ||= {}
  @sources[source.name] ||= source
end

+ (Object) for_remote(name)



27
28
29
# File 'lib/babushka/source.rb', line 27

def self.for_remote name
  Source.new(nil, name, default_remote_for(name))
end

+ (Object) present



15
16
17
18
19
# File 'lib/babushka/source.rb', line 15

def self.present
  source_prefix.glob('*').map {|path|
    Source.for_path(path.p)
  }
end

Instance Method Details

- (Object) ==(other)



100
101
102
103
104
105
# File 'lib/babushka/source.rb', line 100

def == other
  [:name, :uri, :type].all? {|method_name| other.respond_to? method_name } &&
  name == other.name &&
  uri == other.uri &&
  type == other.type
end

- (Object) add!



117
118
119
120
121
122
123
124
# File 'lib/babushka/source.rb', line 117

def add!
  if !remote?
    log "Nothing to add for #{name}."
  else
    raise_unless_addable!
    update!
  end
end

- (Object) clear!



126
127
128
129
# File 'lib/babushka/source.rb', line 126

def clear!
  deps.clear!
  templates.clear!
end

- (Object) description_pieces



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

def description_pieces
  [
    name,
    uri.to_s,
    type,
    ("#{updated_at.round.xsecs} ago" if remote?)
  ]
end

- (Object) find(dep_spec)



107
108
109
110
# File 'lib/babushka/source.rb', line 107

def find dep_spec
  load!
  deps.for(dep_spec)
end

- (Object) find_template(template_spec)



112
113
114
115
# File 'lib/babushka/source.rb', line 112

def find_template template_spec
  load!
  templates.for(template_spec)
end

- (Boolean) implicit?

Returns:

  • (Boolean)


67
68
69
# File 'lib/babushka/source.rb', line 67

def implicit?
  type == :implicit
end

- (Object) inspect



205
206
207
# File 'lib/babushka/source.rb', line 205

def inspect
  "#<Source:#{object_id} '#{name}' (#{path} <- #{uri}) (#{deps.count} dep#{'s' unless deps.count == 1})>"
end

- (Object) load!(should_update = false)



131
132
133
134
135
136
137
138
# File 'lib/babushka/source.rb', line 131

def load! should_update = false
  unless @currently_loading
    @currently_loading = true
    update! if remote? && (!repo? || should_update)
    load_deps! unless implicit? # implicit sources can't be loaded.
    @currently_loading = false
  end
end

- (Object) load_deps!



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/babushka/source.rb', line 140

def load_deps!
  unless @loaded
    path.p.glob('**/*.rb').each {|f|
      Base.sources.load_context :source => self, :path => f do
        load f
      end
    }
    debug "Loaded #{deps.count} deps from #{path}."
    @loaded = true
  end
rescue StandardError, SyntaxError => e
  clear!
  raise SourceLoadError.new(e.message).tap {|raised| raised.set_backtrace(e.backtrace) }
end

- (Boolean) local?

Returns:

  • (Boolean)


71
72
73
# File 'lib/babushka/source.rb', line 71

def local?
  type == :local
end

- (Boolean) present?

Returns:

  • (Boolean)


59
60
61
# File 'lib/babushka/source.rb', line 59

def present?
  path.exists?
end

- (Boolean) remote?

Returns:

  • (Boolean)


75
76
77
# File 'lib/babushka/source.rb', line 75

def remote?
  type == :remote
end

- (Object) repo



79
80
81
# File 'lib/babushka/source.rb', line 79

def repo
  @repo ||= Babushka::GitRepo.new(path)
end

- (Boolean) repo?

Returns:

  • (Boolean)


83
84
85
# File 'lib/babushka/source.rb', line 83

def repo?
  repo.exists? && (repo.root == path)
end

- (Object) type



63
64
65
# File 'lib/babushka/source.rb', line 63

def type
  uri.nil? ? :local : :remote
end

- (Object) update!



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/babushka/source.rb', line 155

def update!
  if @updated
    debug "Already pulled #{name} (#{uri}) this session."
    true
  elsif Base.sources.local_only?
    debug "Not pulling #{name} (#{uri}) - in local-only mode."
    true
  elsif @updated == false
    debug "Not updating #{name} (#{uri}) - it's offline."
  elsif repo.exists? && repo.dirty?
    log "Not updating #{name} (#{path}) because there are local changes."
  elsif repo.exists? && repo.ahead?
    @updated = false # So the ahead? check doesn't run again, for when there's no network.
    log "Not updating #{name} (#{path}) because it's ahead of origin."
  else
    GitHelpers.git(uri, :to => path, :log => true).tap {|result|
      log "Marking #{uri} as offline for this run." unless result
      @updated = result || false
    }
  end
end

- (Object) updated_at



87
88
89
# File 'lib/babushka/source.rb', line 87

def updated_at
  Time.now - File.mtime(path)
end