简体   繁体   中英

Changing Minor value in my version updater

I currently have this small .exe to increment my version number for my program. This .exe builds in Jenkins before my .sln builds which then changes the global.cs of the .sln file. We made incompatible API changes to our program and now want the minor value to change from 1.1.xxx to 1.2.xxx. What should I change in order to achieve my goal.

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

namespace ApplicationName.VersionUpdater
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2) {
                Console.WriteLine("Usage:  ApplicationName.VersionUpdater PathToGlobal.cs RevisionNo");
                return;
            }

            FileInfo file;
            try
            {
                file = new FileInfo(args[0]);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Invalid use:  Global.cs pointer incorrect:  {0}", ex.Message);
                return;
            }

            if (!file.Exists) {
                Console.WriteLine("Invalid use:  Global.cs not found incorrect:  {0}", file.FullName);
                return;
            }

            int revno;
            try
            {
                revno = int.Parse(args[1]);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Invalid use:  Revision number incorrect:  {0}", ex.Message);
                return;
            }

            try
            {
                string content = File.ReadAllText(file.FullName);
                content = Regex.Replace(content, @"(?<=public const string ThisVersion = ""\d+\.\d+\.\d+\.)\d+", revno.ToString());
                File.WriteAllText(file.FullName, content);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception updating Global.cs!  {0}", ex.Message);
                return;
            }

        }
    }
}

**I just need a way to read the Regex code from the File and then adjust it to what i want it. How do I read Regex code from this file and how do I change it manually? **

The Regex.Replace(...) line in the code says to look for a string starting with public const string ThisVersion = " followed by three numbers each followed by a dot. For example public const string ThisVersion = "12.34.56. . The line then looks for one more number which it replaces with a number passed in as a command line parameter.

So calling the utility with (say) ApplicationName.VersionUpdater TheGlobal.cs 42 would cause this string

public const string ThisVersion = "12.34.56.78

to be replaced with

public const string ThisVersion = "12.34.56.42

Note that the regular expression does not look at or modify any characters after the fourth number.

The version numbers supported by this utility program do not match the 1.1.xxx and 1.2.xxx of the question. They do match the form 1.1.1.xxx and 1.1.2.xxx or 1.2.1.xxx .

There are two steps to your solution.

  1. Edit manually the relevant ...Global.cs file to change the 1 to a 2 .

  2. If the last number needs to be restarted to 1 (or some other value) then find where and how the RevisionNo parameter to the utility is generated and change it to start at 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM