Class: Version

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

Class Method Summary collapse

Class Method Details

.extract(text) ⇒ Object


4
5
6
7
8
9
10
11
12
# File 'lib/base/version.rb', line 4

def self.extract(text)
  [/VERSION\s*=\s*['"]([\d.]+)['"]/,
   /[Vv]ersion\s*=\s*['"]([\d.]+)['"]/,
   /Version\(\s*"([\d.]+)"\s*\)/].each do |regex|
    scan = text.scan(regex)
    return scan[0][0] if !scan.nil? && (scan.length.positive? && !scan[0].nil? && scan[0].length.positive?)
  end
  nil
end

.extract_from_file(filename) ⇒ Object


14
15
16
# File 'lib/base/version.rb', line 14

def self.extract_from_file(filename)
  Version.extract IO.read(filename)
end

.extract_from_filelist(filelist) ⇒ Object


18
19
20
21
22
23
24
25
# File 'lib/base/version.rb', line 18

def self.extract_from_filelist(filelist)
  version = nil
  filelist.each do |f|
    version = extract_from_file f
    return version unless version.nil?
  end
  nil
end

.get_versionObject


65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/base/version.rb', line 65

def self.get_version
  Dir.glob("**/*.gemspec").each do |gemspec|
    return Version.read gemspec
  end
  Dir.glob("**/AssemblyInfo.cs").each do |assemblyInfo|
    return Version.read assemblyInfo
  end
  Dir.glob("**/*.wxs").each do |wxs|
    return Version.read wxs
  end
  "0.0"
end

.read(filename) ⇒ Object


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

def self.read(filename)
  return Gem::Specification.load(filename).version.to_s if filename.include?(".gemspec")

  if filename.include?("AssemblyInfo.cs")
    scan = IO.read(filename).scan(/Version\("([\d.]+)"\)/)
    return scan[0][0] if !scan.nil? && (scan.length.positive? && !scan[0].nil? && scan[0].length.positive?)

    # return IO.read(filename).scan(/Version\(\"([\d.]+)\"\)/)[0][0]
    scan = IO.read(filename).scan(/Version="([\d.]+)"/)
    return scan[0][0] if !scan.nil? && (scan.length.positive? && !scan[0].nil? && scan[0].length.positive?)
  end
  "0.0"
end

.update_file(filename, version) ⇒ Object


37
38
39
40
41
42
43
# File 'lib/base/version.rb', line 37

def self.update_file(filename, version)
  if File.exist?(filename)
    orig = IO.read(filename)
    text = Version.update_text orig, version
    File.open(filename, "w") { |f| f.write(text) } if orig != text
  end
end

.update_filelist(filelist, version) ⇒ Object


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

def self.update_filelist(filelist, version)
  filelist.each do |f|
    Version.update_file f, version
  end
end

.update_text(text, version) ⇒ Object


27
28
29
30
31
32
33
34
35
# File 'lib/base/version.rb', line 27

def self.update_text(text, version)
  text = text.gsub(/version\s*=\s*'[\d.]+'/, "version='#{version}'")
  text = text.gsub(/VERSION\s*=\s*'[\d.]+'/, "VERSION='#{version}'")
  text = text.gsub(/version\s*=\s*"[\d.]+"/, "version=\"#{version}\"")
  text = text.gsub(/Version\s*=\s*'[\d.]+'/, "Version='#{version}'")
  text = text.gsub(/Version\s*=\s*"[\d.]+"/, "Version=\"#{version}\"")
  text = text.gsub(/Version\(\s*"[\d.]+"\s*\)/, "Version(\"#{version}\")")
  text = text.gsub(/Name\s*=\s*"Version"\s*Value\s*=\s*"[\d.]+"/, "Name=\"Version\" Value=\"#{version}\"")
end