From f2a13d98a2f15e6062448ef4158f3fdb71de60b9 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Tue, 2 May 2023 13:27:51 +0800 Subject: [PATCH] Fsdk/Misc: support Before this commit, the only way to get the current version of a project was to have AssemblyInfo files with the corresponding attributes, but that approach is very .NET4.x (legacy), so this MSBuild-based one is more in line with the times. --- Fsdk/Fsdk-legacy.fsproj | 8 +++++ Fsdk/Fsdk.fsproj | 5 +++ Fsdk/Misc.fs | 80 ++++++++++++++++++++++++++--------------- Fsdk/packages.config | 1 + 4 files changed, 66 insertions(+), 28 deletions(-) diff --git a/Fsdk/Fsdk-legacy.fsproj b/Fsdk/Fsdk-legacy.fsproj index 712c18e9..a3b0d6f8 100644 --- a/Fsdk/Fsdk-legacy.fsproj +++ b/Fsdk/Fsdk-legacy.fsproj @@ -48,6 +48,11 @@ + + + + ..\packages\System.Xml.XDocument.4.3.0\ref\netstandard1.0\System.Xml.XDocument.dll + @@ -59,6 +64,9 @@ + + + diff --git a/Fsdk/Fsdk.fsproj b/Fsdk/Fsdk.fsproj index 76cacca5..480569cd 100644 --- a/Fsdk/Fsdk.fsproj +++ b/Fsdk/Fsdk.fsproj @@ -46,6 +46,11 @@ /> + + + + + diff --git a/Fsdk/Misc.fs b/Fsdk/Misc.fs index 053e07ec..70292ca9 100644 --- a/Fsdk/Misc.fs +++ b/Fsdk/Misc.fs @@ -5,6 +5,9 @@ open System.IO open System.Text open System.Reflection open System.Linq +open System.Xml +open System.Xml.Linq +open System.Xml.XPath #if LEGACY_FRAMEWORK open System.Configuration open System.Security.Cryptography @@ -636,34 +639,8 @@ module Misc = not(String.IsNullOrEmpty(gitlabUserEmail)) - let GetCurrentVersion(dir: DirectoryInfo) : Version = - let defaultAssemblyVersionFileName = "AssemblyInfo.fs" - - let assemblyVersionFsFiles = - (Directory.EnumerateFiles( - dir.FullName, - defaultAssemblyVersionFileName, - SearchOption.AllDirectories - )) - - let assemblyVersionFsFile = - if assemblyVersionFsFiles.Count() = 1 then - assemblyVersionFsFiles.Single() - else - (Directory.EnumerateFiles( - dir.FullName, - "Common" + defaultAssemblyVersionFileName, - SearchOption.AllDirectories - )) - .SingleOrDefault() - - if assemblyVersionFsFile = null then - Console.Error.WriteLine( - "Canonical AssemblyInfo not found in any subfolder (or found too many), cannot extract version number" - ) - - Environment.Exit 1 - + // TODO: move this as nested func of GetCurrentVersion() + let private GetCurrentVersionFromAssemblyInfos(assemblyVersionFsFile: string) : Version = let assemblyVersionAttribute = "AssemblyVersion" let lineContainingVersionNumber = @@ -713,6 +690,53 @@ module Misc = Version(version) + let GetCurrentVersion(dir: DirectoryInfo) : Version = + let defaultAssemblyVersionFileName = "AssemblyInfo.fs" + + let assemblyVersionFsFiles = + (Directory.EnumerateFiles( + dir.FullName, + defaultAssemblyVersionFileName, + SearchOption.AllDirectories + )) + + let assemblyVersionFsFile = + if assemblyVersionFsFiles.Count() = 1 then + assemblyVersionFsFiles.Single() + else + (Directory.EnumerateFiles( + dir.FullName, + "Common" + defaultAssemblyVersionFileName, + SearchOption.AllDirectories + )) + .SingleOrDefault() + + if not (isNull assemblyVersionFsFile) then + GetCurrentVersionFromAssemblyInfos assemblyVersionFsFile + else + let projectFiles = + (Directory.EnumerateFiles( + dir.FullName, + "*.*proj", + SearchOption.AllDirectories + )) + let versions = + seq { + for projFile in projectFiles do + let xmlDoc = XDocument.Load projFile + let query = "//ApplicationVersion" + let appVersionElements = xmlDoc.XPathSelectElements query + for appVersionElement in appVersionElements do + yield appVersionElement.Value + } |> Seq.distinct + if Seq.isEmpty versions then + failwithf "No tag found in project files" + match Seq.tryExactlyOne versions with + | None -> + let versionsStr = String.Join(",", versions) + failwithf "More than one tag found in project files: %s" versionsStr + | Some version -> + Version version let SqlReadOnlyGroupName = "sqlreadonly" diff --git a/Fsdk/packages.config b/Fsdk/packages.config index cc622f22..364e5929 100644 --- a/Fsdk/packages.config +++ b/Fsdk/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file