Class: Tumbler::Manager::Version

Inherits:
Object
  • Object
show all
Includes:
Informer, Runner
Defined in:
lib/tumbler/manager/version.rb

Constant Summary collapse

DEFAULT_FIELD =
[:major, :minor, :tiny]
INITIAL_VERSION =
'0.0.0'

Constants included from Informer

Informer::Colors

Instance Attribute Summary collapse

Attributes included from Runner

#noop

Instance Method Summary collapse

Methods included from Informer

#inform

Methods included from Runner

#dry, #sh, #sh_with_code

Constructor Details

#initialize(manager, &block) ⇒ Version

Returns a new instance of Version.



12
13
14
15
16
17
# File 'lib/tumbler/manager/version.rb', line 12

def initialize(manager, &block)
  @manager = manager
  filename(manager.default_version_file)
  fields(DEFAULT_FIELD)
  instance_eval(&block) if block
end

Instance Attribute Details

#basefileObject (readonly)

Returns the value of attribute basefile.



7
8
9
# File 'lib/tumbler/manager/version.rb', line 7

def basefile
  @basefile
end

#field_namesObject (readonly)

Returns the value of attribute field_names.



7
8
9
# File 'lib/tumbler/manager/version.rb', line 7

def field_names
  @field_names
end

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/tumbler/manager/version.rb', line 7

def file
  @file
end

Instance Method Details

#baseObject



66
67
68
# File 'lib/tumbler/manager/version.rb', line 66

def base
  @manager.base
end

#bump(level) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tumbler/manager/version.rb', line 48

def bump(level)
  inform "Bumping version to #{@version.bump(level).to_s}" do
    if @manager.noop
      @manager.dry "Bumping version to #{@version.bump(level).to_s}"
    else
      new_file = generate_with_new(@version.bump(level).to_s)
      File.open(file, 'w') {|f| f << new_file }
      reload
    end
  end
end

#commit(from) ⇒ Object



60
61
62
63
64
# File 'lib/tumbler/manager/version.rb', line 60

def commit(from)
  inform "Committing version `#{@basefile}'" do
    sh "git commit #{@basefile} -m'Bumped version from #{from} to #{to_s}'"
  end
end

#extractObject



27
28
29
# File 'lib/tumbler/manager/version.rb', line 27

def extract
  File.read(@file)[/Version\s*=\s*['"](.*?)['"]/i, 1]
end

#fields(f) ⇒ Object



19
20
21
# File 'lib/tumbler/manager/version.rb', line 19

def fields(f)
  @field_names = (f == :all ? @version.field_names : f)
end

#filename(file) ⇒ Object



35
36
37
38
# File 'lib/tumbler/manager/version.rb', line 35

def filename(file)
  @basefile = file
  @file = File.join(@manager.base, file)
end

#generate_with_new(version) ⇒ Object



31
32
33
# File 'lib/tumbler/manager/version.rb', line 31

def generate_with_new(version)
  File.read(@file).gsub(/(Version\s*=\s*['"])(.*?)(['"])/i, "\\1#{version.to_s}\\3")
end

#reloadObject



23
24
25
# File 'lib/tumbler/manager/version.rb', line 23

def reload
  @version = Versionomy.parse(File.exist?(file) ? extract : '0.0.0')
end

#to_sObject



44
45
46
# File 'lib/tumbler/manager/version.rb', line 44

def to_s
  value.to_s
end

#valueObject



40
41
42
# File 'lib/tumbler/manager/version.rb', line 40

def value
  @version
end