Class: Commands::Discover

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

Instance Method Summary collapse

Constructor Details

#initialize(base_command) ⇒ Discover

Returns a new instance of Discover.



276
277
278
279
280
# File 'lib/commands/plugin/commands.rb', line 276

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

Instance Method Details

#extract_repository_uri(uri) ⇒ Object



343
344
345
# File 'lib/commands/plugin/commands.rb', line 343

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

#optionsObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/commands/plugin/commands.rb', line 282

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 = false }
  end
end

#parse!(args) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/commands/plugin/commands.rb', line 297

def parse!(args)
  options.parse!(args)
  args = ['http://wiki.rubyonrails.org/rails/pages/Plugins', 'http://agilewebdevelopment.com/plugins/scrape'] 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



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/commands/plugin/commands.rb', line 323

def scrape(uri)
  require 'open-uri'
  puts "Scraping #{uri}" if $verbose
  dupes = []
  content = open(uri).each do |line|
    begin
      if line =~ /<a[^>]*href=['"]([^'"]*)['"]/ || line =~ /(svn:\/\/[^<|\n]*)/
        uri = $1
        if uri =~ /^\w+:\/\// && uri =~ /\/plugins\// && uri !~ /\/browser\// && uri !~ /^http:\/\/wiki\.rubyonrails/ && uri !~ /http:\/\/instiki/
          uri = extract_repository_uri(uri)
          yield uri unless dupes.include?(uri) || Repositories.instance.exist?(uri)
          dupes << uri
        end
      end
    rescue
      puts "Problems scraping '#{uri}': #{$!.to_s}"
    end
  end
end