Laden...

Subversion - Revisions-Nummer auslesen + SVN-Nr. in Programm-Version eintragen

Erstellt von Beauty vor 12 Jahren Letzter Beitrag vor 12 Jahren 11.036 Views
Beauty Themenstarter:in
79 Beiträge seit 2007
vor 12 Jahren
Subversion - Revisions-Nummer auslesen + SVN-Nr. in Programm-Version eintragen

Beschreibung:

Hier ist ein** kleines Werkzeug für Subversion (SVN)**.
Es bezieht sich auf das Datenformat von Subversion 1.7 (SQLite).
Quellcode zum Auslesen des alten Formats findet man in Reading the revision number of a local copy of a SVN repository.

Mein Code kann folgendes:

Auslesen der aktuellen Revisionsnummer:

SvnTool.GetRevisionNumber(String svnDirectory)

**Aktualisierung der Datei "ApplicationInfo.cs". **
Dabei wird die letzte Stelle der Versionnummer durch die aktuelle SVN Revisionnummer ersetzt.
Ergebnis: Die Versionsnummer der Anwendung (oder Bibliothek) beinhaltet die SVN-Revisionsnummer (z.B. 1.0.0.0 wird zu 1.0.0.666)

UpdateAssemblyInfo(String svnDirectory, String assemblyInfoPath)

Thanks to Wiktor Zychla, who published a demo code how to read out the the revision information from the Subversion database.

Quellcode:


using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;

namespace SubversionTool
{

/// <summary>
/// Class to grabb the current SVN revision number.  <br/>
/// Also it's possible to update the "ApplicationInfo.cs" file to include the SVN revision number to the application or library version.<br/>
/// Note: Works only for the database format of Subversion version 1.7 and newer (SQLite).
/// </summary>
/// <remarks>
/// If you need support of Subversion 1.6 and below, look to URL 
/// http://netpl.blogspot.com/2011/10/reading-revision-number-of-local-copy.html 
/// </remarks>
private class SvnTool
{
    // Written by Wiktor Zychla (October 2011)
    // Source:  http://netpl.blogspot.com/2011/10/reading-revision-number-of-local-copy.html
    // Modified by Andreas Voigt (January 2012)
    // Source:  http://www.mycsharp.de/wbb2/thread.php?threadid=101541

    private const String databaseFile      = "wc.db";
    private const String pattern = "/!svn/ver/(?'version'[0-9]*)/";


    /// <summary>
    /// Grabb SVN information (Database format of Subversion version 1.7) <br/>
    /// If faild, it returns an empty String.
    /// </summary>
    /// <param name="svnDirectory">Absolute path to the Subversion directory (e.g. "C:\myProject\.svn") </param>
    public static String GetRevisionNumber(String svnDirectory)
    {
        String SvnSubfolder = svnDirectory;

        if (Directory.Exists(SvnSubfolder))
        {
            Int32 maxVer = 0;
            String EntriesFile = Directory.GetFiles(SvnSubfolder, databaseFile).FirstOrDefault();

            if (!String.IsNullOrEmpty(EntriesFile))
            {
                Byte[] fileData;
                try
                {
                    fileData = File.ReadAllBytes(EntriesFile);
                }
                catch (Exception)  // e.g. file not readable, because it's locked by an other thread
                {
                    return String.Empty;
                }
                String fileDataString = Encoding.Default.GetString(fileData);

                Regex regex = new Regex(pattern);

                foreach (Match match in regex.Matches(fileDataString))
                {
                    String version = match.Groups["version"].Value;
                    Int32 curVer;

                    if (Int32.TryParse(version, out curVer) == true)
                        if (curVer > maxVer)
                            maxVer = curVer;
                }

                if (maxVer > 0)
                    return maxVer.ToString();
            }
        }
        return String.Empty;
    } // GetRevisionNumber()





