Class: Bump::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/bump/application.rb

Overview

The application

Instance Method Summary collapse

Constructor Details

#initialize(options, help, version, file, logger, command = nil) ⇒ Application

Returns a new instance of Application.

Parameters:

  • options (Hash)

    The cli options

  • help (String)

    The help message

  • version (String)

    The version expression of this command

  • file (String)

    The file name of bump info file

  • logger (Bump::Logger)

    The logger

  • logger (Bump::Command)

    The command executer



12
13
14
15
16
17
18
19
20
# File 'lib/bump/application.rb', line 12

def initialize(options, help, version, file, logger, command = nil)
  @options = options
  @help = help
  @version = version
  @file = file
  @logger = logger
  @command = command
  @command = Bump::Command.new @logger if @command.nil?
end

Instance Method Details

#action_bumpBoolean

The bump action

Returns:

  • (Boolean)

    true iff success



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bump/application.rb', line 158

def action_bump
  bump_info = create_bump_info

  return false if bump_info.nil?

  print_bump_plan bump_info

  unless bump_info.valid?
    print_invalid_bump_info bump_info

    return false
  end

  bump_info.perform_update

  report bump_info

  save_bump_info bump_info

  commit_and_tag bump_info if @options[:commit]

  true
end

#action_helpvoid

This method returns an undefined value.

handler of ‘bmp –help`



55
56
57
58
59
# File 'lib/bump/application.rb', line 55

def action_help
  log @help

  true
end

#action_infovoid

This method returns an undefined value.

handler of ‘bmp [–info]`



124
125
126
127
128
129
130
131
132
# File 'lib/bump/application.rb', line 124

def action_info
  bump_info = create_bump_info

  return false if bump_info.nil?

  print_version_patterns bump_info

  bump_info.valid?
end

#action_versionvoid

This method returns an undefined value.

handler of ‘bmp –version`



46
47
48
49
50
# File 'lib/bump/application.rb', line 46

def action_version
  log @version

  true
end

#bump_levelSymbol

Gets the requested bump level.

Returns:

  • (Symbol)


184
185
186
187
188
# File 'lib/bump/application.rb', line 184

def bump_level
  return :major if @options[:major]
  return :minor if @options[:minor]
  return :patch if @options[:patch]
end

#bump_option?(options) ⇒ Boolean

Returns true iff it’s a bump option.

Parameters:

  • options (Hash)

    The options

Returns:

  • (Boolean)


39
40
41
# File 'lib/bump/application.rb', line 39

def bump_option?(options)
  options[:major] || options[:minor] || options[:patch] || options[:commit] || options[:preid] || options[:release]
end

#commit_and_tag(bump_info) ⇒ Object

Commits current changes and tag it by the current version.

Parameters:



241
242
243
244
245
246
# File 'lib/bump/application.rb', line 241

def commit_and_tag(bump_info)
  @logger.log '===> executing commands'
  @command.exec 'git add .'
  @command.exec "git commit -m '#{bump_info.commit_message}'"
  @command.exec "git tag v#{bump_info.after_version}"
end

#create_bump_infoBump::BumpInfo

Gets the bump info

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bump/application.rb', line 64

def create_bump_info
  repo = BumpInfoRepository.new @file

  begin
    bump_info = repo.from_file
  rescue Errno::ENOENT
    log_red "Error: the file `#{@file}` not found."
    return nil
  end

  bump_info
end

#log(message = '', newline = true) ⇒ void

This method returns an undefined value.

Logs the message

Parameters:

  • message (String) (defaults to: '')
  • newline (Boolean) (defaults to: true)


269
270
271
# File 'lib/bump/application.rb', line 269

def log(message = '', newline = true)
  @logger.log message, newline
end

#log_green(message, newline = true) ⇒ void

This method returns an undefined value.

Logs the message in green

Parameters:

  • message (String)
  • newline (Boolean) (defaults to: true)


287
288
289
# File 'lib/bump/application.rb', line 287

def log_green(message, newline = true)
  log @logger.green(message), newline
end

#log_red(message, newline = true) ⇒ void

This method returns an undefined value.

Logs the message in red

