myCSharp.de - DIE C# und .NET Community
Willkommen auf myCSharp.de! Anmelden | kostenlos registrieren
 
 | Suche | FAQ

» Hauptmenü
myCSharp.de
» Startseite
» Forum
» FAQ
» Artikel
» C#-Snippets
» Jobbörse
» Suche
» Regeln
» Wie poste ich richtig?
» Forum-FAQ

Mitglieder
» Liste / Suche
» Wer ist wo online?

Ressourcen
» openbook: Visual C#
» openbook: OO
» Microsoft Docs

Team
» Kontakt
» Übersicht
» Wir über uns
» Datenschutzerklärung
» Impressum

» myCSharp.de Diskussionsforum
Du befindest Dich hier: Community-Index » Diskussionsforum » Gemeinschaft » .NET-Komponenten und C#-Snippets » Subversion - Revisions-Nummer auslesen + SVN-Nr. in Programm-Version eintragen
Letzter Beitrag | Erster ungelesener Beitrag Druckvorschau | Thema zu Favoriten hinzufügen

Antwort erstellen
Zum Ende der Seite springen  

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

 
Autor
Beitrag « Vorheriges Thema | Nächstes Thema »
Beauty
myCSharp.de-Mitglied

avatar-2333.jpg


Dabei seit: 31.05.2007
Beiträge: 79
Entwicklungsumgebung: Visual C# 2008
Herkunft: Thüringen


Beauty ist offline

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

Beitrag: beantworten | zitieren | editieren | melden/löschen       | Top

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:

C#-Code:
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)

C#-Code:
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:

C#-Code:
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

Dieser Beitrag wurde 2 mal editiert, zum letzten Mal von Beauty am 09.02.2012 14:07.

09.02.2012 13:58 E-Mail | Beiträge des Benutzers | zu Buddylist hinzufügen
Baumstruktur | Brettstruktur       | Top 
myCSharp.de | Forum Der Startbeitrag ist älter als 7 Jahre.
Der letzte Beitrag ist älter als 7 Jahre.
Antwort erstellen


© Copyright 2003-2019 myCSharp.de-Team | Alle Rechte vorbehalten. | Dieses Portal verwendet zum korrekten Betrieb Cookies. 22.05.2019 04:52