33# Name:: Automatic::Ruby
44# Author:: 774 <http://id774.net>
55# Created:: Feb 18, 2012
6- # Updated:: Jan 22 , 2013
7- # Copyright:: 774 Copyright (c) 2012
6+ # Updated:: Mar 19 , 2013
7+ # Copyright:: 774 Copyright (c) 2012-2013
88# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
99
1010root_dir = File . expand_path ( ".." , File . dirname ( __FILE__ ) )
1111$:. unshift root_dir
1212$:. unshift root_dir + '/lib'
13+
1314require 'automatic'
15+ require 'rubygems'
1416require 'optparse'
17+ require 'fileutils'
18+ require 'feedbag'
19+ require 'pp'
1520
1621recipe_path = ""
17- parser = OptionParser . new { |parser |
22+
23+ def abort_with_usage ( subcommand , message )
24+ top_filename = File . basename ( $0)
25+ abort ( "Usage: #{ top_filename } #{ subcommand } #{ message } " )
26+ end
27+
28+ subparsers = {
29+ 'scaffold' => lambda { |argv |
30+ Dir ::entries ( root_dir + '/plugins' ) . sort . each { |path |
31+ dir = ( File . expand_path ( '~/.automatic/plugins/' + path ) )
32+ unless File . exist? ( dir )
33+ FileUtils . mkdir_p ( dir )
34+ puts "Creating #{ dir } "
35+ end
36+ }
37+ dir = ( File . expand_path ( '~/.automatic/assets' ) )
38+ unless File . exist? ( dir )
39+ FileUtils . mkdir_p ( dir )
40+ FileUtils . cp_r ( root_dir + '/assets/siteinfo' , dir + '/siteinfo' )
41+ puts "Creating #{ dir } "
42+ end
43+ dir = ( File . expand_path ( '~/.automatic/config' ) )
44+ unless File . exist? ( dir )
45+ FileUtils . mkdir_p ( dir )
46+ FileUtils . cp_r ( root_dir + '/config' , dir + '/example' )
47+ puts "Creating #{ dir } "
48+ end
49+ dir = ( File . expand_path ( '~/.automatic/db' ) )
50+ unless File . exist? ( dir )
51+ FileUtils . mkdir_p ( dir )
52+ puts "Creating #{ dir } "
53+ end
54+ } ,
55+ 'unscaffold' => lambda { |argv |
56+ dir = ( File . expand_path ( '~/.automatic' ) )
57+ if File . directory? ( dir )
58+ puts "Removing #{ dir } "
59+ FileUtils . rm_r ( dir )
60+ end
61+ } ,
62+ 'autodiscovery' => lambda { |argv |
63+ url = argv . shift || abort_with_usage ( "autodiscovery" , "<url>" )
64+ pp Feedbag . find ( url )
65+ } ,
66+ 'feedparser' => lambda { |argv |
67+ require 'automatic/feed_parser'
68+ url = argv . shift || abort_with_usage ( "feedparser" , "<url>" )
69+ rss_results = Automatic ::FeedParser . get ( url )
70+ pp rss_results
71+ } ,
72+ 'inspect' => lambda { |argv |
73+ require 'automatic/feed_parser'
74+ url = argv . shift || abort_with_usage ( "inspect" , "<url>" )
75+ feeds = Feedbag . find ( url )
76+ pp feeds
77+ rss_results = Automatic ::FeedParser . get ( feeds . pop )
78+ pp rss_results
79+ } ,
80+ 'opmlparser' => lambda { |argv |
81+ require 'automatic/opml'
82+ path = argv . shift
83+ if path . nil?
84+ abort_with_usage ( "opmlparser" , "<opml path>" )
85+ end
86+ parser = Automatic ::OPML ::Parser . new ( File . read ( path ) )
87+ parser . each_outline { |opml , o |
88+ puts "#{ o . xmlUrl } "
89+ }
90+ } ,
91+ 'log' => lambda { |argv |
92+ require 'automatic/log'
93+ level = argv . shift || abort_with_usage ( "log" , "<level> <message>" )
94+ message = ARGV . shift
95+ Automatic ::Log . puts ( level , message )
96+ }
97+ }
98+
99+ parser = OptionParser . new { |parser |
18100 parser . banner = "Usage: automatic [options] arg"
19101 parser . version = Automatic . const_get ( :VERSION )
20- parser . separator "options:"
102+ parser . separator "Args: #{ subparsers . keys . join ( ", " ) } "
103+ parser . separator "Options:"
21104 parser . on ( '-c' , '--config FILE' , String ,
22105 "recipe YAML file" ) { |c | recipe_path = c }
23106 parser . on ( '-h' , '--help' , "show this message" ) {
107+
24108 puts parser
25109 exit
26110 }
@@ -35,15 +119,19 @@ rescue OptionParser::ParseError => err
35119 exit 1
36120end
37121
38- if recipe_path . to_s . empty?
39- if ENV [ "AUTOMATIC_RUBY_ENV" ] == "test"
40- recpie_path = File . join ( root_dir , 'config/default.yml' )
122+ unless recipe_path . to_s . empty?
123+ recipe = Automatic ::Recipe . new ( recipe_path )
124+ Automatic . run ( :recipe => recipe ,
125+ :root_dir => root_dir )
126+ else
127+ parser . order! ( ARGV )
128+ unless ARGV . empty?
129+ Hash . new { |h , k |
130+ puts "No such subcommand: #{ k } "
131+ exit 1
132+ } . update ( subparsers ) [ ARGV . shift ] . call ( ARGV )
41133 else
42134 puts parser
43135 exit 0
44136 end
45137end
46-
47- recipe = Automatic ::Recipe . new ( recipe_path )
48- Automatic . run ( :recipe => recipe ,
49- :root_dir => root_dir )
0 commit comments