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



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sycersion/version_environment.rb', line 128

def create_code_snippet(version_file)
  "\n  In your application you can now access the version with\n\n  > File.read(\#{version_file})\n\n  If you application framework has a defined place to assign\n  the version you could do like so\n\n  > version = File.read(\#{version_file})\n  CODE_SNIP\nend\n"

#create_environmentObject



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

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], f)
  end
rescue IOError => e
  puts e.message
end

#define_version_and_version_fileObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/sycersion/version_environment.rb', line 83

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

#determine_version_and_version_fileObject



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

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



116
117
118
# File 'lib/sycersion/version_environment.rb', line 116

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

#filler_string(fill_char, size) ⇒ Object



120
121
122
# File 'lib/sycersion/version_environment.rb', line 120

def filler_string(fill_char, size)
  fill_char * size
end

#list_versions_and_version_filesObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/sycersion/version_environment.rb', line 105

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



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

def load_environment
  if File.exist?(SYCERSION_ENV)
    @version_file, @version = YAML.load_file(SYCERSION_ENV)
    true
  else
    @version_file = SYCERSION_VER
    @version = '0.1.0'
    @version_files = {}
    @version_string = ''
    false
  end
end


124
125
126
# File 'lib/sycersion/version_environment.rb', line 124

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

#prompt_version_and_version_fileObject



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

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



74
75
76
77
78
79
80
81
# File 'lib/sycersion/version_environment.rb', line 74

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

#saveObject



142
143
144
# File 'lib/sycersion/version_environment.rb', line 142

def save
  create_environment
end

#select_version_and_version_fileObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/sycersion/version_environment.rb', line 94

def select_version_and_version_file
  puts "\nFound version-files\n"
  puts "-------------------\n"
  list_versions_and_version_files
  print "\nChoose version-file and version with number or hit return for [0]: "
  selection = gets.chomp.to_i
  app_version_file = @version_files.keys[selection]
  @version = Sycersion::Semver.version(@version_files[app_version_file][0])
  @version_string = @version_files[@version_file[1]]
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
# 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 "\nChange the configuration file only if you know what you are doing."
  puts 'Otherwise run `sycersion --init` again.'
end