Class: LivecodeServer::Command
- Inherits:
-
Object
- Object
- LivecodeServer::Command
- Defined in:
- lib/livecode_server/command.rb
Class Method Summary collapse
-
.run!(*args) ⇒ Object
Run command.
- .version ⇒ Object
Instance Method Summary collapse
-
#connect(url, *args) ⇒ Object
Connect to a remote server.
-
#disconnect ⇒ Object
Disconnect from remote server.
-
#dispatch!(command, *args) ⇒ Object
Dispatch a command.
-
#initialize(options) ⇒ Command
constructor
A new instance of Command.
-
#restart ⇒ Object
Restart the server.
-
#run(*args) ⇒ Object
Start the server and stay on top.
-
#start(*args) ⇒ Object
Start the server.
-
#status ⇒ Object
Show server status.
-
#stop(*args) ⇒ Object
Stop the server.
-
#update_textmate ⇒ Object
Update TextMate bundle.
Constructor Details
#initialize(options) ⇒ Command
Returns a new instance of Command.
98 99 100 101 |
# File 'lib/livecode_server/command.rb', line 98 def initialize() @app_name = "livecode" @options = {}.merge() end |
Class Method Details
.run!(*args) ⇒ Object
Run command
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 |
# File 'lib/livecode_server/command.rb', line 17 def run!(*args) = {} optparse = OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} [command] [options]\n\n" opts. += " Server control commands:\n" opts. += " start Start the server as a daemon\n" opts. += " stop Stop the server\n" opts. += " restart Restart the server\n" opts. += " run Start the server and stay on top\n" opts. += " status Show server status\n" opts. += "\n" opts. += " Client control commands:\n" opts. += " connect Connect to remote server\n" opts. += " disconnect Disconnect from remote server\n" opts. += "\n" opts. += " Other commands:\n" opts. += " update_textmate Update TextMate bundle\n" opts. += "\n" opts. += " Options:" opts.on('-h', '--help', 'Show this message') do puts opts exit end opts.on('-v', '--version', 'Show version') do puts self.version exit end end optparse.parse! if ARGV.length > 0 command = self.new() run_command = ARGV.shift unless command.dispatch!(run_command, *ARGV) puts "ERROR! Invalid command: #{run_command}\n\n" puts optparse end else puts optparse end end |
.version ⇒ Object
12 13 14 |
# File 'lib/livecode_server/command.rb', line 12 def version "Livecode server " + File.read(File.join(File.dirname(__FILE__), '../../VERSION')) end |
Instance Method Details
#connect(url, *args) ⇒ Object
Connect to a remote server
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/livecode_server/command.rb', line 174 def connect(url, *args) if url =~ /^drb:\/\/.+/ if server_running? daemon_group.stop_all sleep(1) end LivecodeServer.register_uri url puts "Now connected to server on #{url}" else puts "Error: Not a valid server url" end end |
#disconnect ⇒ Object
Disconnect from remote server
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/livecode_server/command.rb', line 188 def disconnect if server_running? puts "Livecode server running as daemon, shutdown to disconnect." elsif LivecodeServer.running? puts "Livecode server disconnected." LivecodeServer.register_shutdown else puts "Not connected to any Livecode servers." end end |
#dispatch!(command, *args) ⇒ Object
Dispatch a command
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/livecode_server/command.rb', line 104 def dispatch!(command, *args) if (self.public_methods - Object.methods - ['dispatch!']).include?(command) begin self.send(command.to_sym, *args) rescue ArgumentError => e puts "Error: #{e}" end return command else return nil end end |
#restart ⇒ Object
Restart the server
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/livecode_server/command.rb', line 149 def restart if server_running? puts "Restarting server.." daemon_group.stop_all sleep(1) daemon_group.start_all sleep(2) puts "Livecode server is now running on #{LivecodeServer.uri}" else puts "Livecode server isn't running, nothing to restart." end end |
#run(*args) ⇒ Object
Start the server and stay on top
129 130 131 132 133 134 135 136 |
# File 'lib/livecode_server/command.rb', line 129 def run(*args) @options[:ontop] = true if server_running? puts "Livecode server is already running on #{LivecodeServer.uri}" else daemon_group.new_application.start end end |
#start(*args) ⇒ Object
Start the server
118 119 120 121 122 123 124 125 126 |
# File 'lib/livecode_server/command.rb', line 118 def start(*args) if server_running? puts "Livecode server is already running on #{LivecodeServer.uri}" else daemon_group.new_application.start sleep(2) puts "Started Livecode server on #{LivecodeServer.uri}" end end |
#status ⇒ Object
Show server status
163 164 165 166 167 168 169 170 171 |
# File 'lib/livecode_server/command.rb', line 163 def status if server_running? puts "Livecode server is running on #{LivecodeServer.uri}" elsif LivecodeServer.running? puts "Remote connected to server on #{LivecodeServer.uri}" else puts "Livecode server isn't running." end end |
#stop(*args) ⇒ Object
Stop the server
139 140 141 142 143 144 145 146 |
# File 'lib/livecode_server/command.rb', line 139 def stop(*args) if server_running? daemon_group.stop_all puts "Livecode server stopped." else puts "Livecode server isn't running, nothing to stop." end end |
#update_textmate ⇒ Object
Update TextMate bundle
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/livecode_server/command.rb', line 200 def update_textmate update_bundle = true bundle_path = File.join(File.dirname(__FILE__), '../../extras/textmate/Ruby Livecode.tmbundle') bundle_path = Pathname.new(bundle_path).realpath.to_s escaped_bundle_path = bundle_path.gsub(/\s/, '\ ') target_path = '/Library/Application Support/TextMate/Bundles/Ruby Livecode.tmbundle' escaped_target_path = target_path.gsub(/\s/, '\ ') # Handle existing files if File.exists?(target_path) if File.symlink?(target_path) if Pathname.new(target_path).realpath.to_s == bundle_path puts "TextMate bundle is already up to date." update_bundle = false else puts "Existing symlink found:\n\n" puts "- " + Pathname.new(target_path).realpath.to_s puts "+ " + bundle_path print "\nReplace? [y/n] " end else print "Existing bundle found, are you sure you want to replace it? [y/n] " end if update_bundle if STDIN.readline.chomp =~ /^y/i `sudo rm -rf #{escaped_target_path}` else update_bundle = false end end end if update_bundle unless File.exists?("/Library/Application Support/TextMate/Bundles") `sudo mkdir -p /Library/Application\\ Support/TextMate/Bundles` end puts "Updating Textmate bundle.." `sudo ln -s #{escaped_bundle_path} #{escaped_target_path}` `osascript -e 'tell app "TextMate" to reload bundles'` end end |