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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/redmine-installer/utils.rb', line 136

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/redmine-installer/utils.rb', line 161

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"


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/redmine-installer/utils.rb', line 105

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/redmine-installer/utils.rb', line 190

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



125
126
127
128
129
130
131
132
133
# File 'lib/redmine-installer/utils.rb', line 125

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



235
236
237
238
239
240
241
242
243
244
# File 'lib/redmine-installer/utils.rb', line 235

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

#root?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/redmine-installer/utils.rb', line 76

def root?
  Process.euid == 0
end

#say(message, lines = 0) ⇒ Object

Print message to stdout



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/redmine-installer/utils.rb', line 85

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



226
227
228
# File 'lib/redmine-installer/utils.rb', line 226

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

#windows?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/redmine-installer/utils.rb', line 72

def windows?
  RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
end