easyai 1.0.5 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 764f51642d04ee3ba299d468f3d09cc6064899605910fdca6568c536de330e0e
4
- data.tar.gz: 29d920f541d56356ee766d87f349ee6e2459b9d5a6fdaede676d9457079bf4f7
3
+ metadata.gz: 3eaaa20803649142336c80f3c56d85e3047588f02cc81266d666147122c43200
4
+ data.tar.gz: fd5ae3775b3baef860d237ea61d59ec14bd4e292cdb2abe477b8dd624ee7533c
5
5
  SHA512:
6
- metadata.gz: 68b0aaec2f2996bf44c808d2e1266f47e4fc624a5194e1a29569b942ca35abfc9cd393baf3b29a5c6263798d591dd5b570d1b289e20da0fc30d05cb419f4ad92
7
- data.tar.gz: 816988a6ec0d2414ee5823621febfb7c28fa7607484f85436b8014a3eb0bab31b2d316e6f342f3939f066f7087bd443dcda9eab1d4302e9a9ca20bb4c2202dd6
6
+ metadata.gz: 7bd32be0384e8e411b7d0984b3a754fba62cc586ab967a7556058d958fbe47492c3445b7e985f7e2cfb4333b1fe001b31db33750c1ffa51cff04612d1368ccff
7
+ data.tar.gz: 1a92f0937530b9cadf52699a884bc2dbd21b11e940b215ca7858f48ab0235eb4c640b3cf3925d57a28683ceed38d6da81e5dfdb347226fff639c9e47fc195fbe
@@ -1,5 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'time'
3
+ require 'colored2'
3
4
 
4
5
  module EasyAI
5
6
  module Base
@@ -39,22 +40,34 @@ module EasyAI
39
40
  notification = load_notification
40
41
  return false unless notification
41
42
 
42
- # 检查显示频率
43
- last_shown = notification['last_shown_at']
44
- if last_shown
45
- hours_since = (Time.now - Time.parse(last_shown)) / 3600
43
+ # 只要当前版本低于最新版本就提醒
44
+ current = notification['current_version'] || EasyAI::VERSION
45
+ latest = notification['latest_version']
46
+
47
+ # 使用版本比较逻辑
48
+ return newer_version?(latest, current) if latest && current
49
+
50
+ true
51
+ end
52
+
53
+ # 版本比较:检查 latest 是否比 current 新
54
+ def newer_version?(latest, current)
55
+ return false unless latest && current
56
+
57
+ # 解析版本号 (major.minor.patch)
58
+ latest_parts = latest.split('.').map(&:to_i)
59
+ current_parts = current.split('.').map(&:to_i)
60
+
61
+ # 比较版本号
62
+ (0..2).each do |i|
63
+ latest_part = latest_parts[i] || 0
64
+ current_part = current_parts[i] || 0
46
65
 
47
- # 根据版本差异决定提醒频率
48
- if major_update?(notification)
49
- return hours_since > 6 # 主版本更新:6小时提醒一次
50
- elsif minor_update?(notification)
51
- return hours_since > 24 # 次版本更新:24小时提醒一次
52
- else
53
- return hours_since > 72 # 补丁更新:72小时提醒一次
54
- end
66
+ return true if latest_part > current_part
67
+ return false if latest_part < current_part
55
68
  end
56
69
 
57
- true
70
+ false
58
71
  end
59
72
 
60
73
  def major_update?(notification)
@@ -79,12 +92,23 @@ module EasyAI
79
92
  latest = notification['latest_version']
80
93
  current = notification['current_version']
81
94
 
82
- # 单行简洁提醒
95
+ # 醒目的框形提醒
83
96
  if major_update?(notification)
84
- puts "💡 重要更新: EasyAI #{latest} 已发布 (当前: #{current})。运行 'gem update easyai' 更新。".yellow
97
+ # 主版本更新使用红色警告框
98
+ puts ""
99
+ puts "┌─────────────────────────────────────────────────────┐".red
100
+ puts "│ 🚨 重要更新: EasyAI #{current} → #{latest} │".red
101
+ puts "│ 更新命令: gem update easyai │".red
102
+ puts "└─────────────────────────────────────────────────────┘".red
85
103
  else
