Class: Webbynode::Io

Inherits:
Object show all
Defined in:
lib/webbynode/io.rb

Defined Under Namespace

Classes: KeyAlreadyExists

Constant Summary collapse

TemplatesPath =
File.join(File.dirname(__FILE__), '..', 'templates')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.home_dirObject



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/webbynode/io.rb', line 173

def self.home_dir
  if is_windows?
    if ENV['USERPROFILE'].nil?
      userdir = "C:/My Documents/"
    else
      userdir = ENV['USERPROFILE']
    end
  else
    userdir = ENV['HOME'] unless ENV['HOME'].nil?
  end
end

.is_windows?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/webbynode/io.rb', line 13

def self.is_windows?
  RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
end

Instance Method Details

#add_general_setting(key, value) ⇒ Object



260
261
262
# File 'lib/webbynode/io.rb', line 260

def add_general_setting(key, value)
  with_general_settings { |s| s[key] = value }
end

#add_line(file, line) ⇒ Object



226
227
228
229
230
231
232
233
234
# File 'lib/webbynode/io.rb', line 226

def add_line(file, line)
  create_if_missing(file)
  contents = File.read(file)
  return if contents.include?("#{line}")
  File.open(file, 'a') do |f| 
    f.puts "" if !contents.empty? and !(contents =~ /\n$/)
    f.puts line
  end
end

#add_multi_setting(key, values) ⇒ Object



272
273
274
# File 'lib/webbynode/io.rb', line 272

def add_multi_setting(key, values)
  with_setting { |s| s[key] = "(#{values.join(" ")})" }
end

#add_setting(key, value) ⇒ Object



268
269
270
# File 'lib/webbynode/io.rb', line 268

def add_setting(key, value)
  with_setting { |s| s[key] = value }
end

#app_nameObject



64
65
66
# File 'lib/webbynode/io.rb', line 64

def app_name
  Dir.pwd.split("/").last.gsub(/[\.| ]/, "_")
end

#config_multi_add(key, new_value) ⇒ Object



280
281
282
283
284
285
# File 'lib/webbynode/io.rb', line 280

def config_multi_add(key, new_value)
  raise "Missing Webbynode config file" unless file_exists?(".webbynode/config")
  config = read_yaml(".webbynode/config")
  (config[key] ||= []) << new_value
  write_yaml(config)
end

#copy_file(from, to) ⇒ Object



113
114
115
# File 'lib/webbynode/io.rb', line 113

def copy_file(from, to)
  FileUtils.cp(from, to)
end

#create_file(file_name, contents, executable = nil) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/webbynode/io.rb', line 193

def create_file(file_name, contents, executable=nil)
  raise "Tried to create real file: #{file_name}" if $testing and !$testing_io
  File.open(file_name, "w") do |file|
    file.write(contents)
  end
  ::FileUtils.chmod 0755, file_name if executable
end

#create_from_template(file, template) ⇒ Object



221
222
223
224
# File 'lib/webbynode/io.rb', line 221

def create_from_template(file, template)
  contents = read_from_template(template)
  create_file(file, contents)
end

#create_if_missing(file_name, contents = "", executable = nil) ⇒ Object



201
202
203
# File 'lib/webbynode/io.rb', line 201

def create_if_missing(file_name, contents="", executable=nil)
  create_file(file_name, contents, executable) unless file_exists?(file_name)
end

#create_local_key(passphrase = "") ⇒ Object



185
186
187
188
189
190
191
# File 'lib/webbynode/io.rb', line 185

def create_local_key(passphrase="")
  unless File.exists?(LocalSshKey)
    mkdir File.dirname(LocalSshKey)
    key_file = LocalSshKey.gsub(/\.pub$/, "")
    exec "ssh-keygen -t rsa -N \"#{passphrase}\" -f \"#{key_file}\""
  end
end

#db_nameObject



68
69
70
# File 'lib/webbynode/io.rb', line 68

