public static class VersionInfo { public const string VersionString = "1.0.3"; }
[assembly: AssemblyVersion(VersionInfo.VersionString)] [assembly: AssemblyFileVersion(VersionInfo.VersionString)]
<ApplicationVersion>1.0.3.%2a</ApplicationVersion>
public class PublishVersionSyncTask : Task { [Required] public string ProjectFilePath { get; set; } [Required] public string VersionStringFilePath { get; set; } [Output] public string Error { get { return this._error; } set { this._error = value; } } string _error; public override bool Execute() { if(!File.Exists(ProjectFilePath)) { Error = $"Project File \"{ProjectFilePath}\" does not exists"; return true; } if(!File.Exists(VersionStringFilePath)) { Error = $"Version File \"{VersionStringFilePath}\" does not exists"; return true; } string versionString = null; var allCodeLines = File.ReadAllLines(VersionStringFilePath); foreach(var codeLine in allCodeLines) { if(codeLine.Contains("VersionString")) { versionString = codeLine.Split('"').Where(s => s.Contains('.')).FirstOrDefault(); break; } } if(String.IsNullOrEmpty(versionString)) { Error = "Can not find version string."; return true; } if(versionString.Split('.').Length != 3) { Error = $"Version string has wrong format: {versionString}. It must be xyz"; return true; } allCodeLines = File.ReadAllLines(ProjectFilePath); List<string> fixedCodeLines = new List<string>(); foreach(var codeLine in allCodeLines) { if(!codeLine.Contains("<ApplicationVersion>")) { fixedCodeLines.Add(codeLine); continue; } if(codeLine.Contains(versionString)) return true; fixedCodeLines.Add($" <ApplicationVersion>{versionString}.%2a</ApplicationVersion>"); } try { if(File.Exists(ProjectFilePath + ".bak")) File.Delete(ProjectFilePath + ".bak"); } catch { Error = $"Can not delete {ProjectFilePath}.bak"; return true; } File.Copy(ProjectFilePath, ProjectFilePath + ".bak"); try { File.Delete(ProjectFilePath); } catch { File.Delete(ProjectFilePath + ".bak"); Error = $"Can not delete {ProjectFilePath}"; return true; } File.WriteAllLines(ProjectFilePath, fixedCodeLines); File.Delete(ProjectFilePath + ".bak"); return true; } }
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <UsingTask TaskName="PublishVersionSynchronizer.PublishVersionSyncTask" AssemblyFile="$(TargetDir)\..\..\lib\PublishVersionSynchronizer\PublishVersionSynchronizer.dll"/> <PropertyGroup> <BuildDependsOn> PublishVersionSync; BeforeBuild; CoreBuild; AfterBuild </BuildDependsOn> </PropertyGroup> <Target Name="PublishVersionSync"> <PublishVersionSyncTask ProjectFilePath="$(MSBuildProjectFullPath)" VersionStringFilePath="$(MSBuildProjectDirectory)\Config\VersionInfo.cs"> <Output PropertyName="ErrorMessage" TaskParameter="Error" /> </PublishVersionSyncTask> <Message Text="(out) Publish version patched" Condition="'$(ErrorMessage)' == ''"/> <Error Condition="'$(ErrorMessage)' != ''" Text="$(ErrorMessage)" /> </Target> </Project>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /><!-- --> <Import Project="PublishVersionSynchronizer.targets" />
Source: https://habr.com/ru/post/345390/