与平台无关的检测git是否已安装的方法

时间:2021-04-26 23:09:10

This is how I detect git in ruby:

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

`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).

但是,这不是跨平台的。它在非unix系统或没有which命令的系统上失败(虽然我不确定它们是什么)。

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

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

  1. works reliably cross-platform, even on Windows
  2. 即使在Windows上,也可以跨平台工作

  3. doesn't output anything to $stdout or $stderr
  4. 不会向$ stdout或$ stderr输出任何内容

  5. small amount of code
  6. 少量的代码


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

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

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.

系统命令在成功时返回true,在失败时返回false,从而节省了$ ?. success的行程?这是使用反引号时所需要的。

4 个解决方案

#1


4  

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

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

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

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

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

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

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

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.

但是,如果你只是想检查git存在而不是位置,不需要做哪个,用一个简单的系统调用并检查结果为$?就足够了。

Hope this helps

希望这可以帮助

#2


1  

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

#3


0  

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

也许在JRuby上运行并使用JGit可以选择真正独立于平台。

#4


0  

I think, you will need to do this:

我想,你需要这样做:

  1. Check if machine is windows and unix based
  2. 检查机器是否为基于Windows的unix

  3. If Unix, use which
  4. 如果是Unix,请使用哪个

  5. If Windows, Is there an equivalent of 'which' on windows?
  6. 如果是Windows,Windows上是否有相应的“哪个”?

#1


4  

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

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

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

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

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

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

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

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.

但是,如果你只是想检查git存在而不是位置,不需要做哪个,用一个简单的系统调用并检查结果为$?就足够了。

Hope this helps

希望这可以帮助

#2


1  

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

#3


0  

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

也许在JRuby上运行并使用JGit可以选择真正独立于平台。

#4


0  

I think, you will need to do this:

我想,你需要这样做:

  1. Check if machine is windows and unix based
  2. 检查机器是否为基于Windows的unix

  3. If Unix, use which
  4. 如果是Unix,请使用哪个

  5. If Windows, Is there an equivalent of 'which' on windows?
  6. 如果是Windows,Windows上是否有相应的“哪个”?