Class: Commands::Discover

Inherits:
Object show all
Defined in:
lib/commands/plugin.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_command) ⇒ Discover

Returns a new instance of Discover.



607
608
609
610
611
# File 'lib/commands/plugin.rb', line 607

def initialize(base_command)
  @base_command = base_command
  @list = false
  @prompt = true
end

Instance Method Details

#extract_repository_uri(uri) ⇒ Object



670
671
672
# File 'lib/commands/plugin.rb', line 670

def extract_repository_uri(uri)
  uri.match(/(svn|https?):.*\/plugins\//i)[0]
end

#optionsObject



613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/commands/plugin.rb', line 613

def options
  OptionParser.new do |o|
    o.set_summary_indent('  ')
    o.banner =    "Usage: #{@base_command.script_name} discover URI [URI [URI]...]"
    o.define_head "Discover repositories referenced on a page."
    o.separator   ""        
    o.separator   "Options:"
    o.separator   ""
    o.on(         "-l", "--list", 
                  "List but don't prompt or add discovered repositories.") { |@list| @prompt = !@list }
    o.on(         "-n", "--no-prompt", 
                  "Add all new repositories without prompting.") { |v| @prompt = !v }
  end
end

#parse!(args) ⇒ Object



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/commands/plugin.rb', line 628

def parse!(args)
  options.parse!(args)
  args = ['http://wiki.rubyonrails.org/rails/pages/Plugins'] if args.empty?
  args.each do |uri|
    scrape(uri) do |repo_uri|
      catch(:next_uri) do
        if @prompt
          begin
            $stdout.print "Add #{repo_uri}? [Y/n] "
            throw :next_uri if $stdin.gets !~ /^y?$/i
          rescue Interrupt
            $stdout.puts
            exit 1
          end
        elsif @list
          puts repo_uri
          throw :next_uri
        end
        Repositories.instance.add(repo_uri)
        puts "discovered: #{repo_uri}" if $verbose or !@prompt
      end
    end
  end
  Repositories.instance.save
end

#scrape(uri) ⇒ Object



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/commands/plugin.rb', line 654

def scrape(uri)
  require 'open-uri'
  puts "Scraping #{uri}" if $verbose
  dupes = []
  content = open(uri).each do |line|
    if line =~ /<a[^>]*href=['"]([^'"]*)['"]/ or line =~ /(svn:\/\/[^<|\n]*)/
      uri = $1
      if uri =~ /\/plugins\// and uri !~ /\/browser\//
        uri = extract_repository_uri(uri)
        yield uri unless dupes.include?(uri) or Repositories.instance.exist?(uri)
        dupes << uri
      end
    end
  end
end