Class: Cova

Inherits:
Thor
  • Object
show all
Includes:
CovaConfig, CovaIO, CovaLog
Defined in:
lib/covamain.rb

Instance Method Summary collapse

Methods included from CovaIO

#_io_app_exists, #_io_clone_repo, #_io_cpdir, #_io_cpfile, #_io_exec, #_io_exec_xctool, #_io_findport, #_io_init, #_io_isdir, #_io_isfile, #_io_lndir, #_io_ls, #_io_mkdir, #_io_open_app_source, #_io_pod_add_repo, #_io_rmdir

Methods included from CovaLog

#_log_block_error, #_log_block_start, #_log_block_success, #_log_info, #_log_line, #_log_line_error, #_log_line_ok

Methods included from CovaConfig

#_config_load

Constructor Details

#initialize(*args) ⇒ Cova

Returns a new instance of Cova.



7
8
9
10
# File 'lib/covamain.rb', line 7

def initialize(*args)
  super
  _io_init
end

Instance Method Details

#build(name) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/covamain.rb', line 477

def build (name)
	if not _cova_installed
			_cova_prompt_install
			return false
		end

	if not _io_app_exists name
		_log_line "App #{name} does not exist"
		_log_line_error
		return false
	end

	Dir.chdir(@dir_cova_apps_home + "/" + name + "/ios")
		_log_block_start "Building app #{name}"
 	if options[:dependencies] or not _io_isfile "Cova.xcworkspace" or not _io_isdir "Pods"
  	if not _cova_build_dependencies
  		_log_block_error
  		return false
  	end
		end

		_io_exec_xctool (options[:tests] ? "test" : "build")
		_io_open_app_source name
end

#bye(name) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/covamain.rb', line 254

def bye (name)
	if not _cova_installed
			_cova_prompt_install
			return false
		end

	if not _io_app_exists name
		_log_line "App #{name} does not exist."
		_log_line_error
		return false
	end

		pid_file = @dir_cova_apps_home + "/" + name + "/store/hi.lock"
		if not _io_isfile pid_file
			_log_info "How rude. Say hi first."
			return false
		end

		File.open(pid_file).each_line {|pid| Process.kill("TERM", pid.to_i)}
		`rm -rf #{pid_file}`

		if not _io_isfile pid_file, false
			_log_info "Bye!"
			return true
		end

		_log_line_error
		return false    	
end

#create(name) ⇒ Object



190
191
192
193
194
195
196
197
198
199
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
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/covamain.rb', line 190

def create (name)

		if not _cova_installed
			_cova_prompt_install
			return false
		end

  	appname = name.strip.delete(' ')
  	_log_block_start "Creating Cova app #{appname}"
  	dir = Dir.pwd + "/" + appname

  	if _io_isdir dir, false or _io_app_exists appname or not _io_mkdir dir or not _io_lndir dir, @dir_cova_apps_home + "/" + appname
  		_log_block_error
  		return false
  	end

  	if not _io_cpdir @dir_cova_home + "/cova/store", dir + "/store"
			_log_block_error
  		return false
  	end

  	if _io_cpdir @dir_cova_home + "/cova/web/app", dir + "/www"
  		config_content = File.read(dir + "/www/config.php")
  		config_content = config_content.gsub(/%cova_home%/, @dir_cova_home)
			File.open(dir + "/www/config.php", 'w') { |file| file.puts config_content }
  	end

  	if _io_cpdir @dir_cova_home + "/cova/ios", dir + "/ios"
 	Dir.chdir(@dir_cova_apps_home + "/" + name + "/ios")
   	
   	#project  = Xcodeproj::Project.new "Cova.xcodeproj"
			#target = project.targets[0]
			#test_target = project.targets[1]
			#test_debug_build_settings = test_target.build_configuration_list.build_configurations[1].build_settings

			# puts test_debug_build_settings
		# target.name = name
			# project.targets[1].name = "#{name}Tests"
			
			#release_build_settings = target.build_configuration_list.build_configurations[0].build_settings
			#debug_build_settings = target.build_configuration_list.build_configurations[1].build_settings
			#project.save_as("Cova.xcodeproj")

  		if options[:build]
   	if not _cova_build_dependencies
   		_log_line "Attempted to build the app dependencies"
   		_log_line_error
 	 		return false
  		end
  			
_io_exec_xctool "test"
_io_open_app_source name
  			return true
  		end

	  	_log_block_success
   	return true
  	end

		_log_block_error
  	return false
