private bool CheckAndUpdate(string fl, Minimatcher assemblyMM, string versionValue, Action <string, string> p) { Bilge.Assert(p != null, "The action used cant be null"); Bilge.VerboseLog("Checking file :" + fl); bool result = assemblyMM.IsMatch(fl); if ((result) && (File.Exists(fl))) { Bilge.Log($"Updating VersioningFile File ({fl}) to ({versionValue})"); hook?.PreUpdateFileAction(fl); // PreUpdateAllAction?.Invoke(fl); //PreUpdateAction?.Invoke(fl); p(fl, versionValue); hook?.PostUpdateFileAction(fl); } return(result); }
/// <summary> /// Modifies the versioning digit using the behaviours rule and information as to whether the next most significant digit /// has changed. /// </summary> /// <param name="higherDigitChanged">Wether or not the next significant digit is changed (required for some behaviours)</param> /// <param name="anyHigherDigitChanged">Whether any of the more significant digits have changed</param> /// <param name="baseDate">A date to work from when date based version digits are used</param> /// <param name="lastBuildDate">The date of the last build, for when digits reset on day rollovers</param> /// <returns>Returns true if the digit changed during the increment</returns> internal bool PerformIncrement(bool higherDigitChanged, bool anyHigherDigitChanged, DateTime lastBuildDate, DateTime baseDate) { #region entry code if (higherDigitChanged) { Bilge.Assert(anyHigherDigitChanged, "Logic error on changed digits"); } #endregion Bilge.VerboseLog($"PerformIncrement Higher {higherDigitChanged} Any {anyHigherDigitChanged} Val {Value} using {Behaviour.ToString()}"); if (!string.IsNullOrEmpty(IncrementOverride)) { Bilge.VerboseLog($"Override Value Present {IncrementOverride} - All Other considerations ignored."); // An override overrules anything else - even fixed. if (IncrementOverride != actualValue) { actualValue = IncrementOverride; return(true); } else { return(false); } } if (Behaviour == VersionIncrementBehaviour.Fixed) { Bilge.VerboseLog("Behaviour Set to Fixed, not doing anything."); return(false); } TimeSpan ts; int versionPriorToIncrement = int.Parse(Value); Bilge.VerboseLog("No override, moving to perform increment"); //unchecked to make it explicit that an overflow wraps around. unchecked { switch (Behaviour) { case VersionIncrementBehaviour.DailyAutoIncrement: if (DateTime.Today == lastBuildDate) { versionPriorToIncrement++; } else { versionPriorToIncrement = 0; } break; case VersionIncrementBehaviour.DaysSinceDate: ts = DateTime.Now - baseDate; versionPriorToIncrement = (int)ts.TotalDays; break; case VersionIncrementBehaviour.WeeksSinceDate: ts = DateTime.Now - baseDate; versionPriorToIncrement = (int)(ts.TotalDays / DAYS_IN_A_WEEK); break; case VersionIncrementBehaviour.AutoIncrementWithReset: if (higherDigitChanged) { versionPriorToIncrement = 0; } else { versionPriorToIncrement++; } break; case VersionIncrementBehaviour.AutoIncrementWithResetAny: if (anyHigherDigitChanged) { versionPriorToIncrement = 0; } else { versionPriorToIncrement++; } break; case VersionIncrementBehaviour.ContinualIncrement: versionPriorToIncrement++; break; } } // Code change to move from uints to ints means that an overflow can create a negative version. This code resets // the value back to zero if an overflow has caused a negative number. if (versionPriorToIncrement < 0) { versionPriorToIncrement = 0; } string tstr = versionPriorToIncrement.ToString(); if (Value != tstr) { Value = tstr; return(true); } return(false); }
/// <summary> /// Either updates an existing version number in a file or creates a new (very basic) assembly info file and adds the verison number to it. The /// version is stored in the attribute that is supplied as the second parameter. /// </summary> /// <param name="fileName">The full path to the file to either update or create</param> /// <param name="targetAttribute">The name of the attribute to write the verison number into</param> /// <param name="vn">The verison number to apply to the code</param> private void UpdateCSFileWithAttribute(string fileName, string targetAttribute, string versionValue) { #region entry code Bilge.Assert(!string.IsNullOrEmpty(fileName), "fileName is null, internal consistancy error."); Bilge.Assert(!string.IsNullOrEmpty(targetAttribute), "target attribute cant be null, internal consistancy error"); Bilge.Assert(versionValue != null, "vn cant be null, internal consistancy error"); #endregion (entry code) Bilge.Log(string.Format("VersionSupport, Asked to update CS file with the {0} attribute", targetAttribute), "Full Filename:" + fileName); var outputFile = new StringBuilder(); if (!File.Exists(fileName)) { Bilge.VerboseLog("There was no file, creating file and adding attribute"); outputFile.Append("using System.Reflection;\r\n"); outputFile.Append($"[assembly: {targetAttribute}(\"{versionValue}\")]\r\n"); } else { // If it does exist we need to verify that it is not readonly. if ((File.GetAttributes(fileName) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { Bilge.Warning("The file is readonly, removing attribs so I can write on it", "fname [" + fileName + "]"); File.SetAttributes(fileName, (File.GetAttributes(fileName) ^ FileAttributes.ReadOnly)); } // Put this in to identify if there were duplicate entries discovered in the file, this should not be valid but helps to reassure that its not the verisoner that has // introduced a compile error into the code. bool replacementMade = false; /* * Regex r = new Regex("\\[\\s*assembly\\s*:\\s*" + targetAttribute + "\\s*\\(\\s*\\\"\\s*[0-9*]+.[0-9*]+.[0-9*]+.[0-9*]+\\s*\\\"\\s*\\)\\s*\\]", RegexOptions.IgnoreCase); */ Regex r = GetRegex(targetAttribute); using (StreamReader sr = new StreamReader(fileName)) { string nextLine = null; while ((nextLine = sr.ReadLine()) != null) { if ((!nextLine.Trim().StartsWith("//")) && (r.IsMatch(nextLine))) { if (replacementMade) { // One would hope that this would not occur outside of testing, yet surprisingly enough this is not the case. throw new ArgumentException("Invalid CSharp File, duplicate verison attribute discovered", fileName); } // its the line we are to replace outputFile.Append("[assembly: " + targetAttribute + "(\""); outputFile.Append(versionValue); outputFile.Append("\")]\r\n"); replacementMade = true; } else { // All lines except the one we are interested in are copied across. outputFile.Append(nextLine + "\r\n"); } } if (!replacementMade) { Bilge.Warning("No " + targetAttribute + " found in file, appending new one."); outputFile.Append($"\r\n[assembly: {targetAttribute}(\"{versionValue}\")]\r\n"); } } } File.WriteAllText(fileName, outputFile.ToString(), Encoding.UTF8); Bilge.Log("The attribute " + targetAttribute + " was applied to the file " + fileName + " Successfully."); }