build_number 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTVkNWYxZDk0MTIyZWYzNzM4MGU4MDU3NDRkNDViYjczNjQ5NTY5Nw==
4
+ NzI4MTY2MTg1OGJmNDZkZjZjZjk0YjYxZTA3YTc2NDE3ZDQzOGFiOQ==
5
5
  data.tar.gz: !binary |-
6
- ZThiN2JmZTBhOTJmNWU2MGQ5MzYzZDlkNTE3MjNmMDE2OTBiNjMxOA==
6
+ NTAwOGU4MTdiNzg3OTdiMTVhMjU3NDg0ZDYyMTVkMzFmM2ZkOGRmYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YWZiNjIyM2QzMDY1YmNkMjExODhmMDIyYmU4NTQxMzVjZTFjN2Y4MjY2N2Qw
10
- MDdkODcyY2ZjN2YxY2ZlZDExMmQ3NGRiNGM1NzBjODYxZjVlMzcyZDQ3M2Zj
11
- ZmNkODE5YWM0Y2I4OTQ0M2I5OTM0NGJhMWFhNmRjNjBhNTBiNDQ=
9
+ MWI1YzVlZDUzZjI4OTFjMjZmMWZiYjQ3YWU3MmRhYzk4ZGU5M2VmMTc2NWZi
10
+ NTNkYTdhMTMyZDFmODI2NDgxOThlM2ZlN2U2NjdiNTRjZDI3MDZhNDA5ZDM1
11
+ NWY4MmYwOWNlZjE5ZDY1MWQ1MTNjZTc2M2M0NzZiZjhkNzdjZDM=
12
12
  data.tar.gz: !binary |-
13
- ZDFlOGFhNTY4YTFiYmJjNWZhY2EwZjkwMzk1NjhlMzcxOWJlMmNhMzM5MjMw
14
- ZDA2ZGRhNmNjMzY1OWUxM2Q1MjE1OTE2YjA3NGNiZTk0ZWUzMjVlOGQ3Zjcz
15
- OWRlODY2Zjc4NmEyZGU0MDNlYWNiY2RiYjBhZmVjYmJlNzhlMjA=
13
+ NTFjOWYzYmRmYzRlYzljOTI5NWY0NmM5ZjI0N2U1M2RiMmVkMTU5MDI3YTZh
14
+ Mzc2MjY4YTlhMjBhNDZjNDBhY2U1MjFkOWRlMzkxZGY0NjM2ZTA3ZWFlYzJl
15
+ MzI5MDRlYWQyMGVjMmYyOTRlMDg0ZWZhNjBmMjViZGVlM2NkZDg=
data/.semver CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 1
4
+ :patch: 2
5
5
  :special: ''