end

#deploy(name) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/covamain.rb', line 433

def deploy (name)
	if not _cova_installed
			_cova_prompt_install
			return false
		end

	if not _io_app_exists name
		_log_line "App #{name} does not exist"
		_log_line_error
		return false
	end

	Dir.chdir(@dir_cova_apps_home + "/" + name)
	if not _io_isfile "app.config"
		_log_line "App configuration file app.config does not exist."
		_log_line_error
  _log_info "Please add the app.config file."
  return false
	end

	config = _config_load "app.config"
	deploy_dir = config['local.www']
	if not deploy_dir + "/#{name}"
			_log_line "Expecting the local.www path to be configured."
		_log_line_error			
		_log_info "Please set the local.www path in your app.config file."
  return false
	end

 `rm -rf #{deploy_dir}/#{name}`
 if _io_cpdir "www", "#{deploy_dir}/#{name}"
			_log_line "Deploying the app to #{deploy_dir}/#{name}"
			_log_line_ok
		return true
		end
	
		_log_line "Deploying the app to #{deploy_dir}/#{name}"
		_log_line_error			
  return false
end

#find(name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/covamain.rb', line 77

def find(name)
		if not _cova_installed
			_cova_prompt_install
			return false
		end

		_log_line "Searching for app #{name}"
		if _io_app_exists name
			_log_line_ok
			return true
		end

		_log_line_error
		return false
end

#hi(name) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/covamain.rb', line 311

def hi (name)
	if not _cova_installed
			_cova_prompt_install
			return false
		end

	if not _io_app_exists name
		_log_line "App #{name} does not exist"
		_log_line_error
		return false
	end

		pid_file = @dir_cova_apps_home + "/" + name + "/store/hi.lock"
		if _io_isfile pid_file, false
			_log_info "You already said hi. Say bye first."
			return true
		end

	port = _io_findport 10080
		if port < 0
			_log_info "Too many handshakes. Try saying hi again."
			return false
		end
	
		db_file = @dir_cova_apps_home + "/" + name + "/store/log.db"		
		if not File.exist?(db_file)
db = SQLite3::Database.new db_file
  db.execute("CREATE TABLE changelog(file, event, date)")
		end

		if not File.exist?(db_file)
			_log_info "Cannot start logging. Try saying hi again."
			return false
		end

		monitor = FSSM::Monitor.new
		monitor.path @dir_cova_apps_home + "/" + name do
			create do |b, r, t| 
if File.exist?(db_file) and not r == "store/log.db" and not r == "store/hi.lock"
		db = SQLite3::Database.new db_file
	db.execute "insert into changelog values ('#{r}', 'created', '" + Time.new.strftime("%Y-%m-%d %H:%M:%S") + "')"
end
			end	
			update do |b, r, t|
if File.exist?(db_file) and not r == "store/log.db" and not r == "store/hi.lock"
		db = SQLite3::Database.new db_file
	db.execute "insert into changelog values ('#{r}', 'updated', '" + Time.new.strftime("%Y-%m-%d %H:%M:%S") + "')"
end
			end	
			delete do |b, r, t|
if File.exist?(db_file) and not r == "store/log.db" and not r == "store/hi.lock"
		db = SQLite3::Database.new db_file
	db.execute "insert into changelog values ('#{r}', 'deleted', '" + Time.new.strftime("%Y-%m-%d %H:%M:%S") + "')"
end
			end
		end	

		pid = fork do
			monitor.run
		end
		# Process.detach(pid)
		File.open(pid_file, 'a') {|f| f.puts pid}
			
		Dir.chdir(@dir_cova_apps_home + "/" + name) 
		# Open3.popen3("php -S localhost:#{port} -t www &> store/server.log") {|stdin, stdout, stderr, wait_thr|
		#   puts "@@@"
		# }

 # pid_server = fork { 
 # `php -S localhost:10080 &> store/server.log`
 # IO.popen("php -S localhost:10080 -t www") do |stdout|
 # 	    puts "stdout = " + stdout.pid
 # end
 # Open3.popen3("php -S localhost:10080 -t www") do |stdin, stdout, stderr, wait_thr|
 #  	puts wait_thr
 #  	puts wait_thr.pid
 # #  while line = stdout.gets
 # #   puts line
 # # end
 #  end
  pid_server = fork {
  	[$stdout, $stderr].each {|fh| fh.reopen File.open("store/server.log", "a") }
  	exec "php -S localhost:10080 -t www"
  }
			File.open(pid_file, 'a') {|f| f.puts pid_server}
  # cmd = 'ping google.com'
			# Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
			#   while line = stdout.gets
			# 	puts "@@@@@"
			#     puts line
			#   end
			# end

 # pipe = IO.popen()
 # File.open(pid_file, 'a') {|f| f.puts pipe.pid}
		# }
		# Process.detach(pid_server)
		# File.open(pid_file, 'a') {|f| f.puts pid_server}
		# puts status

		 # system "php", "-S", "localhost:10080", "-t", "www", "&>", "store/server.log"
		 # fork {