86
- puts "💡 新版本: EasyAI #{latest} 可用。运行 'easyai --check-update' 查看详情。".cyan
104
+ # 普通更新使用黄色提示框
105
+ puts ""
106
+ puts "┌─────────────────────────────────────────────────────┐".yellow
107
+ puts "│ 📦 新版本可用: EasyAI #{current} → #{latest} │".yellow
108
+ puts "│ 更新命令: gem update easyai │".yellow
109
+ puts "└─────────────────────────────────────────────────────┘".yellow
87
110
  end
111
+ puts ""
88
112
  end
89
113
 
90
114
  def show_exit_notification(notification)
@@ -96,7 +120,7 @@ module EasyAI
96
120
  puts " 当前版本: #{current}"
97
121
  puts " 最新版本: #{latest}".green
98
122
  puts " 更新命令: gem update easyai"
99
- puts " 查看详情: easyai --check-update"
123
+ puts " 安装命令: gem install easyai"
100
124
  puts "─" * 50
101
125
  end
102
126
 
@@ -10,10 +10,27 @@ module EasyAI
10
10
  class VersionChecker
11
11
  RUBYGEMS_API = 'https://rubygems.org/api/v1/gems/easyai.json'
12
12
  CACHE_FILE = File.expand_path('~/.easyai/version_cache.yml')
13
- CHECK_INTERVAL = 24 * 60 * 60 # 24小时检查一次
13
+ CHECK_INTERVAL = 5 * 60 * 60 # 5小时检查一次
14
14
 
15
15
  class << self
16
- # 异步检查版本(不阻塞主程序)
16
+ # 同步检查版本(在当前进程中执行)
17
+ def check_sync(cached_latest_version = nil)
18
+ puts " 🔄 执行同步版本检查...".cyan if ENV['EASYAI_DEBUG']
19
+
20
+ return unless should_check?
21
+
22
+ puts " 📡 当前进程中检查版本...".green if ENV['EASYAI_DEBUG']
23
+
24
+ # 如果已经有缓存的版本信息,直接使用,避免重复网络请求
25
+ if cached_latest_version
26
+ puts " ♻️ 复用已获取的版本: #{cached_latest_version}".green if ENV['EASYAI_DEBUG']
27
+ check_and_cache_with_version(cached_latest_version)
28
+ else
29
+ check_and_cache
30
+ end
31
+ end
32
+
33
+ # 异步检查版本(不阻塞主程序)- 保留备用
17
34
  def check_async
18
35
  puts " 🔮 启动异步版本检查...".cyan if ENV['EASYAI_DEBUG']
19
36
 
@@ -54,7 +71,7 @@ module EasyAI
54
71
  false
55
72
  end
56
73
 
57
- # 强制更新检查(同步)
74
+ # 强制更新检查(同步),返回获取到的最新版本
58
75
  def check_force_update!
59
76
  puts " 📌 执行强制更新检查...".cyan if ENV['EASYAI_DEBUG']
60
77
 
@@ -75,9 +92,17 @@ module EasyAI
75
92
  show_force_update_message(latest, EasyAI::VERSION)
76
93
  exit(1)
77
94
  end
95
+ return latest # 返回获取到的版本,供后续使用
78
96
  else
79
- puts " 💾 使用缓存(未过期)".light_black if ENV['EASYAI_DEBUG']
97
+ puts " 💾 使用缓存(未过期)".cyan if ENV['EASYAI_DEBUG']
98
+ # 从缓存中获取版本信息
99
+ if File.exist?(CACHE_FILE)
100
+ cache = YAML.load_file(CACHE_FILE) rescue {}
101
+ return cache['latest_version']
102
+ end
80
103
  end
104
+
105
+ nil
81
106
  end
82
107
 
83
108
  # 同步检查版本(用于 --check-update 命令)
@@ -140,6 +165,16 @@ module EasyAI
140
165
  latest = fetch_latest_version
141
166
  return unless latest
142
167
 
168
+ check_and_cache_with_version(latest)
169
+ rescue => e
170
+ # 静默失败,不影响主程序
171
+ nil
172
+ end
173
+
174
+ # 使用已知的最新版本更新缓存
175
+ def check_and_cache_with_version(latest)
176
+ return unless latest
177
+
143
178
  current = EasyAI::VERSION
