Class: AndParcel::InstallRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/andparcel/parcel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ InstallRequest

Returns a new instance of InstallRequest.



232
233
234
235
# File 'lib/andparcel/parcel.rb', line 232

def initialize(opts)
	@opts=opts
	@root=opts[:dir] || '.'
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



230
231
232
# File 'lib/andparcel/parcel.rb', line 230

def config
  @config
end

#optsObject (readonly)

Returns the value of attribute opts.



230
231
232
# File 'lib/andparcel/parcel.rb', line 230

def opts
  @opts
end

#rootObject (readonly)

Returns the value of attribute root.



230
231
232
# File 'lib/andparcel/parcel.rb', line 230

def root
  @root
end

Instance Method Details

#assets(zf) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/andparcel/parcel.rb', line 372

def assets(zf)
	dir=File.join(@root, 'assets')

	zf.entries.each do |entry|
		if (/^assets\/(.+)$/.match entry.name)
			name=$1
			dest=File.join(dir, File.dirname(name), File.basename(name))
			FileUtils.mkdir_p File.dirname(dest)
			File.unlink(dest) if File.exists?(dest)
			entry.extract dest
		end
	end
	
	true
end

#dependenciesObject

If there are parcels dependent upon this one, inject those as well.



428
429
430
431
432
# File 'lib/andparcel/parcel.rb', line 428

def dependencies
	(@config['dependencies'] || {}).each_pair do |dep, version|
		AndParcel::Parcel.install(dep, opts)
	end
end

#docs(zf) ⇒ Object

For each file in the parcel’s docs/ directory, extract it into parcel/.../docs/, where ... is the ID of the parcel itself.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/andparcel/parcel.rb', line 335

def docs(zf)
	dir=File.join(@parcel_root, 'docs')

	zf.entries.each do |entry|
		if (/^docs\/(.+)$/.match entry.name)
			dest=File.join(dir, $1)
			FileUtils.mkdir_p File.dirname(dest)
			File.unlink(dest) if File.exists?(dest)
			entry.extract dest
		end
	end
	
	true
end

#inject(dest) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/andparcel/parcel.rb', line 276

def inject(dest)
	Zip::ZipFile.open(dest) do |zf|
		begin
			dependencies if
				manifest(zf) if
					assets(zf) if
						resources(zf) if
							docs(zf) if
								libs(zf) if
									load_config(zf)
	
			FileUtils.mv dest, @parcel_root
		ensure
			File.unlink(dest) if File.exists?(dest)
		end
	end
end

#install(target) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/andparcel/parcel.rb', line 237

def install(target)
	dir=File.join(@root, 'parcels')
	
	FileUtils.mkdir_p(dir) if !File.exists?(dir)
	
	if File.exists?(target)
		dest=File.join(dir, File.basename(target))
		FileUtils.cp target, dest
		inject(dest)
	elsif /(http|https):\/\/.+/.match(target)
		install_from_url(target, dir)
	else
		candidate=Dir[target].sort.reverse.first
													 
		if candidate && File.exists?(candidate)
			dest=File.join(dir, File.basename(candidate))
			FileUtils.cp candidate, dest
			inject(dest)
		else
			url=AndParcel.catalogs.find(target)
			
			if url
				install_from_url(url, dir)
			else
				raise "Could not find parcel #{target}"
			end
		end
	end
end

#install_from_url(target, dir) ⇒ Object



267
268
269
270
271
272
273
274
# File 'lib/andparcel/parcel.rb', line 267

def install_from_url(target, dir)
	rio_target=rio(target)
	dest=File.join(dir,
								 rio_target.basename.to_s+rio_target.extname)
	FileUtils.mkdir_p(File.dirname(dest))
	rio(dest) < rio_target
	inject(dest)
end

#libs(zf) ⇒ Object

For each JAR in the parcel’s libs/ directory, copy that JAR into the project’s libs/ directory and note what we installed. We only need to track the files installed outside of the parcel-specific subdirectory in parcels/, as that whole directory can be removed on the corresponding extract command.



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/andparcel/parcel.rb', line 319

def libs(zf)
	zf.entries.each do |entry|
		if ('libs/'==entry.parent_as_string)
			dest=File.join(@root, 'libs', File.basename(entry.name))
			FileUtils.mkdir_p File.dirname(dest)
			File.unlink(dest) if File.exists?(dest)
			entry.extract dest
		end
	end
	
	true
end

#load_config(zf) ⇒ Object

Loads the configuration file for this parcel out of its ZIP archive.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/andparcel/parcel.rb', line 296

def load_config(zf)
	@config=JSON.parse(zf.read('parcel.json'))
	@parcel_root=File.join(@root, 'parcels', @config['name'])
	
	if File.exists?(@parcel_root)
		if [opts[:replace]]
			AndParcel::Parcel.remove(@config['name'], :dir=>opts[:dir])
		else
			raise "Parcel already installed: #{@config['name']}"
		end
	end
	
	FileUtils.mkdir_p @parcel_root

	true
end

#manifest(zf) ⇒ Object

If the parcel has a ManifestMerge.xml file, back up and then open the project’s AndroidManifest.xml file, and merge in anything with a distinct name.



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
# File 'lib/andparcel/parcel.rb', line 391

def manifest(zf)
	if zf.find_entry('ManifestMerge.xml')
		dest=File.join(@root, 'AndroidManifest.xml')
	
		if File.exists? dest
			backup=dest+'~'
			File.unlink backup if File.exists?(backup)
			FileUtils.cp dest, backup
			
			doc=Nokogiri::XML(open(dest))
			app=doc.search('application')[0]
			merge=Nokogiri::XML(zf.read('ManifestMerge.xml'))
			
			merge.root.children.each do |node|
				if node.element?
					if $COMPONENTS.include?(node.name)
						existing=app.xpath("*[@android:name='#{node['name']}']",
															 'android'=>"http://schemas.android.com/apk/res/android")
						
						if existing.size==0
							app.add_child(node.to_s)
						end
					else
						# no support for other elements right now
					end
				end
			end
			
			open(dest, "w") {|f| f.puts doc.to_s }
		end
	end
	
	true
end

#resources(zf) ⇒ Object

For each resource in the parcel’s res/ directory, copy that resource into the project’s res/ directory and note what we installed. We only need to track the files installed outside of the parcel-specific subdirectory in parcels/, as that whole directory can be removed on the corresponding extract command.



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/andparcel/parcel.rb', line 356

def resources(zf)
	dir=File.join(@root, 'res')

	zf.entries.each do |entry|
		if (/^res\/(.+)$/.match entry.name)
			name=$1
			dest=File.join(dir, File.dirname(name), File.basename(name))
			FileUtils.mkdir_p File.dirname(dest)
			File.unlink(dest) if File.exists?(dest)
			entry.extract dest
		end
	end
	
	true
end