Class: Bundler::GemVersionTasks

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/bundler/gem_version_tasks.rb,
lib/bundler/gem_version_tasks/version.rb

Constant Summary collapse

VERSION =
'0.2.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GemVersionTasks

Returns a new instance of GemVersionTasks.



8
9
10
11
# File 'lib/bundler/gem_version_tasks.rb', line 8

def initialize( opts={} )
  @gemspec     = opts.fetch(:gemspec) { Bundler::GemHelper.gemspec }
  @git_command = opts.fetch(:git_command, 'git')
end

Instance Attribute Details

#gemspecObject (readonly)

Returns the value of attribute gemspec.



6
7
8
# File 'lib/bundler/gem_version_tasks.rb', line 6

def gemspec
  @gemspec
end

Class Method Details

.install!Object



49
50
51
# File 'lib/bundler/gem_version_tasks.rb', line 49

def self.install!
  new.install
end

Instance Method Details

#bump(what) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bundler/gem_version_tasks.rb', line 13

def bump(what)
  major,minor,patch = version.segments
  case what
  when :major
    major += 1
    minor  = 0
    patch  = 0
  when :minor
    minor = minor.next
    patch = 0
  when :patch
    patch = patch.next
  else
    raise 'need to specify what to bump in :major, :minor, :patch'
  end
  "#{major}.#{minor}.#{patch}"
end

#commit_version(version_file, message) ⇒ Object



37
38
39
# File 'lib/bundler/gem_version_tasks.rb', line 37

def commit_version(version_file, message)
  `#{@git_command} commit -m "#{message}" #{version_file}`
end

#installObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bundler/gem_version_tasks.rb', line 54

def install
  desc "print current version"
  task 'version' do
    puts  version
  end

  namespace 'version' do
    desc "write a specific version - example: rake version:write VERSION=1.2.3"
    task 'write' do
      new_version = ENV['VERSION']
      replace_version(version_file, new_version)
    end
  end

  namespace 'version:bump' do
    desc "Bump the major version by 1"
    task 'major' do
      new_version = bump(:major)
      replace_version(version_file, new_version)
      puts  new_version
    end

    desc "Bump the minor version by 1"
    task 'minor' do
      new_version = bump(:minor)
      replace_version(version_file, new_version)
      puts  new_version
    end

    desc "Bump the patch version by 1"
    task 'patch' do
      new_version = bump(:patch)
      replace_version(version_file, new_version)
      puts  new_version
    end
  end
end

#replace_version(version_file, version) ⇒ Object



31
32
33
34
35
# File 'lib/bundler/gem_version_tasks.rb', line 31

def replace_version(version_file, version)
  lines = File.readlines(version_file).map { |l| l.gsub(/\s*VERSION\s*=.*/,"VERSION='#{version}'") }
  File.open(version_file,'w') {|f| f.write(lines.join('')) }
  commit_version(version_file, "Bumping to version #{version}")
end

#versionObject



45
46
47
# File 'lib/bundler/gem_version_tasks.rb', line 45

def version
  gemspec.version
end

#version_fileObject



41
42
43
# File 'lib/bundler/gem_version_tasks.rb', line 41

def version_file
  gemspec.files.grep(/\/version.rb$/).first
end