Module: Trop::GemVersion

Defined in:
lib/trop/version.rb,
lib/trop/gem_version.rb

Constant Summary collapse

@@version =
nil
@@gem_version_file =
'next_gem_version'
@@default_commit_message =
'incremented gem version'

Class Method Summary collapse

Class Method Details

.check_lib_version_rbObject



74
75
76
77
78
# File 'lib/trop/gem_version.rb', line 74

def self.check_lib_version_rb
 content = Pathname.new(self.version_file_rb).read
  version_ok = content.scan(/VERSION.*["']+\d+\.\d+[\.\d+]+["']+/)
  version_ok
end

.check_lib_version_rb?Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/trop/gem_version.rb', line 69

def self.check_lib_version_rb?
  return false if (self.check_lib_version_rb.nil?)
  true
end

.commit_and_push(project_directory = nil, msg = nil) {|g| ... } ⇒ Object

Yields:

  • (g)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/trop/gem_version.rb', line 121

def self.commit_and_push(project_directory = nil, msg = nil, &block)
  g = Git.open(project_directory || Dir.pwd, :log => Logger.new(STDOUT))
  g.add(@@gem_version_file)
  g.add(self.version_file_rb)
  g.add(self.gemspec_name)
  g.add('VERSION')
  yield(g) if block_given?
  g.commit(msg || @@default_commit_message)
  v = @@version
  system("git tag -f -a -m 'Version v#{v}' v#{v}")
  system("git push > #{DEVNULL} 2>&1")
  system("git push --tags -f > #{DEVNULL} 2>&1")
  g.push
end

.gem_version_file=(str) ⇒ Object



16
17
18
# File 'lib/trop/gem_version.rb', line 16

def self.gem_version_file=(str)
  @@gem_version_file = str
end

.gemspec_nameObject



20
21
22
# File 'lib/trop/version.rb', line 20

def self.gemspec_name
  Trop::VERSION_NAME + '.gemspec'
end

.increment_and_pushObject



44
45
46
47
48
49
# File 'lib/trop/gem_version.rb', line 44

def self.increment_and_push
  self.update_version(self.increment_version)
  self.rewrite_gemspec
  self.commit_and_push
  self.version
end

.increment_versionObject



51
52
53
54
55
56
57
58
# File 'lib/trop/gem_version.rb', line 51

def self.increment_version
  version = self.next_version
  components = version.split('.')
  components.push((components.pop.to_i + 1).to_s)
  @@version = components.join('.')
  ENV['VERSION'] = @@version
  @@version
end

.init_version_fileObject



38
39
40
41
42
# File 'lib/trop/gem_version.rb', line 38

def self.init_version_file
  file = File.new(@@gem_version_file, 'w')
  file.puts '0.0.1'
  file.close
end

.next_versionObject



20
21
22
23
24
25
26
27
# File 'lib/trop/gem_version.rb', line 20

def self.next_version
  init_version_file unless File.exist?(@@gem_version_file)
  file = File.new(@@gem_version_file, 'r')
  version = file.gets.chomp
  raise "#{@@gem_version_file} file corrupt" unless self.version_format_valid?(version)
  file.close
  version
end

.rewrite_gemspecObject



60
61
62
63
64
65
66
67
# File 'lib/trop/gem_version.rb', line 60

def self.rewrite_gemspec
  spec_path = FileUtils.getwd + '/' + self.gemspec_name
  spec_gem = Gem::Specification::load(spec_path)
  spec_gem.version = @@version || self.next_version
  spec_gem.files = spec_gem.files + Trop::FileFinder::my_gemspec_fffind([File.expand_path('./lib'),File.expand_path('rakelib')])
  File.open(spec_path, 'w') {|f| f.write spec_gem.to_ruby}
  return spec_gem.version
end

.set_version(version) ⇒ Object



29
30
31
32
# File 'lib/trop/gem_version.rb', line 29

def self.set_version(version)
  raise "Invalid version format" unless self.version_format_valid?(version)
  self.update_version(version)
end

.update_lib_version_rb(version) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/trop/gem_version.rb', line 80

def self.update_lib_version_rb(version)
  unless self.check_lib_version_rb?
    puts "version out of sync"
    assert false
    return
  end
    # update
  content = Pathname.new(self.version_file_rb).read
  File.open(self.version_file_rb + '.tmp', 'w') do |out|
    content.each_line do |l|
      ret = l.match(/VERSION.*["']+\d+\.\d+[\.\d+]+["']+/)
      if ret
        out.print "  VERSION = '" + version + "'\n"
      else
        out.print (l)
      end
    end
  end
  # rename
  FileUtils.cp( self.version_file_rb + '.tmp',self.version_file_rb, preserve: false)
end

.update_version(new_version) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/trop/gem_version.rb', line 102

def self.update_version(new_version)
  # 0. ENV
  @@version = new_version
  ENV['VERSION'] = @@version
  # 1. next_version
  file = File.new(@@gem_version_file, 'w')
  file.print new_version + "\n"
  file.close
  # 2. VERSION
  file = File.new('VERSION', 'w')
  file.print new_version + "\n"
  file.close
  # 3.
  self.update_lib_version_rb(@@version)
  # 4. gemspec
  self.rewrite_gemspec
  ENV['VERSION'] = @@version
end

.versionObject



12
13
14
# File 'lib/trop/version.rb', line 12

def self.version
  Trop::VERSION
end

.version_file_rbObject



16
17
18
# File 'lib/trop/version.rb', line 16

def self.version_file_rb
  File.expand_path('version.rb', __dir__)
end

.version_format_valid?(version) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/trop/gem_version.rb', line 34

def self.version_format_valid?(version)
  (version && version =~ /^\d+\.\d+[\.\d+]+$/)
end

.version_nameObject



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

def self.version_name
  Trop::VERSION_NAME
end