# IO.popen "php -S localhost:#{port} -t www &> store/server.log"  do |pipe|
		# puts "parent pid: #{$$}, popen return (child) pid: #{pipe.pid}"
# end
		 # }
		 # File.open(pid_file, 'a') {|f| f.puts $?.pid}
		 # `php -S localhost:#{port} -t www &> store/server.log; echo $$ > som.pid`
		 # puts $?.pid
		# puts pid_server
		# pid_server = fork {
		# 	IO.popen "php -S localhost:10080 -t www &> t.log" do |pipe|
		# 		File.open(pid_file, 'a') {|f| f.puts pipe.pid}
		# 	end
		# }
		# Process.detach(pid_server)		

		_log_info "Hi! Checkout localhost:#{port}"
		return true
end

#imageObject



181
182
183
184
185
186
# File 'lib/covamain.rb', line 181

def image ()
	png = ChunkyPNG::Image.new(320, 480, ChunkyPNG::Color::TRANSPARENT)
		png[1,1] = ChunkyPNG::Color.rgba(10, 20, 30, 128)
		png[2,1] = ChunkyPNG::Color('black @ 0.5')
		png.save('Default.png', :interlace => true)
end

#installObject



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
# File 'lib/covamain.rb', line 38

def install
  _log_block_start "Installing Cova"

  if _io_isdir(@dir_cova_home, false) or _io_isdir(@dir_cova_ios_mods_home, false)
	  _log_info "It looks like Cova is already installed. Please uninstall it first before installing again."	  	 
	  _log_block_error
	  return false
  end

  if _io_mkdir @dir_cova_home and _io_mkdir @dir_cova_apps_home and _io_mkdir @dir_cocoapods_home
  	  if _io_clone_repo @repo_xctool, @dir_cova_home and _io_clone_repo @repo_cova, @dir_cova_home
  	  	  if _io_pod_add_repo "cova", @repo_cova_ios_mods
		  	  _log_block_success
		  	  puts
 				  puts "Congrats! Cove is now installed. Enjoy and build the next great app sensation!".green
 				  puts
 			  	  puts "For more info check out http://cova.io"
 			 	  puts
	  	  	  return true
	  	  end
	  end
  end

  _log_block_error
  return false
end

#listObject



66
67
68
69
70
71
72
73
74
# File 'lib/covamain.rb', line 66

def list
		if not _cova_installed
			_cova_prompt_install
			return false
		end

		_io_ls @dir_cova_apps_home
		return true
end

#log(name) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/covamain.rb', line 285

def log (name)
	if not _cova_installed
			_cova_prompt_install
			return false
		end

	if not _io_app_exists name
		_log_line "App #{name} does not exist"
		_log_line_error
		return false
	end

		db_file = @dir_cova_apps_home + "/" + name + "/store/log.db"
		if File.exist?(db_file)
db = SQLite3::Database.new db_file
			db.execute "select * from changelog" do |row|
puts "[" + row[2] + "] " + row[1] + " " + row[0]
			end
			return true
		end

		_log_info "Looks like there's no activity yet. So do something first."
		return false
end

#newapp(name) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/covamain.rb', line 94

def newapp (name)

	#	`cp -r Cova #{name}`
	#	app_root = "#{name}"
#	Dir.chdir app_root

#	_io_exec "pod repo update cova"
	#	_io_exec "pod install"
	#	_io_exec "open Cova.xcworkspace"

#		`mv app.xcodeproj #{name}.xcodeproj`	
 #   	project  = Xcodeproj::Project.new "#{name}.xcodeproj"
#		target = project.targets[0]
 #   	target.name = name
#		project.targets[1].name = "#{name}Tests"

