private static bool IncrementVersionNumber(ProjectInfo project, VersionTag tag, bool changeFileTime, ref string newVersionNumber) { List <string> FileContent; DateTime FileWriteTimeUtc; int VersionLineIndex; ReadVersionFile(project.InfoFile, tag, out FileContent, out FileWriteTimeUtc, out VersionLineIndex, out int Tabulation); if (VersionLineIndex >= 0) { string VersionLine = FileContent[VersionLineIndex]; VersionLine = GetLineWithIncrementedVersion(VersionLine, Tabulation, tag, ref newVersionNumber); FileContent[VersionLineIndex] = VersionLine; if (changeFileTime) { FileWriteTimeUtc = DateTime.UtcNow; } WriteVersionFile(project.InfoFile, FileContent, FileWriteTimeUtc); return(true); } else { return(false); } }
private static void ReadVersionFile(string infoFile, VersionTag tag, out List <string> fileContent, out DateTime fileWriteTimeUtc, out int versionLineIndex, out int tabulation) { int MaxTry = 5; int CurrentTry = 0; while (!TryReadVersionFile(infoFile, tag, out fileContent, out fileWriteTimeUtc, out versionLineIndex, out tabulation) && CurrentTry++ < MaxTry) { Thread.Sleep(500); } }
private static bool TryReadVersionFile(string infoFile, VersionTag tag, out List <string> fileContent, out DateTime fileWriteTimeUtc, out int versionLineIndex, out int tabulation) { versionLineIndex = -1; tabulation = 0; try { using FileStream Stream = new FileStream(infoFile, FileMode.Open, FileAccess.Read, FileShare.Read); using StreamReader Reader = new StreamReader(Stream, Encoding.UTF8); fileContent = new List <string>(); for (; ;) { string?Line = Reader.ReadLine(); if (Line == null) { break; } if (IsVersionLine(Line.Trim(), tag)) { versionLineIndex = fileContent.Count; tabulation = 0; while (tabulation < Line.Length && Line[tabulation] == ' ') { tabulation++; } } fileContent.Add(Line); } fileWriteTimeUtc = File.GetLastWriteTimeUtc(infoFile); return(true); } catch { fileContent = new List <string>(); fileWriteTimeUtc = DateTime.MinValue; return(false); } }
private static string GetLineWithIncrementedVersion(string line, int tabulation, VersionTag tag, ref string versionNumber) { string ModifiedVersionString; if (versionNumber.Length == 0) { string VersionString = line.Substring(tabulation + tag.TagStart.Length, line.Length - tag.TagStart.Length - tag.TagEnd.Length - tabulation); string[] VersionParts = VersionString.Split('.'); if (VersionParts.Length > 2) { int BuildNumber; if (int.TryParse(VersionParts[VersionParts.Length - 1], out BuildNumber)) { int NewBuildNumber = BuildNumber + 1; VersionParts[VersionParts.Length - 1] = NewBuildNumber.ToString(CultureInfo.InvariantCulture); ModifiedVersionString = string.Empty; foreach (string Part in VersionParts) { if (ModifiedVersionString.Length > 0) { ModifiedVersionString += "."; } ModifiedVersionString += Part; } } else { ModifiedVersionString = VersionString; } } else { ModifiedVersionString = VersionString; } versionNumber = ModifiedVersionString; } else { ModifiedVersionString = versionNumber; } string Prefix = string.Empty; while (tabulation > 0) { Prefix += " "; tabulation--; } return($"{Prefix}{tag.TagStart}{ModifiedVersionString}{tag.TagEnd}"); }
private static bool IsVersionLine(string line, VersionTag tag) { return(line.StartsWith(tag.TagStart, StringComparison.InvariantCulture) && line.EndsWith(tag.TagEnd, StringComparison.InvariantCulture)); }