Top Level Namespace

Defined Under Namespace

Modules: Bizside, Coverage, Resque

Constant Summary collapse

CARRIERWAVE_SANITIZE_REGEXP =

日本語ファイル名のまま保存

/[^[:word:]①-⑨【】「」『』():・&、  \(\)\.\-\+]/
CarrierwaveStringIO =

後方互換性の維持

Bizside::CarrierwaveStringIO

Class Method Summary collapse

Class Method Details

.add_on_nameObject



88
89
90
91
92
93
94
# File 'lib/bizside/task_helper.rb', line 88

def self.add_on_name
  ret = ENV['ADD_ON_NAME']
  unless ret
    ret = Bizside.config.add_on_name if Bizside.config.add_on_name?
  end
  ret
end

.ask(prompt, options = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bizside/task_helper.rb', line 115

def self.ask(prompt, options = {})
  raise 'プロンプトは必須です。' if prompt.to_s.empty?

  if options[:default].to_s.empty?
    print prompt + ': '
  elsif options[:password]
    print prompt + " [FILTERED]: "
  else
    print prompt + " [#{options[:default]}]: "
  end

  answer = ''
  if answer.empty? and options.has_key?(:env_key)
    answer = ENV[options[:env_key].to_s].to_s
  end

  if answer.empty?
    if interactive?
      if options[:password]
        system("stty -echo")
        at_exit do
          system("stty echo")
        end
      end
  
      answer = STDIN.gets.strip
  
      if options[:password]
        system("stty echo")
        puts
      end
    else
      puts
    end
  else
    if options[:password]
      puts 'FILTERED'
    else
      puts answer
    end
  end

  if answer.empty? and options.has_key?(:default)
    answer = options[:default].to_s
  end

  if answer.empty? and options[:required]
    raise "必須です。処理を中止します。"
  end

  if block_given?
    answer = yield answer
  end

  answer
end

.ask_env(env_key, options = {}) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/bizside/task_helper.rb', line 177

def self.ask_env(env_key, options = {})
  cache_file = 'tmp/cache/env'
  cache = File.exist?(cache_file) ? YAML.load_file(cache_file) : {}

  if options.fetch(:cache, false)
    options = options.merge(default: cache.fetch(env_key, options[:default]))
  end

  ENV[env_key] ||= ask(env_key, options.merge(env_key: env_key))

  if options.fetch(:cache, false)
    FileUtils.mkdir_p(File.dirname(cache_file))
    
    cache[env_key] = ENV[env_key]
    File.write(cache_file, YAML.dump(cache))
  else
    if File.exist?(cache_file) and cache[env_key]
      cache.delete(env_key)
      File.write(cache_file, YAML.dump(cache))
    end
  end

  ENV[env_key]
end

.ask_host(prompt, options = {}) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/bizside/task_helper.rb', line 215

def self.ask_host(prompt, options = {})
  if options.has_key?(:default)
    ask(prompt, options)
  else
    ask(prompt, options.merge(default: dev_default_host))
  end
end

.ask_yes(prompt, options = {}) ⇒ Object



172
173
174
175
# File 'lib/bizside/task_helper.rb', line 172

def self.ask_yes(prompt, options = {})
  answer = ask(prompt, options)
  yes_confirmed?(answer, :fail_on_error => true)
end

.dev_default_hostObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/bizside/task_helper.rb', line 223

def self.dev_default_host
  ret = nil

  udp = UDPSocket.new
  begin
    # クラスBの先頭アドレス,echoポート 実際にはパケットは送信されない。
    udp.connect("128.0.0.0", 7)
    ret = Socket.unpack_sockaddr_in(udp.getsockname)[1]
  ensure
    udp.close
  end

  ret
end

.gem_dirObject



111
112
113
# File 'lib/bizside/task_helper.rb', line 111

def self.gem_dir
  File.expand_path('../../../..', __FILE__)
end

.interactive?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/bizside/task_helper.rb', line 107

def self.interactive?
  yes_confirmed?(ENV['INTERACTIVE'] || 'true')
end

.ip_addressesObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/bizside/task_helper.rb', line 250

def self.ip_addresses
  unless @_ip_addresses
    @_ip_addresses = []
    `/sbin/ip route`.split("\n").each do |line|
      elements = line.split.map(&:strip)
      if index = elements.index('src')
        @_ip_addresses << elements[index + 1]
      end
    end
    @_ip_addresses
  end

  @_ip_addresses
end

.prefixObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/bizside/task_helper.rb', line 96

def self.prefix
  ret = ENV['PREFIX']
  unless ret
    ret = Bizside.config.prefix if Bizside.config.prefix?
  end
  raise 'prefix は / から指定してください。' if ret and not ret.start_with?('/')
  ret ||= '/' + add_on_name if Bizside.config.add_on_name?
  ret ||= ask('プリフィックス', :required => true)
  ret
end

.rails_envObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bizside/task_helper.rb', line 49

def self.rails_env
  ret = 'development'

  # RAILS_ENV が未指定の時は、BIZSIDE_ENV を利用できるか確認する
  if ENV['RAILS_ENV'].to_s.empty?
    unless ENV['BIZSIDE_ENV'].to_s.empty?
      case ENV['BIZSIDE_ENV']
      when 'production', 'staging'
        ret = ENV['RAILS_ENV'] = 'production'
      else
        ret = ENV['RAILS_ENV'] = ENV['BIZSIDE_ENV']
      end
    end
  end

  if ENV['RAILS_ENV'].to_s.empty?
    puts 'RAILS_ENVを選択してください。'
    puts '  1 - production'
    puts '* 2 - development'
    puts '  3 - test'
    puts
    print '> '

    if interactive?
      selected = STDIN.gets.strip.to_i
      ret = ['production', 'development', 'test'][selected - 1] if selected > 0
    else
      puts
      raise "必須です。処理を中止します。"
    end
  else
    ret = ENV['RAILS_ENV']
    puts "RAILS_ENV: #{ret}"
  end

  ENV['RAILS_ENV'] ||= ret
  ret
end

.rails_rootObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bizside/task_helper.rb', line 7

def self.rails_root
  if ENV['RAILS_ROOT'].to_s.empty?
    if interactive?
      case ENV['RAILS_ENV']
      when 'production'
        ret = "/home/#{ENV['USER']}/rails_apps/#{self.add_on_name}/current"
      else
        if defined?(Rails)
          ret = Rails.root.to_s
        else
          ret = `pwd`.strip
        end
      end

      print "RAILS_ROOT [#{ret}]: "
      answer = STDIN.gets.strip
      ret = answer unless answer.empty?
    else
      case ENV['RAILS_ENV']
      when 'test'
        if defined?(Rails)
          ret = Rails.root.to_s
        else
          ret = `pwd`.strip
        end
      end
      puts "RAILS_ROOT [#{ret}]: "
    end
  else
    ret = ENV['RAILS_ROOT']
    puts "RAILS_ROOT [#{ret}]: "
  end

  ENV['RAILS_ROOT'] ||= ret

  if ENV['RAILS_ROOT'].to_s.empty?
    raise "必須です。処理を中止します。"
  end

  ret
end

.render(template, options = {}) ⇒ Object



245
246
247
248
# File 'lib/bizside/task_helper.rb', line 245

def self.render(template, options = {})
  FileUtils.mkdir_p(File.dirname(options[:to]))
  File.write(options[:to], ERB.new(File.read(template), 0, '-').result)
end

.run(*commands) ⇒ Object



238
239
240
241
242
243
# File 'lib/bizside/task_helper.rb', line 238

def self.run(*commands)
  commands.each do |c|
    puts c
    fail unless system(c)
  end
end

.yes_confirmed?(yes_value, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/bizside/task_helper.rb', line 202

def self.yes_confirmed?(yes_value, options = {})
  ret = Bizside::Yes.confirmed?(yes_value)
  if ret.nil?
    if options[:fail_on_error]
      fail "yes/no または true/false 形式で入力してください。answer=#{yes_value}"
    else
      ret = false
    end
  end

  ret
end