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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 98

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

.create_launch_url(options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 73

def self.create_launch_url(options)
  if (options[:LaunchBrowser])
    host = options[:Host] || options[:BindAddress] # options[:BindAddress] for WEBrick

    base = "http://#{host}:#{options[:Port]}"

    if options[:url]
      File.join(base, options[:url])
    else
      "http://#{host}:#{options[:Port]}"
    end
  else
    nil
  end
end

.execute(stdout, argv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 10

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 44

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 = Rack::Server.new(options)

    # 起動URL生成
    launch_url = create_launch_url(options)

    # URL設定
    ENV['MILKODE_RELATIVE_URL'] = File.join('/', options[:url]) if options[:url]

    # 起動
    rack_server.start do
      # この時点でoptions[:Host]やoptions[:Port]などの値が壊れてしまっているため事前にURLを生成している
      Launchy.open(launch_url) if launch_url
    end
  else
    create_customize_file(dbdir)
  end
end

.select_dbdirObject



89
90
91
92
93
94
95
96
# File 'lib/milkode/cdweb/cli_cdweb.rb', line 89

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