Class: Sycersion::VersionEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/sycersion/version_environment.rb

Overview

Creates a version and a version file where the version is stored to.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVersionEnvironment

Returns a new instance of VersionEnvironment.



15
16
17
18
19
# File 'lib/sycersion/version_environment.rb', line 15

def initialize
  return if load_environment

  determine_version_and_version_file
end

Instance Attribute Details

#versionObject

Returns the value of attribute version.



13
14
15
# File 'lib/sycersion/version_environment.rb', line 13

def version
  @version
end

Instance Method Details

#create_code_snippet(version_file) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sycersion/version_environment.rb', line 142

def create_code_snippet(version_file)
  <<-CODE_SNIP

  In your application you can now access the version with

  > File.read('#{version_file}')

  If you application framework has a defined place to assign the version you
  could do like so

  > version = if File.exist? ? File.read('#{version_file}') : 'No VERSION!'
  #{describe_adoption_of_version_file}
  CODE_SNIP
end

#create_environmentObject



173
174
175
176
177
178
179
180
181
# File 'lib/sycersion/version_environment.rb', line 173

def create_environment
  FileUtils.mkdir(SYCERSION_DIR) unless Dir.exist?(SYCERSION_DIR)
  File.open(@version_file, 'w') { |f| f.write(@version) }
  File.open(SYCERSION_ENV, 'w') do |f|
    YAML.dump([@version_file, @version, @app_ver_file, @version_string], f)
  end
rescue IOError => e
  puts e.message
end

#define_version_and_version_fileObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/sycersion/version_environment.rb', line 97

def define_version_and_version_file
  puts 'No version and version-file found'
  print "To use #{SYCERSION_VER} hit RETURN or specify own file: "
  selection = gets.chomp
  @version_file = selection.empty? ? SYCERSION_VER : selection
  print 'To use `0.0.1` as initial version hit RETURN or specify own version: '
  selection = gets.chomp.scan(Sycersion::SEMVER_REGEX)
  @version = selection.empty? ? @version : selection
  @version_string = @version
end

#describe_adoption_of_version_fileObject



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/sycersion/version_environment.rb', line 157

def describe_adoption_of_version_file
  return unless @app_ver_file && @version_string

  <<-PROPOSAL

  In #{@app_ver_file} you can change the version assignment to read from
  #{@version_file} and change it accordingly.

  #{@version_string}
  PROPOSAL
end

#determine_version_and_version_fileObject



53
54
55
56
57
58
59
60
# File 'lib/sycersion/version_environment.rb', line 53

def determine_version_and_version_file
  files = Dir.glob('**/*version*')
  files.each do |file|
    next if File.directory?(file)

    retrieve_version_files(file)
  end
end

#digit_counter(object) ⇒ Object



130
131
132
# File 'lib/sycersion/version_environment.rb', line 130

def digit_counter(object)
  object.size.to_s.length
end

#filler_string(fill_char, size) ⇒ Object



134
135
136
# File 'lib/sycersion/version_environment.rb', line 134

def filler_string(fill_char, size)
  fill_char * size
end

#list_versions_and_version_filesObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/sycersion/version_environment.rb', line 119

def list_versions_and_version_files
  digits = digit_counter(@version_files)
  filler = filler_string(' ', digits + 2)
  @version_files.each_with_index do |entry, index|
    choice = "[#{index.to_s.rjust(digits, ' ')}]"
    print_line(choice, 'file:   ', entry[0])
    print_line(filler, 'version:', Sycersion::Semver.version(entry[1][0]))
    print_line(filler, 'line:   ', (entry[1][1]).strip)
  end
end

#load_environmentObject



183
184
185
186
187
188
189
190
191
# File 'lib/sycersion/version_environment.rb', line 183

def load_environment
  if File.exist?(SYCERSION_ENV)
    @version_file, @version = YAML.load_file(SYCERSION_ENV)
    true
  else
    load_environment_default
    false
  end
