Skip to content

Instantly share code, notes, and snippets.

@kattrali
Created July 19, 2012 20:48
Show Gist options
  • Save kattrali/3146714 to your computer and use it in GitHub Desktop.
Save kattrali/3146714 to your computer and use it in GitHub Desktop.
This code parses directories into Java package structure. Its even recursive!
# from https://github.com/kattrali/redcar-code-package-view/blob/master/lib/code_package_view.rb#L43
def self.package_directory(adapter,base_path,path,nodes=[],path_name=File.basename(path))
has_excluded_dirs = false
has_files = false
Dir["#{path.to_s}/*/"].map do |a|
unless File.basename(a) =~ /^\./ # exclude hidden dirs from being packaged
excluded = false
Preferences.excluded_patterns.each do |ex|
if a =~ /#{ex}/ and not excluded
excluded = true
has_excluded_dirs = true
nodes << TreeMirror::Node.new(adapter,a,:dir,false) if path == base_path
end
end
if path == base_path
a_path_name = File.basename(a)
else
a_path_name = path_name+"."+File.basename(a)
end
unless excluded
CodePackageView.package_directory(adapter,base_path,a,nodes,a_path_name)
end
end
end
Dir.glob(File.join(path, "*")).each do |file_path|
if File.file?(file_path)
has_files = true
if path == base_path
nodes << TreeMirror::Node.new(adapter,file_path,:file,false)
end
end
end
nodes << TreeMirror::PackageNode.new(adapter,path_name,path) if (has_files or has_excluded_dirs) and not path == base_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment