Class: Raykit::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/version.rb

Overview

Version functionality

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#start_timeObject

Returns the value of attribute start_time.



8
9
10
# File 'lib/raykit/version.rb', line 8

def start_time
  @start_time
end

Class Method Details

.bump(version) ⇒ Object

increment to last digit of a version string



73
74
75
76
77
78
# File 'lib/raykit/version.rb', line 73

def self.bump(version)
  # major.minor[.build[.revision]]  (example: 1.2.12.102)
  version_ints = version.split(".").map(&:to_i)
  version_ints[-1] = version_ints.last + 1
  version_ints.map(&:to_s).join(".")
end

.bump_file(filename) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/raykit/version.rb', line 80

def self.bump_file(filename)
  warn "Raykit::Version.bump_file filename '#{filename}' does not exist." unless File.exist?(filename)

  old_version = ""
  if filename.include?(".gemspec")
    old_version = detect_from_file(filename, /version\s?=\s?['|"]([.\d]+)['|"]/)
  end
  old_version = detect_from_file(filename, /<Version>([-\w\d.]+)</) if filename.include?(".csproj")

  if old_version.length.positive?
    new_version = bump(old_version)
    set_version_in_file(filename, new_version)
  end

  new_version
end

.detect(name) ⇒ Object

detect a version number based on the NAME variable, if defined



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/raykit/version.rb', line 11

def self.detect(name)
  version = detect_from_file("#{name}/#{name}.csproj", /<Version>([-\w\d.]+)</)
  return version if version.length.positive?
  version = detect_from_file("src/#{name}/#{name}.csproj", /<Version>([-\w\d.]+)</)
  return version if version.length.positive?

  version = detect_from_file("#{name}/Properties/AssemblyInfo.cs", /^\[assembly: AssemblyVersion\("([.\w]+)/)
  return version if version.length.positive?

  version = detect_from_file("Cargo.toml", /version\s+=\s+['"]([\w.]+)['"]/)
  return version if version.length.positive?

  version = detect_from_file("#{name}.gemspec", /version\s+=\s+['"]([\w.]+)['"]/)
  return version if version.length.positive?

  version = detect_from_file("#{name}.gemspec", /version\s?=\s?['|"]([.\d]+)['|"]/)
  return version if version.length.positive?

  version = detect_from_file("#{name}.nuspec", /<[Vv]ersion>([\d.]+)</)
  return version if version.length.positive?
  ""
end

.detect_from_file(filename, regex) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/raykit/version.rb', line 34

def self.detect_from_file(filename, regex)
  version = ""
  if File.exist?(filename)
    match = IO.read(filename).match(regex)
    version = match.captures[0] if !match.nil? && match.captures.length.positive?
  else
    return ""
  end
  version
end

.set_version_in_file(filename, version) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/raykit/version.rb', line 51

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")

  # new_text=text.gsub(/<Version>([-\w\d.]+)</,"<Version>#{version}<")
  # new_text=new_text.gsub(/version[\s]+=\s?['|"]([.\d]+)['|"]/,"version='#{version}'")
  # new_text=new_text.gsub(/Version="([.\d]+)"/,"Version=\"#{version}\"")
  File.open(filename, "w") { |f| f.write(new_text) } if new_text != text
end

.set_version_in_glob(glob_pattern, version) ⇒ Object



45
46
47
48
49
# File 'lib/raykit/version.rb', line 45

def self.set_version_in_glob(glob_pattern, version)
  Dir.glob(glob_pattern).each { |f|
    Raykit::Version::set_version_in_file(f, version)
  }
end

.sync_file_versions(source_filename, destination_filename) ⇒ Object



67
68
69
70
# File 'lib/raykit/version.rb', line 67

def self.sync_file_versions(source_filename, destination_filename)
  version = Version.detect_from_file(source_filename, /<Version>([-\w\d.]+)</, false)
  Version.set_version_in_file(destination_filename, version)
end