Search code examples
web-servicesmercurialmercurial-hook

Call batch file from webservice to write to a text file


This may seem a bit crazy, but if you can tell me a better way please do. I need a webservice that will display the mercurial revision number for the current version.

I have a very simple batch file that writes this number to a text file

cd C:\inetpub\wwwroot\MyWebsite
hg identify --num > services\version.txt 

and a method that should call the batch file then read the contents of the file and, for now, write to screen (I'm doing it in a web page, but will migrate to a simple service later)

Private Sub GetVersionNumber()
    Dim versionFile = Server.MapPath("~/services/version.txt")
    Dim batchFile = Server.MapPath("~/services/version.bat")
    Process.Start(batchFile)

    Dim revision As String = String.Empty
    Using reader As New StreamReader(versionFile)
        Do While reader.Peek() <> -1
            revision = reader.ReadLine()
        Loop
    End Using
    Response.Write(revision)
End Sub

I have granted Everyone write access to the services directory and explicitly on version.txt, but when I hit the page the version.txt file is always empty. If I have the file open in notepad++ when I do then I get a message saying the file has been modified by another program, do I want to reload it. When I do it's empty. If I set some text in the file and save it then visit the web page again the contents of my text file are wiped out.

I had this in a try/catch block but no exception was thrown, so I've removed it (for clarity).

Ultimately all I need is to get the results of
hg identify --num
to display as a string on the web page/service which will be called by a separate SharePoint site


Solution

  • Instead of manually invoking the batch file from your web server every time the page is requested, you can configure a Mercurial update hook to invoke your identify command automatically every time the working copy updates.

    For example, add this to the repository’s .hg\hgrc file:

    [hooks]
    update.writeversion = hg identify --num > services\version.txt
    

    Some links with additional info on hooks: