Class: Makit::Version
- Inherits:
-
Object
- Object
- Makit::Version
- Defined in:
- lib/makit/version.rb,
lib/makit/version_util.rb
Overview
Version utility class for handling version comparisons
Class Method Summary collapse
-
.detect_from_file(filename, regex) ⇒ String?
Detect version using a regex pattern in a specific file.
-
.get_highest_version(versions) ⇒ String?
Get the highest version from a list of version strings.
-
.get_version_from_file(path) ⇒ String
Extract version number from a file based on its extension.
-
.set_version_in_file(filename, version) ⇒ nil
Update version number in a file based on its extension.
-
.set_version_in_files(glob_pattern, version) ⇒ nil
Update version number in multiple files matching a glob pattern.
Class Method Details
.detect_from_file(filename, regex) ⇒ String?
Detect version using a regex pattern in a specific file
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
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”`
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
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
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 |