Class: LivecodeServer::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/livecode_server/command.rb

Class Method Summary collapse

Instance Method Summary collapse

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(options)
	@app_name = "livecode"
	@options = {}.merge(options)
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)
	options = {}
	optparse = OptionParser.new do |opts|
		opts.banner  = "Usage: #{File.basename($0)} [command] [options]\n\n"
		opts.banner += "  Server control commands:\n"
		opts.banner += "    start                            Start the server as a daemon\n"
		opts.banner += "    stop                             Stop the server\n"
		opts.banner += "    restart                          Restart the server\n"
		opts.banner += "    run                              Start the server and stay on top\n"
		opts.banner += "    status                           Show server status\n"
		opts.banner += "\n"
		opts.banner += "  Client control commands:\n"
		opts.banner += "    connect                          Connect to remote server\n"
		opts.banner += "    disconnect                       Disconnect from remote server\n"
		opts.banner += "\n"
		opts.banner += "  Other commands:\n"
		opts.banner += "    update_textmate                  Update TextMate bundle\n"
		opts.banner += "\n"
		opts.banner += "  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(options)
		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

.versionObject



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

#disconnectObject

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

#restartObject

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

#statusObject

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_textmateObject

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