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



78
79
80
81
82
83
# File 'lib/raykit/version.rb', line 78

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/raykit/version.rb', line 85

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
33
34
35
36
37
# 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?

  Dir.glob("**/*.csproj").each { |csproj|
    version = detect_from_file(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



39
40
41
42
43
44
45
46
47
48
# File 'lib/raykit/version.rb', line 39

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/raykit/version.rb', line 56

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



50
51
52
53
54
# File 'lib/raykit/version.rb', line 50

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



72
73
74
75
# File 'lib/raykit/version.rb', line 72

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