144
179
  is_new = newer_version?(latest, current)
145
180
 
@@ -186,21 +221,28 @@ module EasyAI
186
221
  def fetch_latest_version
187
222
  uri = URI(RUBYGEMS_API)
188
223
 
189
- http = Net::HTTP.new(uri.host, uri.port)
224
+ # 创建 HTTP 连接,禁用代理
225
+ http = Net::HTTP.new(uri.host, uri.port, nil, nil) # 后两个参数为 proxy_addr, proxy_port,设为 nil 禁用代理
190
226
  http.use_ssl = true
191
- http.open_timeout = 2 # 快速超时
192
- http.read_timeout = 2
227
+ http.open_timeout = 3 # 快速超时
228
+ http.read_timeout = 3
229
+
230
+ puts " 🌐 直连 RubyGems API (跳过代理)...".cyan if ENV['EASYAI_DEBUG']
193
231
 
194
232
  request = Net::HTTP::Get.new(uri)
195
233
  response = http.request(request)
196
234
 
197
235
  if response.code == '200'
198
236
  data = JSON.parse(response.body)
199
- data['version']
237
+ version = data['version']
238
+ puts " ✅ 获取到最新版本: #{version}".green if ENV['EASYAI_DEBUG']
239
+ version
200
240
  else
241
+ puts " ❌ API 响应错误: #{response.code}".yellow if ENV['EASYAI_DEBUG']
201
242
  nil
202
243
  end
203
244
  rescue => e
245
+ puts " ⚠️ 版本获取失败: #{e.message}".yellow if ENV['EASYAI_DEBUG']
204
246
  nil # 网络错误时静默失败
205
247
  end
206
248
 
@@ -246,11 +288,11 @@ module EasyAI
246
288
  cache = YAML.load_file(CACHE_FILE) rescue {}
247
289
  last_check = cache['last_check_at']
248
290
 
249
- # 如果没有检查记录或缓存超过6小时,立即检查
291
+ # 如果没有检查记录或缓存超过指定时间间隔,立即检查
250
292
  return true unless last_check
251
293
 
252
294
  time_since_check = Time.now - Time.parse(last_check)
253
- time_since_check > 6 * 60 * 60 # 6小时
295
+ time_since_check > CHECK_INTERVAL
254
296
  end
255
297
 
256
298
  # 更新缓存(强制更新场景)
@@ -60,8 +60,6 @@ module EasyAI
60
60
 
61
61
  def run
62
62
  begin
63
- # 在非verbose模式下显示简洁的更新提醒
64
- Base::UpdateNotifier.maybe_show_notification unless @verbose_mode
65
63
 
66
64
  # 首先尝试从远程下载配置
67
65
  remote_config = nil
@@ -1,3 +1,3 @@
1
1
  module EasyAI
2
- VERSION = '1.0.5'
2
+ VERSION = '1.0.8'
3
3
  end
data/lib/easyai.rb CHANGED
@@ -8,6 +8,7 @@ require 'easyai/command/utils'
8
8
  require 'easyai/command/clean'
9
9
  require 'easyai/command/update'
10
10
  require 'easyai/base/version_checker'
11
+ require 'easyai/base/update_notifier'
11
12
 
12
13
  module EasyAI
13
14
  # EasyAI 应用程序主类
@@ -72,10 +73,13 @@ module EasyAI
72
73
  return if skip_check
73
74
 
74
75
  # 强制更新检查(会阻塞执行)
75
- Base::VersionChecker.check_force_update!
76
+ latest_version = Base::VersionChecker.check_force_update!
76
77
 
77
- # 异步更新检查(不阻塞执行)
78
- Base::VersionChecker.check_async
78
+ # 同步更新检查(在当前进程中执行,复用已获取的版本)
79
+ Base::VersionChecker.check_sync(latest_version)
80
+
81
+ # 显示更新提醒(如果有新版本)
82
+ Base::UpdateNotifier.maybe_show_notification
79
83
 
80
84
  puts "✅ #{'版本检查完成'.green}" if ENV['EASYAI_DEBUG']
81
85
  rescue => e
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easyai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wade