bump 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bump (0.1.5)
4
+ bump (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -14,7 +14,14 @@ Bump your gemfile (major, minor, patch):
14
14
  bc. bump patch
15
15
  Bump version 0.1.2 to 0.1.3
16
16
 
17
+ h1. Supported locations
18
+ * VERSION file with "1.2.3"
19
+ * gemspec with `gem.version = "1.2.3"`
20
+ * lib/**/version.rb file with `VERSION = "1.2.3"`
21
+
17
22
  h1. Todo
18
23
 
19
24
  * Handle options properly
25
+ * `VERSION = "1.2.3"` in lib/*.rb
26
+ * gemspec with `Gem::Specification.new "gem-name", "1.2.3" do`
20
27
 
data/bin/bump CHANGED
@@ -1,7 +1,31 @@
1
1
  #!/usr/bin/env ruby
2
- require File.dirname(__FILE__) + '/../lib/bump'
2
+ require 'optparse'
3
+
4
+ options = {
5
+ :commit => true
6
+ }
7
+ OptionParser.new do |opts|
8
+ opts.banner = <<BANNER
9
+ Bump your gem version.
10
+
11
+ Usage:
12
+ bump current # show current version
13
+ bump patch # increase patch version of your gem (1.0.X)
14
+ bump minor # increase minor version of your gem (1.X.0)
15
+ bump major # increase major version of your gem (X.0.0)
3
16
 
4
- bump = Bump::Bump.new(ARGV)
5
- output, status = bump.run
17
+ Options:
18
+ BANNER
19
+ opts.on("--no-commit", "Do not make a commit.") { options[:commit] = false }
20
+ opts.on("-h", "--help","Show this.") { puts opts; exit }
21
+ end.parse!
22
+
23
+ if ARGV.size != 1
24
+ puts "Usage instructions: bump --help"
25
+ exit 1
26
+ end
27
+
28
+ require File.dirname(__FILE__) + '/../lib/bump'
29
+ output, status = Bump::Bump.run(ARGV.first, options)
6
30
  puts output
7
31
  exit status
@@ -1,5 +1,5 @@
1
1
  Gem::Specification.new "bump" do |s|
2
- s.version = "0.2.0"
2
+ s.version = "0.3.0"
3
3
  s.author = "Gregory Marcilhacy"
4
4
  s.email = "g.marcilhacy@gmail.com"
5
5
  s.homepage = "https://github.com/gregorym/bump"
@@ -5,20 +5,14 @@ module Bump
5
5
  class UnfoundVersionFileError < StandardError; end
6
6
 
7
7
  class Bump
8
- attr_accessor :bump
9
-
10
8
  BUMPS = %w(major minor patch)
11
9
  OPTIONS = BUMPS | ["current"]
12
10
  VERSION_REGEX = /(\d+\.\d+\.\d+)/
13
11
 
14
- def initialize(bump)
15
- @bump = bump.is_a?(Array) ? bump.first : bump
16
- end
17
-
18
- def run
19
- case @bump
12
+ def self.run(bump, options)
13
+ case bump
20
14
  when "major", "minor", "patch"
21
- bump(@bump)
15
+ bump(bump, options)
22
16
  when "current"
23
17
  current
24
18
  else
@@ -29,7 +23,7 @@ module Bump
29
23
  rescue UnfoundVersionError
30
24
  ["Unable to find your gem version", 1]
31
25
  rescue UnfoundVersionFileError
32
- ["Unable to find gemspec file", 1]
26
+ ["Unable to find a file with the gem version", 1]
33
27
  rescue TooManyVersionFilesError
34
28
  ["More than one gemspec file", 1]
35
29
  rescue Exception => e
@@ -38,45 +32,58 @@ module Bump
38
32
 
39
33
  private
40
34
 
41
- def bump(part)
35
+ def self.bump(part, options)
42
36
  current, file = current_version
43
37
  next_version = next_version(current, part)
44
38
  replace(file, current, next_version)
39
+ commit(next_version, file) if options[:commit]
45
40
  ["Bump version #{current} to #{next_version}", 0]
46
41
  end
47
42
 
48
- def replace(file, old, new)
43
+ def self.commit(version, file)
44
+ return unless File.directory?(".git")
45
+ raise unless system("git add #{file} && git commit -m 'v#{version}'")
46
+ end
47
+
48
+ def self.replace(file, old, new)
49
49
  content = File.read(file)
50
50
  File.open(file, "w"){|f| f.write(content.gsub(old, new)) }
51
51
  end
52
52
 
53
- def current
53
+ def self.current
54
54
  ["Current version: #{current_version.first}", 0]
55
55
  end
56
56
 
57
- def current_version
58
- version, file = version_from_version_rb || version_from_gemspec || raise(UnfoundVersionFileError)
57
+ def self.current_version
58
+ version, file = (
59
+ version_from_version_rb ||
60
+ version_from_gemspec ||
61
+ version_from_version ||
62
+ raise(UnfoundVersionFileError)
63
+ )
59
64
  raise UnfoundVersionError unless version
60
65
  [version, file]
61
66
  end
62
67
 
63
- def version_from_version_rb
68
+ def self.version_from_version_rb
64
69
  return unless file = find_version_file("*/**/version.rb")
65
- [
66
- File.read(file)[VERSION_REGEX],
67
- file
68
- ]
70
+ return unless version = File.read(file)[VERSION_REGEX]
71
+ [version, file]
69
72
  end
70
73
 
71
- def version_from_gemspec
74
+ def self.version_from_gemspec
72
75
  return unless file = find_version_file("*.gemspec")
73
- [
74
- File.read(file)[/\.version\s*=\s*["']#{VERSION_REGEX}["']/, 1],
75
- file
76
- ]
76
+ return unless version = File.read(file)[/\.version\s*=\s*["']#{VERSION_REGEX}["']/, 1]
77
+ [version, file]
78
+ end
79
+
80
+ def self.version_from_version
81
+ return unless file = find_version_file("VERSION")
82
+ return unless version = File.read(file)[VERSION_REGEX]
83
+ [version, file]
77
84
  end
78
85
 
79
- def find_version_file(pattern)
86
+ def self.find_version_file(pattern)
80
87
  files = Dir.glob(pattern)
81
88
  case files.size
82
89
  when 0 then nil
@@ -86,7 +93,7 @@ module Bump
86
93
  end
87
94
  end
88
95
 
89
- def next_version(current, part)
96
+ def self.next_version(current, part)
90
97
  match = current.match /(\d+)\.(\d+)\.(\d+)/
91
98
  case part
92
99
  when "major"
@@ -2,10 +2,12 @@ require File.dirname(__FILE__) + "/../lib/bump.rb"
2
2
 
3
3
  describe Bump do
4
4
  let(:gemspec){ "fixture.gemspec" }
5
+ let(:version_rb_file){ "lib/foo/version.rb" }
5
6
 
6
7
  around do |example|
7
8
  run "rm -rf fixture && mkdir fixture"
8
9
  Dir.chdir "fixture" do
10
+ `git init && git commit --allow-empty -am 'initial'` # so we never accidentally do commit to the current repo
9
11
  example.call
10
12
  end
11
13
  run "rm -rf fixture"
@@ -17,18 +19,43 @@ describe Bump do
17
19
 
18
20
  it "should fail without command" do
19
21
  write_gemspec
20
- bump("", :fail => true).should include "Invalid option"
22
+ bump("", :fail => true).should include "Usage instructions: bump --help"
23
+ end
24
+
25
+ it "should fail with invalid options" do
26
+ write_gemspec
27
+ bump("xxx", :fail => true).should include "Invalid option"
21
28
  end
22
29
 
23
30
  it "should fail with multiple gemspecs" do
24
31
  write_gemspec
25
- write("xxxx.gemspec", "xxx")
32
+ write("xxxx.gemspec", "Gem::Specification.new{}")
26
33
  bump("current", :fail => true).should include "More than one gemspec file"
27
34
  end
28
35
 
29
36
  it "should fail if version is weird" do
30
- write_gemspec('"a.b.c"')
31
- bump("current", :fail => true).should include "Unable to find your gem version"
37
+ write_gemspec('"1."+"3.4"')
38
+ bump("current", :fail => true).should include "Unable to find a file with the gem version"
39
+ end
40
+
41
+ it "should show help" do
42
+ bump("--help").should include("bump current")
43
+ end
44
+
45
+ context "git" do
46
+ it "should commit the new version" do
47
+ write_gemspec
48
+ bump("patch")
49
+ `git log -1 --pretty=format:'%s'`.should == "v4.2.4"
50
+ `git status`.should include "nothing to commit"
51
+ end
52
+
53
+ it "should not commit if --no-commit flag was given" do
54
+ write_gemspec
55
+ bump("patch --no-commit")
56
+ `git log -1 --pretty=format:'%s'`.should == "initial"
57
+ `git status`.should_not include "nothing to commit"
58
+ end
32
59
  end
33
60
 
34
61
  context ".version in gemspec" do
@@ -71,38 +98,58 @@ describe Bump do
71
98
 
72
99
  context "VERSION in version.rb" do
73
100
  before do
74
- write "lib/foo/version.rb", <<-RUBY.sub(" "*8, "")
75
- module Foo
76
- VERSION = "1.2.3"
77
- end
78
- RUBY
101
+ write_version_rb
79
102
  end
80
103
 
81
104
  it "show current" do
82
105
  bump("current").should include("1.2.3")
83
- read("lib/foo/version.rb").should include(' VERSION = "1.2.3"')
106
+ read(version_rb_file).should include(' VERSION = "1.2.3"')
84
107
  end
85
108
 
86
109
  it "should bump VERSION" do
87
110
  bump("minor").should include("1.3.0")
88
- read("lib/foo/version.rb").should include(' VERSION = "1.3.0"')
111
+ read(version_rb_file).should include(' VERSION = "1.3.0"')
89
112
  end
90
113
 
91
114
  it "should bump Version" do
92
- write "lib/foo/version.rb", <<-RUBY.sub(" "*8, "")
115
+ write version_rb_file, <<-RUBY.sub(" "*8, "")
93
116
  module Foo
94
117
  Version = "1.2.3"
95
118
  end
96
119
  RUBY
97
120
  bump("minor").should include("1.3.0")
98
- read("lib/foo/version.rb").should include(' Version = "1.3.0"')
121
+ read(version_rb_file).should include(' Version = "1.3.0"')
99
122
  end
100
123
 
101
124
  it "should bump if a gemspec exists and leave it alone" do
102
- write_gemspec "Foo::VERSION"
125
+ write_gemspec "'1.'+'2.3'"
103
126
  bump("minor").should include("1.3.0")
104
- read("lib/foo/version.rb").should include(' VERSION = "1.3.0"')
105
- read(gemspec).should include('version = Foo::VERSION')
127
+ read(gemspec).should include("version = '1.'+'2.3'")
128
+ end
129
+ end
130
+
131
+ context "version in VERSION" do
132
+ before do
133
+ write "VERSION", "1.2.3\n"
134
+ end
135
+
136
+ it "show current" do
137
+ bump("current").should include("1.2.3")
138
+ read("VERSION").should == "1.2.3\n"
139
+ end
140
+
141
+ it "should bump version" do
142
+ bump("minor").should include("1.3.0")
143
+ read("VERSION").should == "1.3.0\n"
144
+ end
145
+
146
+ it "should bump if a gemspec & version.rb exists and leave it alone" do
147
+ write_gemspec "File.read('VERSION')"
148
+ write_version_rb "File.read('VERSION')"
149
+ bump("minor").should include("1.3.0")
150
+ read("VERSION").should == "1.3.0\n"
151
+ read(version_rb_file).should include("VERSION = File.read('VERSION')")
152
+ read(gemspec).should include("version = File.read('VERSION')")
106
153
  end
107
154
  end
108
155
 
@@ -135,4 +182,12 @@ describe Bump do
135
182
  end
136
183
  RUBY
137
184
  end
185
+
186
+ def write_version_rb(version = '"1.2.3"')
187
+ write version_rb_file, <<-RUBY.sub(" "*6, "")
188
+ module Foo
189
+ VERSION = #{version}
190
+ end
191
+ RUBY
192
+ end
138
193
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-14 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: g.marcilhacy@gmail.com