vikingio 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,512 +1,539 @@
1
- module VikingIO
2
- module CLI
3
- module Commands
4
-
5
- BASE_PATH = "http://build1.vikingio.com/"
6
- UNITY_COMPILER_PATH_OSX = "/Applications/Unity/Unity.app/Contents/Frameworks/MonoBleedingEdge/bin/gmcs"
7
- UNITY_MONO_PATH_OSX = "/Applications/Unity/Unity.app/Contents/Frameworks/MonoBleedingEdge/bin/mono"
8
- UNITY_MONO_DLL_PATH_OSX = "/Applications/Unity/Unity.app/Contents/Frameworks/MonoBleedingEdge/lib/"
9
- TEMPLATES_DIR = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "templates")) #File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "templates"))
10
- CSC_V35_64_PATH = File.expand_path("c:/Windows/Microsoft.Net/Framework64/v3.5/csc.exe")
11
-
12
- COMMANDS = [
13
- ["help", {:short => "", :full => ""}],
14
- ["login", {:short => "Authenticate with VikingIO", :full => ""}],
15
- ["logout", {:short => "Clear your authentication credentials", :full => ""}],
16
- ["create", {:short => "Create a new VikingIO application", :full => "Ex: vikingio create [app-name]"}],
17
- ["generate", {:short => "Generate a VikingIO node", :full => "Ex: vikingio generate node GameServer"}],
18
- #["link", {:short => "Link the current directory with an existing VikingIO application", :full => "Ex: vikingio link [app-name]"}],
19
- ["unlink", {:short => "Unlink the current directory with it's VikingIO application", :full => "Ex: vikingio unlink"}],
20
- #["compile", {:short => "Compile your application on the server.", :full => "Once compiled, your application is ready to deploy."}],
21
- ["deploy", {:short => "Deploy your application", :full => "The latest build will be deployed to our cloud infrastructure. [NOTE: Billing will begin immediately after your application is deployed.]"}],
22
- #["destroy", {:short => "Destroy all production nodes in the VikingIO cloud", :full => "This will completely shut down and remove all of your production nodes for this application."}],
23
- ["run", {:short => "Compile and run your application locally", :full => ""}],
24
- #["status", {:short => "Get status of nodes", :full => ""}],
25
- #[""]
26
- ]
27
-
28
- def self.run(command, args)
29
- puts "VikingIO Version: #{VikingIO::CLI::VERSION}"
30
-
31
- case command
32
- when "help"
33
- help(args)
34
- when "login"
35
- login(args)
36
- when "logout"
37
- logout(args)
38
- when "create"
39
- create(args)
40
- when "generate"
41
- generate(args)
42
- when "link"
43
- link(args)
44
- when "unlink"
45
- unlink(args)
46
- when "run"
47
- compile_and_run(args)
48
- when "deploy"
49
- deploy(args)
50
- #when "compile"
51
- # compile_on_server(args)
52
- when "destroy"
53
- destroy(args)
54
- when "status"
55
- status(args)
56
- else
57
- help(args)
58
- end
59
- end
60
-
61
- def self.status(*args)
62
- app_data = VikingIO::CLI.get_app_data
63
- user_key = VikingIO::CLI.get_netrc_key
64
- puts VikingIO::CLI::Client.get_status(user_key, app_data["key"])
65
- end
66
-
67
- def self.destroy(*args)
68
-
69
- puts ""
70
- puts "****** WARNING *******"
71
- puts "This will shut down all of the server node instances for this application. You will have to re-deploy from scratch."
72
- puts ""
73
-
74
- puts "Proceed? (y/n)"
75
- y_or_n = STDIN.gets.chomp.downcase
76
-
77
- if ["y", "yes"].include?(y_or_n)
78
-
79
- else
80
- puts "Aborting."
81
- end
82
-
83
- end
84
-
85
- # def self.compile_on_server(*args)
86
- # args.flatten!
87
- #
88
- # app_data = VikingIO::CLI.get_app_data
89
- # user_key = VikingIO::CLI.get_netrc_key
90
- #
91
- # puts "Synchronizing '#{app_data["name"]}' files..."
92
- #
93
- # VikingIO::CLI::Client.request_build_job(user_key, app_data["key"])
94
- #
95
- # Dir.chdir(File.join(Dir.pwd, "Assets", "VikingIO")) do
96
- # files = Dir.glob("**/**/**").reject {|x| x.start_with?("Client") }.reject {|x| Dir.exist?(x) }.reject {|x| x.end_with?(".meta") }
97
- # files.each do |file|
98
- # puts file + "..."
99
- # VikingIO::CLI::Client.synchronize_file(user_key, app_data["key"], file)
100
- # end
101
- # end
102
- #
103
- # puts "Compiling..."
104
- #
105
- # VikingIO::CLI::Client.compile_server(user_key, app_data["key"], app_data["name"])
106
- #
107
- # puts "done."
108
- # end
109
-
110
- def self.deploy(*args)
111
- args.flatten!
112
-
113
- app_data = VikingIO::CLI.get_app_data
114
- user_key = VikingIO::CLI.get_netrc_key
115
- node_data = VikingIO::CLI.get_node_data
116
-
117
- tmp_dir = File.join(Dir.tmpdir, "vikingio")
118
-
119
- compile = !args.include?("--skip-compilation")
120
- compilation_success = true
121
-
122
- if args.include?("--check")
123
- new_node_ct = VikingIO::CLI::Client.deploy_check(user_key, app_data["key"]).to_i
124
-
125
- if new_node_ct == 0
126
- puts "No new nodes will be created."
127
- else
128
- puts "#{new_node_ct} new node(s) will be created."
129
- end
130
- return
131
- end
132
-
133
- if compile
134
- if !self.compile_nodes(tmp_dir, node_data)
135
- compilation_success = false
136
- else
137
- `cd #{File.join(tmp_dir, "VikingIO")} && tar -zcvf Data.tar.gz Data`
138
- puts "Deploying data..."
139
- VikingIO::CLI::Client.deploy_data(user_key, app_data["key"], File.join(tmp_dir, "VikingIO", "Data.tar.gz"))
140
- `rm -rf #{File.join(tmp_dir, "VikingIO", "Data")}*`
141
- `cd #{File.join(tmp_dir, "VikingIO")} && tar -zcvf #{File.join(tmp_dir, "VikingIO.tar.gz")} .`
142
- puts "Deploying nodes..."
143
- VikingIO::CLI::Client.deploy_servers(user_key, app_data["key"], File.join(tmp_dir, "VikingIO.tar.gz"))
144
- end
145
- end
146
-
147
- if compilation_success
148
-
149
- new_node_ct = VikingIO::CLI::Client.deploy_check(user_key, app_data["key"]).to_i
150
- deploy = true
151
-
152
- if new_node_ct != 0
153
- puts "#{new_node_ct} new node(s) will be created. Continue? (y/n)"
154
- y_or_n = STDIN.gets.chomp.downcase
155
-
156
- if !["y", "yes"].include?(y_or_n)
157
- deploy = false
158
- puts "Aborting."
159
- end
160
-
161
- end
162
-
163
- if deploy
164
- puts "Deploying"
165
- puts VikingIO::CLI::Client.deploy(user_key, app_data["key"])
166
- end
167
-
168
- # VikingIO::CLI::Client.deploy(user_key, app_data["key"], node_data.to_json)
169
- else
170
- puts "Compilation failed. Aborting deploy."
171
- end
172
- end
173
-
174
- def self.compile_nodes(tmp_dir, node_data)
175
- begin
176
- FileUtils::rm_rf(tmp_dir)
177
- FileUtils::mkdir_p(tmp_dir)
178
-
179
- if IS_WINDOWS
180
- working_directory = `echo %cd%`.chomp
181
- else
182
- working_directory = Dir.pwd
183
- end
184
-
185
- puts "Copying project"
186
- FileUtils::cp_r(File.join(File.expand_path(working_directory), "Assets", "VikingIO"), tmp_dir)
187
-
188
- puts "Removing Client directory"
189
- FileUtils::rm_rf(File.join(tmp_dir, "VikingIO", "Client"))
190
-
191
- puts "Removing meta files"
192
- meta_files = Dir["#{tmp_dir}/VikingIO/**/*.meta"]
193
- meta_files.each {|x| FileUtils.rm_rf(x) }
194
-
195
- puts "Loading Main class"
196
- templates_dir = File.join(File.dirname(__FILE__), "..", "..", "..", "templates")
197
- program_template = File.read(File.join(templates_dir, "Program.cs"))
198
-
199
- app_data = VikingIO::CLI.get_app_data
200
-
201
- #puts "Loading App Data: #{app_data}"
202
- #dlls_dir = File.join(File.dirname(__FILE__), "../../../dlls")
203
- #viking_dll_path = File.join(dlls_dir, "VikingIO.Network.dll")
204
- #FileUtils::cp(viking_dll_path, File.join(tmp_dir, "VikingIO"))
205
-
206
- cs_files = Dir["#{tmp_dir}/VikingIO/**/*.cs"]
207
-
208
- references = Dir["#{tmp_dir}/VikingIO/Server/External/**/*.dll"]
209
- reference_str = references.map{|x| "-reference:#{x}" }.join(" ")
210
-
211
- log_file = File.join(tmp_dir, "VikingIO", "log")
212
-
213
- node_data.each do |node|
214
- @exit_status = nil
215
- @exit_msg = ""
216
- puts "Compiling '" + node["node_class"] + "' node"
217
- node_class = node["node_class"]
218
-
219
- program_path = File.join(tmp_dir, "VikingIO", node_class + ".Main.cs")
220
-
221
- puts app_data["name"]
222
- File.open(program_path, "w") do |f|
223
- f << program_template.gsub("[MODULE_NAME]", node_class).gsub("[APP_NAME]", app_data["name"])
224
- end
225
-
226
- all_files = cs_files + [program_path]
227
- out_file = "#{tmp_dir}/VikingIO/#{node_class}.exe"
228
-
229
-
230
- library_path = File.join(tmp_dir, "VikingIO", "Server", "External")
231
- csc_path = "c:\\Windows\\Microsoft.Net\\Framework64\\v3.5\\csc.exe"
232
-
233
- if IS_WINDOWS
234
- references = Dir["#{tmp_dir}/VikingIO/Server/External/**/*.dll"]
235
- reference_str = references.map {|x| "/r:#{File.expand_path(x)}" }.join(" ")
236
- cs_files = Dir["#{tmp_dir}/VikingIO/**/*.cs"]
237
- cs_files_str = cs_files.map {|x| File.expand_path(x) }.join(" ")
238
-
239
- Open3.popen3("cd #{tmp_dir}/VikingIO && #{CSC_V35_64_PATH} #{reference_str} /out:#{node_class}.exe /recurse:Server\\*.cs /recurse:Shared\\.cs #{node_class}.Main.cs") {|stdin, stdout, stderr, wait_thr|
240
- pid = wait_thr.pid # pid of the started process.
241
-
242
- @exit_status = wait_thr.value # Process::Status object returned.
243
-
244
- stderr.each do |x|
245
- @exit_msg << x
246
- end
247
- }
248
- else
249
- Open3.popen3("#{UNITY_COMPILER_PATH_OSX} #{reference_str} -lib:#{library_path} -optimize #{all_files.join(" ")} -out:#{out_file}") {|stdin, stdout, stderr, wait_thr|
250
- pid = wait_thr.pid # pid of the started process.
251
-
252
- @exit_status = wait_thr.value # Process::Status object returned.
253
-
254
- stderr.each do |x|
255
- @exit_msg << x
256
- end
257
- }
258
- end
259
-
260
- references.each {|x| FileUtils.cp x, File.join(tmp_dir, "VikingIO") }
261
-
262
- if @exit_status.success? == false
263
- puts "ERROR COMPILING: #{@exit_msg}"
264
- return false;
265
- end
266
- end
267
-
268
- node_data.each {|x| FileUtils.rm_rf File.join(tmp_dir, "VikingIO", "#{x}.cs") }
269
-
270
- src_files = Dir.glob("#{tmp_dir}/VikingIO/**").reject {|x| x.end_with?("exe") || x.end_with?("Data") || x.end_with?("dll") }
271
- src_files.each {|x| FileUtils.rm_rf x }
272
-
273
- rescue Exception => e
274
- puts "Error compiling: #{e}"
275
- return false
276
- end
277
- end
278
-
279
- def self.compile_and_run(*args)
280
-
281
- tmp_dir = File.join(Dir.tmpdir, "vikingio")
282
- node_data = VikingIO::CLI.get_node_data
283
- default_port = 14242
284
-
285
- if self.compile_nodes(tmp_dir, node_data)
286
- pids = []
287
-
288
- node_data.each do |node|
289
- exe_file = "#{tmp_dir}/VikingIO/#{node["node_class"]}.exe"
290
- if IS_WINDOWS
291
- pids << spawn("#{exe_file} #{default_port}")
292
- else
293
- pids << spawn("DYLD_LIBRARY_PATH=#{UNITY_MONO_DLL_PATH_OSX} #{UNITY_MONO_PATH_OSX} #{exe_file} #{default_port}")
294
- end
295
- default_port += 1
296
- end
297
-
298
- begin
299
- puts "Press ctrl-C to stop"
300
- while true do
301
- sleep 0.1
302
- end
303
- rescue Interrupt => e
304
- pids.each {|pid| Process.kill("INT", pid) }
305
- puts "Stopped processes: #{pids.join(",")}."
306
- end
307
- else
308
-
309
- end
310
- end
311
-
312
- def self.link(*args)
313
- end
314
-
315
- def self.unlink(*args)
316
- VikingIO::CLI.unlink_app
317
- puts "Application data cleared."
318
- end
319
-
320
- def self.create(*args)
321
- #args.flatten!
322
-
323
- #if !VikingIO::CLI.authenticated?
324
- # puts "Please login first."
325
- # return
326
- #end
327
-
328
- #if VikingIO::CLI.linked?
329
- # puts "A VikingIO application is already linked to this directory. Use 'vikingio unlink' to clear the application data for this directory."
330
- # return
331
- #end
332
-
333
- #app_name = args.join(" ") rescue nil
334
-
335
- #app = VikingIO::CLI::Client.create_app(app_name)
336
-
337
- #if app["status"] == "error"
338
- # puts app["message"]
339
- # return;
340
- #end
341
-
342
- #VikingIO::CLI.link_app(app)
343
-
344
- #puts "Created application '#{app['name']}'."
345
-
346
- self.generate("--force")
347
- end
348
-
349
- def self.generate(*args)
350
- args.flatten!
351
-
352
- assets_path = File.join(Dir.pwd, "Assets")
353
- viking_assets_path = File.join(assets_path, "VikingIO")
354
- app_data = VikingIO::CLI.get_app_data
355
-
356
- if args[0] == "node"
357
- if args[1] == "" || args[1] == nil
358
- puts "Please enter a camel cased name for your node. i.e. GameServer"
359
- return
360
- end
361
-
362
- node_data = VikingIO::CLI::Client.create_node(args[1], app_data["key"])
363
- VikingIO::CLI.save_node_data(node_data["nodes"])
364
-
365
- puts ""
366
-
367
-
368
- if node_data["status"] == "error"
369
- puts node_data["msg"]
370
- puts "\nIf you want to create a new node, delete your existing node with 'vikingio destroy node #{node_data["nodes"][0]["node_class"]}'. \nThis will shut down any deployed and running node instances.\n\n"
371
- end
372
-
373
- node_output_path = File.join(viking_assets_path, "Server", "#{args[1]}.cs")
374
- if File.exist?(node_output_path)
375
- puts "#{node_output_path} already exists. Ignoring template generation. Delete the file first if you want to override it."
376
- else
377
- server_node_template = File.read(File.join(TEMPLATES_DIR, "ServerNodeTemplate.cs")).gsub("[SERVER_NAME]", args[1])
378
- File.open(node_output_path, 'w') { |file| file.write(server_node_template) }
379
- end
380
-
381
- return
382
-
383
- node_output_path = File.join(viking_assets_path, "Server", "#{args[1]}.cs")
384
-
385
- if !File.exist?(node_output_path)
386
- server_node_template = File.read(File.join(TEMPLATES_DIR, "ServerNodeTemplate.cs")).gsub("[SERVER_NAME]", args[1])
387
- File.open(node_output_path, 'w') { |file| file.write(server_node_template) }
388
- node_data = VikingIO::CLI.get_node_data
389
- node_data << args[1]
390
- VikingIO::CLI.save_node_data(node_data)
391
- end
392
-
393
- puts "Node '#{args[1]}' created."
394
- return
395
- end
396
-
397
- if args[0] == "message"
398
- puts "Message '#{args[1]}' created."
399
- server_node_template = File.read(File.join(TEMPLATES_DIR, "MessageTemplate.cs")).gsub("[MESSAGE_NAME]", args[1])
400
- File.open(File.join(viking_assets_path, "Shared", "Messages", "#{args[1]}.cs"), 'w') { |file| file.write(server_node_template) }
401
- return
402
- end
403
-
404
- if File.exist?(viking_assets_path) && !args.include?("--force")
405
- puts "VikingIO already exists in your Assets folder. Run this command again with the --force command to overwrite."
406
- return
407
- end
408
-
409
- puts "Generating skeleton application"
410
-
411
- # puts "Generating folder structure..."
412
- FileUtils::mkdir_p(File.join(viking_assets_path, "Client", "External"))
413
- FileUtils::mkdir_p(File.join(viking_assets_path, "Server", "External"))
414
- FileUtils::mkdir_p(File.join(viking_assets_path, "Shared", "Messages"))
415
- FileUtils::mkdir_p(File.join(viking_assets_path, "Data"))
416
-
417
- dlls_dir = File.join(File.dirname(__FILE__), "..", "..", "..", "dlls")
418
-
419
- config_dest = File.join(viking_assets_path, "Client", "VikingIO.Config.cs")
420
-
421
- # puts "Copying template files..."
422
- FileUtils.cp(File.join(TEMPLATES_DIR, "NetworkManager.cs"), File.join(viking_assets_path, "Client", "NetworkManager.cs"))
423
- FileUtils.cp(File.join(TEMPLATES_DIR, "VikingIO.Config.cs"), config_dest)
424
-
425
- contents = File.read(config_dest).gsub("[APP_NAME]", app_data["name"]).gsub("[APP_ID]", app_data["id"].to_s)
426
-
427
- File.open(config_dest, 'w') { |file| file.write(contents) }
428
-
429
- File.open(File.join(viking_assets_path, "Nodefile"), "w") do |f|
430
- f << [].to_json
431
- end
432
-
433
- # puts "Copying dlls..."
434
- FileUtils.cp(File.join(dlls_dir, "VikingIO.Network.dll"), File.join(viking_assets_path, "Server", "External", "VikingIO.Network.dll"))
435
- FileUtils.cp(File.join(dlls_dir, "VikingIO.Network.Client.dll"), File.join(viking_assets_path, "Client", "External", "VikingIO.Network.Client.dll"))
436
- puts "Done."
437
- end
438
-
439
- def self.help(*args)
440
- args.flatten!
441
- if args.size == 0
442
- puts "\nCommands: \n\n"
443
-
444
- COMMANDS.each do |cmd|
445
- if cmd[1][:short].nil?
446
- puts cmd[0]
447
- else
448
- puts "#{cmd[0]} - #{cmd[1][:short]}"
449
- end
450
- end
451
-
452
- puts ""
453
- else
454
- cmd = COMMANDS.find {|x| x[0] == args[0] }
455
-
456
- puts "\n=========== " + cmd[0] + " ===========\n\n"
457
- puts cmd[1][:short] + "\n\n"
458
- puts cmd[1][:full]
459
- end
460
- puts ""
461
- end
462
-
463
- def self.login(*args)
464
- email = ""
465
- password = ""
466
-
467
- puts "Enter your email:"
468
- email = STDIN.gets.chomp
469
-
470
- puts ""
471
- puts "Enter your password:"
472
- password = STDIN.noecho(&:gets).chomp
473
-
474
- if email.nil? || email == "" || password.nil? || password == ""
475
- puts "Email or Password empty"
476
- return
477
- end
478
-
479
- app_key = VikingIO::CLI::Client.authenticate(email, password)
480
-
481
- if app_key.nil?
482
- puts "Invalid authentication."
483
- return
484
- end
485
-
486
- if VikingIO::CLI.netrc_exist?
487
- if VikingIO::CLI.in_netrc?
488
- VikingIO::CLI.update_netrc_key(app_key)
489
- else
490
- VikingIO::CLI.add_netrc_key(email, app_key)
491
- end
492
- puts "Authenticated #{email}."
493
- else
494
- VikingIO::CLI.create_netrc(email, app_key)
495
- puts "Authenticated #{email}."
496
- end
497
-
498
- end
499
-
500
- def self.logout(*args)
501
- VikingIO::CLI.remove_netrc_key
502
-
503
- puts "Authentication cleared."
504
- end
505
-
506
- end
507
- end
508
- end
509
-
510
-
511
-
512
-
1
+ module VikingIO
2
+ module CLI
3
+ module Commands
4
+
5
+ BASE_PATH = "http://build1.vikingio.com/"
6
+ UNITY_COMPILER_PATH_OSX = "/Applications/Unity/Unity.app/Contents/Frameworks/MonoBleedingEdge/bin/gmcs"
7
+ UNITY_MONO_PATH_OSX = "/Applications/Unity/Unity.app/Contents/Frameworks/MonoBleedingEdge/bin/mono"
8
+ UNITY_MONO_DLL_PATH_OSX = "/Applications/Unity/Unity.app/Contents/Frameworks/MonoBleedingEdge/lib/"
9
+ TEMPLATES_DIR = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "templates")) #File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "templates"))
10
+ CSC_V35_64_PATH = File.expand_path("c:/Windows/Microsoft.Net/Framework64/v3.5/csc.exe")
11
+
12
+ COMMANDS = [
13
+ ["help", {:short => "", :full => ""}],
14
+ ["login", {:short => "Authenticate with VikingIO", :full => ""}],
15
+ ["logout", {:short => "Clear your authentication credentials", :full => ""}],
16
+ ["create", {:short => "Create a new VikingIO application", :full => "Ex: vikingio create [app-name]"}],
17
+ ["generate", {:short => "Generate a VikingIO node", :full => "Ex: vikingio generate node GameServer"}],
18
+ #["link", {:short => "Link the current directory with an existing VikingIO application", :full => "Ex: vikingio link [app-name]"}],
19
+ ["unlink", {:short => "Unlink the current directory with it's VikingIO application", :full => "Ex: vikingio unlink"}],
20
+ #["compile", {:short => "Compile your application on the server.", :full => "Once compiled, your application is ready to deploy."}],
21
+ ["deploy", {:short => "Deploy your application", :full => "The latest build will be deployed to our cloud infrastructure. [NOTE: Billing will begin immediately after your application is deployed.]"}],
22
+ #["destroy", {:short => "Destroy all production nodes in the VikingIO cloud", :full => "This will completely shut down and remove all of your production nodes for this application."}],
23
+ ["run", {:short => "Compile and run your application locally", :full => ""}],
24
+ #["status", {:short => "Get status of nodes", :full => ""}],
25
+ #[""]
26
+ ]
27
+
28
+ def self.run(command, args)
29
+ puts "VikingIO Version: #{VikingIO::CLI::VERSION}"
30
+
31
+ case command
32
+ when "help"
33
+ help(args)
34
+ when "login"
35
+ login(args)
36
+ when "logout"
37
+ logout(args)
38
+ when "create"
39
+ create(args)
40
+ when "generate"
41
+ generate(args)
42
+ when "link"
43
+ link(args)
44
+ when "unlink"
45
+ unlink(args)
46
+ when "run"
47
+ compile_and_run(args)
48
+ when "deploy"
49
+ deploy(args)
50
+ #when "compile"
51
+ # compile_on_server(args)
52
+ when "destroy"
53
+ destroy(args)
54
+ when "status"
55
+ status(args)
56
+ else
57
+ help(args)
58
+ end
59
+ end
60
+
61
+ def self.status(*args)
62
+ app_data = VikingIO::CLI.get_app_data
63
+ user_key = VikingIO::CLI.get_netrc_key
64
+ puts VikingIO::CLI::Client.get_status(user_key, app_data["key"])
65
+ end
66
+
67
+ def self.destroy(*args)
68
+
69
+ puts ""
70
+ puts "****** WARNING *******"
71
+ puts "This will shut down all of the server node instances for this application. You will have to re-deploy from scratch."
72
+ puts ""
73
+
74
+ puts "Proceed? (y/n)"
75
+ y_or_n = STDIN.gets.chomp.downcase
76
+
77
+ if ["y", "yes"].include?(y_or_n)
78
+
79
+ else
80
+ puts "Aborting."
81
+ end
82
+
83
+ end
84
+
85
+ # def self.compile_on_server(*args)
86
+ # args.flatten!
87
+ #
88
+ # app_data = VikingIO::CLI.get_app_data
89
+ # user_key = VikingIO::CLI.get_netrc_key
90
+ #
91
+ # puts "Synchronizing '#{app_data["name"]}' files..."
92
+ #
93
+ # VikingIO::CLI::Client.request_build_job(user_key, app_data["key"])
94
+ #
95
+ # Dir.chdir(File.join(Dir.pwd, "Assets", "VikingIO")) do
96
+ # files = Dir.glob("**/**/**").reject {|x| x.start_with?("Client") }.reject {|x| Dir.exist?(x) }.reject {|x| x.end_with?(".meta") }
97
+ # files.each do |file|
98
+ # puts file + "..."
99
+ # VikingIO::CLI::Client.synchronize_file(user_key, app_data["key"], file)
100
+ # end
101
+ # end
102
+ #
103
+ # puts "Compiling..."
104
+ #
105
+ # VikingIO::CLI::Client.compile_server(user_key, app_data["key"], app_data["name"])
106
+ #
107
+ # puts "done."
108
+ # end
109
+
110
+ def self.deploy(*args)
111
+ args.flatten!
112
+
113
+ app_data = VikingIO::CLI.get_app_data
114
+ user_key = VikingIO::CLI.get_netrc_key
115
+ node_data = VikingIO::CLI.get_node_data
116
+
117
+ tmp_dir = File.join(Dir.tmpdir, "vikingio")
118
+
119
+ compile = !args.include?("--skip-compilation")
120
+ compilation_success = true
121
+
122
+ if args.include?("--check")
123
+ new_node_ct = VikingIO::CLI::Client.deploy_check(user_key, app_data["key"]).to_i
124
+
125
+ if new_node_ct == 0
126
+ puts "No new nodes will be created."
127
+ else
128
+ puts "#{new_node_ct} new node(s) will be created."
129
+ end
130
+ return
131
+ end
132
+
133
+ if compile
134
+ if !self.compile_nodes(tmp_dir, node_data)
135
+ compilation_success = false
136
+ else
137
+
138
+ if IS_WINDOWS
139
+ directory = "#{File.join(tmp_dir, "VikingIO", "Data")}"
140
+ zipfile_name = "#{File.join(tmp_dir, "Data.zip")}"
141
+
142
+ Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
143
+ Dir[File.join(directory, '**', '**')].each do |file|
144
+ zipfile.add(file.sub(directory, ''), file)
145
+ end
146
+ end
147
+
148
+ VikingIO::CLI::Client.deploy_data(user_key, app_data["key"], zipfile_name)
149
+
150
+ FileUtils.rm_rf(directory)
151
+
152
+ directory = "#{File.join(tmp_dir, "VikingIO", File::SEPARATOR)}"
153
+ zipfile_name = "#{File.join(tmp_dir, "VikingIO.zip")}"
154
+
155
+ Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
156
+ Dir[File.join(directory, '**', '**')].each do |file|
157
+ zipfile.add(file.sub(directory, ''), file)
158
+ end
159
+ end
160
+
161
+ VikingIO::CLI::Client.deploy_servers(user_key, app_data["key"], zipfile_name)
162
+ else
163
+ `cd #{File.join(tmp_dir, "VikingIO")} && tar -zcvf Data.tar.gz Data`
164
+ puts "Deploying data..."
165
+ VikingIO::CLI::Client.deploy_data(user_key, app_data["key"], File.join(tmp_dir, "VikingIO", "Data.tar.gz"))
166
+ `rm -rf #{File.join(tmp_dir, "VikingIO", "Data")}*`
167
+ `cd #{File.join(tmp_dir, "VikingIO")} && tar -zcvf #{File.join(tmp_dir, "VikingIO.tar.gz")} .`
168
+ puts "Deploying nodes..."
169
+ VikingIO::CLI::Client.deploy_servers(user_key, app_data["key"], File.join(tmp_dir, "VikingIO.tar.gz"))
170
+ end
171
+ end
172
+ end
173
+
174
+ if compilation_success
175
+
176
+ new_node_ct = VikingIO::CLI::Client.deploy_check(user_key, app_data["key"]).to_i
177
+ deploy = true
178
+
179
+ if new_node_ct != 0
180
+ puts "#{new_node_ct} new node(s) will be created. Continue? (y/n)"
181
+ y_or_n = STDIN.gets.chomp.downcase
182
+
183
+ if !["y", "yes"].include?(y_or_n)
184
+ deploy = false
185
+ puts "Aborting."
186
+ end
187
+
188
+ end
189
+
190
+ if deploy
191
+ puts "Deploying"
192
+ puts VikingIO::CLI::Client.deploy(user_key, app_data["key"])
193
+ end
194
+
195
+ # VikingIO::CLI::Client.deploy(user_key, app_data["key"], node_data.to_json)
196
+ else
197
+ puts "Compilation failed. Aborting deploy."
198
+ end
199
+ end
200
+
201
+ def self.compile_nodes(tmp_dir, node_data)
202
+ begin
203
+ FileUtils::rm_rf(tmp_dir)
204
+ FileUtils::mkdir_p(tmp_dir)
205
+
206
+ if IS_WINDOWS
207
+ working_directory = `echo %cd%`.chomp
208
+ else
209
+ working_directory = Dir.pwd
210
+ end
211
+
212
+ puts "Copying project"
213
+ FileUtils::cp_r(File.join(File.expand_path(working_directory), "Assets", "VikingIO"), tmp_dir)
214
+
215
+ puts "Removing Client directory"
216
+ FileUtils::rm_rf(File.join(tmp_dir, "VikingIO", "Client"))
217
+
218
+ puts "Removing meta files"
219
+ meta_files = Dir["#{tmp_dir}/VikingIO/**/*.meta"]
220
+ meta_files.each {|x| FileUtils.rm_rf(x) }
221
+
222
+ puts "Generating Main"
223
+ templates_dir = File.join(File.dirname(__FILE__), "..", "..", "..", "templates")
224
+ program_template = File.read(File.join(templates_dir, "Program.cs"))
225
+
226
+ app_data = VikingIO::CLI.get_app_data
227
+
228
+ #puts "Loading App Data: #{app_data}"
229
+ #dlls_dir = File.join(File.dirname(__FILE__), "../../../dlls")
230
+ #viking_dll_path = File.join(dlls_dir, "VikingIO.Network.dll")
231
+ #FileUtils::cp(viking_dll_path, File.join(tmp_dir, "VikingIO"))
232
+
233
+ cs_files = Dir["#{tmp_dir}/VikingIO/**/*.cs"]
234
+
235
+ references = Dir["#{tmp_dir}/VikingIO/Server/External/**/*.dll"]
236
+ reference_str = references.map{|x| "-reference:#{x}" }.join(" ")
237
+
238
+ log_file = File.join(tmp_dir, "VikingIO", "log")
239
+
240
+ node_data.each do |node|
241
+ @exit_status = nil
242
+ @exit_msg = ""
243
+ puts "Compiling '" + node["node_class"] + "' node"
244
+ node_class = node["node_class"]
245
+
246
+ program_path = File.join(tmp_dir, "VikingIO", node_class + ".Main.cs")
247
+
248
+ puts app_data["name"]
249
+ File.open(program_path, "w") do |f|
250
+ f << program_template.gsub("[MODULE_NAME]", node_class).gsub("[APP_NAME]", app_data["name"])
251
+ end
252
+
253
+ all_files = cs_files + [program_path]
254
+ out_file = "#{tmp_dir}/VikingIO/#{node_class}.exe"
255
+
256
+
257
+ library_path = File.join(tmp_dir, "VikingIO", "Server", "External")
258
+ csc_path = "c:\\Windows\\Microsoft.Net\\Framework64\\v3.5\\csc.exe"
259
+
260
+ if IS_WINDOWS
261
+ references = Dir["#{tmp_dir}/VikingIO/Server/External/**/*.dll"]
262
+ reference_str = references.map {|x| "/r:#{File.expand_path(x)}" }.join(" ")
263
+ cs_files = Dir["#{tmp_dir}/VikingIO/**/*.cs"]
264
+ cs_files_str = cs_files.map {|x| File.expand_path(x) }.join(" ")
265
+
266
+ Open3.popen3("cd #{tmp_dir}/VikingIO && #{CSC_V35_64_PATH} #{reference_str} /out:#{node_class}.exe /recurse:Server\\*.cs /recurse:Shared\\.cs #{node_class}.Main.cs") {|stdin, stdout, stderr, wait_thr|
267
+ pid = wait_thr.pid # pid of the started process.
268
+
269
+ @exit_status = wait_thr.value # Process::Status object returned.
270
+
271
+ stderr.each do |x|
272
+ @exit_msg << x
273
+ end
274
+ }
275
+ else
276
+ Open3.popen3("#{UNITY_COMPILER_PATH_OSX} #{reference_str} -lib:#{library_path} -optimize #{all_files.join(" ")} -out:#{out_file}") {|stdin, stdout, stderr, wait_thr|
277
+ pid = wait_thr.pid # pid of the started process.
278
+
279
+ @exit_status = wait_thr.value # Process::Status object returned.
280
+
281
+ stderr.each do |x|
282
+ @exit_msg << x
283
+ end
284
+ }
285
+ end
286
+
287
+ references.each {|x| FileUtils.cp x, File.join(tmp_dir, "VikingIO") }
288
+
289
+ if @exit_status.success? == false
290
+ puts "ERROR COMPILING: #{@exit_msg}"
291
+ return false;
292
+ end
293
+ end
294
+
295
+ node_data.each {|x| FileUtils.rm_rf File.join(tmp_dir, "VikingIO", "#{x}.cs") }
296
+
297
+ src_files = Dir.glob("#{tmp_dir}/VikingIO/**").reject {|x| x.end_with?("exe") || x.end_with?("Data") || x.end_with?("dll") }
298
+ src_files.each {|x| FileUtils.rm_rf x }
299
+
300
+ rescue Exception => e
301
+ puts "Error compiling: #{e}"
302
+ return false
303
+ end
304
+ end
305
+
306
+ def self.compile_and_run(*args)
307
+
308
+ tmp_dir = File.join(Dir.tmpdir, "vikingio")
309
+ node_data = VikingIO::CLI.get_node_data
310
+ default_port = 14242
311
+
312
+ if self.compile_nodes(tmp_dir, node_data)
313
+ pids = []
314
+
315
+ node_data.each do |node|
316
+ exe_file = "#{tmp_dir}/VikingIO/#{node["node_class"]}.exe"
317
+ if IS_WINDOWS
318
+ pids << spawn("#{exe_file} #{default_port}")
319
+ else
320
+ pids << spawn("DYLD_LIBRARY_PATH=#{UNITY_MONO_DLL_PATH_OSX} #{UNITY_MONO_PATH_OSX} #{exe_file} #{default_port}")
321
+ end
322
+ default_port += 1
323
+ end
324
+
325
+ begin
326
+ puts "Press ctrl-C to stop"
327
+ while true do
328
+ sleep 0.1
329
+ end
330
+ rescue Interrupt => e
331
+ pids.each {|pid| Process.kill("INT", pid) }
332
+ puts "Stopped processes: #{pids.join(",")}."
333
+ end
334
+ else
335
+
336
+ end
337
+ end
338
+
339
+ def self.link(*args)
340
+ end
341
+
342
+ def self.unlink(*args)
343
+ VikingIO::CLI.unlink_app
344
+ puts "Application data cleared."
345
+ end
346
+
347
+ def self.create(*args)
348
+ args.flatten!
349
+
350
+ if !VikingIO::CLI.authenticated?
351
+ puts "Please login first."
352
+ return
353
+ end
354
+
355
+ if VikingIO::CLI.linked?
356
+ puts "A VikingIO application is already linked to this directory. Use 'vikingio unlink' to clear the application data for this directory."
357
+ return
358
+ end
359
+
360
+ app_name = args.join(" ") rescue nil
361
+
362
+ app = VikingIO::CLI::Client.create_app(app_name)
363
+
364
+ if app["status"] == "error"
365
+ puts app["message"]
366
+ return;
367
+ end
368
+
369
+ VikingIO::CLI.link_app(app)
370
+
371
+ puts "Created application '#{app['name']}'."
372
+
373
+ self.generate("--force")
374
+ end
375
+
376
+ def self.generate(*args)
377
+ args.flatten!
378
+
379
+ assets_path = File.join(Dir.pwd, "Assets")
380
+ viking_assets_path = File.join(assets_path, "VikingIO")
381
+ app_data = VikingIO::CLI.get_app_data
382
+
383
+ if args[0] == "node"
384
+ if args[1] == "" || args[1] == nil
385
+ puts "Please enter a camel cased name for your node. i.e. GameServer"
386
+ return
387
+ end
388
+
389
+ node_data = VikingIO::CLI::Client.create_node(args[1], app_data["key"])
390
+ VikingIO::CLI.save_node_data(node_data["nodes"])
391
+
392
+ puts ""
393
+
394
+
395
+ if node_data["status"] == "error"
396
+ puts node_data["msg"]
397
+ puts "\nIf you want to create a new node, delete your existing node with 'vikingio destroy node #{node_data["nodes"][0]["node_class"]}'. \nThis will shut down any deployed and running node instances.\n\n"
398
+ end
399
+
400
+ node_output_path = File.join(viking_assets_path, "Server", "#{args[1]}.cs")
401
+ if File.exist?(node_output_path)
402
+ puts "#{node_output_path} already exists. Ignoring template generation. Delete the file first if you want to override it."
403
+ else
404
+ server_node_template = File.read(File.join(TEMPLATES_DIR, "ServerNodeTemplate.cs")).gsub("[SERVER_NAME]", args[1])
405
+ File.open(node_output_path, 'w') { |file| file.write(server_node_template) }
406
+ end
407
+
408
+ return
409
+
410
+ node_output_path = File.join(viking_assets_path, "Server", "#{args[1]}.cs")
411
+
412
+ if !File.exist?(node_output_path)
413
+ server_node_template = File.read(File.join(TEMPLATES_DIR, "ServerNodeTemplate.cs")).gsub("[SERVER_NAME]", args[1])
414
+ File.open(node_output_path, 'w') { |file| file.write(server_node_template) }
415
+ node_data = VikingIO::CLI.get_node_data
416
+ node_data << args[1]
417
+ VikingIO::CLI.save_node_data(node_data)
418
+ end
419
+
420
+ puts "Node '#{args[1]}' created."
421
+ return
422
+ end
423
+
424
+ if args[0] == "message"
425
+ puts "Message '#{args[1]}' created."
426
+ server_node_template = File.read(File.join(TEMPLATES_DIR, "MessageTemplate.cs")).gsub("[MESSAGE_NAME]", args[1])
427
+ File.open(File.join(viking_assets_path, "Shared", "Messages", "#{args[1]}.cs"), 'w') { |file| file.write(server_node_template) }
428
+ return
429
+ end
430
+
431
+ if File.exist?(viking_assets_path) && !args.include?("--force")
432
+ puts "VikingIO already exists in your Assets folder. Run this command again with the --force command to overwrite."
433
+ return
434
+ end
435
+
436
+ puts "Generating skeleton application"
437
+
438
+ # puts "Generating folder structure..."
439
+ FileUtils::mkdir_p(File.join(viking_assets_path, "Client", "External"))
440
+ FileUtils::mkdir_p(File.join(viking_assets_path, "Server", "External"))
441
+ FileUtils::mkdir_p(File.join(viking_assets_path, "Shared", "Messages"))
442
+ FileUtils::mkdir_p(File.join(viking_assets_path, "Data"))
443
+
444
+ dlls_dir = File.join(File.dirname(__FILE__), "..", "..", "..", "dlls")
445
+
446
+ config_dest = File.join(viking_assets_path, "Client", "VikingIO.Config.cs")
447
+
448
+ # puts "Copying template files..."
449
+ FileUtils.cp(File.join(TEMPLATES_DIR, "NetworkManager.cs"), File.join(viking_assets_path, "Client", "NetworkManager.cs"))
450
+ FileUtils.cp(File.join(TEMPLATES_DIR, "VikingIO.Config.cs"), config_dest)
451
+
452
+ contents = File.read(config_dest).gsub("[APP_NAME]", app_data["name"]).gsub("[APP_ID]", app_data["id"].to_s)
453
+
454
+ File.open(config_dest, 'w') { |file| file.write(contents) }
455
+
456
+ File.open(File.join(viking_assets_path, "Nodefile"), "w") do |f|
457
+ f << [].to_json
458
+ end
459
+
460
+ # puts "Copying dlls..."
461
+ FileUtils.cp(File.join(dlls_dir, "VikingIO.Network.dll"), File.join(viking_assets_path, "Server", "External", "VikingIO.Network.dll"))
462
+ FileUtils.cp(File.join(dlls_dir, "VikingIO.Network.Client.dll"), File.join(viking_assets_path, "Client", "External", "VikingIO.Network.Client.dll"))
463
+ puts "Done."
464
+ end
465
+
466
+ def self.help(*args)
467
+ args.flatten!
468
+ if args.size == 0
469
+ puts "\nCommands: \n\n"
470
+
471
+ COMMANDS.each do |cmd|
472
+ if cmd[1][:short].nil?
473
+ puts cmd[0]
474
+ else
475
+ puts "#{cmd[0]} - #{cmd[1][:short]}"
476
+ end
477
+ end
478
+
479
+ puts ""
480
+ else
481
+ cmd = COMMANDS.find {|x| x[0] == args[0] }
482
+
483
+ puts "\n=========== " + cmd[0] + " ===========\n\n"
484
+ puts cmd[1][:short] + "\n\n"
485
+ puts cmd[1][:full]
486
+ end
487
+ puts ""
488
+ end
489
+
490
+ def self.login(*args)
491
+ email = ""
492
+ password = ""
493
+
494
+ puts "Enter your email:"
495
+ email = STDIN.gets.chomp
496
+
497
+ puts ""
498
+ puts "Enter your password:"
499
+ password = STDIN.noecho(&:gets).chomp
500
+
501
+ if email.nil? || email == "" || password.nil? || password == ""
502
+ puts "Email or Password empty"
503
+ return
504
+ end
505
+
506
+ app_key = VikingIO::CLI::Client.authenticate(email, password)
507
+
508
+ if app_key.nil?
509
+ puts "Invalid authentication."
510
+ return
511
+ end
512
+
513
+ if VikingIO::CLI.netrc_exist?
514
+ if VikingIO::CLI.in_netrc?
515
+ VikingIO::CLI.update_netrc_key(app_key)
516
+ else
517
+ VikingIO::CLI.add_netrc_key(email, app_key)
518
+ end
519
+ puts "Authenticated #{email}."
520
+ else
521
+ VikingIO::CLI.create_netrc(email, app_key)
522
+ puts "Authenticated #{email}."
523
+ end
524
+
525
+ end
526
+
527
+ def self.logout(*args)
528
+ VikingIO::CLI.remove_netrc_key
529
+
530
+ puts "Authentication cleared."
531
+ end
532
+
533
+ end
534
+ end
535
+ end
536
+
537
+
538
+
539
+