Class: Milkode::CLI_Cdweb

Inherits:
Object
  • Object
show all
Defined in:
lib/milkode/cdweb/cli_cdweb.rb

Class Method Summary collapse

Class Method Details

.create_customize_file(dbdir) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 117

def self.create_customize_file(dbdir)
  fname = File.join(dbdir, "milkweb.yaml")
  
  if File.exist? fname
    puts "Already exist '#{fname}'"
  else
    puts <<EOF
Create '#{fname}'.
  Please customize yaml parameter.
EOF

    File.open(fname, "w") do |f|
      f.write <<EOF
---
:home_title  : "Milkode"
:home_icon   : "/images/MilkodeIcon135.png"

:header_title: "Milkode"
:header_icon : "/images/MilkodeIcon135.png"

:display_about_milkode: true
EOF
    end
  end
end

.execute(stdout, argv) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 58

def self.execute(stdout, argv)
  options = {
    :environment => ENV['RACK_ENV'] || "development",
    :pid         => nil,
    :Port        => 9292,
    :Host        => "0.0.0.0",
    :AccessLog   => [],
    :config      => "config.ru",
    # ----------------------------
    :server      => "thin",
    :LaunchBrowser => true,
    :DbDir => select_dbdir,
  }

  opts = OptionParser.new("#{File.basename($0)}")
  opts.on('--db DB_DIR', 'Database dir (default : current_dir)') {|v| options[:DbDir] = v }
  opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") {|host| options[:Host] = host }
  opts.on('-p', '--port PORT', 'use PORT (default: 9292)') {|v| options[:Port] = v }
  opts.on("-s", "--server SERVER", "serve using SERVER (default : thin)") {|s| options[:server] = s }
  opts.on('-n', '--no-browser', 'No launch browser.') {|v| options[:LaunchBrowser] = false }
  opts.on('--customize', 'Create customize file.') {|v| options[:customize] = true }

  # --hostが'-h'を上書きするので、'-h'を再定義してあげる
  opts.on_tail("-h", "-?", "--help", "Show this message") do
    puts opts
    exit
  end
  
  opts.parse!(argv)
  
  # 実行!!
  execute_with_options(options)
end

.execute_with_options(stdout, options) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 92

def self.execute_with_options(stdout, options)
  dbdir = File.expand_path(options[:DbDir])
  
  unless options[:customize]
    # 使用するデータベースの位置設定
    Database.setup(dbdir)

    # サーバースクリプトのある場所へ移動
    FileUtils.cd(File.dirname(__FILE__))

    # Rackサーバー起動
    Rack::Server.start(options)
  else
    create_customize_file(dbdir)
  end
end

.select_dbdirObject



109
110
111
112
113
114
115
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 109

def self.select_dbdir
  if (Dbdir.dbdir?('.') || !Dbdir.dbdir?(Dbdir.default_dir))
    '.'
  else
    Dbdir.default_dir
  end
end