From ca31b55507f73456446e53e4df7ddf3366b47180 Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Sat, 24 Aug 2024 09:46:34 -0600 Subject: [PATCH] make date-based version easier to read --- .github/workflows/ci.yml | 29 ++++++++-------------------- build-and-test.ps1 | 14 ++++++++++++-- build/make-version.ps1 | 40 +++++++++++++++++++++++++++++++++++++++ src/Directory.Build.props | 4 ++-- version.txt | 2 +- 5 files changed, 63 insertions(+), 26 deletions(-) create mode 100755 build/make-version.ps1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f139b86b..9b54e4a2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,22 +4,8 @@ on: [push, pull_request] jobs: - create-version-suffix: - runs-on: ubuntu-latest - outputs: - version-suffix: ${{ steps.create-version-suffix.outputs.VERSION_SUFFIX }} - steps: - - id: create-version-suffix - shell: pwsh - run: | - $versionSuffix = [System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss") - $versionString = "VERSION_SUFFIX=$versionSuffix" - Write-Host $versionString - Write-Output $versionString | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append - build: runs-on: ${{ matrix.value.os }} - needs: create-version-suffix strategy: matrix: value: [{ os: windows-latest, architecture: x64 }, { os: windows-latest, architecture: arm64 }, { os: ubuntu-latest, architecture: x64 }] @@ -34,13 +20,9 @@ jobs: - uses: actions/setup-go@v5 with: go-version: 1.19 - - name: promote version suffix - shell: pwsh - run: | - Write-Output "VERSION_SUFFIX=${{ needs.create-version-suffix.outputs.version-suffix }}" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append - name: Build and test shell: pwsh - run: ./build-and-test.ps1 -configuration Release -architecture ${{ matrix.value.architecture }} + run: ./build-and-test.ps1 -configuration Release -architecture ${{ matrix.value.architecture }} -ci - name: publish artifact uses: actions/upload-artifact@v4 with: @@ -55,17 +37,22 @@ jobs: cross_compile: runs-on: ubuntu-latest - needs: create-version-suffix steps: - uses: actions/checkout@v4 with: submodules: recursive + - name: Calculate version + shell: bash + run: echo "VERSION_SUFFIX=$(pwsh ./build/make-version.ps1 -suffix beta)" | tee -a $GITHUB_OUTPUT - uses: devcontainers/ci@v0.3 with: push: never runCmd: ./build-and-test.ps1 -configuration Release -architecture arm64 env: | - VERSION_SUFFIX=${{ needs.create-version-suffix.outputs.version-suffix }} + DOTNET_NOLOGO=true + DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true + DOTNET_CLI_TELEMETRY_OPTOUT=true + VERSION_SUFFIX - name: re-map path variables from container shell: pwsh run: | diff --git a/build-and-test.ps1 b/build-and-test.ps1 index ad0193b0..77717b86 100755 --- a/build-and-test.ps1 +++ b/build-and-test.ps1 @@ -4,6 +4,7 @@ param ( [string][Alias('c')]$configuration = "Debug", [string][Alias('a')]$architecture, + [switch]$ci, [switch]$noTest, [string]$deployTo ) @@ -21,6 +22,12 @@ function Set-EnvironmentVariable([string]$name, [string]$value) { try { $runTests = -Not $noTest + if ($ci) { + $env:VERSION_SUFFIX = ./build/make-version.ps1 -suffix beta + } + + Write-Host "Using VERSION_SUFFIX=$env:VERSION_SUFFIX" + # assign architecture if ($architecture -eq '') { if ($IsWindows) { @@ -119,8 +126,11 @@ try { } else { $packageVersionPrefix = (Get-Content "$PSScriptRoot/version.txt" | Out-String).Trim() - $packageVersionSuffix = if ("$env:VERSION_SUFFIX" -eq "") { "0" } else { $env:VERSION_SUFFIX } - $packageVersion = "$packageVersionPrefix.$packageVersionSuffix" + $packageVersionSuffix = "$env:VERSION_SUFFIX" + if ($packageVersionSuffix -ne "") { + $packageVersionSuffix = ".$packageVersionSuffix" + } + $packageVersion = "$packageVersionPrefix$packageVersionSuffix" $packageArchitecture = if ($architecture -eq "x64") { "amd64" } else { "arm64" } $packageName = "bcad_${packageVersion}_$packageArchitecture.deb" Set-EnvironmentVariable "secondary_artifact_name" "deb-$architecture" diff --git a/build/make-version.ps1 b/build/make-version.ps1 new file mode 100755 index 00000000..0fb174a0 --- /dev/null +++ b/build/make-version.ps1 @@ -0,0 +1,40 @@ +#!/usr/bin/pwsh + +[CmdletBinding(PositionalBinding = $false)] +param ( + [string]$suffix = "dev" +) + +Set-StrictMode -version 2.0 +$ErrorActionPreference = "Stop" + +try { + # get date of latest commit + $date = git show --no-patch --format=%cs HEAD + + # count commits with that same date + $lines = git log '--pretty=%cs' -n 99 + $count = 0 + foreach ($line in $lines) { + if ($line -ne $date) { + break + } + + $count++ + } + + # format date as version + $parts = $date -Split '-' + $year = $parts[0] # e.g., 2024 from '2024-12-25' + $month = $parts[1] # e.g., 12 from '2024-12-25' + $day = $parts[2] # e.g., 25 from '2024-12-25' + $version = "$suffix.$year-$month-$day.$count" + Write-Output $version + +} +catch { + Write-Host $_ + Write-Host $_.Exception + Write-Host $_.ScriptStackTrace + exit 1 +} diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 04b8b436..4be2596f 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -7,8 +7,8 @@ {HintPathFromItem};{TargetFrameworkDirectory};{RawFileName} $(MSBuildThisFileDirectory).. <_VersionSuffix>$(VERSION_SUFFIX) - <_VersionSuffix Condition="'$(_VersionSuffix)' == ''">0 - $([System.IO.File]::ReadAllText('$(RepoRoot)\version.txt').Trim()).0-beta.$(_VersionSuffix) + <_VersionSuffix Condition="'$(_VersionSuffix)' == ''">dev + $([System.IO.File]::ReadAllText('$(RepoRoot)\version.txt').Trim())-$(_VersionSuffix) $(RepoRoot)\artifacts $(ArtifactsDir)\packages $(ArtifactsDir)\bin\$(MSBuildProjectName) diff --git a/version.txt b/version.txt index 3b04cfb6..0ea3a944 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.2 +0.2.0