module SmallCage::Commands class Import def self.execute(opts) new(opts).execute end def initialize(opts) @opts = opts if @opts[:from] == 'default' @opts[:from] = 'base,standard' end @project_dir = Pathname.new(__FILE__) + '../../../../project' end def qp(str = '') print str unless @opts[:quiet] end def qps(str = '') puts str unless @opts[:quiet] end def execute @dest = Pathname.new(@opts[:to]) Dir.mkdir(@dest) unless @dest.exist? return unless @dest.directory? from = @opts[:from].split(/,/) from.each do |f| qps qps "Import: #{f}" if f =~ %r{^https?://} import_external(f) elsif f =~ %r{^\w+$} import(f) else end end end def import(from) d = @project_dir + from return unless d.directory? @entries = local_entries(d) unless @opts[:quiet] return unless confirm_entries end import_entries end def import_external @entries = external_entries unless @opts[:quiet] return unless confirm_entries end import_entries end def import_entries failed = [] @entries.each do |e| if e.overwrite? qps 'M /' + e.path elsif !e.exist? qps 'A /' + e.path elsif e.to.directory? # nothing else qps '? /' + e.path end begin e.import rescue failed << e qps 'F /' + e.path end end unless failed.empty? qps 'FAILED:' failed.each do |e| qps ' ' + e.path end end end private :import_entries def local_entries(d) result = [] Dir.chdir(d) do Dir.glob('**/*') do |f| e = ImportEntry.new e.path = f e.from = d + f e.to = @dest + f result << e end end result end private :local_entries def external_entries(uri) if uri !~ %r{/$} uri += '/' end mfuri = uri + 'Manifest.html' source = open(mfuri) { |io| io.read } result = [] files = source.scan(%r{