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

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

Defined Under Namespace

Modules: UI

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

command_name, #initialize_options, run, use_cache?

Methods included from Funlog::Mixin

#pindo_log_instance

Methods included from Pindoconfig::Mixin

#pindo_single_config

Constructor Details

#initialize(argv) ⇒ Run

Returns a new instance of Run.



46
47
48
49
50
51
52
53
54
# File 'lib/pindo/command/web/run.rb', line 46

def initialize(argv)
    @options = initialize_options(argv)

    @args_port = (@options[:port] || '8000').to_i
    @args_debug = @options[:debug] || false

    super
    @additional_args = argv.remainder!
end

Class Method Details

.option_itemsObject



37
38
39
# File 'lib/pindo/command/web/run.rb', line 37

def self.option_items
    @option_items ||= Pindo::Options::UtilsOptions.select(:port, :debug)
end

.optionsObject

命令的选项列表



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

def self.options
    option_items.map(&:to_claide_option).concat(super)
end

Instance Method Details

#runObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pindo/command/web/run.rb', line 71

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)
    
    # 使用WebGlServerHelper启动HTTP服务器
    server = Pindo::WebServer::WebGlServerHelper.start_http_server(webgl_res_dir, @args_port, @args_debug)
    
    if server
        # 获取本地IP地址
        local_ip = Pindo::WebServer::WebGlServerHelper.get_local_ip
        
        # 显示访问地址
        UI.puts("启动WebGL HTTP服务器,端口: #{@args_port},目录: #{webgl_res_dir}")
        UI.puts("原始WebGL本地预览地址: http://localhost:#{@args_port}/")
        UI.puts("模拟设备本地预览地址: http://localhost:#{@args_port}/responsive")
        UI.puts("原始WebGL局域网预览地址: http://#{local_ip}:#{@args_port}/")
        UI.puts("模拟设备局域网预览地址: http://#{local_ip}:#{@args_port}/responsive")
        
        # 打开浏览器展示WebGL内容 - 直接打开预览页面
        system("open http://localhost:#{@args_port}/responsive")
        
        # 捕获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

#validate!Object



56
57
58
# File 'lib/pindo/command/web/run.rb', line 56

def validate!
    super
end