Class: Rlt::Commands::Cmt

Inherits:
Object
  • Object
show all
Defined in:
lib/rlt/commands/cmt.rb

Constant Summary collapse

CONF_SUBJECT_TEMPLATE =
'subject_template'
CONF_BODY_TEMPLATE =
'body_template'
CONF_PRE =
'pre'

Class Method Summary collapse

Class Method Details

.adjust_body_template(body, config, branch_name) ⇒ Object



95
96
97
98
99
# File 'lib/rlt/commands/cmt.rb', line 95

def self.adjust_body_template(body, config, branch_name)
  template = config[CONF_BODY_TEMPLATE]
  return body if config.nil? || template.nil?
  ERB.new(template).result binding
end

.adjust_subject_template(subject, config, branch_name) ⇒ Object

rubocop:enable Metrics/MethodLength



89
90
91
92
93
# File 'lib/rlt/commands/cmt.rb', line 89

def self.adjust_subject_template(subject, config, branch_name)
  template = config[CONF_SUBJECT_TEMPLATE]
  return subject if config.nil? || template.nil?
  ERB.new(template).result binding
end

.ask_and_commit(add_all, config) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/rlt/commands/cmt.rb', line 31

def self.ask_and_commit(add_all, config)
  branch_name = Utils::GitUtil.current_branch_name
  Utils::Logger.info "Committing to '#{branch_name}'"
  (subject, body) = subject_and_body(config, branch_name)
  Utils::GitUtil.add_all if add_all
  commit(subject, body)
end

.ask_bodyObject



71
72
73
74
75
# File 'lib/rlt/commands/cmt.rb', line 71

def self.ask_body
  puts 'Body: (Insert empty line to finish)'
  lines = ask_multiline_until_done('>', :magenta)
  lines.join("\n")
end

.ask_multiline_until_done(message, active_color) ⇒ Object

rubocop:disable Metrics/MethodLength



78
79
80
81
82
83
84
85
86
# File 'lib/rlt/commands/cmt.rb', line 78

def self.ask_multiline_until_done(message, active_color)
  lines = []
  loop do
    line = TTY::Prompt.new.ask(message, active_color: active_color)
    break if line.nil?
    lines << line
  end
  lines
end

.ask_subjectObject



49
50
51
52
53
54
55
# File 'lib/rlt/commands/cmt.rb', line 49

def self.ask_subject
  prompt = TTY::Prompt.new
  subject = prompt.ask('Subject:', active_color: :magenta) do |q|
    q.required true
  end
  garden_subject(subject)
end

.commit(subject, body) ⇒ Object



45
46
47
# File 'lib/rlt/commands/cmt.rb', line 45

def self.commit(subject, body)
  Utils::GitUtil.commit_with_long_message("#{subject}\n\n#{body}".strip)
end

.first_letter_upcase(str) ⇒ Object



67
68
69
# File 'lib/rlt/commands/cmt.rb', line 67

def self.first_letter_upcase(str)
  str[0].upcase + str[1..-1]
end

.garden_subject(subject) ⇒ Object



57
58
59
60
61
# File 'lib/rlt/commands/cmt.rb', line 57

def self.garden_subject(subject)
  subject = remove_period(subject)
  subject = first_letter_upcase(subject)
  subject
end


20
21
22
# File 'lib/rlt/commands/cmt.rb', line 20

def self.print_abort_due_to_pre_script_failure
  Utils::Logger.error 'Aborted due to the pre-script failed.'
end

.remove_period(str) ⇒ Object



63
64
65
# File 'lib/rlt/commands/cmt.rb', line 63

def self.remove_period(str)
  str.gsub(/\.$/, '')
end

.run(config, add_all) ⇒ Object



14
15
16
17
18
# File 'lib/rlt/commands/cmt.rb', line 14

def self.run(config, add_all)
  result = run_pre_script_if_any(config)
  return print_abort_due_to_pre_script_failure unless result
  ask_and_commit(add_all, config)
end

.run_pre_script_if_any(config) ⇒ Object



24
25
26
27
28
29
# File 'lib/rlt/commands/cmt.rb', line 24

def self.run_pre_script_if_any(config)
  return true if config[CONF_PRE].nil?
  Utils::Logger.info 'Executing pre-script:'
  Utils::Logger.desc "  #{config[CONF_PRE]}"
  Utils::Shell.new.run_safely config[CONF_PRE]
end

.subject_and_body(config, branch_name) ⇒ Object



39
40
41
42
43
# File 'lib/rlt/commands/cmt.rb', line 39

def self.subject_and_body(config, branch_name)
  subject = adjust_subject_template(ask_subject, config, branch_name)
  body = adjust_body_template(ask_body, config, branch_name)
  [subject, body]
end