versionifier 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/v+ CHANGED
@@ -1,64 +1,83 @@
1
1
  #!/usr/bin/env ruby
2
+ require "versionifier"
2
3
 
3
- the_project = { :name => Dir.pwd.split("/").last }
4
-
4
+ # Get the project name and the version file path
5
+ the_project = {
6
+ :name => Dir.pwd.split("/").last }
5
7
  the_version_file = {
6
- :path => Dir.pwd + "/lib/" + the_project[:name] + "/version.rb"
7
- }
8
+ :path => Dir.pwd + "/lib/" + the_project[:name] + "/version.rb" }
8
9
 
10
+ # Exit if version file not found
9
11
  raise ArgumentError, "Version file not found on #{the_version_file[:path]}" unless File.exists? the_version_file[:path]
10
12
 
13
+ # Read the current version from the version file
11
14
  the_version_file[:content] = File.read the_version_file[:path]
15
+ the_matches = the_version_file[:content].match /VERSION\s+=\s+"(\d+?\.\d+?\.\d+?)"/
12
16
 
13
- the_matches = the_version_file[:content].match /VERSION\s+=\s+"(\d+?)\.(\d+?)\.(\d+?)"/
17
+ # Create a version from the data
18
+ current_version = Versionifier::Version.new the_matches[1]
14
19
 
15
- the_version = {
16
- :major => the_matches[1].to_i,
17
- :regular => the_matches[2].to_i,
18
- :minor => the_matches[3].to_i
19
- }
20
+ instructions = <<-INST
20
21
 
21
- if ARGV.empty?
22
- puts ""
23
- puts "Working on #{the_project[:name]} (#{the_version[:major]}.#{the_version[:regular]}.#{the_version[:minor]})"
24
- puts ""
25
- puts "Usage: "
26
- puts " v+ 1 # 0.0.4 -> 0.0.5"
27
- puts " v+ 1.0 # 0.0.4 -> 0.1.4"
28
- puts " v+ 1.0.0 # 0.0.4 -> 1.0.4"
29
- puts ""
30
- puts "Any combination is valid. For example:"
31
- puts " v+ 1.0.2 # 0.0.4 -> 1.0.6"
22
+ Working on #{the_project[:name]} (#{current_version})"
32
23
 
33
- else
24
+ Usage:
25
+ v+ 1 # 0.0.4 -> 0.0.5
26
+ v+ 1.x # 0.0.4 -> 0.1.4
27
+ v+ 1.x.x # 0.0.4 -> 1.0.4
28
+
29
+ '!' at the end of the argument resets minor version numbers:
30
+ v+ 1.x! # 0.0.4 -> 0.1.0
31
+ v+ 2.x.x! # 0.0.4 -> 2.0.0
32
+ INST
33
+
34
+ # Depending on the arguments...
35
+ unless ARGV.empty?
36
+ the_argument = ARGV.shift
34
37
 
35
- the_argument = { :raw => ARGV.shift }
36
- the_new_version = the_version.clone
38
+ # Check if argument is reset
39
+ if the_argument.end_with? "!"
40
+ the_argument = the_argument[0..the_argument.length-2]
41
+ options = :reset
42
+ else
43
+ options = nil
44
+ end
37
45
 
38
- if the_match = the_argument[:raw].match(/^\d+$/)
39
- the_argument.merge! :major => 0, :regular => 0, :minor => the_match[0].to_i
40
- elsif the_match = the_argument[:raw].match(/^(\d+?)\.(\d+)$/)
41
- the_argument.merge! :major => 0, :regular => the_match[1].to_i, :minor => the_match[2].to_i
42
- elsif the_match = the_argument[:raw].match(/^(\d+?)\.(\d+?)\.(\d+)$/)
43
- the_argument.merge! :major => the_match[1].to_i,
44
- :regular => the_match[2].to_i,
45
- :minor => the_match[3].to_i
46
+ incrementation = 0
47
+ which = nil
48
+ # Check if argument is properly formed
49
+ if matches = the_argument.match(/^(\d+)\.x\.x$|^(\d+)\.x$|^(\d+)$/)
50
+ if matches[1]
51
+ incrementation = matches[1].to_i
52
+ which = :major
53
+ elsif matches[2]
54
+ incrementation = matches[2].to_i
55
+ which = :regular
56
+ elsif matches[3]
57
+ incrementation = matches[3].to_i
58
+ which = :minor
59
+ end
46
60
  else
47
- raise ArgumentError, "The argument must follow the format <number>[.<number>[.<number>]]"
61
+ puts "Malformed argument. Please follow usage instructions:"
62
+ puts instructions
63
+ Process.exit!
48
64
  end
