/
powser
executable file
·112 lines (95 loc) · 2.99 KB
/
powser
1
2
3
4
5
6
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
48
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/ruby
require 'rubygems'
require 'thor'
require 'fileutils'
require 'net/https'
require 'powser/version'
module Powser
class CLI < Thor
include Thor::Actions
default_task :help
SERVER_PATH = File.dirname(__FILE__) + "/../lib/powser"
POWDER_CONFIG = ".powder"
desc "start", "Start a webserver to provide powder functionality from your browser. The server is located at http://powser.dev. Pass the name of the pow link that you want powser to monitor."
def start(app = nil)
return unless is_powable?
app ||= get_pow_name
Dir.chdir(SERVER_PATH) do
`powder link powser`
File.open("app", "w") do |f|
f.puts(app)
end
say "Powser defaults to #{app}."
end
end
desc "stop", "Kill the powser server."
def stop(app = nil)
app ||= get_pow_name
Dir.chdir(SERVER_PATH) do
`powder unlink`
say "Powser no longer defaults to #{app}."
end
end
desc "restart", "Restart the powser server."
def restart
Dir.chdir(SERVER_PATH) do
`powder restart`
say "Powser will restart."
end
end
desc "version", "Shows the version."
def version
say "powser #{Powser::VERSION}"
end
private
def current_dir_name
File.basename(%x{pwd}.chomp)
end
def configured_pow_name
return nil unless File.exists?(POWDER_CONFIG)
File.foreach(POWDER_CONFIG) do |line|
next if line =~ /^($|#)/
return line.chomp
end
return nil
end
def current_dir_pow_name
current_dir_name.tr('_', '-')
end
def get_pow_name
configured_pow_name || current_dir_pow_name
end
def is_powable?
if File.exists?('config.ru') || File.exists?('public/index.html')
true
elsif legacy = (is_rails2_app? || is_radiant_app?)
say "This appears to be a #{legacy} application. You need a config.ru file."
if yes? "Do you want to autogenerate a basic config.ru for #{legacy}?"
uri = URI.parse("https://raw.github.com/gist/909308")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
create_file "config.ru", http.request(request).body
return true
else
say "Did not create config.ru"
return false
end
else
say "This does not appear to be a rack app as there is no config.ru."
say "Pow can also host static apps if there is an index.html in public/"
return false
end
end
def is_rails2_app?
File.exists?('config/environment.rb') &&
!`grep RAILS_GEM_VERSION config/environment.rb`.empty? ? 'Rails 2' : nil
end
def is_radiant_app?
File.exists?('config/environment.rb') &&
!`grep Radiant::Initializer config/environment.rb`.empty? ? 'Radiant' : nil
end
end
end
Powser::CLI.start