|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using Nuke.Common.IO; |
| 4 | +using Serilog; |
| 5 | + |
| 6 | +internal static class AppVersionTask |
| 7 | +{ |
| 8 | + internal static void SetAppVersion(AbsolutePath rootDirectory) |
| 9 | + { |
| 10 | + string[] appVersionNumberAppContent = GetAppVersionNumberFileContent(rootDirectory); |
| 11 | + string appVersion = GetAppVersion(appVersionNumberAppContent); |
| 12 | + string sdkVersion = GetSdkVersion(appVersionNumberAppContent); |
| 13 | + |
| 14 | + var csharpUpdater = new CSharpUpdater(appVersion, sdkVersion); |
| 15 | + IReadOnlyCollection<AbsolutePath> assemblyVersionFiles |
| 16 | + = rootDirectory.GlobFiles("**/*AssemblyVersion.cs"); |
| 17 | + foreach (AbsolutePath file in assemblyVersionFiles) |
| 18 | + { |
| 19 | + Log.Information("Updating app version in {File}...", file); |
| 20 | + csharpUpdater.UpdateFile(file); |
| 21 | + } |
| 22 | + |
| 23 | + var projectUpdater = new ProjectUpdater(sdkVersion); |
| 24 | + IReadOnlyCollection<AbsolutePath> projectFiles |
| 25 | + = rootDirectory.GlobFiles("**/*DevToys.Api.csproj"); |
| 26 | + foreach (AbsolutePath file in projectFiles) |
| 27 | + { |
| 28 | + Log.Information("Updating project version in {File}...", file); |
| 29 | + projectUpdater.UpdateFile(file); |
| 30 | + } |
| 31 | + |
| 32 | + var appxManifestUpdater = new AppxManifestUpdater(appVersion); |
| 33 | + IReadOnlyCollection<AbsolutePath> appxmanifestFiles |
| 34 | + = rootDirectory.GlobFiles("**/*.appxmanifest"); |
| 35 | + foreach (AbsolutePath file in appxmanifestFiles) |
| 36 | + { |
| 37 | + Log.Information("Updating app version in {File}...", file); |
| 38 | + appxManifestUpdater.UpdateFile(file); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private static string[] GetAppVersionNumberFileContent(AbsolutePath rootDirectory) |
| 43 | + { |
| 44 | + AbsolutePath appVersionNumberFile = rootDirectory / "tools" / "app-version-number.txt"; |
| 45 | + if (!appVersionNumberFile.FileExists()) |
| 46 | + { |
| 47 | + Log.Error("Unable to find the app version number in {AppVersionNumberFile}...", appVersionNumberFile); |
| 48 | + throw new FileNotFoundException("Unable to find the app version number file.", appVersionNumberFile.ToString()); |
| 49 | + } |
| 50 | + |
| 51 | + return File.ReadAllLines(appVersionNumberFile); |
| 52 | + } |
| 53 | + |
| 54 | + private static string GetAppVersion(string[] appVersionNumberAppContent) |
| 55 | + { |
| 56 | + return GetVersion(appVersionNumberAppContent, "app"); |
| 57 | + } |
| 58 | + |
| 59 | + private static string GetSdkVersion(string[] appVersionNumberAppContent) |
| 60 | + { |
| 61 | + return GetVersion(appVersionNumberAppContent, "sdk"); |
| 62 | + } |
| 63 | + |
| 64 | + private static string GetVersion(string[] appVersionNumberAppContent, string name) |
| 65 | + { |
| 66 | + for (int i = 0; i < appVersionNumberAppContent.Length; i++) |
| 67 | + { |
| 68 | + if (appVersionNumberAppContent[i].StartsWith(name + ":")) |
| 69 | + { |
| 70 | + return appVersionNumberAppContent[i].Substring(name.Length + 1); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return string.Empty; |
| 75 | + } |
| 76 | +} |
0 commit comments