Class: MemoRack::CLI
- Inherits:
-
Object
- Object
- MemoRack::CLI
- Defined in:
- lib/memorack/cli.rb
Class Method Summary collapse
-
.define_options(command, *banner, &block) ⇒ Object
オプション解析を定義する.
- .run(argv = ARGV, options = {}) ⇒ Object
Instance Method Summary collapse
-
#action(command, argv, options = {}) ⇒ Object
サブコマンドの実行.
-
#banner(opts, method, *args) ⇒ Object
サブコマンド・オプションのバナー作成.
-
#dir_earch(dir, match = '**/*', flag = File::FNM_DOTMATCH) ⇒ Object
ディレクトリを繰返す.
-
#has_action?(command) ⇒ Boolean
サブコマンドが定義されているか?.
-
#i18n_init ⇒ Object
I18n を初期化する.
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
-
#memorack_build(options, *argv) ⇒ Object
静的サイトのビルド.
-
#memorack_create(options, *argv) ⇒ Object
テンプレートの作成.
-
#memorack_server(options, *argv) ⇒ Object
サーバーの実行.
-
#memorack_theme(options, *argv) ⇒ Object
テーマ関連の操作.
- #run(argv = ARGV, options = {}) ⇒ Object
-
#show_themes(domain, themes) ⇒ Object
テーマ一覧を表示する.
-
#t(code, locals = {}, options = {}) ⇒ Object
I18n で翻訳する.
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
16 17 18 |
# File 'lib/memorack/cli.rb', line 16 def initialize i18n_init end |
Class Method Details
.define_options(command, *banner, &block) ⇒ Object
オプション解析を定義する
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/memorack/cli.rb', line 114 def self.(command, *, &block) define_method "options_#{command}" do |argv| = {} OptionParser.new { |opts| begin opts. = (opts, command, *) instance_exec(opts, argv, , &block) rescue => e abort e.to_s end } end end |
Instance Method Details
#action(command, argv, options = {}) ⇒ Object
サブコマンドの実行
97 98 99 100 101 102 103 104 105 |
# File 'lib/memorack/cli.rb', line 97 def action(command, argv, = {}) command = command.gsub(/-/, '_') # オプション解析 = "options_#{command}" .merge!(send(, argv)) if respond_to?() send("memorack_#{command}", , *argv) end |
#banner(opts, method, *args) ⇒ Object
サブコマンド・オプションのバナー作成
108 109 110 111 |
# File 'lib/memorack/cli.rb', line 108 def (opts, method, *args) subcmd = method.to_s.gsub(/^.+_/, '') ["Usage: #{opts.program_name} #{subcmd}", *args].join(' ') end |
#dir_earch(dir, match = '**/*', flag = File::FNM_DOTMATCH) ⇒ Object
ディレクトリを繰返す
67 68 69 70 71 72 73 74 75 |
# File 'lib/memorack/cli.rb', line 67 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) ⇒ Boolean
サブコマンドが定義されているか?
92 93 94 |
# File 'lib/memorack/cli.rb', line 92 def has_action?(command) respond_to? "memorack_#{command}" end |
#i18n_init ⇒ Object
I18n を初期化する
51 52 53 54 55 56 57 58 |
# File 'lib/memorack/cli.rb', line 51 def i18n_init I18n.load_path = Dir[File.('../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) ⇒ Object
静的サイトのビルド
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/memorack/cli.rb', line 303 def memorack_build(, *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([:url], '').gsub(/\/$/, '') if [:url] builder = MemoRack::Builder.new(theme: [:theme], root: path, tmpdir: tmpdir, site: site) builder.generate() end puts "Build '#{path}' -> '#{[:output]}'" end |
#memorack_create(options, *argv) ⇒ Object
テンプレートの作成
219 220 221 222 223 224 225 |
# File 'lib/memorack/cli.rb', line 219 def memorack_create(, *argv) path = argv.shift abort "File exists '#{path}'" if File.exists?(path) FileUtils.copy_entry(File.('../template', __FILE__), path) puts "Created '#{path}'" end |
#memorack_server(options, *argv) ⇒ Object
サーバーの実行
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/memorack/cli.rb', line 278 def memorack_server(, *argv) path ||= argv.shift path ||= 'content' abort "Directory not exists '#{path}'" unless File.exists?(path) abort "Not directory '#{path}'" unless File.directory?(path) = [: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: [:theme], root: path, tmpdir: tmpdir) end } [:app] = app Rack::Server.new().start end |
#memorack_theme(options, *argv) ⇒ Object
テーマ関連の操作
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/memorack/cli.rb', line 228 def memorack_theme(, *argv) theme_or_file = argv.shift themes = File.("../themes", __FILE__) dir = [:dir] if theme_or_file theme = theme_or_file.gsub(%r(/.*), '') if [: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 = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/memorack/cli.rb', line 20 def run(argv = ARGV, = {}) subcmd = nil parser = OptionParser.new do |opts| begin opts.version = LONG_VERSION || VERSION opts. = <<-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, ) end |
#show_themes(domain, themes) ⇒ Object
テーマ一覧を表示する
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/memorack/cli.rb', line 78 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 = {}) ⇒ Object
I18n で翻訳する
61 62 63 64 |
# File 'lib/memorack/cli.rb', line 61 def t(code, locals = {}, = {}) [:scope] ||= [:usage] sprintf(I18n.t(code, ), locals) end |