Class: BuildpackSupport::BuildpackVersion

Inherits:
Object
  • Object
show all
Extended by:
DirectoryFinder
Defined in:
lib/buildpack_support/buildpack_version.rb

Overview

A representation of the buildpack’s version. The buildpack’s version is determined using the following algorithm:

  1. using the config/version.yml file if it exists

  2. using git to determine the remote and hash if the buildpack is in a git repository

  3. unknown

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DirectoryFinder

load_path_peer

Constructor Details

#initialize(should_log = true) ⇒ BuildpackVersion

Creates a new instance



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/buildpack_support/buildpack_version.rb', line 48

def initialize(should_log = true)
  configuration = ConfigurationUtils.new(should_log).load 'version'
  @hash         = configuration['hash'] || hash
  @offline      = configuration['offline'] || ENV['OFFLINE'].to_b
  @remote       = configuration['remote'] || remote
  @version      = configuration['version'] || ENV['VERSION'] || @hash

  return unless should_log

  logger = Logging::LoggerFactory.instance.get_logger BuildpackVersion
  logger.debug { to_s }
end

Instance Attribute Details

#offlineBoolean (readonly)

Returns true if the buildpack is offline, false otherwise.

Returns:

  • (Boolean)

    true if the buildpack is offline, false otherwise



37
38
39
# File 'lib/buildpack_support/buildpack_version.rb', line 37

def offline
  @offline
end

#versionString? (readonly)

Returns the version name of this version, or nil if it cannot be determined.

Returns:

  • (String, nil)

    the version name of this version, or nil if it cannot be determined



45
46
47
# File 'lib/buildpack_support/buildpack_version.rb', line 45

def version
  @version
end

Instance Method Details

#to_hashHash

Returns a Hash representation of the buildpack version.

Returns:

  • (Hash)

    a representation of the buildpack version



64
65
66
67
68
69
70
71
72
73
# File 'lib/buildpack_support/buildpack_version.rb', line 64

def to_hash
  h            = {}

  h['hash']    = @hash if @hash
  h['offline'] = @offline if @offline
  h['remote']  = @remote if @remote
  h['version'] = @version if @version

  h
end

#to_s(human_readable = true) ⇒ String

Creates a string representation of the version. The string representation looks like the following: [[<VERSION> [(offline)] | ] <REMOTE>#<HASH>] | [unknown]. Some examples:

2.1.2 (offline) | github.com/cloudfoundry/java-buildpack.git#12345 (custom version number, offline buildpack) abcde | github.com/cloudfoundry/java-buildpack.git#abcde (default version number, online buildpack) github.com/cloudfoundry/java-buildpack#12345 (cloned buildpack) unknown (un-packaged, un-cloned)

Parameters:

  • human_readable (Boolean) (defaults to: true)

    whether the output should be human readable or machine readable

Returns:

  • (String)

    a String representation of the version



85
86
87
88
89
90
91
92
93
94
# File 'lib/buildpack_support/buildpack_version.rb', line 85

def to_s(human_readable = true)
  s = []
  s << @version if @version
  s << (human_readable ? '(offline)' : 'offline') if @offline
  s << '|' if @version && human_readable
  s << "#{@remote}##{@hash}" if @remote && @hash
  s << 'unknown' if s.empty?

  s.join(human_readable ? ' ' : '-')
end