Parameters:

  • message (String)
  • newline (Boolean) (defaults to: true)


278
279
280
# File 'lib/bump/application.rb', line 278

def log_red(message, newline = true)
  log @logger.red(message), newline
end

#mainvoid

This method returns an undefined value.

The entry point



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/bump/application.rb', line 251

def main
  case select_action
  when :version
    action_version
  when :help
    action_help
  when :info
    action_info
  when :bump
    action_bump
  end
end

Prints the version bump plan.

Parameters:



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/bump/application.rb', line 192

def print_bump_plan(bump_info)
  level = bump_level
  print_bump_plan_level level, bump_info unless level.nil?

  preid = @options[:preid]
  print_bump_plan_preid preid, bump_info unless preid.nil?

  print_bump_plan_release bump_info if @options[:release]

  log
end

Prints the bump plan for the given level.

Parameters:

  • level (Symbol)

    The level

  • bump_info (Bump::BumpInfo)

    The bump info



207
208
209
210
211
212
# File 'lib/bump/application.rb', line 207

def print_bump_plan_level(level, bump_info)
  bump_info.bump level

  log "Bump #{level} level"
  print_version_transition bump_info
end

Prints the bump plan for the give preid.

Parameters:



216
217
218
219
220
221
222
# File 'lib/bump/application.rb', line 216

def print_bump_plan_preid(preid, bump_info)
  bump_info.preid = preid

  log 'Set pre-release version id: ', false
  log_green preid
  print_version_transition bump_info
end

Prints the bump plan for the release

Parameters:



226
227
228
229
230
231
# File 'lib/bump/application.rb', line 226

def print_bump_plan_release(bump_info)
  bump_info.preid = nil

  log 'Remove pre-release version id'
  print_version_transition bump_info
end

This method returns an undefined value.

Checks the bumping is possible.

Parameters:



137
138
139
140
141
142
143
# File 'lib/bump/application.rb', line 137

def print_invalid_bump_info(bump_info)
  log_red 'Some patterns are invalid!'
  log_red 'Stops updating version numbers.'
  log

  print_version_patterns bump_info
end

Prints the pattern info for the given rule

Parameters:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bump/application.rb', line 102

def print_rule(rule)
  unless rule.file_exists
    log_red "  #{rule.file}:", false
    log_red " '#{rule.before_pattern}' (file not found)"

    return
  end

  log "  #{rule.file}:", false

  unless rule.pattern_exists
    log_red " '#{rule.before_pattern}' (pattern not found)"

    return
  end

  log_green " '#{rule.before_pattern}'"
end

This method returns an undefined value.

Shows the version patterns.

Parameters:



89
90
91
92
93
94
95
96
97
98
# File 'lib/bump/application.rb', line 89

def print_version_patterns(bump_info)
  log 'Current Version:', false
  log_green " #{bump_info.before_version}"

  log 'Version patterns:'

  bump_info.update_rules.each do |rule|
    print_rule rule
  end
end

Prints the version transition.

Parameters:



235
236
237
# File 'lib/bump/application.rb', line 235

def print_version_transition(bump_info)
  log_green "  #{bump_info.before_version} => #{bump_info.after_version}"
end

#report(bump_info) ⇒ Object

Reports the bumping details.

Parameters:



147
148
149
150
151
152
153
154
# File 'lib/bump/application.rb', line 147

def report(bump_info)
  bump_info.update_rules.each do |rule|
    log rule.file.to_s
    log '  Performed pattern replacement:'
    log_green "    '#{rule.before_pattern}' => '#{rule.after_pattern}'"
    log
  end
end

#save_bump_info(bump_info) ⇒ void

This method returns an undefined value.

Saves the bump info

Parameters:



80
81
82
83
84
# File 'lib/bump/application.rb', line 80

def save_bump_info(bump_info)
  repo = BumpInfoRepository.new @file

  repo.save bump_info
end

#select_actionSymbol

Returns a symbol which represents the action to perform.

Returns:

  • (Symbol)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bump/application.rb', line 24

def select_action
  return :help if @options[:help]

  return :version if @options[:version]

  return :info if @options[:info]

  return :bump if bump_option? @options

  # command without options invokes info action
  :info
end