Class: Sycersion::VersionEnvironment
- Inherits:
-
Object
- Object
- Sycersion::VersionEnvironment
- 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
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #create_code_snippet(version_file) ⇒ Object
- #create_environment ⇒ Object
- #define_version_and_version_file ⇒ Object
- #describe_adoption_of_version_file ⇒ Object
- #determine_version_and_version_file ⇒ Object
- #digit_counter(object) ⇒ Object
- #filler_string(fill_char, size) ⇒ Object
-
#initialize ⇒ VersionEnvironment
constructor
A new instance of VersionEnvironment.
- #list_versions_and_version_files ⇒ Object
- #load_environment ⇒ Object
- #load_environment_default ⇒ Object
- #print_line(prescriptor, descriptor, value) ⇒ Object
- #prompt_version_and_version_file ⇒ Object
- #resume_version_and_version_file ⇒ Object
- #save ⇒ Object
- #select_version_and_version_file ⇒ Object
- #setup ⇒ Object
- #summary ⇒ Object
- #summary_of_configuration ⇒ Object
- #summary_of_environment ⇒ Object
Constructor Details
#initialize ⇒ VersionEnvironment
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
#version ⇒ Object
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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/sycersion/version_environment.rb', line 130 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_environment ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/sycersion/version_environment.rb', line 161 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. end |
#define_version_and_version_file ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sycersion/version_environment.rb', line 85 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_file ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sycersion/version_environment.rb', line 145 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_file ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sycersion/version_environment.rb', line 53 def determine_version_and_version_file files = Dir.glob('**/*version*') files.each do |file| File.readlines(file, chomp: true).each do |line| scan_result = line.scan(Sycersion::SEMVER_LAX_REGEX) if scan_result[0] @version_files[file] = [scan_result[0], line] break end end end end |
#digit_counter(object) ⇒ Object
118 119 120 |
# File 'lib/sycersion/version_environment.rb', line 118 def digit_counter(object) object.size.to_s.length end |
#filler_string(fill_char, size) ⇒ Object
122 123 124 |
# File 'lib/sycersion/version_environment.rb', line 122 def filler_string(fill_char, size) fill_char * size end |
#list_versions_and_version_files ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/sycersion/version_environment.rb', line 107 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_environment ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/sycersion/version_environment.rb', line 171 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_default ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/sycersion/version_environment.rb', line 181 def load_environment_default @version_file = SYCERSION_VER @version = '0.1.0' @version_files = {} @app_ver_file = '' @version_string = '' end |
#print_line(prescriptor, descriptor, value) ⇒ Object
126 127 128 |
# File 'lib/sycersion/version_environment.rb', line 126 def print_line(prescriptor, descriptor, value) puts "#{prescriptor} #{descriptor} #{value}" end |
#prompt_version_and_version_file ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/sycersion/version_environment.rb', line 66 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_file ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/sycersion/version_environment.rb', line 76 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 |
#save ⇒ Object
157 158 159 |
# File 'lib/sycersion/version_environment.rb', line 157 def save create_environment end |
#select_version_and_version_file ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/sycersion/version_environment.rb', line 96 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 |
#setup ⇒ Object
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 |
#summary ⇒ Object
29 30 31 32 |
# File 'lib/sycersion/version_environment.rb', line 29 def summary summary_of_configuration summary_of_environment end |
#summary_of_configuration ⇒ Object
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_environment ⇒ Object
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 |