Module: BuildNumber

Defined in:
lib/build_number.rb,
lib/build_number/version.rb

Constant Summary collapse

STORAGE_FILE_NAME =
'.build_number'
DEFAULT_ENV_VAR_NAME =
'BUILD_NUMBER'
VERSION =
'0.1.3'

Class Method Summary collapse

Class Method Details

.current(dir = nil) ⇒ Object

Returns the current build number.



13
14
15
16
# File 'lib/build_number.rb', line 13

def self.current(dir=nil)
  set_env(dir)
  ENV[env_var_name]
end

.env_var_nameObject

Gets the name of the environment variable.



57
58
59
# File 'lib/build_number.rb', line 57

def self.env_var_name
  @env_var_name || DEFAULT_ENV_VAR_NAME
end

.env_var_name=(name) ⇒ Object

Sets the name of the environment variable.



62
63
64
# File 'lib/build_number.rb', line 62

def self.env_var_name=(name)
  @env_var_name = name
end

.find_or_create_file(dir = nil) ⇒ Object

Looks for a storage file in the given directory and returns the path to it. If not provided the current working directory is used. If no file is found, parent directories are searched.



37
38
39
40
# File 'lib/build_number.rb', line 37

def self.find_or_create_file(dir=nil)
  dir ||= Dir.pwd
  find_file(dir) || create_file(dir)
end

.increment(dir = nil) ⇒ Object

Reads and increments the value in the storage file. Returns the current build number.



26
27
28
29
30
31
32
# File 'lib/build_number.rb', line 26

def self.increment(dir=nil)
  file = find_or_create_file dir

  read(file).tap do |current|
    save file, current + 1
  end
end

.next(dir = nil) ⇒ Object

Returns the next build number without incrementing it.



19
20
21
22
# File 'lib/build_number.rb', line 19

def self.next(dir=nil)
  file = find_or_create_file dir
  @next ||= read(file)
end

.read(path) ⇒ Object

Returns the value stored in the storage file.



43
44
45
46
47
# File 'lib/build_number.rb', line 43

def self.read(path)
  File.open(path, 'rb') do |file|
    file.read.chomp.to_i
  end
end

.save(path, build_number = 0) ⇒ Object

Saves the build number to the storage file.



50
51
52
53
54
# File 'lib/build_number.rb', line 50

def self.save(path, build_number=0)
  open(path, 'w') do |io|
    io.write build_number.to_s
  end
end

.set_env(dir = nil) ⇒ Object

Reads the current build number and sets the appropriate environment variable.



8
9
10
# File 'lib/build_number.rb', line 8

def self.set_env(dir=nil)
  ENV[env_var_name] = ENV[env_var_name] || increment(dir).to_s
end