Class: EasyAI::Base::VersionChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/easyai/base/version_checker.rb

Constant Summary collapse

RUBYGEMS_API =
'https://rubygems.org/api/v1/gems/easyai.json'
VERSION_INFO_FILE =
File.expand_path('~/.easyai/version_info.yml')
CHECK_INTERVAL =

5小时检查一次

5 * 60 * 60

Class Method Summary collapse

Class Method Details

.check_asyncObject

异步检查版本(不阻塞主程序)- 保留备用



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/easyai/base/version_checker.rb', line 34

def check_async
  puts "  🔮 启动异步版本检查...".cyan if ENV['EASYAI_DEBUG']
  
  return unless should_check?
  
  puts "  🚀 后台检查进程已启动".green if ENV['EASYAI_DEBUG']
  
  # 使用 fork 在后台检查(Unix系统)
  if Process.respond_to?(:fork)
    pid = fork do
      # 子进程中执行检查
      check_and_cache
      exit(0)
    end
    # 父进程立即继续,不等待子进程
    Process.detach(pid) if pid
  else
    # Windows 系统使用线程
    Thread.new { check_and_cache }
  end
end

.check_force_update!Object

强制更新检查(同步),返回获取到的最新版本



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
# File 'lib/easyai/base/version_checker.rb', line 75

def check_force_update!
  puts "  📌 执行强制更新检查...".cyan if ENV['EASYAI_DEBUG']
  
  # 如果缓存中已有需要强制更新的信息,直接使用
  if require_force_update?
    cache = YAML.load_file(VERSION_INFO_FILE) rescue {}
    show_force_update_message(cache['latest_version'], EasyAI::VERSION)
    exit(1)
  end
  
  # 如果缓存过期或不存在,立即检查
  if should_check_immediately?
    puts "  🔄 正在从 RubyGems 获取最新版本...".cyan if ENV['EASYAI_DEBUG']
    latest = fetch_latest_version
    if latest && needs_major_or_minor_update?(latest, EasyAI::VERSION)
      # 更新缓存
      update_cache_for_force_update(latest, EasyAI::VERSION)
      show_force_update_message(latest, EasyAI::VERSION)
      exit(1)
    end
    return latest  # 返回获取到的版本,供后续使用
  else
    puts "  💾 使用缓存(未过期)".cyan if ENV['EASYAI_DEBUG']
    # 从缓存中获取版本信息
    if File.exist?(VERSION_INFO_FILE)
      cache = YAML.load_file(VERSION_INFO_FILE) rescue {}
      return cache['latest_version']
    end
  end
  
  nil
end

.check_nowObject

同步检查版本(用于 –check-update 命令)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/easyai/base/version_checker.rb', line 109

def check_now
  latest = fetch_latest_version
  current = EasyAI::VERSION
  
  if latest && newer_version?(latest, current)
    show_update_message(latest, current)
    true
  else
    puts "✓ 您正在使用最新版本 (#{current})".green
    false
  end
rescue => e
  puts "⚠ 无法检查更新: #{e.message}".yellow if ENV['DEBUG']
  false
end

.check_sync(cached_latest_version = nil) ⇒ Object

同步检查版本(在当前进程中执行)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/easyai/base/version_checker.rb', line 17

def check_sync(cached_latest_version = nil)
  puts "  🔄 执行同步版本检查...".cyan if ENV['EASYAI_DEBUG']
  
  return unless should_check?
  
  puts "  📡 当前进程中检查版本...".green if ENV['EASYAI_DEBUG']
  
  # 如果已经有缓存的版本信息,直接使用,避免重复网络请求
  if cached_latest_version
    puts "  ♻️ 复用已获取的版本: #{cached_latest_version}".green if ENV['EASYAI_DEBUG']
    check_and_cache_with_version(cached_latest_version)
  else
    check_and_cache
  end
end

.require_force_update?Boolean

检查是否需要强制更新

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/easyai/base/version_checker.rb', line 57

def require_force_update?
  return false if ENV['EASYAI_SKIP_FORCE_UPDATE']  # 紧急跳过选项
  
  # 检查缓存
  if File.exist?(VERSION_INFO_FILE)
    cache = YAML.load_file(VERSION_INFO_FILE) rescue {}
    latest = cache['latest_version']
    current = EasyAI::VERSION  # 直接使用实际版本
    
    return needs_major_or_minor_update?(latest, current)
  end
  
  false
rescue
  false
end

.show_cached_reminderObject

显示缓存的更新提醒(如果有)



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/easyai/base/version_checker.rb', line 126

def show_cached_reminder
  return unless File.exist?(VERSION_INFO_FILE)
  
  cache = YAML.load_file(VERSION_INFO_FILE) rescue {}
  return unless cache['new_version_available']
  return if cache['reminder_shown_at'] && 
           Time.now - Time.parse(cache['reminder_shown_at']) < 3600  # 1小时内不重复提醒
  
  latest = cache['latest_version']
  current = EasyAI::VERSION
  
  if newer_version?(latest, current)
    puts "\n#{get_update_banner(latest, current)}\n"
    
    # 更新提醒时间
    cache['reminder_shown_at'] = Time.now.to_s
    save_cache(cache)
  end
end