vikingio 0.0.4 → 0.0.5

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