49
65
 
50
- the_new_version[:major] += the_argument[:major]
51
- the_new_version[:regular] += the_argument[:regular]
52
- the_new_version[:minor] += the_argument[:minor]
66
+ # Get the final file
67
+ the_final_version = current_version.increment which, incrementation, options
68
+ the_final_file = the_version_file[:content].gsub(/VERSION\s+=\s+"\d+?\.\d+?\.\d+?"/, "VERSION = \"#{the_final_version}\"")
53
69
 
54
- the_final_file = the_version_file[:content].gsub(/VERSION\s+=\s+"\d+?\.\d+?\.\d+?"/, "VERSION = \"#{the_new_version[:major]}.#{the_new_version[:regular]}.#{the_new_version[:minor]}\"")
55
-
56
70
  # Save the file
57
71
  File.open the_version_file[:path], "w" do |file|
58
72
  file.write the_final_file
59
73
  end
60
-
61
- puts "In #{the_project[:name]} (#{the_version[:major]}.#{the_version[:regular]}.#{the_version[:minor]}) "
62
- puts " - Version number updated to (#{the_new_version[:major]}.#{the_new_version[:regular]}.#{the_new_version[:minor]})"
63
-
74
+
75
+ puts "In #{the_project[:name]} (#{current_version}) "
76
+ puts " - Version number updated to (#{the_final_version})"
77
+
78
+ else
79
+
80
+ # Print instructions
81
+ puts instructions
82
+
64
83
  end
