Module: Pkg::Util::Version

Defined in:
ext/packaging/lib/packaging/util/version.rb

Overview

Utility methods used for versioning projects for various kinds of packaging

Constant Summary collapse

GIT =
Pkg::Util::Tool::GIT

Class Method Summary collapse

Class Method Details

.el_versionObject

Utility method to return the dist method if this is a redhat box. We use this in rpm packaging to define a dist macro, and we use it in the pl:fetch task to disable ssl checking for redhat 5 because it has a certs bundle so old by default that it’s useless for our purposes.



250
251
252
253
254
255
256
257
# File 'ext/packaging/lib/packaging/util/version.rb', line 250

def el_version
  if File.exists?('/etc/fedora-release')
    nil
  elsif File.exists?('/etc/redhat-release')
    rpm = Pkg::Util::Tool.find_tool('rpm', :required => true)
    return %x{#{rpm} -q --qf \"%{VERSION}\" $(#{rpm} -q --whatprovides /etc/redhat-release )}
  end
end

.fail_on_dirty_sourceObject



171
172
173
174
175
176
177
# File 'ext/packaging/lib/packaging/util/version.rb', line 171

def fail_on_dirty_source
  if source_dirty?
    fail "
The source tree is dirty, e.g. there are uncommited changes. Please
commit/discard changes and try again."
  end
end

.get_base_pkg_versionObject



138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/packaging/lib/packaging/util/version.rb', line 138

def get_base_pkg_version
  dash = get_dash_version
  if dash.include?("rc")
    # Grab the rc number
    rc_num = dash.match(/rc(\d+)/)[1]
    ver = dash.sub(/-?rc[0-9]+/, "-0.#{Pkg::Config.release}rc#{rc_num}").gsub(/(rc[0-9]+)-(\d+)?-?/, '\1.\2')
  else
    ver = dash.gsub('-','.') + "-#{Pkg::Config.release}"
  end

  ver.split('-')
end

.get_dash_versionObject



103
104
105
106
107
108
109
# File 'ext/packaging/lib/packaging/util/version.rb', line 103

def get_dash_version
  if info = git_describe_version
    info.join('-')
  else
    get_pwd_version
  end
end

.get_debversionObject



151
152
153
# File 'ext/packaging/lib/packaging/util/version.rb', line 151

def get_debversion
  get_base_pkg_version.join('-') << "#{Pkg::Config.packager}1"
end

.get_dot_versionObject



130
131
132
# File 'ext/packaging/lib/packaging/util/version.rb', line 130

def get_dot_version
  get_dash_version.gsub('-', '.')
end

.get_ips_versionObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/packaging/lib/packaging/util/version.rb', line 116

def get_ips_version
  if info = git_describe_version
    version, commits, dirty = info
    if commits.to_s.match('^rc[\d]+')
      commits = info[2]
      dirty   = info[3]
    end
    osrelease = uname_r
    "#{version},#{osrelease}-#{commits.to_i}#{dirty ? '-dirty' : ''}"
  else
    get_pwd_version
  end
end

.get_origversionObject



155
156
157
# File 'ext/packaging/lib/packaging/util/version.rb', line 155

def get_origversion
  Pkg::Config.debversion.split('-')[0]
end

.get_pwd_versionObject



134
135
136
# File 'ext/packaging/lib/packaging/util/version.rb', line 134

def get_pwd_version
  Dir.pwd.split('.')[-1]
end

.get_rpmreleaseObject



163
164
165
# File 'ext/packaging/lib/packaging/util/version.rb', line 163

def get_rpmrelease
  get_base_pkg_version[1]
end

.get_rpmversionObject



159
160
161
# File 'ext/packaging/lib/packaging/util/version.rb', line 159

def get_rpmversion
  get_base_pkg_version[0]
end

.git_co(ref) ⇒ Object



8
9
10
11
12
13
# File 'ext/packaging/lib/packaging/util/version.rb', line 8

def git_co(ref)
  Pkg::Util.in_project_root do
    %x{#{GIT} reset --hard ; #{GIT} checkout #{ref}}
    $?.success? or fail "Could not checkout #{ref} git branch to build package from...exiting"
  end
end

.git_describeObject



22
23
24
25
26
# File 'ext/packaging/lib/packaging/util/version.rb', line 22

def git_describe
  Pkg::Util.in_project_root do
    %x{#{GIT} describe}.strip
  end
end

.git_describe_versionObject

Return information about the current tree, using ‘git describe`, ready for further processing.

Returns an array of one to four elements, being:

  • version (three dot-joined numbers, leading v stripped)

  • the string ‘rcX’ (if the last tag was an rc release, where X is the rc number)

  • commits (string containing integer, number of commits since that version was tagged)

  • dirty (string ‘dirty’ if local changes exist in the repo)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/packaging/lib/packaging/util/version.rb', line 76

def git_describe_version
  return nil unless is_git_repo and raw = run_git_describe_internal
  # reprocess that into a nice set of output data
  # The elements we select potentially change if this is an rc
  # For an rc with added commits our string will be something like '0.7.0-rc1-63-g51ccc51'
  # and our return will be [0.7.0, rc1, 63, <dirty>]
  # For a final with added commits, it will look like '0.7.0-63-g51ccc51'
  # and our return will be [0.7.0, 64, <dirty>]
  info = raw.chomp.sub(/^v/, '').split('-')
  if git_ref_type == "tag"
    version_string = info.compact
  elsif info[1].to_s.match('^[\d]+')
    version_string = info.values_at(0,1,3).compact
  else
    version_string = info.values_at(0,1,2,4).compact
  end
  version_string
end

.git_project_nameObject

Return the basename of the project repo



62
63
64
65
66
# File 'ext/packaging/lib/packaging/util/version.rb', line 62

def git_project_name
  Pkg::Util.in_project_root do
    %x{#{GIT} config --get remote.origin.url}.split('/')[-1].chomp(".git").chomp
  end
end

.git_ref_typeObject

Return the ref type of HEAD on the current branch



36
37
38
39
40
# File 'ext/packaging/lib/packaging/util/version.rb', line 36

def git_ref_type
  Pkg::Util.in_project_root do
    %x{#{GIT} cat-file -t #{git_describe}}.strip
  end
end

.git_shaObject

return the sha of HEAD on the current branch



29
30
31
32
33
# File 'ext/packaging/lib/packaging/util/version.rb', line 29

def git_sha
  Pkg::Util.in_project_root do
    %x{#{GIT} rev-parse HEAD}.strip
  end
end

.git_sha_or_tagObject

If HEAD is a tag, return the tag. Otherwise return the sha of HEAD.



43
44
45
46
47
48
49
# File 'ext/packaging/lib/packaging/util/version.rb', line 43

def git_sha_or_tag
  if git_ref_type == "tag"
    git_describe
  else
    git_sha
  end
end

.git_tagged?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'ext/packaging/lib/packaging/util/version.rb', line 15

def git_tagged?
  Pkg::Util.in_project_root do
    %x{#{GIT} describe >/dev/null 2>&1}
    $?.success?
  end
end

.is_final?Boolean

Determines if this package is a final package via the selected version_strategy. There are currently two supported version strategies.

This method calls down to the version strategy indicated, defaulting to the rc_final strategy. The methods themselves will return false if it is a final release, so their return values are collected and then inverted before being returned.

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'ext/packaging/lib/packaging/util/version.rb', line 187

def is_final?
  ret = nil
  case Pkg::Config.version_strategy
    when "rc_final"
      ret = is_rc?
    when "odd_even"
      ret = is_odd?
    when "zero_based"
      ret = is_less_than_one?
    when nil
      ret = is_rc?
  end
  return (! ret)
end

.is_git_repo?Boolean Also known as: is_git_repo

Return true if we’re in a git repo, otherwise false

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'ext/packaging/lib/packaging/util/version.rb', line 52

def is_git_repo?
  Pkg::Util.in_project_root do
    %x{#{GIT} rev-parse --git-dir > /dev/null 2>&1}
    $?.success?
  end
end

.is_less_than_one?Boolean

the pre-1.0 strategy (node classifier) final: ‘1.8.0’ ‘1.8.0-63’ ‘1.8.1-63-dirty’ development: ‘0.7.0’ ‘0.7.0-63’ ‘0.7.1-63-dirty’

Returns:

  • (Boolean)


241
242
243
244
# File 'ext/packaging/lib/packaging/util/version.rb', line 241

def is_less_than_one?
  return TRUE if get_dash_version.match(/^(\d+)\.\d+\.\d+/)[1].to_i.zero?
  return FALSE
end

.is_odd?Boolean

the odd_even strategy (mcollective) final: ‘0.8.0’ ‘1.8.0-63’ ‘0.8.1-63-dirty’ development: ‘0.7.0’ ‘1.7.0-63’ ‘0.7.1-63-dirty’

Returns:

  • (Boolean)


227
228
229
230
# File 'ext/packaging/lib/packaging/util/version.rb', line 227

def is_odd?
  return TRUE if get_dash_version.match(/^\d+\.(\d+)\.\d+/)[1].to_i.odd?
  return FALSE
end

.is_rc?Boolean

the rc_final strategy (default) Assumes version strings in the formats: final: ‘0.7.0’ ‘0.7.0-63’ ‘0.7.0-63-dirty’ development: ‘0.7.0rc1 (we don’t actually use this format anymore, but once did) ‘0.7.0-rc1’ ‘0.7.0-rc1-63’ ‘0.7.0-rc1-63-dirty’

Returns:

  • (Boolean)


213
214
215
216
# File 'ext/packaging/lib/packaging/util/version.rb', line 213

def is_rc?
  return TRUE if get_dash_version =~ /^\d+\.\d+\.\d+-*rc\d+/
  return FALSE
end

.run_git_describe_internalObject

This is a stub to ease testing…



96
97
98
99
100
101
# File 'ext/packaging/lib/packaging/util/version.rb', line 96

def run_git_describe_internal
  Pkg::Util.in_project_root do
    raw = %x{#{GIT} describe --tags --dirty 2>/dev/null}
    $?.success? ? raw : nil
  end
end

.source_dirty?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'ext/packaging/lib/packaging/util/version.rb', line 167

def source_dirty?
  git_describe_version.include?('dirty')
end

.uname_rObject



111
112
113
114
# File 'ext/packaging/lib/packaging/util/version.rb', line 111

def uname_r
  uname = Pkg::Util::Tool.find_tool('uname', :required => true)
  %x{#{uname} -r}.chomp
end

.versionbump(workdir = nil) ⇒ Object

This is to support packages that only burn-in the version number in the release artifact, rather than storing it two (or more) times in the version control system. Razor is a good example of that; see github.com/puppetlabs/Razor/blob/master/lib/project_razor/version.rb for an example of that this looks like.

If you invoke this the version will only be modified in the temporary copy, with the intent that it never change the official source tree.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'ext/packaging/lib/packaging/util/version.rb', line 267

def versionbump(workdir=nil)
  version = ENV['VERSION'] || Pkg::Config.version.to_s.strip
  new_version = '"' + version + '"'

  version_file = "#{workdir ? workdir + '/' : ''}#{Pkg::Config.version_file}"

  # Read the previous version file in...
  contents = IO.read(version_file)

  # Match version files containing 'VERSION = "x.x.x"' and just x.x.x
  if version_string = contents.match(/VERSION =.*/)
    old_version = version_string.to_s.split()[-1]
  else
    old_version = contents
  end

  puts "Updating #{old_version} to #{new_version} in #{version_file}"
  if contents.match("@DEVELOPMENT_VERSION@")
    contents.gsub!("@DEVELOPMENT_VERSION@", version)
  elsif contents.match('version\s*=\s*[\'"]DEVELOPMENT[\'"]')
    contents.gsub!(/version\s*=\s*['"]DEVELOPMENT['"]/, "version = '#{version}'")
  elsif contents.match("VERSION = #{old_version}")
    contents.gsub!("VERSION = #{old_version}", "VERSION = #{new_version}")
  elsif contents.match("#{Pkg::Config.project.upcase}VERSION = #{old_version}")
    contents.gsub!("#{Pkg::Config.project.upcase}VERSION = #{old_version}", "#{Pkg::Config.project.upcase}VERSION = #{new_version}")
  else
    contents.gsub!(old_version, Pkg::Config.version)
  end

  # ...and write it back on out.
  File.open(version_file, 'w') {|f| f.write contents }
end