#		debug_build_settings = target.build_configuration_list.build_configurations[1].build_settings

	# `cp ../cova/cova/cova-Info.plist #{name}-Info.plist`
	# `cp ../cova/cova/AppDelegate.h AppDelegate.h`
	# `cp ../cova/cova/AppDelegate.m AppDelegate.m`
	# `cp ../cova/cova/cova-Prefix.pch #{name}-Prefix.pch`
	# `cp ../cova/cova/main.m main.m`
		#info_plist_file = project.new_file("app-Info.plist")
		#pch_file = project.new_file("#{name}-Prefix.pch")
		# main_file = project.new_file("main.m")
		# delegate_h_file = project.new_file("AppDelegate.h")
		# delegate_m_file = project.new_file("AppDelegate.m")
		# target.add_file_references([main_file, delegate_h_file, delegate_m_file])
  #       release_build_settings = target.build_configuration_list.build_configurations[0].build_settings
  #       release_build_settings['INFOPLIST_FILE'] = "#{name}-Info.plist"
  #       release_build_settings['GCC_PREFIX_HEADER'] = "#{name}-Prefix.pch"
		# debug_build_settings = target.build_configuration_list.build_configurations[1].build_settings
  #       debug_build_settings['INFOPLIST_FILE'] = "#{name}-Info.plist"
  #       debug_build_settings['GCC_PREFIX_HEADER'] = "#{name}-Prefix.pch"
		# project.add_system_framework "CoreData", target
  #   	project.add_system_framework "UIKit", target
  #   	project.add_system_framework "Foundation", target

#		project.save_as("#{name}.xcodeproj")
#		_io_exec "open #{name}.xcodeproj"

    #debug_build_settings['PRODUCT_NAME'] = name
    #puts 
    #puts debug_build_settings['PRODUCT_NAME'] 

	#puts project.pretty_print

	#info_plist = project.main_group.new_file("#{name}-Info.plist")
		#target.add_file_references([info_plist])

	#debug = project.build_configurations
	#pp debug

	#config   = project.configuration_list :ios
		
		#project.add_system_framework "CoreData", target
	#project.add_system_framework "UIKit", target
	#project.add_system_framework "Foundation", target

	#src = project.new_group "src"
	#res = project.new_group "res"
	#test = project.new_group "test"

		#File.open("src/main.m", 'w') {|f| f.write("#import <UIKit/UIKit.h>\r\n#import \"AppDelegate.h\"\r\nint main(int argc, char *argv[]) {\r\n@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }\r\n}")}
		#File.open("src/AppDelegate.h", 'w') {|f| f.write("#import <UIKit/UIKit.h>\r\n@interface AppDelegate : UIResponder <UIApplicationDelegate>\r\n @property (strong, nonatomic) UIWindow *window; \r\n@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; \r\n@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; \r\n@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; \r\n- (void)saveContext; \r\n- (NSURL *)applicationDocumentsDirectory; \r\n@end")}
		#main_file = src.new_file("src/main.m")
		#delegate_file = src.new_file("src/AppDelegate.h")
		
		#project.save_as("#{name}.xcodeproj")

		#File.open("Podfile", 'w') {|f| f.write("platform :ios, '5.0'\r\npod 'covamod-core'")}
		#_io_exec "pod setup"
		#_io_exec "pod repo update cova"
		#_io_exec "pod install"
		#_io_exec "open #{name}.xcworkspace"
		
		#_io_exec "open #{name}.xcodeproj"
end

#setupObject



175
176
177
178
# File 'lib/covamain.rb', line 175

def setup ()
	_io_exec "curl -fsSL https://raw.github.com/mxcl/homebrew/go"
		_io_exec "brew update"
end

#statusObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/covamain.rb', line 13

def status
   _log_block_start "Checking if Cova is installed"
   if _io_isdir(@dir_cova_home) and _io_isdir(@dir_cova_ios_mods_home)
   		_log_block_success
   		return true
   end
   _log_block_error
   _cova_prompt_install
   return false
end

#uninstallObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/covamain.rb', line 25

def uninstall
   _log_block_start "Uninstalling Cova"
   _io_rmdir(@dir_cova_home)
   _io_rmdir(@dir_cova_ios_mods_home)
   if _io_isdir(@dir_cova_home, false) or _io_isdir(@dir_cova_ios_mods_home, false)
   		_log_block_error
   		return false
   end
   _log_block_success
   return true
end