Class: Powify::App

Inherits:
Object
  • Object
show all
Extended by:
Powify
Defined in:
lib/powify/app.rb

Constant Summary collapse

AVAILABLE_METHODS =
%w(create link new destroy unlink remove restart always_restart always_restart_off browse open rename environment env logs help)

Constants included from Powify

POWPATH

Class Method Summary collapse

Methods included from Powify

config, current_path, extension

Class Method Details

.always_restart(args = []) ⇒ Object

powify always_restart powify always_restart foo



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/powify/app.rb', line 63

def always_restart(args = [])
  app_name = args[0] ? args[0].strip.to_s.downcase : File.basename(current_path)
  symlink_path = "#{POWPATH}/#{app_name}"
  if File.exists?(symlink_path)
    FileUtils.mkdir_p("#{symlink_path}/tmp")
    %x{touch #{symlink_path}/tmp/always_restart.txt}
    $stdout.puts "#{app_name} will now restart after every request!"
  else
    $stdout.puts "Powify could not find an app to always restart with the name #{app_name}"
    $stdout.puts "Type `powify server list` for a full list of applications."
  end
end

.always_restart_off(args = []) ⇒ Object

powify always_restart_off powify always_restart_off foo



78
79
80
81
82
83
84
85
86
87
# File 'lib/powify/app.rb', line 78

def always_restart_off(args = [])
  app_name = args[0] ? args[0].strip.to_s.downcase : File.basename(current_path)
  restart_txt_path = "#{POWPATH}/#{app_name}/tmp/always_restart.txt"
  if File.exists?(restart_txt_path)
    FileUtils.rm_f(restart_txt_path)
    $stdout.puts "#{app_name} will no longer restart after every request!"
  else
    $stdout.puts "Powify detemined that the app with name #{app_name} is not set to always restart."
  end
end

.browse(args = []) ⇒ Object Also known as: open

powify browse powify browse foo powify browse foo test



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/powify/app.rb', line 92

def browse(args = [])
  app_name = args[0] ? args[0].strip.to_s.downcase : File.basename(current_path)
  ext = args[1] || extension
  symlink_path = "#{POWPATH}/#{app_name}"
  if File.exists?(symlink_path)
    %x{open http://#{app_name}.#{ext}}
  else
    $stdout.puts "Powify could not find an app to browse with the name #{app_name}"
    Powify::Server.list
  end
end

.create(args = []) ⇒ Object Also known as: link, new

powify create powify create foo



15
16
17
18
19
20
21
# File 'lib/powify/app.rb', line 15

def create(args = [])
  app_name = args[0] ? args[0].strip.to_s.downcase : File.basename(current_path)
  symlink_path = "#{POWPATH}/#{app_name}"
  FileUtils.ln_s(current_path, symlink_path)
  $stdout.puts "Successfully created pow app #{app_name}!"
  $stdout.puts "Type `powify browse #{app_name}` to open the application in your browser."
end

.destroy(args = []) ⇒ Object Also known as: unlink, remove

powify destroy powify destroy foo



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/powify/app.rb', line 27

def destroy(args = [])
  app_name = args[0] ? args[0].strip.to_s.downcase : File.basename(current_path)
  symlink_path = "#{POWPATH}/#{app_name}"
  if File.exists?(symlink_path)
    FileUtils.rm(symlink_path)
    $stdout.puts "Successfully destroyed pow app #{app_name}!"
    $stdout.puts "If this was an accident, type `powify create #{app_name}` to re-create the app."
  else
    $stdout.puts "Powify could not find an app named `#{app_name}` on this server."
    $stdout.puts "By default, powify tries to look for an application with the same name as the current directory."
    $stdout.puts "If your application has a different name than the working directory, you'll need to specify in the command:"
    $stdout.puts "\n\tpowify destroy [NAME]\n\n"
    $stdout.puts "If your app was named `foo`, you would type:"
    $stdout.puts "\n\tpowify destroy foo\n\n"
  end
end

.environment(args = []) ⇒ Object Also known as: env

powify environment production powify environment foo production



122
123
124
125
126
127
128
129
130
# File 'lib/powify/app.rb', line 122

def environment(args = [])
  return if args.empty?
  app_name, env = File.basename(current_path), args[0].strip.to_s.downcase
  app_name, env = args[0].strip.to_s.downcase, args[1].strip.to_s.downcase if args.size > 1
  symlink_path = "#{POWPATH}/#{app_name}"
  %x{echo export RAILS_ENV=#{env} > #{symlink_path}/.powenv}
  $stdout.puts "Successfully changed environment to #{env}."
  restart [app_name]
end

.logs(args = []) ⇒ Object

powify logs powify logs foo



135
136
137
138
139
# File 'lib/powify/app.rb', line 135

def logs(args = [])
  app_name = args[0] ? args[0].strip.to_s.downcase : File.basename(current_path)
  symlink_path = "#{POWPATH}/#{app_name}"
  system "tail -f #{symlink_path}/log/#{ENV['RAILS_ENV']||'development'}.log"
end

.rename(args = []) ⇒ Object

powify rename bar powify rename foo bar



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/powify/app.rb', line 107

def rename(args = [])
  return if args.empty?
  original_app_name, new_app_name = File.basename(current_path), args[0].strip.to_s.downcase
  original_app_name, new_app_name = args[0].strip.to_s.downcase, args[1].strip.to_s.downcase if args.size > 1
  original_symlink_path, new_symlink_path = "#{POWPATH}/#{original_app_name}", "#{POWPATH}/#{new_app_name}"

  FileUtils.rm(original_symlink_path)
  FileUtils.ln_s(current_path, new_symlink_path)

  $stdout.puts "Succesfully renamed #{original_app_name} to #{new_app_name}."
  $stdout.puts "Type `powify browse #{new_app_name}` to open the application in your browser."
end

.restart(args = []) ⇒ Object

powify restart powify restart foo



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/powify/app.rb', line 48

def restart(args = [])
  app_name = args[0] ? args[0].strip.to_s.downcase : File.basename(current_path)
  symlink_path = "#{POWPATH}/#{app_name}"
  if File.exists?(symlink_path)
    FileUtils.mkdir_p("#{symlink_path}/tmp")
    %x{touch #{symlink_path}/tmp/restart.txt}
    $stdout.puts "Successfully restarted #{app_name}!"
  else
    $stdout.puts "Powify could not find an app to restart with the name #{app_name}"
    $stdout.puts "Type `powify server list` for a full list of applications."
  end
end

.run(args) ⇒ Object



7
8
9
10
11
# File 'lib/powify/app.rb', line 7

def run(args)
  method = args[0].strip.to_s.downcase
  raise "The command `#{args.first}` does not exist!" unless AVAILABLE_METHODS.include?(method)
  self.send(method, args[1..-1])
end