class MemoRack::CLI
Public Class Methods
define_options(command, *banner, &block)
click to toggle source
オプション解析を定義する
# File lib/memorack/cli.rb, line 113 def self.define_options(command, *banner, &block) define_method "options_#{command}" do |argv| options = {} OptionParser.new { |opts| begin opts.banner = banner(opts, command, *banner) instance_exec(opts, argv, options, &block) rescue => e abort e.to_s end } options end end
new()
click to toggle source
# File lib/memorack/cli.rb, line 15 def initialize i18n_init end
run(argv = ARGV, options = {})
click to toggle source
# File lib/memorack/cli.rb, line 11 def self.run(argv = ARGV, options = {}) CLI.new.run(argv, options) end
Public Instance Methods
action(command, argv, options = {})
click to toggle source
サブコマンドの実行
# File lib/memorack/cli.rb, line 96 def action(command, argv, options = {}) command = command.gsub(/-/, '_') # オプション解析 options_method = "options_#{command}" options.merge!(send(options_method, argv)) if respond_to?(options_method) send("memorack_#{command}", options, *argv) end
dir_earch(dir, match = '**/*', flag = File::FNM_DOTMATCH) { |file| ... }
click to toggle source
ディレクトリを繰返す
# File lib/memorack/cli.rb, line 66 def dir_earch(dir, match = '**/*', flag = File::FNM_DOTMATCH) Dir.chdir(dir) { |d| Dir.glob(match, flag).sort.each { |file| next if File.basename(file) =~ /^[.]{1,2}$/ file = File.join(file, '') if File.directory?(file) yield(file) } } end
has_action?(command)
click to toggle source
サブコマンドが定義されているか?
# File lib/memorack/cli.rb, line 91 def has_action?(command) respond_to? "memorack_#{command}" end
i18n_init()
click to toggle source
I18n を初期化する
# File lib/memorack/cli.rb, line 50 def i18n_init I18n.load_path = Dir[File.expand_path('../locales/*.yml', __FILE__)] I18n.backend.load_translations I18n.enforce_available_locales = false locale = ENV['LANG'][0, 2].to_sym if ENV['LANG'] I18n.locale = locale if I18n.available_locales.include?(locale) end
memorack_build(options, *argv)
click to toggle source
静的サイトのビルド
# File lib/memorack/cli.rb, line 302 def memorack_build(options, *argv) path = argv.shift path = 'content/' unless path abort "Directory not exists '#{path}'" unless File.exists?(path) require 'memorack/builder' require 'tmpdir' Dir.mktmpdir do |tmpdir| site = {} site[:url] = File.join(options[:url], '').gsub(/\/$/, '') if options[:url] builder = MemoRack::Builder.new(theme: options[:theme], root: path, tmpdir: tmpdir, site: site) builder.generate(options) end puts "Build '#{path}' -> '#{options[:output]}'" end
memorack_create(options, *argv)
click to toggle source
テンプレートの作成
# File lib/memorack/cli.rb, line 218 def memorack_create(options, *argv) path = argv.shift abort "File exists '#{path}'" if File.exists?(path) FileUtils.copy_entry(File.expand_path('../template', __FILE__), path) puts "Created '#{path}'" end
memorack_server(options, *argv)
click to toggle source
サーバーの実行
# File lib/memorack/cli.rb, line 277 def memorack_server(options, *argv) path ||= argv.shift path ||= 'content' abort "Directory not exists '#{path}'" unless File.exists?(path) abort "Not directory '#{path}'" unless File.directory?(path) server_options = options[:server] # サーバーの起動 require 'rack/builder' require 'rack/handler/webrick' app = Rack::Builder.new { require 'memorack' require 'tmpdir' Dir.mktmpdir do |tmpdir| run MemoRack::MemoApp.new(nil, theme: options[:theme], root: path, tmpdir: tmpdir) end } server_options[:app] = app Rack::Server.new(server_options).start end
memorack_theme(options, *argv)
click to toggle source
テーマ関連の操作
# File lib/memorack/cli.rb, line 227 def memorack_theme(options, *argv) theme_or_file = argv.shift themes = File.expand_path("../themes", __FILE__) dir = options[:dir] if theme_or_file theme = theme_or_file.gsub(%r(/.*), '') if options[:copy] # テーマをコピー theme_dir = File.join(themes, theme) abort "Theme not exists '#{theme}'" unless File.directory?(theme_dir) from = File.join(themes, theme_or_file) abort "File not exists '#{theme_or_file}'" unless File.exists?(from) path = name = File.basename(from) path = File.join(dir, name) if File.directory?(dir) && File.directory?(from) FileUtils.copy_entry(from, path) puts "Created '#{path}'" else # テーマの情報を表示 app = MemoRack::MemoApp.new(nil, theme: theme, root: dir) theme_dir = app.themes.first abort "Theme not exists '#{theme}'" unless theme_dir # 継承関係の表示 theme_chain = app.themes.collect { |path| name = File.basename(path) File.dirname(path) == themes ? "[#{name}]" : name } puts theme_chain.join(' --> ') # ファイル一覧の表示 dir_earch(theme_dir) { |file| puts " #{file}" } end else # テーマ一覧を表示 show_themes('MemoRack', themes) show_themes('User', dir) end end
run(argv = ARGV, options = {})
click to toggle source
# File lib/memorack/cli.rb, line 19 def run(argv = ARGV, options = {}) subcmd = nil parser = OptionParser.new do |opts| begin opts.version = LONG_VERSION || VERSION opts.banner = <<-BANNER.gsub(/^\t+/,'') Usage: #{opts.program_name} create [options] PATH #{opts.program_name} theme [options] [THEME] #{opts.program_name} server [options] [PATH] #{opts.program_name} build [options] [PATH] BANNER opts.separator "" opts.on("-h", "--help", t(:help)) { abort opts.help } opts.order!(argv) subcmd = argv.shift abort opts.help unless subcmd abort opts.help unless has_action?(subcmd) rescue => e abort e.to_s end end action(subcmd, argv, options) end
show_themes(domain, themes)
click to toggle source
テーマ一覧を表示する
# File lib/memorack/cli.rb, line 77 def show_themes(domain, themes) return unless File.directory?(themes) puts "#{domain}:" Dir.open(themes) { |dir| dir.sort.each { |file| next if /^\./ =~ file puts " #{file}" } } end
t(code, locals = {}, options = {})
click to toggle source
I18n で翻訳する
# File lib/memorack/cli.rb, line 60 def t(code, locals = {}, options = {}) options[:scope] ||= [:usage] sprintf(I18n.t(code, options), locals) end