Module: Rake::Gitversion

Extended by:
DSL
Defined in:
lib/rake/gitversion.rb,
lib/rake/gitversion/version.rb

Constant Summary collapse

VERSION_FILE_PATH =

Path to VERSION file

'VERSION'.freeze
VERSION =
'0.1.2'.freeze

Class Method Summary collapse

Class Method Details

.install_rake_tasksObject

Create Rake task ‘set_version’ which writes generated version string to VERSION file.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rake/gitversion.rb', line 32

def self.install_rake_tasks
  desc('Get version from git and save to VERSION file')
  task(:set_version) do
    version = version_from_git
    path = VERSION_FILE_PATH

    begin
      File.open(path, 'w') do |file|
        file.write(version)
      end
    rescue StandardError => error
      pp error
      raise("File #{path} unwritable.")
    end
  end
end

.version_from_git(git_desc = nil) ⇒ Object

Get version from git via ‘git describe’. Requires annotated git tags in format ‘v<MAJOR>.<MINOR>’ like ‘v4.11’

Parameters:

  • git_desc (String) (defaults to: nil)

    output from ‘git describe –long –dirty=-dirty’



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rake/gitversion.rb', line 16

def self.version_from_git(git_desc = nil)
  if git_desc.nil?
    git_desc = `git describe --long --dirty=-dirty`
    if git_desc.empty?
      raise("ERROR: git describe failed. Make sure you are in git and" +
              " there are annotated version tags like v0.1.")
    end
  end
  match = git_desc.match(/v(\d+)\.(\d+)-(\d+)-\w+(-dirty)?/)
  raise "'#{git_desc}' has invalid format" unless match
  patchlevel = match[4] ? "#{match[3].to_i + 1}.dev" : match[3]
  "#{match[1]}.#{match[2]}.#{patchlevel}"
end