# -*- coding: utf-8 -*- require 'thor' require 'thor/actions' require 'uuidtools' #require 'typhoeus' require 'rest_client' require 'pp' # require 'redcarpet' module Hse class CLI < ::Thor include Thor::Actions attr_accessor :appdir, :config_file, :debug method_option "appname", :type => :string, :banner => "set the name of the application for templating. defaults to the basename of the appdir" class_option "appdir", :type => :string, :banner => "set the application directory to work with. assumes the current directory" # class_option "config", # :aliases => '-c', # :type => :string, # :banner => "use a specific soca config.js" # class_option "debug", # :type => :boolean, # :default => false, # :aliases => '-d', # :banner => "set log level to debug" # class_option "version", # :type => :boolean, # :aliases => '-v', # :banner => "print version and exit" default_task :help # прочитать файл блога # создать документ: _id, timestamp -> красивый date, # залить # прочитать из базы -> получить _rev # обновить # а для этого вначале прочитать .couchrc # и создать .couchrc # desc 'build_old [FILE]', 'read FILE and build json doc with uniq _uuid' # def build_(fname = 'default') # document = {} # puts "APDIR.self #{self.appdir}" # puts "APDIR #{self.appdir}" # path = File.expand_path(File.join(appdir, fname)) # puts "path #{path}" # uuid = UUIDTools::UUID.sha1_create UUIDTools::UUID_DNS_NAMESPACE, "#{fname}" # puts "_ID = #{path} - #{uuid}" # lines = File.readlines path # lines.reject!{|c|c.empty? || c == "\n"} # document[:title] = lines.shift.strip # document[:tags] = lines.shift.strip.split(/[[:punct:]]| /).reject!{|c|c.empty?} # markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true) # body = markdown.render lines.join("\n") # document[:body] = body # say "----------", :green # pp document # document # end def initialize(*) super self.appdir = options[:appdir] || File.expand_path(Dir.pwd) # self.config_file = options[:config] # self.debug = options[:debug] self.source_paths << File.expand_path(File.join(File.dirname(__FILE__), 'templates')) end # desc 'get [URL]', 'get URL' # def get(url, env = 'default') # remote(env).get url # end # desc 'getid [_ID]', 'get doc _ID from couchdb' # def getid(_id = 'default') # uuid = UUIDTools::UUID.sha1_create UUIDTools::UUID_DNS_NAMESPACE, "#{_id}" # puts "_ID = #{_id} - #{uuid}" # end desc 'get [ID]', 'get doc by id' def get(id = false, env = 'default') remote(env).get_id id end desc 'user [EMAIL] push=false', 'get/push user by email' def user(email = false, push = false, env = 'default') remote(env).user email, push end desc 'build [FILE]', 'parse FILE' def build(fname = 'default') doc = parse.build fname pp doc end desc 'check [FILE]', 'check the correctness of module in FILE' def check(fname = 'default') doc = parse.build fname if doc[:title].nil? say "error: title should be filled", :red elsif doc[:en].nil? say "error: English title should be filled", :red elsif doc[:author].nil? say "error: author should be filled", :red elsif doc[:tags].nil? || doc[:tags].empty? say "error: tags should be filled", :red else say "module ok", :green end end desc 'taglist [COURSE]', 'print full list of tags for COURSE course' def taglist(course = 'default', env = 'default') #remote(env).get taglist, course case course when "Anti4ka" say %w(tag tag1 tag2 tag2) when "" else say "error: no course #{course}", :red end end desc 'push [FILE]', 'parse FILE, build json DOC and push DOC' def push(fname = false, env = 'default') doc = build fname puts "BUILD DOC_ID = #{doc[:_id]}" revision = remote(env).get_doc_revision(doc) doc['_rev'] = revision if revision puts "rev ====== #{revision}" post_body = JSON.generate doc puts "pushing document id #{doc[:_id]}" remote(env).put(doc[:_id], post_body) end desc 'generate [APPDIR]', 'generates .couchapprc' def generate(to = nil) self.appdir = to if to self.destination_root = appdir directory('modules') directory('posts') directory('panels') directory('events') template('couchapprc.erb', '.couchapprc') end private def appname @appname = options[:name] || File.basename(appdir) end def remote(env) Hse::Remote.new appdir, env #, config_file rescue => e say e.message, :red exit end def parse Hse::Parse.new appdir #, config_file rescue => e say e.message, :red exit end end end