Run Tortoise SVN command line commands with a hidden window

佐手、 提交于 2019-12-10 18:25:43

问题


I have Tortoise SVN with a command-line interface installed. The installation path is C:\Program Files\TortoiseSVN\bin where svn.exe is used whenever I use any SVN command.

I developed a Ruby Windows application which is run as a background process. This application runs the command like

svn info "#{path_to_repository}"

This command invokes svn.exe as I mentioned.

The problem is, svn.exe flashes a command prompt for a second and terminates, thus if I run svn info ten times for ten different repositories then the screen flickers ten times as this command is developed to run in a timely fashion, the screen flickers ten times regularly.

What I need is a way to run SVN commands through Tortoise SVN without the svn.exe popping up the screen and closing.


回答1:


Ruby has numerous way of executing command in shell, however, with all the options a command line popup seem to appear when using in GUI App.

Depending on what details you are looking for in svn info, one option you can use something like 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.

If you have very specific and minimal needs, then, you can also choose to build a small REST API which can query the subversion server using command line. You can in that case call that REST API to fetch the data and avoid popping up of command windows.

If you are really short on time or do not have server infrastructure to host REST API, then, you can think of creating a Ruby App that runs a socket server and can run the shell command on receiving commands from client. Then, you can make your GUI App connect to the socket server using socket client and ask the server app to execute svn info and return result. Go through the tutorial on building such interacting apps. You can then choose to run them side-by-side on same PC.

Another alternative is to use Ruby SVN bindings. It may require some digging around to get this to work.

Here is quick starter code:

server.rb - a ruby TCP server that accepts commands and executes them in shell

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 A Shoes GUI app that issues svn info command to TCP server being run by server.rb

require '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 should be launched using shoes app.rb command.




回答2:


This behavior is not specific to Ruby but to the Windows command-line interpreter. There are several ways to work around it.

  • Try running the svn command prefixed by cmd.exe /C which should not flash the command prompt window. A variation of that is to use start /min as a prefix instead. This doesn't work under all circumstances and I don't have a Ruby on Windows handy to check.
  • Create a .vbs wrapper for your command. Since .vbs is not handled by the command-line interpreter, its window will not be created. See "How to run a batch file without launching a 'command window'?" for details.
  • The best option is to use a WinAPI wrapper gem to get access to ShellExecute function which is pretty flexible:

    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)
    

    This example is taken from "Launching Apps and Printing Docs with the Windows Shell" where you can find more details.

    For your purpose it would be something like

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

Learn more about ShellExecute usage.



来源:https://stackoverflow.com/questions/31962364/run-tortoise-svn-command-line-commands-with-a-hidden-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!