运行 带有隐藏 window 的 Tortoise SVN 命令行命令

Run Tortoise SVN command line commands with a hidden window

我安装了带有命令行界面的 Tortoise SVN。安装路径是 C:\Program Files\TortoiseSVN\bin 其中 svn.exe 每当我使用任何 SVN 命令时都会使用。

我开发了一个 Ruby Windows 应用程序,它是 运行 作为后台进程。此应用程序 运行 的命令类似于

svn info "#{path_to_repository}"

如前所述,此命令会调用 svn.exe

问题是,svn.exe 闪烁命令提示符一秒钟然后终止,因此如果我 运行 svn info 十次不同的存储库那么屏幕闪烁十次这个命令及时开发到运行,屏幕有规律地闪烁十次。

我需要的是一种通过 Tortoise SVN 运行 SVN 命令的方法,而不需要 svn.exe 弹出屏幕并关闭。

Ruby has numerous way of executing command in shell,但是,在 GUI 应用程序中使用所有选项时,似乎都会出现一个命令行弹出窗口。

根据您在 svn info 中查找的详细信息,您可以使用 WebSVN and see if you can want to scrape the GUI or get data from its RSS feed. Take a look at demo site of this product.

之类的选项。

如果您有非常具体和最小的需求,那么,您也可以选择构建一个小型 REST API,它可以使用命令行查询 subversion 服务器。在这种情况下,您可以调用 REST API 来获取数据并避免弹出 command windows.

如果您真的时间紧迫或者没有服务器基础架构来托管 REST API,那么,您可以考虑创建一个 Ruby 应用程序 运行 作为套接字服务器并且可以 运行 shell 命令从客户端接收命令。然后,您可以让您的 GUI 应用程序使用套接字客户端连接到套接字服务器,并要求服务器应用程序执行 svn info 和 return 结果。 Go through the tutorial on building such interacting apps。然后,您可以选择 运行 在同一台 PC 上并排显示它们。

另一种方法是使用 Ruby SVN bindings. It may require some digging around 来实现它。

这是快速入门代码:

server.rb - ruby 接受命令并在 shell

中执行命令的 TCP 服务器
require 'socket'

server = TCPServer.open(2000)   # Socket to listen on port 2000
puts "Listening now #{server.addr}"
loop {
  Thread.start(server.accept) do |client|
    cmd = client.gets

    puts "Processing #{cmd} from #{client.peeraddr}"

    IO.popen(cmd) { |s| result = []; 
        while (line = s.gets) do 
            client.puts line.chop 
        end; 
    }

    client.close
  end
}

app.rb Shoes GUI app 向 运行 的 TCP 服务器发出 svn info 命令 server.rb

需要'socket'

Shoes.app {

    stack do 
        @push = button "Get SVN Info"
        @note = para ""
    end

    @push.click {

        hostname = 'localhost'
        port = 2000

        result  = []
        s = TCPSocket.open(hostname, port)
        s.puts "svn info trunk/backend"

        while line = s.gets 
          result << line.chop
        end
        s.close

        @note.replace result.join("\n")
    }
}

app.rb 应使用 shoes app.rb 命令启动。

此行为并非特定于 Ruby,而是特定于 Windows 命令行解释器。有几种方法可以解决它。

  • 尝试 运行 以 cmd.exe /C 为前缀的 svn 命令,该命令不应闪烁命令提示符 window。一种变体是使用 start /min 作为前缀。这并非在所有情况下都有效,而且我在 Windows 上没有 Ruby 方便检查。
  • 为您的命令创建一个 .vbs 包装器。由于 .vbs 不由命令行解释器处理,因此不会创建其 window。有关详细信息,请参阅“How to run a batch file without launching a 'command window'?”。
  • 最好的选择是使用 WinAPI 包装器 gem 来访问非常灵活的 ShellExecute 函数:

    require 'win32ole'
    
    # Create an instance of the Windows Shell object...
    
    shell = WIN32OLE.new('Shell.Application')
    
    # The shell object's ShellExecute method performs a specified operation on a specified file. The syntax is...
    
    shell.ShellExecute(FILE, ARGUMENTS, DIRECTORY, OPERATION, SHOW)
    

    此示例取自“Launching Apps and Printing Docs with the Windows Shell”,您可以在其中找到更多详细信息。

    为了你的目的,它会是这样的

    shell.ShellExecute('svn.exe', 'info', path_to_repository, 'open', 0)
    

详细了解 ShellExecute 用法。