data/bin/v+.bck ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Get the project name and the version file path
4
+ the_project = {
5
+ :name => Dir.pwd.split("/").last }
6
+ the_version_file = {
7
+ :path => Dir.pwd + "/lib/" + the_project[:name] + "/version.rb" }
8
+
9
+ # Exit if version file not found
10
+ raise ArgumentError, "Version file not found on #{the_version_file[:path]}" unless File.exists? the_version_file[:path]
11
+
12
+ # Read the version file
13
+ the_version_file[:content] = File.read the_version_file[:path]
14
+
15
+ the_matches = the_version_file[:content].match /VERSION\s+=\s+"(\d+?)\.(\d+?)\.(\d+?)"/
16
+
17
+ the_version = {
18
+ :major => the_matches[1].to_i,
19
+ :regular => the_matches[2].to_i,
20
+ :minor => the_matches[3].to_i
21
+ }
22
+
23
+ if ARGV.empty?
24
+ puts ""
25
+ puts "Working on #{the_project[:name]} (#{the_version[:major]}.#{the_version[:regular]}.#{the_version[:minor]})"
26
+ puts ""
27
+ puts "Usage: "
28
+ puts " v+ 1 # 0.0.4 -> 0.0.5"
29
+ puts " v+ 1.0 # 0.0.4 -> 0.1.4"
30
+ puts " v+ 1.0.0 # 0.0.4 -> 1.0.4"
31
+ puts ""
32
+ puts "Any combination is valid. For example:"
33
+ puts " v+ 1.0.2 # 0.0.4 -> 1.0.6"
34
+
35
+ else
36
+
37
+ the_argument = { :raw => ARGV.shift }
38
+ the_new_version = the_version.clone
39
+
40
+ if the_match = the_argument[:raw].match(/^\d+$/)
41
+ the_argument.merge! :major => 0, :regular => 0, :minor => the_match[0].to_i
42
+ elsif the_match = the_argument[:raw].match(/^(\d+?)\.(\d+)$/)
43
+ the_argument.merge! :major => 0, :regular => the_match[1].to_i, :minor => the_match[2].to_i
44
+ elsif the_match = the_argument[:raw].match(/^(\d+?)\.(\d+?)\.(\d+)$/)
45
+ the_argument.merge! :major => the_match[1].to_i,
46
+ :regular => the_match[2].to_i,
47
+ :minor => the_match[3].to_i
48
+ else
49
+ raise ArgumentError, "The argument must follow the format <number>[.<number>[.<number>]]"
50
+ end
51
+
52
+ the_new_version[:major] += the_argument[:major]
53
+ the_new_version[:regular] += the_argument[:regular]
54
+ the_new_version[:minor] += the_argument[:minor]
55
+
56
+ the_final_file = the_version_file[:content].gsub(/VERSION\s+=\s+"\d+?\.\d+?\.\d+?"/, "VERSION = \"#{the_new_version[:major]}.#{the_new_version[:regular]}.#{the_new_version[:minor]}\"")
57
+
58
+ # Save the file
59
+ File.open the_version_file[:path], "w" do |file|
60
+ file.write the_final_file
61
+ end
62
+
63
+ puts "In #{the_project[:name]} (#{the_version[:major]}.#{the_version[:regular]}.#{the_version[:minor]}) "
64
+ puts " - Version number updated to (#{the_new_version[:major]}.#{the_new_version[:regular]}.#{the_new_version[:minor]})"
65
+
66
+ end
@@ -0,0 +1,15 @@
1
+ Given /the version number (.+?)$/ do |full_version|
2
+ @original_version = Versionifier::Version.new full_version
3
+ end
4
+
5
+ When /I increment (.+?) by (\d+?)$/ do |which, quant|
6
+ @final_version = @original_version.increment which.to_sym, quant.to_i
7
+ end
8
+
9
+ When /I increment (.+?) by (\d+?) and reset everything under/ do |which, quant|
10
+ @final_version = @original_version.increment which.to_sym, quant.to_i, :reset
11
+ end
12
+
13
+ Then /I should get (.+?)$/ do |final_version|
14
+ @final_version.should == final_version
15
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH << File.expand_path("../../../lib", __FILE__)
2
+ require "versionifier"
@@ -0,0 +1,27 @@
1
+ Feature: Easily update version number
2
+ In order to manage version numbers from shell
3
+ As a lazy gem developer
4
+ I want to have convenience methods to update version numbers
5
+
6
+ Scenario Outline: Incrementing numbers
7
+ Given the version number <input>
8
+ When I increment <which> by <quant>
9
+ Then I should get <out>
10
+
11
+ Scenarios: Regular incrementations
12
+ | input | which | quant | out |
13
+ | 0.0.4 | major | 1 | 1.0.4 |
14
+ | 0.2.5 | regular | 4 | 0.6.5 |
15
+ | 1.5.3 | minor | 3 | 1.5.6 |
16
+
17
+ Scenario Outline: Reseting minor and regular
18
+ Given the version number <input>
19
+ When I increment <which> by <quant> and reset everything under
20
+ Then I should get <out>
21
+
22
+ Scenarios: Incrementations with reset
23
+ | input | which | quant | out |
24
+ | 0.0.5 | regular | 1 | 0.1.0 |
25
+ | 0.3.9 | major | 2 | 2.0.0 |
26
+ | 4.5.2 | regular | 5 | 4.10.0 |
27
+ | 3.7.1 | major | 1 | 4.0.0 |
@@ -1,3 +1,40 @@
1
1
  module Versionifier
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
+
4
+ class Version
5
+ attr_accessor :major, :regular, :minor
6
+ def initialize(the_version)
7
+ splitted = the_version.split "."
8
+ @major = splitted[0].to_i
9
+ @regular = splitted[1].to_i
10
+ @minor = splitted[2].to_i
11
+ end
12
+
13
+ def increment(which, quant, options = nil)
14
+ new_version = self.clone
15
+ new_version.send "#{which}=".to_sym, self.send(which) + quant
16
+ if options == :reset
17
+ case which
18
+ when :major
19
+ new_version.regular = 0; new_version.minor = 0
20
+ when :regular
21
+ new_version.minor = 0
22
+ end
23
+ end
24
+ new_version
25
+ end
26
+
27
+ def to_s; "#{@major}.#{@regular}.#{@minor}"; end
28
+ def eql?(other)
29
+ if other.is_a? String
30
+ return other == self.to_s
31
+ else
32
+ return super other
33
+ end
34
+ end
35
+
36
+ def ==(other)
37
+ return self.eql? other
38
+ end
39
+ end
3
40
  end
data/lib/versionifier.rb CHANGED
@@ -1,3 +1 @@
1
- module Versionifier
2
- # Your code goes here...
3
- end
1
+ require "versionifier/version"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: versionifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.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: 2011-08-04 00:00:00.000000000 -03:00
12
+ date: 2011-08-08 00:00:00.000000000 -03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
  description: Version number update gem
@@ -17,6 +17,7 @@ email:
17
17
  - xavier.via.canel@gmail.com
18
18
  executables:
19
19
  - v+
20
+ - v+.bck
20
21
  extensions: []
21
22
  extra_rdoc_files: []
22
23
  files:
@@ -25,6 +26,10 @@ files:
25
26
  - README.rdoc
26
27
  - Rakefile
27
28
  - bin/v+
29
+ - bin/v+.bck
30
+ - features/steps/versionify.rb
31
+ - features/support/env.rb
32
+ - features/versionify.feature
28
33
  - lib/versionifier.rb
29
34
  - lib/versionifier/version.rb
30
35
  - versionifier.gemspec