Class: AndParcel::LintRequest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ LintRequest

Returns a new instance of LintRequest.



510
511
512
# File 'lib/andparcel/parcel.rb', line 510

def initialize(opts)
	@opts=opts
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



508
509
510
# File 'lib/andparcel/parcel.rb', line 508

def opts
  @opts
end

#outObject (readonly)

Returns the value of attribute out.



508
509
510
# File 'lib/andparcel/parcel.rb', line 508

def out
  @out
end

Instance Method Details

#do_simple_resource(fr) ⇒ Object

Confirm that a simple resource’s name has the prefix, and repair it if requested. A simple resource is a resource whose name is the filename. Repairing it simply means renaming the file to one that has the desired prefix.



555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/andparcel/parcel.rb', line 555

def do_simple_resource(fr)
	if !(@prefix_regexp.match(File.basename(fr.local)))
		out.puts "#{fr.local} does not have proper prefix: #{@prefix}"
		@issues+=1
	
		if opts[:repair]
			rename=File.dirname(fr.local)+'/'+@prefix+File.basename(fr.local)
			FileUtils.mv(fr.local, rename)		
			out.puts "...fixed!"
		end
	end
end

#do_xml_resource(fr, doc) ⇒ Object

Confirm that an “XML resource” has the prefix for all of its names. In this case, an “XML resource” refers to any XML file containing multiple actual resources, (e.g., res/values/strings.xml), as indicated by a <resources> root element. The rule is that each named child must have the prefix at the start of the name. Repairing this, if requested, involves modifying the XML file to adjust the name attributes.



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/andparcel/parcel.rb', line 576

def do_xml_resource(fr, doc)
	dirty=false
	
	doc.xpath('//@name').each do |node|
		if (!$WHITELIST.include?(node.parent.name)) &&
				!(@prefix_regexp.match(node.value))
			out.puts "#{node.name}=\"#{node.value}\" in #{fr.local} does not have proper prefix: #{@prefix}"
			@issues+=1
			
			if opts[:repair]
				node.value=@prefix+node.value
				dirty=true
			end
		end
	end
	
	if dirty
		open(fr.local, "w") {|f| f.puts doc.to_s }
	end
end

#lint(pkg_info) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/andparcel/parcel.rb', line 514

def lint(pkg_info)
	@out=opts[:out] || $stderr
	path=File.join(opts[:project], pkg_info)
	@config=JSON.parse(open(path) {|f| f.read})
	@prefix=(@config['name'].gsub('.', '_').gsub('-','_'))+'_'
	@prefix_regexp=Regexp.compile(@prefix+'.*')
	@issues=0
	
	resources=AndParcel.glob(opts[:res], opts[:project])
	
	resources.each do |fr|
		if fr.local.split('').last!='~'	
			if File.extname(fr.local)=='.xml'
				doc=Nokogiri::XML(open(fr.local))
				
				if (doc.root.name=='resources')
					do_xml_resource(fr, doc)
				end

				do_simple_resource(fr)
			end
		end
	end
	
	AndParcel.glob(opts[:assets], opts[:project]).each do |fr|
		if fr.local.split('').last!='~' && File.exists?(fr.local)
			do_simple_resource(fr)
		end
	end
	
	if @issues>0
		out.puts "#{@issues} issues reported #{'and fixed' if opts[:repair]}"
	end
	
	return(@issues==0)
end