def db_name
  app_name.gsub(/[-._]/, "")      
end

#delete_file(file_name) ⇒ Object



205
206
207
# File 'lib/webbynode/io.rb', line 205

def delete_file(file_name)
  File.delete(file_name)
end

#directory?(s) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/webbynode/io.rb', line 97

def directory?(s)
  File.directory?(s)
end

#exec(s, redirect_stderr = true) ⇒ Object



78
79
80
# File 'lib/webbynode/io.rb', line 78

def exec(s, redirect_stderr=true)
  `#{s}#{redirect_stderr ? " 2>&1" : ""}`
end

#exec2(s, redirect_stderr = true) ⇒ Object



82
83
84
85
# File 'lib/webbynode/io.rb', line 82

def exec2(s, redirect_stderr=true)
  `#{s}#{redirect_stderr ? " 2>&1" : ""}`
  $?
end

#exec3(s, redirect_stderr = false) ⇒ Object



87
88
89
90
# File 'lib/webbynode/io.rb', line 87

def exec3(s, redirect_stderr=false)
  result = `#{s}#{redirect_stderr ? " 2>&1" : ""}`
  [$? == 0, result]
end

#exec_in_path?(file) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/webbynode/io.rb', line 36

def exec_in_path?(file)
  search_in_path do |f| 
    if is_windows?
      File.executable?("#{f}/#{file}") || 
      File.executable?("#{f}/#{file}.exe") || 
      File.executable?("#{f}/#{file}.bat") || 
      File.executable?("#{f}/#{file}.cmd")
    else
      File.executable?("#{f}/#{file}")
    end
  end
end

#execute(s) ⇒ Object



92
93
94
95
# File 'lib/webbynode/io.rb', line 92

def execute(s)
  Kernel.exec s
  $?
end

#exists_in_path?(file) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/webbynode/io.rb', line 32

def exists_in_path?(file)
  search_in_path { |f| File.exists?("#{f}/#{file}") }
end

#file_exists?(f) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/webbynode/io.rb', line 101

def file_exists?(f)
  File.exists?(f)
end

#file_matches(file, regexp) ⇒ Object



60
61
62
# File 'lib/webbynode/io.rb', line 60

def file_matches(file, regexp)
  File.read(file) =~ regexp
end

#general_settingsObject



240
241
242
# File 'lib/webbynode/io.rb', line 240

def general_settings
  @general_settings ||= properties("#{Io.home_dir}/.webbynode")
end

#is_windows?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/webbynode/io.rb', line 28

def is_windows?
  Io.is_windows?
end

#list_files(dir) ⇒ Object



24
25
26
# File 'lib/webbynode/io.rb', line 24

def list_files(dir)
  Dir.glob(dir)
end

#load_setting(key) ⇒ Object



276
277
278
# File 'lib/webbynode/io.rb', line 276

def load_setting(key)
  properties(".webbynode/settings")[key]
end

#log(text, notify = false) ⇒ Object



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
# File 'lib/webbynode/io.rb', line 117

def log(text, notify=false)
  notify = :simple unless notify

  case notify
  when :notify
    notify = true
    puts "#{text}" 
  
  when :simple
    notify = false
    puts "#{text}" 
  
  when :start
    notify = true
    puts "[Webbynode] #{text}" 
  
  when :quiet_start
    notify = false
    puts "[Webbynode] #{text}" 
  
  when :action
    notify = false
    puts "            #{text}"
  
  when :warning
    notify = false
    puts "WARNING: #{text}"
  
  when :action
    notify = false
    puts "            #{text}"
  
  when :finish
    notify = true
    puts
    puts "[Webbynode] #{text}"
    
  when :error
    notify = true
    puts
    puts "[Webbynode] ERROR: #{text}"
    
  else
    notify = false
    puts "            #{text}"
    
  end
      
  Webbynode::Notify.message(text) if notify
end

#log_and_exit(text, notify = false) ⇒ Object



