//public static void SetProjectVersionInfo3(string projectPath, string configuration) { // var autoVersion = new AutoVersionTask() { // ProjectPath = projectPath, // Configuration = configuration // }; // autoVersion.Execute(); //} //public static void SetProjectVersionInfo_Buildalyzer(string projectPath) { // var manager = new AnalyzerManager(); // var projectAnalyzer = manager.GetProject(projectPath); // var analyzerResults = projectAnalyzer.Build().Results; // var result = analyzerResults.First(); //} public static void SetProjectVersionInfo(string projectPath) { var project = _Extensions.GetProject(projectPath); var configuration = project.GetProperty("$(Configuration)").EvaluatedValue; var newVersion = new CsProjectVersion(); newVersion.BumpPrefixAndSuffix(DateTime.UtcNow, configuration); newVersion.VersionPart1 = project.GetProperty(nameof(newVersion.VersionPart1)).EvaluatedValue.As(newVersion.VersionPart1); newVersion.VersionPart2 = project.GetProperty(nameof(newVersion.VersionPart2)).EvaluatedValue.As(newVersion.VersionPart2); newVersion.VersionPart3 = project.GetProperty(nameof(newVersion.VersionPart3)).EvaluatedValue.As(newVersion.VersionPart3); newVersion.VersionPart4 = project.GetProperty(nameof(newVersion.VersionPart4)).EvaluatedValue.As(newVersion.VersionPart4); project.SetProperty(VersionPrefix, newVersion.VersionSuffix); project.SetProperty(VersionSuffix, newVersion.VersionSuffix); // Select Debug configuration var debugPropertyGroup = project.GetPropertyGroupDebug(); debugPropertyGroup.SetProperty(VersionSuffix, newVersion.VersionSuffix); // Select Release configuration var releasePropertyGroup = project.GetPropertyGroupRelease(); releasePropertyGroup.SetProperty(VersionSuffix, string.Empty); //Save project.Save(); }
public bool TryAutoVersion(XDocument xmlProject) { var defaultNamespace = xmlProject.Root.GetDefaultNamespace(); var defaultNamespacePrefix = "ns"; var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); xmlNamespaceManager.AddNamespace(defaultNamespacePrefix, defaultNamespace.NamespaceName); XElement GetPropertyGroupElement(string xElementName) { return(xmlProject.Root.XPathSelectElement($"{defaultNamespacePrefix}:PropertyGroup/{defaultNamespacePrefix}:{xElementName}", xmlNamespaceManager)); } var oldVersion = new CsProjectVersion( GetPropertyGroupElement(Program.VersionPart1)?.Value ?? string.Empty, GetPropertyGroupElement(Program.VersionPart2)?.Value ?? string.Empty, GetPropertyGroupElement(Program.VersionPart3)?.Value ?? string.Empty, GetPropertyGroupElement(Program.VersionPart4)?.Value ?? string.Empty ); var tagName = "Version"; Console.WriteLine($"Old {tagName} is {oldVersion.Version}"); //Log.LogMessage(MessageImportance.Low, $"Old {Program.VersionSuffix} is {elementVersionSuffix.Value}"); //int? GetNewVersion(BumpType bumpType, int? oldValue, int? newValue = null) { // switch (bumpType) { // case BumpType.Bump: return oldValue + 1; // case BumpType.CustomValue: return newValue; // case BumpType.DateTime_ddHH: return NewVersionDate.ddHH(); // case BumpType.DateTime_mmss: return NewVersionDate.mmss(); // case BumpType.DateTime_YYMM: return NewVersionDate.YYMM(); // case BumpType.Reset: return 0; // } // return oldValue; //} var newVersion = oldVersion; newVersion.BumpVersionPart3(NewVersionDate); newVersion.BumpVersionPart4(NewVersionDate); newVersion.BumpVersionSuffix(NewVersionDate, Configuration); //newVersion.VersionPart1 = GetNewVersion(settings.BumpTypeVersionPart1, newVersion.VersionPart1, settings.NewVersionPart1).Value; //newVersion.VersionPart2 = GetNewVersion(settings.BumpTypeVersionPart2, newVersion.VersionPart2, settings.NewVersionPart2).Value; //newVersion.VersionPart3 = GetNewVersion(settings.BumpTypeVersionPart3, newVersion.VersionPart3, settings.NewVersionPart3).Value; //newVersion.VersionPart4 = GetNewVersion(settings.BumpTypeVersionPart4, newVersion.VersionPart4, settings.NewVersionPart4); Console.WriteLine($"New {tagName} is {newVersion.Version}"); if (newVersion.Version != oldVersion.Version) { Console.WriteLine($"Changing {tagName} to {newVersion.Version}..."); GetPropertyGroupElement(Program.VersionPrefix).Value = newVersion.VersionPrefix; GetPropertyGroupElement(Program.VersionSuffix).Value = newVersion.VersionSuffix; GetRequiredPropertyInfo("New" + tagName).SetValue(this, newVersion.Version); return(true); } return(false); }