Class: Pindo::Options::GlobalOptionsState

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pindo/options/core/global_options_state.rb

Overview

全局参数状态管理器(单例,简化版)职责:

  1. 管理当前命令的参数状态

  2. 管理文件缓存的加载、保存和应用

Instance Method Summary collapse

Constructor Details

#initializeGlobalOptionsState

Returns a new instance of GlobalOptionsState.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pindo/options/core/global_options_state.rb', line 14

def initialize
  # 运行时状态(内存)
  @current_command = nil        # 当前命令名称
  @current_options = nil        # 当前参数配置对象
  @current_directory = nil      # 当前项目目录
  @cache_enabled = false        # 是否启用缓存

  # 缓存状态(文件)
  @cache_data = {}              # 缓存数据:{ 项目目录 => { 命令 => 参数Hash } }
  @cache_loaded = false         # 是否已加载缓存文件

  # 调试
  @verbose = ENV['PINDO_VERBOSE'] == '1'

  ensure_cache_dir
end

Instance Method Details

#[](key) ⇒ Any

访问当前参数值

Parameters:

  • key (Symbol)

    参数键名

Returns:

  • (Any)

    参数值



182
183
184
# File 'lib/pindo/options/core/global_options_state.rb', line 182

def [](key)
  @current_options ? @current_options[key] : nil
end

#[]=(key, value) ⇒ Object

设置当前参数值

Parameters:

  • key (Symbol)

    参数键名

  • value (Any)

    参数值



189
190
191
# File 'lib/pindo/options/core/global_options_state.rb', line 189

def []=(key, value)
  @current_options[key] = value if @current_options
end

#cache_dirObject

缓存目录



201
202
203
# File 'lib/pindo/options/core/global_options_state.rb', line 201

def cache_dir
  File.join(Dir.home, '.pindo', 'cache')
end

#cache_file_pathObject

缓存文件路径



196
197
198
# File 'lib/pindo/options/core/global_options_state.rb', line 196

def cache_file_path
  File.join(cache_dir, 'options_cache.json')
end

#clearObject

清除当前命令状态



169
170
171
172
173
174
175
176
177
# File 'lib/pindo/options/core/global_options_state.rb', line 169

def clear
  # 如果启用了缓存,保存当前参数
  save_cache_to_file if @cache_enabled && @current_options

  @current_command = nil
  @current_options = nil
  @current_directory = nil
  @cache_enabled = false
end

#clear_current_cacheObject

清除当前命令的缓存



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pindo/options/core/global_options_state.rb', line 136

def clear_current_cache
  return unless @current_directory && @current_command

  if @cache_data[@current_directory.to_sym]
    @cache_data[@current_directory.to_sym].delete(@current_command.to_sym)

    # 如果项目目录下没有其他命令缓存了,删除整个项目目录
    if @cache_data[@current_directory.to_sym].empty?
      @cache_data.delete(@current_directory.to_sym)
    end

    # 立即保存到文件
    save_cache_to_file_immediate
  end
end

#display_cached_params(cached_params) ⇒ Object

显示缓存的参数



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
# File 'lib/pindo/options/core/global_options_state.rb', line 86

def display_cached_params(cached_params)
  # 根据命令名显示友好的描述
  group_desc = case @current_command
               when 'ios:autobuild'
                 'iOS 构建'
               when 'and:autobuild', 'android:autobuild'
                 'Android 构建'
               when 'web:autobuild'
                 'Web 构建'
               else
                 @current_command
               end

  puts "\n检测到之前的参数 (#{group_desc}):"
  puts "────────────────────────────────────────"

  cached_params.each do |key, value|
    # 跳过内部字段
    next if key.to_s.start_with?('__')
    next if value.nil?

    # 格式化显示参数
    key_name = format_param_name(key)
    puts "  #{key_name}: #{value}"
  end

  puts "────────────────────────────────────────"
end

#ensure_cache_dirObject

确保缓存目录存在



206
207
208
# File 'lib/pindo/options/core/global_options_state.rb', line 206

def ensure_cache_dir
  FileUtils.mkdir_p(cache_dir) unless Dir.exist?(cache_dir)
end

#format_param_name(key) ⇒ Object

格式化参数名称



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pindo/options/core/global_options_state.rb', line 116

def format_param_name(key)
  case key.to_s
  when 'bundleid', 'bundle_id'
    'Bundle ID'
  when 'build_type'
    '构建类型'
  when 'upload'
    '上传JPS'
  when 'send'
    '发送通知'
  when 'proj', 'project_name'
    '项目名称'
  when 'scheme'
    'Scheme'
  else
    key.to_s
  end
end

#load_cache_from_fileObject

从文件加载缓存



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/pindo/options/core/global_options_state.rb', line 211