168
169
170
171
# File 'lib/webbynode/io.rb', line 168

def log_and_exit(text, notify=false)
  log(text, notify)
  exit
end

#mkdir(path) ⇒ Object



72
73
74
75
76
# File 'lib/webbynode/io.rb', line 72

def mkdir(path)
  raise "Tried to create real directory: #{path}" if $testing
  # TODO: raise "Tried to create real folder: #{path}" if $testing
  FileUtils.mkdir_p(path)
end

#open_file(f, a, &blk) ⇒ Object



109
110
111
# File 'lib/webbynode/io.rb', line 109

def open_file(f, a, &blk)
  File.open(f, a, &blk)
end

#properties(s) ⇒ Object



236
237
238
# File 'lib/webbynode/io.rb', line 236

def properties(s)
  (@properties||={})[s] = Properties.new(s)
end

#random_password(len = 10) ⇒ Object



17
18
19
20
21
22
# File 'lib/webbynode/io.rb', line 17

def random_password(len=10)
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  newpass = ""
  1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
  return newpass
end

#read_config(config_file) ⇒ Object



295
296
297
298
299
300
301
# File 'lib/webbynode/io.rb', line 295

def read_config(config_file)
  read_file(config_file).split("\n").inject({}) do |hash, line|
    line_parts = line.split("=")
    hash[line_parts.first.strip.to_sym] = line_parts.last.strip
    hash
  end
end

#read_file(f) ⇒ Object



105
106
107
# File 'lib/webbynode/io.rb', line 105

def read_file(f)
  File.read(f)
end

#read_from_template(template) ⇒ Object



217
218
219
# File 'lib/webbynode/io.rb', line 217

def read_from_template(template)
  read_file File.join(templates_path, template)
end

#read_yaml(file) ⇒ Object



287
288
289
# File 'lib/webbynode/io.rb', line 287

def read_yaml(file)
  YAML.load_file(file)
end

#remove_setting(key) ⇒ Object



264
265
266
# File 'lib/webbynode/io.rb', line 264

def remove_setting(key)
  with_setting { |s| s.remove key }
end

#rename_file(old_name, new_name) ⇒ Object



209
210
211
# File 'lib/webbynode/io.rb', line 209

def rename_file(old_name, new_name)
  File.rename(old_name, new_name)
end

#search_in_path(&blk) ⇒ Object



49
50
51
52
53
# File 'lib/webbynode/io.rb', line 49

def search_in_path(&blk)
  return false unless block_given?
  entries = ENV['PATH'].split(is_windows? ? ";" : ":")
  entries.any? &blk
end

#sed(file, from, to) ⇒ Object



55
56
57
58
# File 'lib/webbynode/io.rb', line 55

def sed(file, from, to)
  contents = File.read(file).gsub(from, to)
  File.open(file, 'w') { |f| f.write(contents) }
end

#templates_pathObject



213
214
215
# File 'lib/webbynode/io.rb', line 213

def templates_path
  TemplatesPath
end

#with_general_settings {|general_settings| ... } ⇒ Object

Yields:



250
251
252
253
# File 'lib/webbynode/io.rb', line 250

def with_general_settings(&blk)
  yield general_settings
  general_settings.save
end

#with_setting(&blk) ⇒ Object



255
256
257
258
# File 'lib/webbynode/io.rb', line 255

def with_setting(&blk)
  mkdir('.webbynode') unless directory?('.webbynode')
  with_settings_for ".webbynode/settings", &blk
end

#with_settings_for(file) {|settings| ... } ⇒ Object

Yields:

  • (settings)


244
245
246
247
248
# File 'lib/webbynode/io.rb', line 244

def with_settings_for(file, &blk)
  settings = properties(file)
  yield settings
  settings.save
end

#write_yaml(obj) ⇒ Object



291
292
293
# File 'lib/webbynode/io.rb', line 291

def write_yaml(obj)
  create_file(file, obj.to_yaml)
end