Module: AlpacaBuildTool::Versioning

Defined in:
lib/alpacabuildtool/versioning/versioning.rb

Overview

Versioning provides methods to load and save version for solutions

Constant Summary collapse

FILE_NAME =

File name where semantic version is stored

'.semver'
SemVerFileNotFound =

Error thrown by versioning if version file not found

Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.find(solution_folder = nil, dir = '') ⇒ Object

Returns Version instance from .semver file found by reverse search from current working directory/dir up to root directory

[solution_folder]

directory from which to start searching

.semver file(nil by default)
[dir]

base directory for .semver file(current working

directory by default)


42
43
44
45
46
47
48
49
# File 'lib/alpacabuildtool/versioning/versioning.rb', line 42

def self.find(solution_folder = nil, dir = '')
  solution_folder = Pathname.new(solution_folder) if solution_folder
  semver = find_file FILE_NAME, dir, solution_folder || Pathname.new('.')
  fail SemVerFileNotFound if semver.nil?
  hash = YAML.load_file(semver) || {}
  hash[:file] = semver
  Version.new hash
end

.find_file(name, dir, cur) ⇒ Object

Helper method to recursivly find file

name

file name to find

dir

relative path to file

cur

current folder to start searching from


57
58
59
60
61
62
# File 'lib/alpacabuildtool/versioning/versioning.rb', line 57

def self.find_file(name, dir, cur)
  file = File.join(cur.expand_path, dir, name)
  return file if File.exist?(file)
  return nil if cur.expand_path.root?
  find_file(name, dir, cur.parent)
end

.init(dir = '') ⇒ Object

Initializes semantic versioning

[dir]

directory where to place .semver(current working

directory by default)


24
25
26
27
28
29
30
31
# File 'lib/alpacabuildtool/versioning/versioning.rb', line 24

def self.init(dir = '')
  hash = { major: 0, minor: 0, patch: 0, special: '', metadata: '' }
  FileUtils.mkdir_p(dir) unless File.directory?(dir)
  file = File.join dir, FILE_NAME
  open(file, 'w') { |io| io.write YAML.dump(hash) }
  hash[:file] = file
  Version.new hash
end

.parse(file) ⇒ Object

Returns Version instance from specific file

[file]

path to file that contains .semver content


68
69
70
71
72
# File 'lib/alpacabuildtool/versioning/versioning.rb', line 68

def self.parse(file)
  hash = YAML.load_file(file)
  hash[:file] = file
  Version.new hash
end

.save(version, file = nil) ⇒ Object

Save changes done to version

version

instance of Version class

file

specific file to save to(nil by default)


79
80
81
82
# File 'lib/alpacabuildtool/versioning/versioning.rb', line 79

def self.save(version, file = nil)
  file ||= version.file
  open(file, 'w') { |io| io.write version.to_yaml }
end