From b7df5958f2ae988f747760ac46d6bb4cb088d15d Mon Sep 17 00:00:00 2001 From: Andrii Chebukin Date: Fri, 22 Nov 2024 20:11:59 +0400 Subject: [PATCH] ci: fix retrieving version for publishing to GitHub also extract helpers to a separate file --- build/Changelog.fs | 17 +++++++------ build/Helpers.fs | 63 ++++++++++++++++++++++++++++++++++++++++++++++ build/build.fs | 58 +----------------------------------------- build/build.fsproj | 1 + 4 files changed, 75 insertions(+), 64 deletions(-) create mode 100644 build/Helpers.fs diff --git a/build/Changelog.fs b/build/Changelog.fs index ada09c5..4c135ee 100644 --- a/build/Changelog.fs +++ b/build/Changelog.fs @@ -3,6 +3,7 @@ module Changelog open System open Fake.Core open Fake.IO +open Helpers let isEmptyChange = function @@ -62,10 +63,9 @@ let mkReleaseNotes changelog (latestEntry : Changelog.ChangelogEntry) gitHubRepo { latestEntry with Description = Some description }.ToString () let getVersionNumber envVarName ctx = - let args = ctx.Context.Arguments let verArg = - args + ctx.Context.Arguments |> List.tryHead |> Option.defaultWith (fun () -> Environment.environVarOrDefault envVarName "") @@ -100,21 +100,24 @@ let mutable changelogBackupFilename = "" let updateChangelog changelogPath (changelog : Fake.Core.Changelog.Changelog) gitHubRepoUrl ctx = - let verStr = ctx |> getVersionNumber "RELEASE_VERSION" + let newVersion = + if isPublishToGitHub ctx then + changelog.LatestEntry.SemVer + else + ctx |> getVersionNumber "RELEASE_VERSION" |> SemVer.parse + let description, unreleasedChanges = match changelog.Unreleased with | None -> None, [] | Some u -> u.Description, u.Changes - let newVersion = SemVer.parse verStr - changelog.Entries |> List.tryFind (fun entry -> entry.SemVer = newVersion) |> Option.iter (fun entry -> Trace.traceErrorfn "Version %s already exists in %s, released on %s" - verStr + newVersion.AsString changelogPath (if entry.Date.IsSome then entry.Date.Value.ToString ("yyyy-MM-dd") @@ -128,7 +131,7 @@ let updateChangelog changelogPath (changelog : Fake.Core.Changelog.Changelog) gi |> Option.iter (fun entry -> Trace.traceErrorfn "You're trying to release version %s, but a later version %s already exists, released on %s" - verStr + newVersion.AsString entry.SemVer.AsString (if entry.Date.IsSome then entry.Date.Value.ToString ("yyyy-MM-dd") diff --git a/build/Helpers.fs b/build/Helpers.fs new file mode 100644 index 0000000..fd16058 --- /dev/null +++ b/build/Helpers.fs @@ -0,0 +1,63 @@ +module Helpers + +open System +open Fake.Core +open Fake.DotNet +open Fake.Tools + +let releaseBranch = "main" + +let environVarAsBoolOrDefault varName defaultValue = + let truthyConsts = [ "1"; "Y"; "YES"; "T"; "TRUE" ] + Environment.environVar varName + |> ValueOption.ofObj + |> ValueOption.map (fun envvar -> + truthyConsts + |> List.exists (fun ``const`` -> String.Equals (``const``, envvar, StringComparison.InvariantCultureIgnoreCase))) + |> ValueOption.defaultValue defaultValue + +let isRelease (targets : Target list) = + targets + |> Seq.map (fun t -> t.Name) + |> Seq.exists ((=) "PublishToNuGet") + +let invokeAsync f = async { f () } + +let configuration (targets : Target list) = + let defaultVal = if isRelease targets then "Release" else "Debug" + + match Environment.environVarOrDefault "CONFIGURATION" defaultVal with + | "Debug" -> DotNet.BuildConfiguration.Debug + | "Release" -> DotNet.BuildConfiguration.Release + | config -> DotNet.BuildConfiguration.Custom config + +let failOnBadExitAndPrint (p : ProcessResult) = + if p.ExitCode <> 0 then + p.Errors |> Seq.iter Trace.traceError + + failwithf "failed with exitcode %d" p.ExitCode + +let isPublishToGitHub ctx = ctx.Context.FinalTarget = "PublishToGitHub" + +type TargetParameter with + + member ctx.IsPublishToGitHub = isPublishToGitHub ctx + +let isCI = lazy environVarAsBoolOrDefault "CI" false + +// CI Servers can have bizarre failures that have nothing to do with your code +let rec retryIfInCI times fn = + match isCI.Value with + | true -> + if times > 1 then + try + fn () + with _ -> + retryIfInCI (times - 1) fn + else + fn () + | _ -> fn () + +let failOnWrongBranch () = + if Git.Information.getBranchName "" <> releaseBranch then + failwithf "Not on %s. If you want to release please switch to this branch." releaseBranch diff --git a/build/build.fs b/build/build.fs index 1665147..6067ce4 100644 --- a/build/build.fs +++ b/build/build.fs @@ -9,15 +9,7 @@ open Fake.Core.TargetOperators open Fake.Api open Fake.BuildServer open Argu - -let environVarAsBoolOrDefault varName defaultValue = - let truthyConsts = [ "1"; "Y"; "YES"; "T"; "TRUE" ] - Environment.environVar varName - |> ValueOption.ofObj - |> ValueOption.map (fun envvar -> - truthyConsts - |> List.exists (fun ``const`` -> String.Equals (``const``, envvar, StringComparison.InvariantCultureIgnoreCase))) - |> ValueOption.defaultValue defaultValue +open Helpers //----------------------------------------------------------------------------- // Metadata and Configuration @@ -69,7 +61,6 @@ let gitHubRepoUrl = $"https://github.com/%s{gitOwner}/%s{gitRepoName}" let documentationRootUrl = $"https://%s{gitOwner}.github.io/%s{gitRepoName}" -let releaseBranch = "main" let readme = "README.md" let changelogFile = "CHANGELOG.md" @@ -96,53 +87,6 @@ let githubToken = Environment.environVarOrNone "GITHUB_TOKEN" let nugetToken = Environment.environVarOrNone "NUGET_TOKEN" -//----------------------------------------------------------------------------- -// Helpers -//----------------------------------------------------------------------------- - - -let isRelease (targets : Target list) = - targets - |> Seq.map (fun t -> t.Name) - |> Seq.exists ((=) "PublishToNuGet") - -let invokeAsync f = async { f () } - -let configuration (targets : Target list) = - let defaultVal = if isRelease targets then "Release" else "Debug" - - match Environment.environVarOrDefault "CONFIGURATION" defaultVal with - | "Debug" -> DotNet.BuildConfiguration.Debug - | "Release" -> DotNet.BuildConfiguration.Release - | config -> DotNet.BuildConfiguration.Custom config - -let failOnBadExitAndPrint (p : ProcessResult) = - if p.ExitCode <> 0 then - p.Errors |> Seq.iter Trace.traceError - - failwithf "failed with exitcode %d" p.ExitCode - - -let isCI = lazy environVarAsBoolOrDefault "CI" false - -// CI Servers can have bizarre failures that have nothing to do with your code -let rec retryIfInCI times fn = - match isCI.Value with - | true -> - if times > 1 then - try - fn () - with _ -> - retryIfInCI (times - 1) fn - else - fn () - | _ -> fn () - -let failOnWrongBranch () = - if Git.Information.getBranchName "" <> releaseBranch then - failwithf "Not on %s. If you want to release please switch to this branch." releaseBranch - - module dotnet = let watch cmdParam program args = DotNet.exec cmdParam (sprintf "watch %s" program) args diff --git a/build/build.fsproj b/build/build.fsproj index 86538f0..3f5c4f9 100644 --- a/build/build.fsproj +++ b/build/build.fsproj @@ -11,6 +11,7 @@ +