Created
February 17, 2012 08:01
-
-
Save devboy/1851710 to your computer and use it in GitHub Desktop.
haXe organize imports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HXML = ENV["hxml"] | |
module TextFileUtil | |
attr_accessor :file | |
def file_lines | |
File.open(@file).readlines | |
end | |
def file_dir | |
File.dirname @file | |
end | |
end | |
class Hxml | |
include TextFileUtil | |
def class_paths | |
@class_paths ||= file_lines.keep_if{|s| s.start_with? "-cp "}.map{|s| File.join( file_dir, s.gsub("-cp ","").chomp.strip ) } | |
end | |
def lib_paths | |
@libs ||= begin | |
libs = [] | |
file_lines.keep_if{|s| s.start_with? "-lib "}.each{|s| libs += get_haxelib_paths( s.gsub("-lib ","") )} | |
libs | |
end | |
end | |
def get_haxelib_paths(lib) | |
`haxelib path #{lib}`.lines.to_a.delete_if{|l| l.start_with? "-D" }.map{|s| s.chomp.strip } | |
end | |
end | |
class HxFile | |
include TextFileUtil | |
def self.for_file file | |
hxfile = HxFile.new | |
hxfile.file = file | |
hxfile | |
end | |
def package | |
@package ||= begin | |
if has_package? | |
file_lines.keep_if{|l| !l.index(/package (\s*[.a-zA-Z0-9_]*\s*);/).nil? }.first.scan(/package (\s*[.a-zA-Z0-9_]*\s*);/).first.first.chomp.strip.gsub(";","") | |
else | |
"" | |
end | |
end | |
end | |
def has_package? | |
!file_lines.keep_if{|l| !l.index(/package (\s*[.a-zA-Z0-9_]*\s*);/).nil? }.empty? | |
end | |
def imports | |
@imports ||= scan_lines /import (\s*[.a-zA-Z0-9_]*\s*);/ | |
end | |
def classes | |
@classes ||= scan_lines /class (\s*\S*\s*)/ | |
end | |
def interfaces | |
@interfaces ||= scan_lines /interface (\s*\S*\s*)/ | |
end | |
def enums | |
@enums ||= scan_lines /enum (\s*\S*\s*)/ | |
end | |
def typedefs | |
@typedefs ||= scan_lines /typedef (\s*\S*\s*)/ | |
end | |
def scan_lines expression | |
file_lines.keep_if{|l| !l.index(expression).nil? }.map{|s| s.scan(expression) }.flatten.map{|s| s.chomp.strip.gsub(";","")} | |
end | |
end | |
task :default do | |
puts "Organizing imports of project: #{HXML}" | |
hxml = Hxml.new | |
hxml.file = HXML | |
files = (hxml.class_paths + hxml.lib_paths).map{|p| Dir.glob( File.join( p, "**/*.hx" ) ) }.flatten.uniq.map{|f| HxFile.for_file f } | |
files.each{|f| | |
unused_imports = f.imports.delete_if{ |i| i.empty? }.delete_if{ |i| | |
identity = i.split(".").last | |
f.file_lines.reject{|l| !l.include?(identity) || l.include?("import ") }.count > 0 | |
} | |
unused_imports.delete_if{ |i| | |
if i.split(".").count < 2 | |
true | |
else | |
filename = i.split(".").last; | |
package = (i.split(".") - [filename]).join(".") | |
files.reject{|fi| fi.package != package || File.basename(fi.file, ".hx") != filename }.reject{|fi| !fi.classes.include?(filename) && !fi.interfaces.include?(filename) && !fi.enums.include?(filename) && !fi.typedefs.include?(filename) }.count > 0 | |
end | |
} | |
if( !hxml.class_paths.reject{|c| f.file.include? c}.empty? && !unused_imports.empty? ) | |
puts f.file, "Removing imports:", unused_imports | |
text = File.read(f.file) | |
unused_imports.each{|i| text.gsub!(/import\s*#{i}\s*;/,"")} | |
File.open(f.file, 'w') {|f| f.write(text) } | |
end | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment