Module: Redmine::Installer::Utils::Methods

Defined in:
lib/redmine-installer/utils.rb

Instance Method Summary collapse

Instance Method Details

#ask(message = nil, options = {}) ⇒ Object

Asking on 1 line



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/redmine-installer/utils.rb', line 128

def ask(message=nil, options={})
  # Translate message
  if message.is_a?(Symbol)
    message = translate(message)
  end
  default = options[:default]

  if default
    message << " [#{default}]"
  end

  if !options[:without_colon]
    message << ': '
  end

  say(message)
  input = gets(options[:hide])

  # Ctrl-D or enter was pressed
  return default if input.empty?

  input
end

#choose(message, choices, options = {}) ⇒ Object

User can choose from selection



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/redmine-installer/utils.rb', line 153

def choose(message, choices, options={})
  choices = choices.to_a
  default = options[:default]
  index = 1

  say(message, 1) unless message.nil?
  choices.each do |(key, message)|
    if key == default
      pre = '*'
    else
      pre = ' '
    end

    say(" #{pre}#{index}) #{message}", 1)
    index += 1
  end

  input = ask('> ', without_colon: true).to_i
  puts

  # Without default is input 0
  return default if input.zero? || input > choices.size

  choices[input-1][0]
end

#colorize(text) ⇒ Object

Colorize text based on XML marks

Examples:

colorize("<bright><on_black><white>text</white></on_black></bright>")
# => "\e[1m\e[40m\e[37mtext\e[0m\e[0m\e[0m"


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/redmine-installer/utils.rb', line 97

def colorize(text)
  return unless text.is_a?(String)

  text.gsub!(/<([a-zA-Z0-9_]+)>/) do
    if ANSI::CHART.has_key?($1.to_sym)
      ANSI.send($1)
    else
      "<#{$1}>"
    end
  end
  text.gsub!(/<\/([a-zA-Z0-9_]+)>/) do
    if ANSI::CHART.has_key?($1.to_sym)
      ANSI.clear
    else
      "</#{$1}>"
    end
  end
end

#commandObject



27
28
29
# File 'lib/redmine-installer/utils.rb', line 27

def command
  Redmine::Installer::Command.instance
end

#confirm(message, default = true) ⇒ Object

Say ‘Do you want?’ -> YES, NO



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/redmine-installer/utils.rb', line 182

def confirm(message, default=true)
  # Translate message
  if message.is_a?(Symbol)
    message = translate(message)
  end

  # Colorize message
  colorize(message)

  yes = t(:yes_t)
  no  = t(:no_t)

  if default
    yes.upcase!
  else
    no.upcase!
  end

  message << " (#{yes}/#{no}): "

  $stdout.print(message)
  answer = gets

  return default if answer.empty?

  if answer[0].downcase == yes[0].downcase
    return true
  else
    return false
  end
end

#error(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/redmine-installer/utils.rb', line 31

def error(*args)
  # Translate message
  if args.first.is_a?(Symbol)
    message = translate(*args)
  else
    message = args.first
  end

  # Colorize message
  colorize(message)

  raise Redmine::Installer::Error, message
end

#gets(hide = false) ⇒ Object

Instead of ‘super` take only what I need



117
118
119
120
121
122
123
124
125
# File 'lib/redmine-installer/utils.rb', line 117

def gets(hide=false)
  if hide
    input = $stdin.noecho{|io| io.gets}.to_s.chomp
    $stdout.puts # noecho is also for enter
    input
  else
    $stdin.gets.to_s.chomp
  end
end

#notif(title, message = nil, image = nil) ⇒ Object

Notifications



227
228
229
230
231
232
233
234
235
236
# File 'lib/redmine-installer/utils.rb', line 227

def notif(title, message=nil, image=nil)
  return unless Redmine::Installer.config.notif

  thread = ::Notifier.notify(
    title: title.to_s,
    message: message.to_s,
    image: image.to_s
  )
  thread.join
end

#pluginObject

Generals



23
24
25
# File 'lib/redmine-installer/utils.rb', line 23

def plugin
  Redmine::Installer::Plugin
end

#say(message, lines = 0) ⇒ Object

Print message to stdout



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/redmine-installer/utils.rb', line 77

def say(message, lines=0)
  # Translate message
  if message.is_a?(Symbol)
    message = translate(message)
  end

  # Colorize message
  colorize(message)

  $stdout.print(message)
  lines.times { $stdout.puts }
  $stdout.flush
end

#some_plugins?Boolean

Check if there are plugins in plugin dir

Returns:

  • (Boolean)


68
69
70
# File 'lib/redmine-installer/utils.rb', line 68

def some_plugins?
  Dir.glob(File.join('plugins', '*')).select{|record| File.directory?(record)}.any?
end

#translate(*args) ⇒ Object Also known as: t

Localizations



218
219
220
# File 'lib/redmine-installer/utils.rb', line 218

def translate(*args)
  I18n.translate(*args)
end

#try_create_dir(dir) ⇒ Object

Try create a dir When mkdir raise an error (permission problem) method ask user if wants exist or try again



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/redmine-installer/utils.rb', line 48

def try_create_dir(dir)
  begin
    FileUtils.mkdir_p(dir)
  rescue
    choices = {}
    choices[:exit] = t(:exit)
    choices[:try_again] = t(:try_again)

    answer = choose(t(:dir_not_exist_and_cannot_be_created, dir: dir), choices, default: :exit)

    case answer
    when :exit
      error ''
    when :try_again
      try_create_dir(dir)
    end
  end
end