Class: Pindo::Command::Web::Run

Inherits:
Pindo::Command::Web show all
Defined in:
lib/pindo/command/web/run.rb

Defined Under Namespace

Modules: UI Classes: BrotliFileHandler, WebGLPageHandler

Constant Summary

Constants inherited from Pindo::Command

DEFAULT_OPTIONS, DEFAULT_ROOT_OPTIONS

Instance Attribute Summary

Attributes inherited from Pindo::Command

#args_help_flag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pindo::Command

run

Methods included from Funlog::Mixin

#pindo_log_instance

Methods included from Pindoconfig::Mixin

#pindo_single_config

Methods included from Githelper

#add_branch, #add_tag, #add_tag_with_check, #clone_clang_repo, #clone_devclang_repo, #clone_pindo_common_config_repo, #clone_pindo_env_config_repo, #clong_buildconfig_repo, #get_repo_base_name, #getcode_to_dir, #git_addpush_repo, #git_latest_commit_id, #git_root_directory, #is_git_directory?, #local_branch_exists?, #local_tag_exists?, #prepare_gitenv, #process_need_add_files, #remote_branch_exists?, #remote_tag_exists?, #remove_branch, #remove_tag

Methods included from Executable

capture_command, #executable, execute_command, which, which!

Constructor Details

#initialize(argv) ⇒ Run

Returns a new instance of Run.



42
43
44
45
46
47
48
# File 'lib/pindo/command/web/run.rb', line 42

def initialize(argv)
    @args_port = argv.option('port', '8000').to_i  # 添加端口参数,默认8000
    @args_debug = argv.flag?('debug', false)  # 是否显示调试信息

    super
    @additional_args = argv.remainder!
end

Class Method Details

.optionsObject

命令的选项列表



33
34
35
36
37
38
39
40
# File 'lib/pindo/command/web/run.rb', line 33

def self.options
    [
        # 添加HTTP服务器端口选项
        ['--port',   '指定HTTP服务器的端口,默认为8000'],
        # 添加调试选项
        ['--debug',  '显示详细的调试信息']
    ].concat(super)
end

Instance Method Details

#runObject



224
225
226
227
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
# File 'lib/pindo/command/web/run.rb', line 224

def run
    pindo_project_dir = Dir.pwd
    build_helper = Pindo::BuildHelper.share_instance
    project_type = build_helper.project_type(pindo_project_dir)

    webgl_res_dir = Dir.pwd
    case project_type
    when :unity
        webgl_res_dir = File.join(pindo_project_dir, "GoodPlatform/WebGL/build")
    end

    # 确保路径规范化
    webgl_res_dir = File.expand_path(webgl_res_dir)
    
    # 只在调试模式下显示这些信息
    UI.debug("WebGL资源目录: #{webgl_res_dir}", @args_debug)
    UI.debug("目录存在: #{File.directory?(webgl_res_dir)}", @args_debug)
    
    # 启动HTTP服务器而不是简单打开目录
    server = start_http_server(webgl_res_dir, @args_port)
    
    if server
        # 打开浏览器展示WebGL内容
        system("open http://localhost:#{@args_port}/index.html")
        
        # 捕获Ctrl+C信号来优雅地关闭服务器
        trap('INT') { server.shutdown }
        trap('TERM') { server.shutdown }
        
        # 启动服务器主循环
        UI.puts("服务器运行中,按Ctrl+C停止...")
        server.start
    else
        # 如果服务器启动失败,回退到简单打开目录
        UI.puts("无法启动HTTP服务器,将直接打开WebGL目录")
        system "open #{webgl_res_dir}"
    end
end

#start_http_server(webgl_dir, port) ⇒ Object

启动HTTP服务器



149
150
151
152
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/pindo/command/web/run.rb', line 149

def start_http_server(webgl_dir, port)
    begin
        # 确保目录存在
        unless File.directory?(webgl_dir)
            UI.puts("错误: WebGL目录不存在: #{webgl_dir}")
            return nil
        end

        # 检查index.html是否存在
        if !File.exist?(File.join(webgl_dir, "index.html"))
            UI.puts("警告: WebGL目录中未找到index.html文件")
        end

        # 创建自定义日志格式器,只记录错误
        custom_log = [
            [:error, WEBrick::AccessLog::COMMON_LOG_FORMAT],
        ]

        # 采用直接的方法配置服务器
        server = WEBrick::HTTPServer.new(
            :Port => port,
            :DocumentRoot => webgl_dir,
            :Logger => WEBrick::Log.new(nil, WEBrick::Log::ERROR),
            :AccessLog => @args_debug ? nil : [],
            :DoNotReverseLookup => true
        )

        # 挂载处理程序
        server.mount("/", WebGLPageHandler, webgl_dir, @args_debug)
        
        # 挂载Brotli处理器(后缀为.br的文件)
        paths_with_br = []
        Find.find(webgl_dir) do |path|
            if path.end_with?('.br') && File.file?(path)
                rel_path = path.sub(webgl_dir, '')
                paths_with_br << rel_path
            end
        end
        
        # 挂载每个.br文件
        paths_with_br.each do |br_path|
            server.mount(br_path, BrotliFileHandler, webgl_dir, @args_debug)
        end
        
        # 只在调试模式下显示详细信息
        if @args_debug
            UI.puts("找到.br文件: #{paths_with_br.join(', ')}")
            
            UI.puts("WebGL目录内容:")
            Dir.entries(webgl_dir).each do |entry|
                next if entry == "." || entry == ".."
                UI.puts("  - #{entry}")
                
                # 递归显示子目录中的文件(限制为一级)
                entry_path = File.join(webgl_dir, entry)
                if File.directory?(entry_path)
                    Dir.entries(entry_path).each do |subentry|
                        next if subentry == "." || subentry == ".."
                        UI.puts("    - #{entry}/#{subentry}")
                    end
                end
            end
        end

        # 启动服务器并返回
        UI.puts("启动WebGL HTTP服务器,端口: #{port},目录: #{webgl_dir}")
        UI.puts("请访问: http://localhost:#{port}/index.html")
        return server
    rescue Exception => e
        UI.puts("启动HTTP服务器失败: #{e.message}")
        UI.debug(e.backtrace.join("\n"), @args_debug)
        return nil
    end
end

#validate!Object



50
51
52
# File 'lib/pindo/command/web/run.rb', line 50

def validate!
    super
end