def load_cache_from_file
  return if @cache_loaded

  if File.exist?(cache_file_path)
    begin
      content = File.read(cache_file_path)
      @cache_data = JSON.parse(content, symbolize_names: true)
      log_verbose("加载缓存文件: #{cache_file_path}")
      log_verbose("缓存内容: #{@cache_data.inspect}")
    rescue StandardError => e
      log_verbose("加载缓存文件失败: #{e.message}")
      @cache_data = {}
    end
  else
    log_verbose("缓存文件不存在: #{cache_file_path}")
  end

  @cache_loaded = true
end

#load_cached_valuesHash

加载缓存的参数值(带用户确认)

Returns:

  • (Hash)

    缓存的参数值



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
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pindo/options/core/global_options_state.rb', line 44

def load_cached_values
  return {} unless @current_directory && @current_command

  cached_params = @cache_data.dig(@current_directory.to_sym, @current_command.to_sym)

  # 没有缓存数据,直接返回空Hash
  unless cached_params && cached_params.is_a?(Hash) && cached_params.any?
    log_verbose("没有找到缓存参数: #{@current_directory} / #{@current_command}")
    return {}
  end

  # 检查环境变量是否强制使用缓存
  force_build = ENV['PINDO_FORCE_BUILD']

  if force_build && !force_build.empty?
    # 自动使用缓存
    puts "\n检测到 PINDO_FORCE_BUILD 环境变量,自动使用缓存的参数"
    log_verbose("加载缓存参数: #{cached_params.inspect}")
    return cached_params
  end

  # 显示缓存的参数
  display_cached_params(cached_params)

  # 询问用户是否使用缓存
  require 'highline/import'
  cli = HighLine.new
  confirm = cli.agree("\n是否使用以上缓存的参数? (y/n) ")

  if confirm
    puts "使用缓存的参数\n"
    log_verbose("加载缓存参数: #{cached_params.inspect}")
    cached_params
  else
    puts "清除缓存,使用新参数\n"
    # 清除当前命令的缓存
    clear_current_cache
    {}
  end
end

#log_verbose(message) ⇒ Object

调试日志 ====================


263
264
265
# File 'lib/pindo/options/core/global_options_state.rb', line 263

def log_verbose(message)
  puts "[GlobalOptionsState] #{message}" if @verbose
end

#prepare_cache(command_name, directory) ⇒ Object

准备缓存(在创建 OptionConfiguration 之前调用)

Parameters:

  • command_name (String)

    命令名称

  • directory (String)

    项目目录



36
37
38
39
40
# File 'lib/pindo/options/core/global_options_state.rb', line 36

def prepare_cache(command_name, directory)
  @current_command = command_name
  @current_directory = directory
  load_cache_from_file
end

#save_cache_to_fileObject

保存当前参数到缓存文件



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/pindo/options/core/global_options_state.rb', line 232

def save_cache_to_file
  return unless @current_directory && @current_command && @current_options

  # 确保缓存数据结构存在
  @cache_data[@current_directory.to_sym] ||= {}

  # 提取当前参数值(排除 nil 值)
  current_params = {}
  @current_options.instance_variable_get(:@values).each do |key, value|
    current_params[key] = value unless value.nil?
  end

  # 保存到缓存
  @cache_data[@current_directory.to_sym][@current_command.to_sym] = current_params

  # 写入文件
  save_cache_to_file_immediate
end

#save_cache_to_file_immediateObject

立即保存缓存数据到文件(内部方法)



252
253
254
255
256
257
258
259
# File 'lib/pindo/options/core/global_options_state.rb', line 252

def save_cache_to_file_immediate
  begin
    File.write(cache_file_path, JSON.pretty_generate(@cache_data))
    log_verbose("保存缓存文件: #{cache_file_path}")
  rescue StandardError => e
    log_verbose("保存缓存文件失败: #{e.message}")
  end
end

#set_command(command_name, options, directory: nil, enable_cache: false) ⇒ Object

设置当前命令上下文

Parameters:

  • command_name (String)

    命令名称(如 “ios:autobuild”)

  • options (OptionConfiguration)

    参数配置对象

  • directory (String) (defaults to: nil)

    项目目录

  • enable_cache (Boolean) (defaults to: false)

    是否启用缓存



157
158
159
160
161
162
163
164
165
166
# File 'lib/pindo/options/core/global_options_state.rb', line 157

def set_command(command_name, options, directory: nil, enable_cache: false)
  @current_command = command_name
  @current_options = options
  @current_directory = directory || Dir.pwd
  @cache_enabled = enable_cache

  log_verbose("设置命令: #{command_name}")
  log_verbose("项目目录: #{@current_directory}")
  log_verbose("缓存启用: #{@cache_enabled}")
end