Module: EasyAI::CrossPlatform

Defined in:
lib/easyai/base/cross_platform.rb

Class Method Summary collapse

Class Method Details

.capture_without_env(cmd, env_vars = []) ⇒ Object

跨平台执行命令并获取输出,临时清除环境变量



42
43
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
# File 'lib/easyai/base/cross_platform.rb', line 42

def self.capture_without_env(cmd, env_vars = [])
  if env_vars.empty?
    return `#{cmd}`
  end

  if Gem.win_platform?
    # Windows: 保存并清除环境变量
    old_env = {}
    env_vars.each do |key|
      old_env[key] = ENV[key]
      ENV.delete(key)
    end

    begin
      output = `#{cmd}`
    ensure
      # 恢复环境变量
      old_env.each { |k, v| ENV[k] = v if v }
    end

    output
  else
    # Unix/Linux/macOS: 使用 env -u
    env_unset = env_vars.map { |v| "-u #{v}" }.join(' ')
    `env #{env_unset} #{cmd}`
  end
end

.command_exists?(command) ⇒ Boolean

跨平台检测命令是否存在

Returns:

  • (Boolean)


4
5
6
7
8
9
10
# File 'lib/easyai/base/cross_platform.rb', line 4

def self.command_exists?(command)
  if Gem.win_platform?
    system("where #{command} >nul 2>&1")
  else
    system("which #{command} > /dev/null 2>&1")
  end
end

.format_command(cmd) ⇒ Object

平台特定的命令格式化



140
141
142
143
144
145
146
147
# File 'lib/easyai/base/cross_platform.rb', line 140

def self.format_command(cmd)
  if Gem.win_platform?
    # Windows 特殊处理
    cmd.gsub('2>/dev/null', '2>nul').gsub('>/dev/null', '>nul')
  else
    cmd
  end
end

.null_deviceObject

获取空设备路径



71
72
73
# File 'lib/easyai/base/cross_platform.rb', line 71

def self.null_device
  Gem.win_platform? ? 'nul' : '/dev/null'
end

.platformObject

检测操作系统平台



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/easyai/base/cross_platform.rb', line 127

def self.platform
  if Gem.win_platform?
    :windows
  elsif RUBY_PLATFORM.include?('darwin')
    :macos
  elsif RUBY_PLATFORM.include?('linux')
    :linux
  else
    :unknown
  end
end

.run_without_env(cmd, env_vars = []) ⇒ Object

跨平台执行命令,临时清除环境变量



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/easyai/base/cross_platform.rb', line 13

def self.run_without_env(cmd, env_vars = [])
  if env_vars.empty?
    return system(cmd)
  end

  if Gem.win_platform?
    # Windows: 保存并清除环境变量
    old_env = {}
    env_vars.each do |key|
      old_env[key] = ENV[key]
      ENV.delete(key)
    end

    begin
      result = system(cmd)
    ensure
      # 恢复环境变量
      old_env.each { |k, v| ENV[k] = v if v }
    end

    result
  else
    # Unix/Linux/macOS: 使用 env -u
    env_unset = env_vars.map { |v| "-u #{v}" }.join(' ')
    system("env #{env_unset} #{cmd}")
  end
end

.set_file_permissions(file_path, mode = 0600) ⇒ Object

跨平台设置文件权限



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/easyai/base/cross_platform.rb', line 76

def self.set_file_permissions(file_path, mode = 0600)
  return unless File.exist?(file_path)

  if !Gem.win_platform?
    begin
      File.chmod(mode, file_path)
    rescue => e
      # 忽略权限设置错误,某些文件系统可能不支持
    end
  end
  # Windows 使用默认的文件权限(通常只有当前用户可访问)
end

.shell_config_filesObject

获取 Shell 配置文件路径



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
119
120
121
122
123
124
# File 'lib/easyai/base/cross_platform.rb', line 90

def self.shell_config_files
  files = []

  if Gem.win_platform?
    # Windows PowerShell 配置
    powershell_profile = File.expand_path("~/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1")
    powershell_core_profile = File.expand_path("~/Documents/PowerShell/Microsoft.PowerShell_profile.ps1")

    files << powershell_core_profile if File.exist?(File.dirname(powershell_core_profile))
    files << powershell_profile if File.exist?(File.dirname(powershell_profile))
  else
    # Unix/Linux/macOS Shell 配置
    current_shell = ENV['SHELL']&.split('/')&.last || 'bash'

    case current_shell
    when 'zsh'
      files << File.expand_path("~/.zshrc")
    when 'bash'
      bash_profile = File.expand_path("~/.bash_profile")
      bashrc = File.expand_path("~/.bashrc")
      files << (File.exist?(bash_profile) ? bash_profile : bashrc)
    when 'fish'
      fish_config = File.expand_path("~/.config/fish/config.fish")
      files << fish_config
    else
      files << File.expand_path("~/.profile")
    end

    # 添加 ~/.profile 作为备份
    profile_file = File.expand_path("~/.profile")
    files << profile_file unless files.include?(profile_file)
  end

  files
end