6
6
  :metadata: ''
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- build_number (0.1.0)
4
+ build_number (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -28,7 +28,23 @@ In your project's Rakefile:
28
28
  require 'build_number'
29
29
  BuildNumber.set_env
30
30
 
31
- # ENV['BUILD_NUMBER'] is now set and incremented for the next build!
31
+ # ENV['BUILD_NUMBER'] is now set and incremented for the next build.
32
+ ```
33
+
34
+ Alternatively, you can get the value directly. In either case, the value will be incremented only
35
+ once per build.
36
+
37
+ ```ruby
38
+ build_number = BuildNumber.current # Reads the build number and increments the
39
+ # value for the next build. Subsequent
40
+ # calls return the same value.
41
+ ```
42
+
43
+ You can also customize the name of the environment variable.
44
+
45
+ ```ruby
46
+ BuildNumber.env_var_name = 'A_CUSTOM_ENV_VAR_NAME'
47
+ BuildNumber.set_env
32
48
  ```
33
49
 
34
50
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module BuildNumber
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
data/lib/build_number.rb CHANGED
@@ -1,12 +1,22 @@
1
1
  require 'build_number/version'
2
2
 
3
3
  module BuildNumber
4
- FILE_NAME = '.build_number'
4
+ STORAGE_FILE_NAME = '.build_number'
5
+ DEFAULT_ENV_VAR_NAME = 'BUILD_NUMBER'
5
6
 
7
+ # Reads the current build number and sets the appropriate environment variable.
6
8
  def self.set_env(dir=nil)
7
- ENV['BUILD_NUMBER'] = ENV['BUILD_NUMBER'] || increment(dir).to_s
9
+ ENV[env_var_name] = ENV[env_var_name] || increment(dir).to_s
8
10
  end
9
11
 
12
+ # Returns the current build number.
13
+ def self.current(dir=nil)
14
+ set_env(dir)
15
+ ENV[env_var_name]
16
+ end
17
+
18
+ # Reads and increments the value in the storage file. Returns the
19
+ # current build number.
10
20
  def self.increment(dir=nil)
11
21
  file = find_or_create_file dir
12
22
 
@@ -15,40 +25,54 @@ module BuildNumber
15
25
  end
16
26
  end
17
27
 
28
+ # Looks for a storage file in the given directory and returns the path to it.
29
+ # If not provided the current working directory is used. If no file is found,
30
+ # parent directories are searched.
18
31
  def self.find_or_create_file(dir=nil)
19
32
  dir ||= Dir.pwd
20
- file = find_file dir
21
- file = create_file dir if file.nil?
22
- file
33
+ find_file(dir) || create_file(dir)
23
34
  end
24
35
 
36
+ # Returns the value stored in the storage file.
25
37
  def self.read(path)
26
- file = File.open(path, 'rb')
27
- current = file.read.chomp.to_i
28
- file.close
29
- current
38
+ File.open(path, 'rb') do |file|
39
+ file.read.chomp.to_i
40
+ end
30
41
  end
31
42
 
32
- def self.save(path, next_build_number=0)
33
- open(path, 'w') { |io| io.write next_build_number.to_s }
43
+ # Saves the build number to the storage file.
44
+ def self.save(path, build_number=0)
45
+ open(path, 'w') do |io|
46
+ io.write build_number.to_s
47
+ end
48
+ end
49
+
50
+ # Gets the name of the environment variable.
51
+ def self.env_var_name
52
+ @env_var_name || DEFAULT_ENV_VAR_NAME
53
+ end
54
+
55
+ # Sets the name of the environment variable.
56
+ def self.env_var_name=(name)
57
+ @env_var_name = name
34
58
  end
35
59
 
36
60
  private
37
61
 
38
- def self.find_file(dir=nil)
62
+ def self.find_file(dir)
39
63
  raise "#{dir} is not a directory" unless File.directory? dir
40
- file_path = file_name dir
64
+ path = file_name dir
41
65
 
42
66
  Dir.chdir dir do
43
- until File.exists? file_path do
44
- return nil if is_root_path? file_path
45
- file_path = parent_path file_path
67
+ until File.exists? path do
68
+ return nil if root_path? path
69
+ path = parent_path path
46
70
  end
47
- return file_path
71
+ return path
48
72
  end
49
73
  end
50
74
 
51
- def self.is_root_path?(path)
75
+ def self.root_path?(path)
52
76
  File.dirname(path).match(/(\w:\/|\/)$/i)
53
77
  end
54
78
 
@@ -58,12 +82,12 @@ module BuildNumber
58
82
  end
59
83
 
60
84
  def self.create_file(dir=nil)
61
- file = file_name(dir)
62
- save file
63
- file
85
+ file_name(dir).tap do |file|
86
+ save file
87
+ end
64
88
  end
65
89
 
66
90
  def self.file_name(dir)
67
- File.join dir, FILE_NAME
91
+ File.join dir, STORAGE_FILE_NAME
68
92
  end
69
- end
93
+ end
@@ -2,55 +2,104 @@ require 'tmpdir'
2
2
  require 'build_number'
3
3
 
4
4
  describe BuildNumber do
5
- before :each do
6
- @tmp_dir = Dir.mktmpdir
7
- end
5
+ BUILD_NUMBER = 'BUILD_NUMBER'
6
+
7
+ let(:tmp_dir) { Dir.mktmpdir }
8
+ let(:build_number) { Random.new.rand(100) }
9
+ let(:build_number_path) { "#{tmp_dir}/.build_number" }
8
10
 
9
11
  after :each do
10
- FileUtils.remove_entry_secure @tmp_dir
12
+ FileUtils.remove_entry_secure tmp_dir
13
+ ENV.delete(BUILD_NUMBER)
11
14
  end
12
15
 
13
- it 'should create a .build_number file when none exists' do
14
- path = BuildNumber.find_or_create_file @tmp_dir
15
- path.should eq("#{@tmp_dir}/.build_number")
16
+ it 'should set the env var' do
17
+ BuildNumber.set_env tmp_dir
18
+ ENV.include?(BUILD_NUMBER).should be_true
16
19
  end
17
20
 
18
- it 'should find the .build_number file in parent directory' do
19
- path = BuildNumber.find_or_create_file @tmp_dir
21
+ it 'should return same build number' do
22
+ current1 = BuildNumber.current tmp_dir
23
+ current2 = BuildNumber.current tmp_dir
20
24
 
21
- nested_dir = "#{File.dirname(path)}/deeply/nested/path"
22
- FileUtils.mkdir_p nested_dir
23
- BuildNumber.find_or_create_file(nested_dir).should eq(path)
25
+ current1.should eq(current2)
24
26
  end
25
27
 
26
- it 'should save and load build number' do
27
- build_number = random_build_number
28
- path = BuildNumber.find_or_create_file @tmp_dir
29
- BuildNumber.save path, build_number
30
- BuildNumber.read(path).should eq(build_number)
31
- end
28
+ describe 'when looking for the .build_number file' do
29
+ let(:path) { BuildNumber.find_or_create_file tmp_dir }
30
+
31
+ it 'should have the right name' do
32
+ path.should eq(build_number_path)
33
+ end
32
34
 
33
- it 'should raise when directory does not exist' do
34
- expect {
35
- BuildNumber.find_or_create_file '/i/hopefully/dont/exist'
36
- }.to raise_error
35
+ it 'should be created when it does not exist' do
36
+ File.exists?(path).should be_true
37
+ end
38
+
39
+ it 'should look in parent directories' do
40
+ nested_dir = "#{File.dirname(path)}/deeply/nested/path"
41
+ FileUtils.mkdir_p nested_dir
42
+
43
+ BuildNumber.find_or_create_file(nested_dir).should eq(path)
44
+ end
45
+
46
+ it 'should raise when the provided directory does not exist' do
47
+ expect {
48
+ BuildNumber.find_or_create_file '/i/hopefully/dont/exist'
49
+ }.to raise_error
50
+ end
37
51
  end
38
52
 
39
- it "should set the 'BUILD_NUMBER' env var" do
40
- BuildNumber.set_env @tmp_dir
41
- ENV.include?('BUILD_NUMBER').should be_true
53
+ describe 'when the .build_number file exists' do
54
+ let(:path) { BuildNumber.find_or_create_file tmp_dir }
55
+
56
+ before :each do
57
+ BuildNumber.save path, build_number
58
+ end
59
+
60
+ it 'should read build number' do
61
+ BuildNumber.read(path).should eq(build_number)
62
+ end
63
+
64
+ it 'should read and increment the current build number' do
65
+ BuildNumber.current(tmp_dir).should eq(build_number.to_s)
66
+ BuildNumber.read(path).should eq(build_number + 1)
67
+ end
42
68
  end
43
69
 
44
- it "should not create a .build_number file if the 'BUILD_NUMBER' env var is already set" do
45
- ENV['BUILD_NUMBER'] = random_build_number.to_s
46
- BuildNumber.set_env @tmp_dir
70
+ describe 'when using a custom env var name' do
71
+ CUSTOM_BUILD_NUMBER = 'CUSTOM_BUILD_NUM'
72
+
73
+ before :all do
74
+ BuildNumber.env_var_name = CUSTOM_BUILD_NUMBER
75
+ end
76
+
77
+ after :all do
78
+ BuildNumber.env_var_name = BuildNumber::DEFAULT_ENV_VAR_NAME
79
+ end
47
80
 
48
- File.exists?("#{@tmp_dir}/.build_number").should be_false
49
- ENV['BUILD_NUMBER'] = nil
81
+ after :each do
82
+ ENV.delete(CUSTOM_BUILD_NUMBER)
83
+ end
84
+
85
+ it 'should set a custom env var' do
86
+ BuildNumber.set_env tmp_dir
87
+ ENV.include?(CUSTOM_BUILD_NUMBER).should be_true
88
+ end
50
89
  end
51
90
 
52
- def random_build_number
53
- rng = Random.new
54
- rng.rand(100)
91
+ describe 'when the env var is externally set' do
92
+ before :each do
93
+ ENV[BUILD_NUMBER] = build_number.to_s
94
+ end
95
+
96
+ it 'should not create a .build_number file' do
97
+ BuildNumber.set_env tmp_dir
98
+ File.exists?(build_number_path).should be_false
99
+ end
100
+
101
+ it 'should not increment the value' do
102
+ BuildNumber.current(tmp_dir).should eq(ENV[BUILD_NUMBER])
103
+ end
55
104
  end
56
105
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: build_number
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Scaduto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-28 00:00:00.000000000 Z
11
+ date: 2014-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler