Class: Bumpversion::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/bumpversion/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, reader) ⇒ Writer

Returns a new instance of Writer.



3
4
5
6
# File 'lib/bumpversion/writer.rb', line 3

def initialize(options, reader)
  @options = options
  @reader = reader
end

Instance Method Details

#content_config_fileObject



13
14
15
# File 'lib/bumpversion/writer.rb', line 13

def content_config_file
  @content_config_file ||= File.read(@options[:config_file])
end

#reader_filesObject



8
9
10
11
# File 'lib/bumpversion/writer.rb', line 8

def reader_files
  @reader.reader!
  @reader.files_to_write
end

#update_config_file!Object



26
27
28
29
# File 'lib/bumpversion/writer.rb', line 26

def update_config_file!
  new_content = content_config_file.gsub(@options[:current_version], @options[:new_version])
  File.write(@options[:config_file], new_content)
end

#validate!Object



17
18
19
20
21
22
23
24
# File 'lib/bumpversion/writer.rb', line 17

def validate!
  file_exists = File.exist? @options[:config_file]
  file_has_current_version = (file_exists && content_config_file.include?("current-version"))
  file_current_version_match = file_has_current_version && content_config_file.include?(@options[:current_version])
  valid = !file_exists || !file_has_current_version || file_current_version_match

  raise "Version File does not Match with Current Version" unless valid
end

#write!Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/bumpversion/writer.rb', line 31

def write!
  reader_files.each do |file_to_write|
    validate!

    new_content = file_to_write[:content].gsub(@options[:current_version], @options[:new_version])
    File.write(file_to_write[:filename], new_content)

    update_config_file!
  end
end