Find and increment the assembly version info in an Assembly info file
public class AssemblyVersionIncrementor
{
public void IncrementAssemblyVersionString(FileSystemInfo fileInfo)
{
string[] lines = File.ReadAllLines(fileInfo.FullName);
lines = IncrementAssemblyVersionString(lines);
File.WriteAllLines(fileInfo.FullName, lines);
}
internal string[] IncrementAssemblyVersionString(string[] lines)
{
string[] res = new string[lines.Length];
int ix = 0;
foreach (string line in lines)
{
res[ix++] = IncrementAssemblyVersionString(line);
}
return res;
}
// Could be modified to find other types of Version Info by
// making the "AssemblyVersion" a parameter
internal string IncrementAssemblyVersionString(string line)
{
string res = line;
if (line.Contains("AssemblyVersion"))
{
string newLine = string.Empty;
string str = line;
// Check for comments
int commentStart = line.IndexOf("//");
// TODO /* ... */ style comments
if (commentStart >= 0)
{
str = line.Substring(0, commentStart);
}
if (str.Contains("AssemblyVersion"))
{
int start = str.IndexOf('"') + 1;
int end = str.IndexOf('"', start);
string assemblyVerStr = str.Substring(start, end - start);
Version av = new Version(assemblyVerStr);
Version newVer = av.IncrementRevision();
newLine = line.Substring(0, start) + newVer.ToString() +
line.Substring(end, line.Length - end);
}
res = newLine.Length > 0 ? newLine : line;
}
return res;
}
}
uses the following Version extension class:
internal static class VersionExtender
{
public static Version Increment(this Version version)
{
return Increment(version, 0x1L);
}
public static Version IncrementMajor(this Version version)
{
return Increment(version, 0x1000000000000L);
}
public static Version IncrementMinor(this Version version)
{
return Increment(version, 0x100000000L);
}
public static Version IncrementBuild(this Version version)
{
return Increment(version, 0x10000L);
}
public static Version IncrementRevision(this Version version)
{
return Increment(version, 0x1L);
}
// Good demo of using >> and << operators as well
private static Version Increment(this Version version, ulong inc)
{
ulong versnNum = (((ulong)version.Major & 0xFFFF) << 48) +
(((ulong)version.Minor & 0xFFFF) << 32) +
(((ulong)version.Build & 0xFFFF) << 16) +
((ulong)version.Revision & 0xFFFF);
versnNum += inc;
UInt16 major = (UInt16)(versnNum >> 48);
UInt16 minor = (UInt16)((versnNum >> 32) & 0xFFFF);
UInt16 build = (UInt16)((versnNum >> 16) & 0xFFFF);
UInt16 revision = (UInt16)(versnNum & 0xFFFF);
return new Version(major, minor, build, revision);
}
}
No comments:
Post a Comment