Class: Makit::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/version.rb,
lib/makit/version_util.rb

Overview

Version utility class for handling version comparisons

Class Method Summary collapse

Class Method Details

.detect_from_file(filename, regex) ⇒ String?

Detect version using a regex pattern in a specific file

Parameters:

  • filename (String)

    Path to the file to search

  • regex (Regexp)

    Regular expression pattern to match version

Returns:

  • (String, nil)

    The extracted version or nil if no match found

Raises:

  • (RuntimeError)

    If file doesn’t exist



55
56
57
58
59
60
# File 'lib/makit/version.rb', line 55

def self.detect_from_file(filename, regex)
  raise "unable to find version in #{filename}" unless File.exist?(filename)

  match = File.read(filename).match(regex)
  match.captures[0] if !match.nil? && match.captures.length.positive?
end

.get_highest_version(versions) ⇒ String?

Get the highest version from a list of version strings

Parameters:

  • versions (Array<String>)

    array of version strings

Returns:

  • (String, nil)

    highest version string or nil if empty



17
18
19
# File 'lib/makit/version.rb', line 17

def self.get_highest_version(versions)
  versions.max { |a, b| Gem::Version.new(a) <=> Gem::Version.new(b) }
end

.get_version_from_file(path) ⇒ String

Extract version number from a file based on its extension

Supports multiple file formats:

  • .csproj files: ‘<Version>x.y.z</Version>`

  • .wxs files: ‘Version=“x.y.z”`

  • .yml files: ‘VERSION: “x.y.z”`

Parameters:

  • path (String)

    Path to the file containing version information

Returns:

  • (String)

    The extracted version string

Raises:

  • (RuntimeError)

    If file doesn’t exist or has unrecognized extension



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

def self.get_version_from_file(path)
  raise "file #{path}does not exist" unless File.exist?(path)

  extension = File.extname(path)
  case extension
  when ".csproj"
    Makit::Version.detect_from_file(path, /<Version>([-\w\d.]+)</)
  when ".wxs"
    Makit::Version.detect_from_file(path, / Version="([\d.]+)"/)
  when ".yml"
    Makit::Version.detect_from_file(path, /VERSION:\s*["']?([\d.]+)["']?/)
  when ".rb"
    Makit::Version.detect_from_file(path, /VERSION = "([\d.]+)"/)
  else
    raise "unrecognized file type"
  end
end

.set_version_in_file(filename, version) ⇒ nil

Update version number in a file based on its extension

Supports updating versions in multiple file formats:

  • .yml files

  • .gemspec files

  • .csproj files

  • .nuspec files

  • .wxs files

  • .toml files

Parameters:

  • filename (String)

    Path to the file to update

  • version (String)

    New version string to set

Returns:

  • (nil)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/makit/version.rb', line 75

def self.set_version_in_file(filename, version)
  text = File.read(filename)
  #   VERSION = "0.0.138rake" (.rb file)
  new_text = text
  new_text = text.gsub(/VERSION:\s?['|"]([.\d]+)['|"]/, "VERSION: \"#{version}\"") if filename.include?(".yml")
  new_text = text.gsub(/version\s?=\s?['|"]([.\d]+)['|"]/, "version='#{version}'") if filename.include?(".gemspec")
  new_text = text.gsub(/<Version>([-\w\d.]+)</, "<Version>#{version}<") if filename.include?(".csproj")
  new_text = text.gsub(/<version>([-\w\d.]+)</, "<version>#{version}<") if filename.include?(".nuspec")
  new_text = text.gsub(/ Version="([\d.]+)"/, " Version=\"#{version}\"") if filename.include?(".wxs")
  new_text = text.gsub(/VERSION = "([\d.]+)"/, "VERSION = \"#{version}\"") if filename.include?(".rb")
  new_text = text.gsub(/version\s+=\s+['"]([\w.]+)['"]/, "version=\"#{version}\"") if filename.include?(".toml")
  File.write(filename, new_text) if new_text != text
end

.set_version_in_files(glob_pattern, version) ⇒ nil

Update version number in multiple files matching a glob pattern

Parameters:

  • glob_pattern (String)

    Glob pattern to match files (e.g., ‘*/.csproj’)

  • version (String)

    New version string to set in all matching files

Returns:

  • (nil)


94
95
96
97
98
# File 'lib/makit/version.rb', line 94

def self.set_version_in_files(glob_pattern, version)
  Dir.glob(glob_pattern).each do |filename|
    set_version_in_file(filename, version)
  end
end