#!/usr/bin/env ruby # -*- coding: utf-8 -*- # Name:: Automatic::Ruby # Author:: 774 # Created:: Feb 18, 2012 # Updated:: Feb 21, 2014 # Copyright:: Copyright (c) 2012-2014 Automatic Ruby Developers. # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. root_dir = File.expand_path("..", File.dirname(__FILE__)) $:.unshift root_dir $:.unshift root_dir + '/lib' require 'automatic' require 'rubygems' require 'optparse' require 'fileutils' require 'feedbag' require 'pp' recipe_path = "" def abort_with_usage(subcommand, message) top_filename = File.basename($0) abort("Usage: #{top_filename} #{subcommand} #{message}") end subparsers = { 'scaffold' => lambda {|argv| Dir::entries(root_dir + '/plugins').sort.each {|path| dir = (File.expand_path('~/.automatic/plugins/' + path)) unless File.exist?(dir) FileUtils.mkdir_p(dir) puts "Creating #{dir}" end } dir = (File.expand_path('~/.automatic/assets')) unless File.exist?(dir) FileUtils.mkdir_p(dir) FileUtils.cp_r(root_dir + '/assets/siteinfo', dir + '/siteinfo') puts "Creating #{dir}" end dir = (File.expand_path('~/.automatic/config')) unless File.exist?(dir) FileUtils.mkdir_p(dir) FileUtils.cp_r(root_dir + '/config', dir + '/example') puts "Creating #{dir}" end dir = (File.expand_path('~/.automatic/db')) unless File.exist?(dir) FileUtils.mkdir_p(dir) puts "Creating #{dir}" end }, 'unscaffold' => lambda {|argv| dir = (File.expand_path('~/.automatic')) if File.directory?(dir) puts "Removing #{dir}" FileUtils.rm_r(dir) end }, 'autodiscovery' => lambda {|argv| url = argv.shift || abort_with_usage("autodiscovery", "") pp Feedbag.find(url) }, 'feedparser' => lambda {|argv| require 'automatic/feed_parser' url = argv.shift || abort_with_usage("feedparser", "") rss_results = Automatic::FeedParser.get_url(url) pp rss_results }, 'inspect' => lambda {|argv| require 'automatic/feed_parser' url = argv.shift || abort_with_usage("inspect", "") feeds = Feedbag.find(url) pp feeds rss_results = Automatic::FeedParser.get_url(feeds.pop) pp rss_results }, 'opmlparser' => lambda {|argv| require 'automatic/opml' path = argv.shift if path.nil? abort_with_usage("opmlparser", "") end parser = Automatic::OPML::Parser.new(File.read(path)) parser.each_outline {|opml, o| puts "#{o.xmlUrl}" } }, 'log' => lambda {|argv| require 'automatic/log' level = argv.shift || abort_with_usage("log", " ") message = ARGV.shift Automatic::Log.puts(level, message) } } parser = OptionParser.new {|parser| parser.version = Automatic.const_get(:VERSION) parser.banner = "Automatic #{parser.version} Usage: automatic [options] [subcommand]" parser.separator "SubCommands:" subparsers.keys.each {|key| parser.separator " " + key } #{subparsers.keys.join(", ")}" parser.separator "Options:" parser.on('-c', '--config FILE', String, "recipe YAML file"){|c| recipe_path = c} parser.on('-h', '--help', "show this message") { puts parser exit 1 } } begin Automatic::Log.puts("info", "Loading #{recipe_path}") unless recipe_path.empty? parser.parse! rescue OptionParser::ParseError => err $stderr.puts err.message $stderr.puts parser.help exit 2 end unless recipe_path.to_s.empty? recipe = Automatic::Recipe.new(recipe_path) Automatic.run(:recipe => recipe, :root_dir => root_dir) else parser.order!(ARGV) unless ARGV.empty? Hash.new {|h, k| puts "No such subcommand: #{k}" exit 1 }.update(subparsers)[ARGV.shift].call(ARGV) else puts parser exit 1 end end