检测 git 是否安装的独立于平台的方式

发布于 2024-10-10 04:58:59 字数 691 浏览 7 评论 0原文

这就是我在 ruby​​ 中检测 git 的方法:

`which git 2>/dev/null` and $?.success?

但是,这不是跨平台的。它在非 UNIX 系统或没有 which 命令的系统上失败(尽管我不确定这些是什么)。

我需要一种方法来检测满足这些条件的 git:

  1. 跨平台可靠工作,即使在 Windows 上
  2. 也不向 $stdout 或 $stderr 输出任何内容
  3. 少量代码

更新: 解决方案是避免在 Windows 上完全使用 which 并将输出重定向到 NUL

require 'rbconfig'
void = RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw/ ? 'NUL' : '/dev/null'
system "git --version >>#{void} 2>&1"

system 命令在成功时返回 true,在失败时返回 false,从而避免了使用反引号时需要使用 $?.success? 的情况。

This is how I detect git in ruby:

`which git 2>/dev/null` and $?.success?

However, this is not cross-platform. It fails on non-unix systems or those without the which command (although I'm not sure what those are).

I need a way to detect git that satisfies these conditions:

  1. works reliably cross-platform, even on Windows
  2. doesn't output anything to $stdout or $stderr
  3. small amount of code

Update: the solution is to avoid using which altogether and to redirect output to NUL on Windows.

require 'rbconfig'
void = RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw/ ? 'NUL' : '/dev/null'
system "git --version >>#{void} 2>&1"

The system command returns true on success and false on failure, saving us the trip to $?.success? which is needed when using backticks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我鬧 2024-10-17 04:58:59

Windows 上没有 /dev/null 这样的东西。

我们在不同项目中采用的一种方法是基于 RbConfig::CONFIG['host_os'] 定义 NULL

NULL = RbConfig::CONFIG['host_os'] =~ /mingw|mswin/ ? 'NUL' : '/dev/null'

然后使用它将 STDOUT 和 STDERR 重定向到它。

至于这一点,我在 我的博客

但是,如果您只想检查 git 的存在而不是位置,则无需执行以下操作,只需使用简单的系统调用并检查生成的 $? 即可足够的。

希望这有帮助

There is not such thing as /dev/null on Windows.

One approach we have been taking in different projects is define NULL based on RbConfig::CONFIG['host_os']

NULL = RbConfig::CONFIG['host_os'] =~ /mingw|mswin/ ? 'NUL' : '/dev/null'

Then use that to redirect both STDOUT and STDERR to it.

As for which, I made a trivial reference on my blog

But, if you just want to check git presence and not location, no need to do which, with a simple system call and check of the resulting in $? will be enough.

Hope this helps

初吻给了烟 2024-10-17 04:58:59

这是一种解决方案,可以避免花费大量时间来检测可执行文件,并且还能够可靠地检测可执行文件所在的位置。它是 which 的替代方案。

def which cmd
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    }
  end
  return nil
end

用法:

# Mac OS X
where 'ruby'
#=> /opt/local/Cellar/ruby-enterprise-edition/2010.02/bin/ruby

# Windows
where 'ruby'
#=> C:\Program Files\Ruby192\bin/ruby.exe

This is a solution which avoids shelling out to detect executables and is also able to reliably detect where the executable is located. It's an alternative to which.

def which cmd
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    }
  end
  return nil
end

Usage:

# Mac OS X
where 'ruby'
#=> /opt/local/Cellar/ruby-enterprise-edition/2010.02/bin/ruby

# Windows
where 'ruby'
#=> C:\Program Files\Ruby192\bin/ruby.exe
橘和柠 2024-10-17 04:58:59

也许在 JRuby 上运行并使用 JGit 可能是真正独立于平台的一个选择。

Perhaps running on JRuby and using JGit could be an option to really go platform-independent.

旧伤还要旧人安 2024-10-17 04:58:59

我想,你需要这样做:

  1. 检查机器是否是基于Windows和Unix的
  2. 如果是Unix,使用which
  3. 如果是Windows,Windows 上是否有“which”的等效项?

I think, you will need to do this:

  1. Check if machine is windows and unix based
  2. If Unix, use which
  3. If Windows, Is there an equivalent of 'which' on windows?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文