    /// <summary>
    /// Grabb and save the SVN revision number to the version information of the "AssemblyInfo.cs" file. 
    /// As result the last number of the application version is equal to the SVN revision. (e.g. "1.0.0.666")
    /// </summary>
    /// <param name="svnDirectory">Absolute path to the Subversion directory (e.g. "C:\myProject\.svn") </param>
    /// <param name="assemblyInfoPath">Absolute path to the file "AssemblyInfo.cs"</param>
    public static Boolean UpdateAssemblyInfo(String svnDirectory, String assemblyInfoPath)
    {
        // get current revision
        String revision = GetRevisionNumber(svnDirectory);
        if (revision == "")
        {
            Console.WriteLine("WARNING: Can't update information about Subversion revision."
                            + " (Failed to grabb from SVN database)");
            return false;
        }


        //---- update file "AssemblyInfo.cs" ----

        try
        {
            if (File.Exists(assemblyInfoPath) == false)
            {
                Console.WriteLine("WARNING: Can't update information about Subversion revision. "
                                + "(File 'AssemblyInfo.cs' not found)");
                return false;
            }

            Boolean doUpdate = false;

            // read file content
            FileStream assFileStream = new FileStream(assemblyInfoPath, FileMode.Open, FileAccess.Read);
            StreamReader assFileReader = new StreamReader(assFileStream);
            Encoding encodingType = assFileReader.CurrentEncoding;  // important for saving
            String assemblyInfoFileContent = assFileReader.ReadToEnd();
            assFileReader.Close();
            assFileStream.Close();


            //-- update "AssemblyVersion" entry --

            // build pattern for [assembly: AssemblyVersion("1.1.0.0")]
            String assemblyPattern_1 = @"(\[assembly: AssemblyVersion\(#\d+\.\d+\.\d+\.)\d+(#\)\])";
            assemblyPattern_1 = Regex.Replace(assemblyPattern_1, "#", "\"");  // replace symbols # by "

            // try to find pattern
            Match match_1 = Regex.Match(assemblyInfoFileContent, assemblyPattern_1);

            if (match_1.Success)
            {
                String replacement_1 = match_1.Groups[1] + revision + match_1.Groups[2];

                // check if content (revision number) changed
                if (match_1.Value != replacement_1)
                {
                    // update file content
                    assemblyInfoFileContent = assemblyInfoFileContent.Replace(match_1.Value, replacement_1);
                    doUpdate = true;
                }
            }


            //-- update "AssemblyFileVersion" entry --

            // build pattern for [assembly: AssemblyFileVersion("1.1.0.0")]
            String assemblyPattern_2 = @"(\[assembly: AssemblyFileVersion\(#\d+\.\d+\.\d+\.)\d+(#\)\])";
            assemblyPattern_2 = Regex.Replace(assemblyPattern_2, "#", "\"");  // replace symbols # by "

            // try to find pattern
            Match match_2 = Regex.Match(assemblyInfoFileContent, assemblyPattern_2);

            if (match_2.Success)
            {
                String replacement_2 = match_2.Groups[1] + revision + match_2.Groups[2];

                // check if content (revision number) changed
                if (match_2.Value != replacement_2)
                {
                    // update file content
                    assemblyInfoFileContent = assemblyInfoFileContent.Replace(match_2.Value, replacement_2);
                    doUpdate = true;
                }
            }



            // save to file (if needed)
            if (doUpdate)
            {
                FileStream assFileStream2 = new FileStream(assemblyInfoPath, FileMode.Truncate, FileAccess.Write);
                StreamWriter assFileWriter = new StreamWriter(assFileStream2, encodingType);
                assFileWriter.Close();
                assFileStream2.Close();
            }

        }
        catch (IOException e)
        {
            Console.WriteLine("WARNING: Can't update information about Subversion revision. "
                            + "(Problem at access to file 'AssemblyInfo.cs')\n    " + e.Message);
            return false;
        }

        return true;

    } // UpdateAssemblyInfo()


} // class SvnTool

} // namespace SubversionTool


Schlagwörter: SVN, Subversion, number, version, versionnumber revision, application, Versionsnummer, Programmversion, auslesen,
csharp, cs, csharp, C#, code, snippet, class