build_number 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.semver +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +17 -1
- data/lib/build_number/version.rb +1 -1
- data/lib/build_number.rb +47 -23
- data/spec/build_number_spec.rb +82 -33
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzI4MTY2MTg1OGJmNDZkZjZjZjk0YjYxZTA3YTc2NDE3ZDQzOGFiOQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NTAwOGU4MTdiNzg3OTdiMTVhMjU3NDg0ZDYyMTVkMzFmM2ZkOGRmYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MWI1YzVlZDUzZjI4OTFjMjZmMWZiYjQ3YWU3MmRhYzk4ZGU5M2VmMTc2NWZi
|
10
|
+
NTNkYTdhMTMyZDFmODI2NDgxOThlM2ZlN2U2NjdiNTRjZDI3MDZhNDA5ZDM1
|
11
|
+
NWY4MmYwOWNlZjE5ZDY1MWQ1MTNjZTc2M2M0NzZiZjhkNzdjZDM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTFjOWYzYmRmYzRlYzljOTI5NWY0NmM5ZjI0N2U1M2RiMmVkMTU5MDI3YTZh
|
14
|
+
Mzc2MjY4YTlhMjBhNDZjNDBhY2U1MjFkOWRlMzkxZGY0NjM2ZTA3ZWFlYzJl
|
15
|
+
MzI5MDRlYWQyMGVjMmYyOTRlMDg0ZWZhNjBmMjViZGVlM2NkZDg=
|
data/.semver
CHANGED
data/Gemfile.lock
CHANGED
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
|
data/lib/build_number/version.rb
CHANGED
data/lib/build_number.rb
CHANGED
@@ -1,12 +1,22 @@
|
|
1
1
|
require 'build_number/version'
|
2
2
|
|
3
3
|
module BuildNumber
|
4
|
-
|
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[
|
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
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
current
|
38
|
+
File.open(path, 'rb') do |file|
|
39
|
+
file.read.chomp.to_i
|
40
|
+
end
|
30
41
|
end
|
31
42
|
|
32
|
-
|
33
|
-
|
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
|
62
|
+
def self.find_file(dir)
|
39
63
|
raise "#{dir} is not a directory" unless File.directory? dir
|
40
|
-
|
64
|
+
path = file_name dir
|
41
65
|
|
42
66
|
Dir.chdir dir do
|
43
|
-
until File.exists?
|
44
|
-
return nil if
|
45
|
-
|
67
|
+
until File.exists? path do
|
68
|
+
return nil if root_path? path
|
69
|
+
path = parent_path path
|
46
70
|
end
|
47
|
-
return
|
71
|
+
return path
|
48
72
|
end
|
49
73
|
end
|
50
74
|
|
51
|
-
def self.
|
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
|
-
|
62
|
-
|
63
|
-
|
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,
|
91
|
+
File.join dir, STORAGE_FILE_NAME
|
68
92
|
end
|
69
|
-
end
|
93
|
+
end
|
data/spec/build_number_spec.rb
CHANGED
@@ -2,55 +2,104 @@ require 'tmpdir'
|
|
2
2
|
require 'build_number'
|
3
3
|
|
4
4
|
describe BuildNumber do
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
12
|
+
FileUtils.remove_entry_secure tmp_dir
|
13
|
+
ENV.delete(BUILD_NUMBER)
|
11
14
|
end
|
12
15
|
|
13
|
-
it 'should
|
14
|
-
|
15
|
-
|
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
|
19
|
-
|
21
|
+
it 'should return same build number' do
|
22
|
+
current1 = BuildNumber.current tmp_dir
|
23
|
+
current2 = BuildNumber.current tmp_dir
|
20
24
|
|
21
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
40
|
-
BuildNumber.
|
41
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
49
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
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.
|
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-
|
11
|
+
date: 2014-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|