Class: EasyAI::EasyAIConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/easyai/config/easyai_config.rb

Overview

EasyAIConfig 类负责配置仓库的下载、更新和文件解密

Constant Summary collapse

REPO_URL =
"https://gitee.com/goodtools/EasyAISetting.git"
EASYAI_DIR =
File.expand_path('~/.easyai')
CONFIG_REPO_DIR =
File.join(EASYAI_DIR, 'EasyAISetting')

Class Method Summary collapse

Class Method Details

.cleanup(verbose = false) ⇒ Object

清理所有临时文件



77
78
79
80
81
82
83
# File 'lib/easyai/config/easyai_config.rb', line 77

def cleanup(verbose = false)
  if @temp_dir && Dir.exist?(@temp_dir)
    FileUtils.rm_rf(@temp_dir)
    puts "✓ 已清理临时目录" if verbose
  end
  @temp_dir = nil
end

.get_config(filename, options = {}) ⇒ Hash?

核心接口:获取解密后的配置内容

Parameters:

  • filename (String)

    配置文件名

  • options (Hash) (defaults to: {})

    选项

Returns:

  • (Hash, nil)

    解密后的配置内容



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

def get_config(filename, options = {})
  return nil unless filename

  ensure_repo_ready(options[:verbose])
  filename = normalize_filename(filename)

  # 查找加密文件的实际路径(支持子目录)
  encrypted_path = find_encrypted_file(filename)
  return nil unless encrypted_path

  # 解密到临时目录
  temp_decrypted_path = get_temp_path(filename)

  # 解密文件
  return nil unless decrypt_file(encrypted_path, temp_decrypted_path, options[:verbose])

  # 读取并解析配置
  result = read_json_file(temp_decrypted_path, options[:verbose])

  # 立即清理临时文件
  FileUtils.rm_f(temp_decrypted_path) if File.exist?(temp_decrypted_path)

  result
end

.get_config_path(filename, options = {}) ⇒ Object

获取配置文件路径(用于需要文件路径的场景)



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

def get_config_path(filename, options = {})
  return nil unless filename

  ensure_repo_ready(options[:verbose])
  filename = normalize_filename(filename)

  # 查找加密文件的实际路径(支持子目录)
  encrypted_path = find_encrypted_file(filename)
  return nil unless encrypted_path

  # 解密到临时目录
  temp_decrypted_path = get_temp_path(filename)

  # 解密文件
  return nil unless decrypt_file(encrypted_path, temp_decrypted_path, options[:verbose])

  File.exist?(temp_decrypted_path) ? temp_decrypted_path : nil
end

.initialize(options = {}) ⇒ Object

初始化配置仓库



19
20
21
22
23
24
25
# File 'lib/easyai/config/easyai_config.rb', line 19

def initialize(options = {})
  verbose = options[:verbose] || false
  ensure_repo_ready(verbose)
  # 确保临时目录存在
  @temp_dir ||= File.join(Dir.tmpdir, "easyai_#{Process.pid}")
  FileUtils.mkdir_p(@temp_dir) unless Dir.exist?(@temp_dir)
end