/// <summary> /// Increments the specified version. /// </summary> /// <param name="currentVersion">The current version.</param> /// <param name="buildStartDate">The build start date.</param> /// <param name="projectStartDate">The project start date.</param> /// <returns>The incremented version.</returns> internal StringVersion Increment(StringVersion currentVersion, DateTime buildStartDate, DateTime projectStartDate, SolutionItem solutionItem) { IncrementContext context = new IncrementContext(currentVersion, buildStartDate, projectStartDate, solutionItem.Filename); BaseIncrementor[] incrementors = new BaseIncrementor[] { Major, Minor, Build, Revision }; for (int i = 0; i < 4; i++) { BaseIncrementor incrementor = incrementors[i]; if (incrementor == null) { continue; } VersionComponent component = (VersionComponent)i; incrementor.Increment(context, component); if (!context.Continue) { break; } } return(context.NewVersion); /*int major = Major == null ? currentVersion.Major : Major.Increment(currentVersion.Major, buildStartDate, projectStartDate, solutionItem.Filename); * int minor = Minor == null ? currentVersion.Minor : Minor.Increment(currentVersion.Minor, buildStartDate, projectStartDate, solutionItem.Filename); * int build = Build == null ? currentVersion.Build : Build.Increment(currentVersion.Build, buildStartDate, projectStartDate, solutionItem.Filename); * int revision = Revision == null ? currentVersion.Revision : Revision.Increment(currentVersion.Revision, buildStartDate, projectStartDate, solutionItem.Filename); * * return new Version(major, minor, build, revision);*/ }
internal StringVersion Increment(StringVersion currentVersion, DateTime buildStartDate, DateTime projectStartDate, SolutionItem solutionItem) { IncrementContext incrementContext = new IncrementContext(currentVersion, buildStartDate, projectStartDate, solutionItem.Filename); BaseIncrementor[] array = new BaseIncrementor[] { this.Major, this.Minor, this.Build, this.Revision }; for (int i = 0; i < 4; i++) { BaseIncrementor baseIncrementor = array[i]; if (baseIncrementor != null) { VersionComponent versionComponent = (VersionComponent)i; baseIncrementor.Increment(incrementContext, versionComponent); if (!incrementContext.Continue) { break; } } } return(incrementContext.NewVersion); }
public override bool Equals(object obj) { // If parameter cannot be cast to ThreeDPoint return false: StringVersion sv = obj as StringVersion; if ((object)sv == null) { return(false); } // Return true if the fields match: return(this == sv); }
internal StringVersion Increment(StringVersion currentVersion, DateTime buildStartDate, DateTime projectStartDate, SolutionItem solutionItem) { IncrementContext incrementContext = new IncrementContext(currentVersion, buildStartDate, projectStartDate, solutionItem.Filename); BaseIncrementor[] array = new BaseIncrementor[] { this.Major, this.Minor, this.Build, this.Revision }; for (int i = 0; i < 4; i++) { BaseIncrementor baseIncrementor = array[i]; if (baseIncrementor != null) { VersionComponent versionComponent = (VersionComponent)i; baseIncrementor.Increment(incrementContext, versionComponent); if (!incrementContext.Continue) { break; } } } return incrementContext.NewVersion; }
private void UpdateVersion(SolutionItem solutionItem, string regexPattern, string assemblyFile, string debugAttribute) { var filecontent = File.ReadAllText(assemblyFile); try { const RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase; var m = Regex.Match(filecontent, regexPattern, options); if (!m.Success) { Logger.Write($"Failed to locate attribute \"{debugAttribute}\" in file \"{assemblyFile}\".", LogLevel.Error); return; } var sep = Regex.Match(m.Groups["FullVersion"].Value, "(?<Separator>[\\s,.]+)", options); if (!sep.Success) { Logger.Write( $"Failed to fetch version separator on attribute \"{debugAttribute}\" in file \"{assemblyFile}\".", LogLevel.Error); return; } StringVersion currentVersion; string msg; try { currentVersion = new StringVersion( Regex.Replace(m.Groups["FullVersion"].Value, $"[^\\d{sep.Groups["Separator"].Value}]+", "0") .Replace(sep.Groups["Separator"].Value, ".")); } catch (Exception ex) { msg = $"Error occured while parsing value of {debugAttribute} ({m.Groups["FullVersion"].Value}).\n{ex}"; throw (new Exception(msg, ex)); } var newVersion = solutionItem.IncrementSettings.VersioningStyle.Increment(currentVersion, solutionItem .IncrementSettings .IsUniversalTime ? _buildStartDate .ToUniversalTime() : _buildStartDate, solutionItem .IncrementSettings .StartDate, solutionItem); if (newVersion == currentVersion) { return; } bool success; if (_package.IsCommandLine) { filecontent = filecontent.Remove(m.Groups["FullVersion"].Index, m.Groups["FullVersion"].Length); filecontent = filecontent.Insert(m.Groups["FullVersion"].Index, newVersion.ToString()); try { File.WriteAllText(assemblyFile, filecontent); success = true; } catch (Exception ex) { Logger.Write(ex.Message, LogLevel.Warn); success = false; } } else { var doCloseWindow = !solutionItem.DTE.ItemOperations.IsFileOpen(assemblyFile, null); string replaceWith; if (!solutionItem.IncrementSettings.ReplaceNonNumerics && Regex.IsMatch(m.Groups["FullVersion"].Value, $"[^\\d{sep.Groups["Separator"].Value}]+")) { var mergedVersion = m.Groups["FullVersion"].Value.Replace(sep.Groups["Separator"].Value, ".").Split('.'); if (Regex.IsMatch(mergedVersion[0], "[\\d]+")) { mergedVersion[0] = newVersion.Major; } if (Regex.IsMatch(mergedVersion[1], "[\\d]+")) { mergedVersion[1] = newVersion.Minor; } if (Regex.IsMatch(mergedVersion[2], "[\\d]+")) { mergedVersion[2] = newVersion.Build; } if (Regex.IsMatch(mergedVersion[3], "[\\d]+")) { mergedVersion[3] = newVersion.Revision; } // ReSharper disable once CoVariantArrayConversion replaceWith = m.Value.Replace(m.Groups["FullVersion"].Value, string.Format("{0}.{1}.{2}.{3}", mergedVersion) .Replace(".", sep.Groups["Separator"].Value)); } else { replaceWith = m.Value.Replace(m.Groups["FullVersion"].Value, newVersion.ToString(4) .Replace(".", sep.Groups["Separator"].Value)); } var projectItem = DTE.Solution.FindProjectItem(assemblyFile); if (projectItem == null) { throw (new ApplicationException($"Failed to find project item \"{assemblyFile}\".")); } //var doc = projectItem.Document; var window = projectItem.Open(EnvDTE.Constants.vsViewKindTextView); if (window == null) { throw (new ApplicationException("Could not open project item.")); } var doc = window.Document; if (doc == null) { throw (new ApplicationException("Located project item but no document.")); } success = doc.ReplaceText(m.Value, replaceWith); if (doCloseWindow) { window.Close(vsSaveChanges.vsSaveChangesYes); } else { doc.Save(assemblyFile); } } msg = $"{solutionItem.Name} {debugAttribute}: {newVersion}"; if (success) { msg += " [SUCCESS]"; } else { msg += " [FAILED]"; } Logger.Write(msg); } catch (Exception ex) { Logger.Write($"Error occured while updating version.\n{ex}", LogLevel.Error); } }
private void UpdateVersion(SolutionItem solutionItem, string regexPattern, string assemblyFile, string debugAttribute) { string text = File.ReadAllText(assemblyFile); try { RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace; Match match = Regex.Match(text, regexPattern, options); if (!match.Success) { Logger.Write(string.Concat(new string[] { "Failed to locate attribute \"", debugAttribute, "\" in file \"", assemblyFile, "\"." }), LogLevel.Error, assemblyFile, 1); } else { Match match2 = Regex.Match(match.Groups["FullVersion"].Value, "(?<Separator>[\\s,.]+)", options); if (!match2.Success) { Logger.Write(string.Concat(new string[] { "Failed to fetch version separator on attribute \"", debugAttribute, "\" in file \"", assemblyFile, "\"." }), LogLevel.Error, assemblyFile, 2); } else { StringVersion stringVersion = null; try { stringVersion = new StringVersion(Regex.Replace(match.Groups["FullVersion"].Value, "[^\\d" + match2.Groups["Separator"].Value + "]+", "0").Replace(match2.Groups["Separator"].Value, ".")); } catch (Exception ex) { string text2 = string.Format("Error occured while parsing value of {0} ({1}).\n{2}", debugAttribute, match.Groups["FullVersion"].Value, ex); throw new Exception(text2, ex); } StringVersion stringVersion2 = solutionItem.IncrementSettings.VersioningStyle.Increment(stringVersion, solutionItem.IncrementSettings.IsUniversalTime ? this._buildStartDate.ToUniversalTime() : this._buildStartDate, solutionItem.IncrementSettings.StartDate, solutionItem); if (stringVersion2 != stringVersion) { bool flag = false; if (this._connect.IsCommandLineBuild) { text = text.Remove(match.Groups["FullVersion"].Index, match.Groups["FullVersion"].Length); text = text.Insert(match.Groups["FullVersion"].Index, stringVersion2.ToString()); try { File.WriteAllText(assemblyFile, text); flag = true; } catch (Exception ex) { Logger.Write(ex.Message, LogLevel.Warning); flag = false; } } else { bool flag2 = !solutionItem.DTE.ItemOperations.IsFileOpen(assemblyFile, null); string replaceText = string.Empty; if (!solutionItem.IncrementSettings.ReplaceNonNumerics && Regex.IsMatch(match.Groups["FullVersion"].Value, "[^\\d" + match2.Groups["Separator"].Value + "]+")) { string[] array = match.Groups["FullVersion"].Value.Replace(match2.Groups["Separator"].Value, ".").Split(new char[] { '.' }); if (Regex.IsMatch(array[0], "[\\d]+")) { array[0] = stringVersion2.Major; } if (Regex.IsMatch(array[1], "[\\d]+")) { array[1] = stringVersion2.Minor; } if (Regex.IsMatch(array[2], "[\\d]+")) { array[2] = stringVersion2.Build; } if (Regex.IsMatch(array[3], "[\\d]+")) { array[3] = stringVersion2.Revision; } replaceText = match.Value.Replace(match.Groups["FullVersion"].Value, string.Format("{0}.{1}.{2}.{3}", array).Replace(".", match2.Groups["Separator"].Value)); } else { replaceText = match.Value.Replace(match.Groups["FullVersion"].Value, stringVersion2.ToString(4).Replace(".", match2.Groups["Separator"].Value)); } ProjectItem projectItem = this._connect.ApplicationObject.Solution.FindProjectItem(assemblyFile); if (projectItem == null) { throw new ApplicationException("Failed to find project item \"" + assemblyFile + "\"."); } Window window = projectItem.Open("{7651A703-06E5-11D1-8EBD-00A0C90F26EA}"); if (window == null) { throw new ApplicationException("Could not open project item."); } Document document = window.Document; if (document == null) { throw new ApplicationException("Located project item & window but no document."); } flag = document.ReplaceText(match.Value, replaceText, 0); if (flag2) { window.Close(vsSaveChanges.vsSaveChangesYes); } else { document.Save(assemblyFile); } } string text2 = string.Concat(new object[] { solutionItem.Name, " ", debugAttribute, ": ", stringVersion2 }); if (flag) { text2 += " [SUCCESS]"; } else { text2 += " [FAILED]"; } Logger.Write(text2, LogLevel.Info); } } } } catch (Exception ex) { Logger.Write("Error occured while updating version.\n" + ex.ToString(), LogLevel.Error, assemblyFile, 1); } }
public override bool Equals(object obj) { StringVersion stringVersion = obj as StringVersion; return(stringVersion != null && this == stringVersion); }
private void UpdateVersion(SolutionItem solutionItem, string regexPattern, string assemblyFile, string debugAttribute) { string filecontent = File.ReadAllText(assemblyFile); try { RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase; Match m = Regex.Match(filecontent, regexPattern, options); if (!m.Success) { Logger.Write("Failed to locate attribute \"" + debugAttribute + "\" in file \"" + assemblyFile + "\".", LogLevel.Error, assemblyFile, 1); return; } Match sep = Regex.Match(m.Groups["FullVersion"].Value, "(?<Separator>[\\s,.]+)", options); if (!sep.Success) { Logger.Write("Failed to fetch version separator on attribute \"" + debugAttribute + "\" in file \"" + assemblyFile + "\".", LogLevel.Error, assemblyFile, 2); return; } StringVersion currentVersion = null; try { // Make sure we don't have anything what is not a number in the version currentVersion = new StringVersion(Regex.Replace(m.Groups["FullVersion"].Value, "[^\\d" + sep.Groups["Separator"].Value + "]+", "0").Replace(sep.Groups["Separator"].Value, ".")); } catch (Exception ex) { string msg = string.Format("Error occured while parsing value of {0} ({1}).\n{2}", debugAttribute, m.Groups["FullVersion"].Value, ex); throw (new Exception(msg, ex)); } StringVersion newVersion = solutionItem.IncrementSettings.VersioningStyle.Increment(currentVersion, solutionItem.IncrementSettings.IsUniversalTime ? _buildStartDate.ToUniversalTime() : _buildStartDate, solutionItem.IncrementSettings.StartDate, solutionItem); if (newVersion != currentVersion) { bool success = false; if (_connect.IsCommandLineBuild) { filecontent = filecontent.Remove(m.Groups["FullVersion"].Index, m.Groups["FullVersion"].Length); filecontent = filecontent.Insert(m.Groups["FullVersion"].Index, newVersion.ToString()); try { File.WriteAllText(assemblyFile, filecontent); success = true; } catch (Exception ex) { Logger.Write(ex.Message, LogLevel.Warning); success = false; } } else { bool doCloseWindow = !solutionItem.DTE.ItemOperations.IsFileOpen(assemblyFile, null); string replaceWith = string.Empty; if (!solutionItem.IncrementSettings.ReplaceNonNumerics && Regex.IsMatch(m.Groups["FullVersion"].Value, "[^\\d" + sep.Groups["Separator"].Value + "]+")) { //the version number is not only pure numbers...Thread 77828 from philjones88 string[] mergedVersion = m.Groups["FullVersion"].Value.Replace(sep.Groups["Separator"].Value, ".").Split('.'); if (Regex.IsMatch(mergedVersion[0], "[\\d]+")) { mergedVersion[0] = newVersion.Major; } if (Regex.IsMatch(mergedVersion[1], "[\\d]+")) { mergedVersion[1] = newVersion.Minor; } if (Regex.IsMatch(mergedVersion[2], "[\\d]+")) { mergedVersion[2] = newVersion.Build; } if (Regex.IsMatch(mergedVersion[3], "[\\d]+")) { mergedVersion[3] = newVersion.Revision; } replaceWith = m.Value.Replace(m.Groups["FullVersion"].Value, String.Format("{0}.{1}.{2}.{3}", mergedVersion).Replace(".", sep.Groups["Separator"].Value)); } else { replaceWith = m.Value.Replace(m.Groups["FullVersion"].Value, newVersion.ToString(4).Replace(".", sep.Groups["Separator"].Value)); } ProjectItem projectItem = _connect.ApplicationObject.Solution.FindProjectItem(assemblyFile); if (projectItem == null) { throw (new ApplicationException("Failed to find project item \"" + assemblyFile + "\".")); } Window window = projectItem.Open(Constants.vsViewKindTextView); if (window == null) { throw (new ApplicationException("Could not open project item.")); } Document doc = window.Document; if (doc == null) { throw (new ApplicationException("Located project item & window but no document.")); } success = doc.ReplaceText(m.Value, replaceWith, 0); if (doCloseWindow) { window.Close(vsSaveChanges.vsSaveChangesYes); } else { doc.Save(assemblyFile); } } #if OLDSTUFF filecontent = filecontent.Remove(m.Groups["FullVersion"].Index, m.Groups["FullVersion"].Length); filecontent = filecontent.Insert(m.Groups["FullVersion"].Index, newVersion.ToString()); success = WriteFileContent(filename, filecontent); #endif string msg = solutionItem.Name + " " + debugAttribute + ": " + newVersion; if (success) { msg += " [SUCCESS]"; } else { msg += " [FAILED]"; } Logger.Write(msg, LogLevel.Info); } } catch (Exception ex) { Logger.Write("Error occured while updating version.\n" + ex.ToString(), LogLevel.Error, assemblyFile, 1); } }