end

#load_environment_defaultObject



193
194
195
196
197
198
199
# File 'lib/sycersion/version_environment.rb', line 193

def load_environment_default
  @version_file = SYCERSION_VER
  @version = '0.1.0'
  @version_files = {}
  @app_ver_file = ''
  @version_string = ''
end


138
139
140
# File 'lib/sycersion/version_environment.rb', line 138

def print_line(prescriptor, descriptor, value)
  puts "#{prescriptor} #{descriptor} #{value}"
end

#prompt_version_and_version_fileObject



78
79
80
81
82
83
84
85
86
# File 'lib/sycersion/version_environment.rb', line 78

def prompt_version_and_version_file
  if File.exist?(SYCERSION_VER)
    resume_version_and_version_file
  elsif @version_files.empty?
    define_version_and_version_file
  else
    select_version_and_version_file
  end
end

#resume_version_and_version_fileObject



88
89
90
91
92
93
94
95
# File 'lib/sycersion/version_environment.rb', line 88

def resume_version_and_version_file
  print "Current version-file #{@version_file}. To keep hit RETURN otherwise specify new: "
  selection = gets.chomp
  @version_file = selection.empty? ? @version_file : selection
  print "Current version #{@version}. To keep hit RETURN otherwise enter new: "
  selection = gets.chomp
  @version = selection.empty? ? @version : selection
end

#retrieve_version_files(file) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/sycersion/version_environment.rb', line 62

def retrieve_version_files(file)
  File.readlines(file, chomp: true).each do |line|
    scan_result = scan_for_version(line)
    if scan_result
      @version_files[file] = [scan_result, line]
      break
    end
  end
end

#saveObject



169
170
171
# File 'lib/sycersion/version_environment.rb', line 169

def save
  create_environment
end

#scan_for_version(line) ⇒ Object



72
73
74
75
76
# File 'lib/sycersion/version_environment.rb', line 72

def scan_for_version(line)
  line.scan(Sycersion::SEMVER_LAX_REGEX)[0]
rescue ArgumentError
  []
end

#select_version_and_version_fileObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/sycersion/version_environment.rb', line 108

def select_version_and_version_file
  puts "\nFound files with versions\n"
  puts "-------------------------\n"
  list_versions_and_version_files
  print "\nChoose file and version with number or hit return for [0]: "
  selection = gets.chomp.to_i
  @app_ver_file = @version_files.keys[selection]
  @version = Sycersion::Semver.version(@version_files[@app_ver_file][0])
  @version_string = @version_files[@app_ver_file][1].strip
end

#setupObject



21
22
23
24
25
26
27
# File 'lib/sycersion/version_environment.rb', line 21

def setup
  puts "\nSetup the version environment"
  puts "=============================\n"
  prompt_version_and_version_file
  create_environment
  summary
end

#summaryObject



29
30
31
32
# File 'lib/sycersion/version_environment.rb', line 29

def summary
  summary_of_configuration
  summary_of_environment
end

#summary_of_configurationObject



34
35
36
37
38
39
40
# File 'lib/sycersion/version_environment.rb', line 34

def summary_of_configuration
  puts "\nYour configuration"
  puts "------------------\n"
  puts "\nVersion #{@version}"
  puts "Version-file #{@version_file}\n"
  puts create_code_snippet(@version_file)
end

#summary_of_environmentObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/sycersion/version_environment.rb', line 42

def summary_of_environment
  puts "\nWhere to find the configuration files"
  puts "-------------------------------------\n"
  puts "\nDirectory:                      #{SYCERSION_DIR}"
  puts "Configuration file:             #{SYCERSION_ENV}\n"
  puts "Application version-file:       #{@app_ver_file}" if @app_ver_file
  puts "Application version assignment: #{@version_string}" if @version_string
  puts "\nIf you change the configuration file and encounter odd behaviour,"
  puts 're-run `sycersion --init`.'
end