lib/makit/version.rb in makit-0.0.39 vs lib/makit/version.rb in makit-0.0.40

- old
+ new

@@ -1,12 +1,52 @@ # frozen_string_literal: true module Makit - VERSION = "0.0.39" + VERSION = "0.0.40" class Version # given an array of version strings, return the highest version def self.get_highest_version(versions) versions.sort { |a, b| Gem::Version.new(a) <=> Gem::Version.new(b) }.last + end + + def self.get_version_from_file(path) + if(!File.exist?(path)) + raise "file #{path}does not exist" + end + + extension = File.extname(path) + #switch = extension.downcase + #case switch + #if extension == ".csproj" + switch = extension.downcase + case switch + when ".csproj" + Makit::Version.detect_from_file(path, /<Version>([-\w\d.]+)</) + when ".wxs" + Makit::Version.detect_from_file(path, / Version="([\d\.]+)"/) + else + raise "unrecognized file type" + end + end + + def self.detect_from_file(filename, regex) + if File.exist?(filename) + match = IO.read(filename).match(regex) + match.captures[0] if !match.nil? && match.captures.length.positive? + else + raise "unable to find version in #{filename}" + end + end + + def self.set_version_in_file(filename, version) + text = IO.read(filename) + new_text = text + 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\s+=\s+['"]([\w.]+)['"]/, "version=\"#{version}\"") if filename.include?(".toml") + File.open(filename, "w") { |f| f.write(new_text) } if new_text != text end end end