Class: Swee::Engine

Inherits:
Object show all
Defined in:
lib/swee/engine.rb

Constant Summary collapse

@@__instance__ =

> 实例

nil
@@__server__ =

> 服务器

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Engine

Returns a new instance of Engine.



49
50
51
52
53
54
55
56
57
58
# File 'lib/swee/engine.rb', line 49

def initialize options
  # 初始化配置
  @config = Swee.init_config

  # 读取用户配置
  require_user_appconfig

  # 合并配置
  merge_config! options
end

Class Method Details

.boot!(argv) ⇒ Object

启动服务



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/swee/engine.rb', line 113

def boot! argv
  cmd = argv.shift || "s"
  options = parse_options.merge({:cmd => cmd})

  case cmd
  when "new"
    proj_name = argv.shift
    if proj_name.nil?
      puts "格式错误,请输入: swee new 项目名称"
      return
    end
    require_relative './installer'
    Installer.run(proj_name)
    return
  when "g"  # => Todo: generation 生成
    return
  else
    require_files
    cache_file_mtime
    @@__instance__ = self.new options
    parse_cmd cmd,options
  end
end

.cache_file_mtimeObject



181
182
183
# File 'lib/swee/engine.rb', line 181

def cache_file_mtime
  Lodder.cache_file_mtime
end

.configObject

配置实例



77
78
79
# File 'lib/swee/engine.rb', line 77

def config
  instance.send :config
end

.instanceObject

当前实例



73
74
75
# File 'lib/swee/engine.rb', line 73

def instance
  @@__instance__
end

.parse_cmd(cmd, options) ⇒ Object

解析命令 c -> 命令行 s -> 启动服务



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/swee/engine.rb', line 140

def parse_cmd cmd,options
  case cmd
  when "c"   # => 命令行模式
    require 'irb'
    IRB.start
  when "s"   # => 启动服务器
    start_server!
  else
    raise "命令不合法"
  end
end

.parse_optionsObject

命令行解析



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/swee/engine.rb', line 94

def parse_options
  options = {}
  OptionParser.new do |opts|
    opts.banner = "swee框架使用参数如下"

    opts.on("-p", "--port PORT", Integer,
            "端口 (默认: 3000)") do |port|
      options[:listen] = port
    end

    opts.on("-e", "--env ENV",
            "环境 development 或 production (默认为development)") do |e|
      options[:env] = e
    end
  end.parse!
  options
end

.require_filesObject

加载全部文件



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/swee/engine.rb', line 153

def require_files
  # require 加载器 Lodder
  require_relative './lodder'
  Lodder.all

  _app_path = ENV["app_path"]

  # _app_env = Application.config["app_env"]

  # if (begin
  #       require "active_record"
  #     rescue
  #       false
  #     end)
  #   ActiveRecord::Base.establish_connection YAML::load(File.open(ENV["app_path"]+'/database.yml'))[_app_env]
  # end

  # 加载路由
  require File.expand_path('./routes', _app_path)

  # 加载控制器
  Dir.glob("#{_app_path}/controllers/*.rb") { |f| require  f }

  # 加载模型
  Dir.glob("#{_app_path}/models/*.rb") { |f| require  ENV["app_path"] + "/models/"+File.basename(f, '.*') }

end

.restart_server!Object

重启服务器



186
187
188
189
190
191
192
# File 'lib/swee/engine.rb', line 186

def restart_server!
  @@__server__ = nil
  GC.start
  require_files
  cache_file_mtime
  start_server!
end

.serverObject

服务器实例



81
82
83
# File 'lib/swee/engine.rb', line 81

def server
  @@__server__
end

.start_server!Object

启动服务器



195
196
197
198
# File 'lib/swee/engine.rb', line 195

def start_server!
  @@__server__ = Server.new
  @@__server__.run!
end

Instance Method Details

#merge_config!(options) ⇒ Object

合并配置



67
68
69
# File 'lib/swee/engine.rb', line 67

def merge_config! options
  @config.default_config! options
end

#require_user_appconfigObject

加载 app配置



61
62
63
64
# File 'lib/swee/engine.rb', line 61

def require_user_appconfig
  # require_relative './application'
  Lodder.app_require
end