From fec29be0ad8ec7c77d953a7083d92a6759fbe798 Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Thu, 17 Oct 2024 20:45:15 +0200 Subject: [PATCH] fix: move build source info to a separate location In order not to affect the output of alr --version --- alire.toml | 11 --- dev/build.sh | 2 - scripts/version-patcher.ps1 | 3 +- scripts/version-patcher.py | 55 ----------- scripts/version-patcher.sh | 5 +- src/alire/alire-meta.ads | 14 +++ src/alr/alr-commands-version.adb | 3 + .../version_patcher/src/version_patcher.adb | 91 ++++++++++++------- 8 files changed, 81 insertions(+), 103 deletions(-) delete mode 100644 scripts/version-patcher.py create mode 100644 src/alire/alire-meta.ads diff --git a/alire.toml b/alire.toml index b5da93505..4659a68cf 100644 --- a/alire.toml +++ b/alire.toml @@ -127,14 +127,3 @@ command = ["pwsh", "scripts/version-patcher.ps1"] [actions.'case(os)'.'...'] type = "pre-build" command = ["scripts/version-patcher.sh"] - -# Afterwards we leave an extra note, so people manually building don't use a -# misleading commit. -[[actions]] -[actions.'case(os)'.windows] -type = "post-build" -command = ["pwsh", "scripts/version-patcher.ps1", "_or_later"] - -[actions.'case(os)'.'...'] -type = "post-build" -command = ["scripts/version-patcher.sh", "_or_later"] diff --git a/dev/build.sh b/dev/build.sh index 336f043c0..89942d58d 100755 --- a/dev/build.sh +++ b/dev/build.sh @@ -12,5 +12,3 @@ scripts/version-patcher.sh echo "Building with ALIRE_OS=$ALIRE_OS and $(gnat --version | head -1)" gprbuild "-j$ALIRE_BUILD_JOBS" -r -p -P "$(dirname $0)/../alr_env.gpr" "$@" - -scripts/version-patcher.sh _or_later diff --git a/scripts/version-patcher.ps1 b/scripts/version-patcher.ps1 index 1d1408bae..e0c71f13f 100644 --- a/scripts/version-patcher.ps1 +++ b/scripts/version-patcher.ps1 @@ -22,4 +22,5 @@ if (Test-Path $bin) { & $bin @args Write-Output "Resulting version file:" -Get-Content src/alire/alire-version.ads | Select-String "Current_Str" +Get-Content src/alire/alire-meta.ads | Select-String "Commit" +Get-Content src/alire/alire-meta.ads | Select-String "Changes" diff --git a/scripts/version-patcher.py b/scripts/version-patcher.py deleted file mode 100644 index 7088bb313..000000000 --- a/scripts/version-patcher.py +++ /dev/null @@ -1,55 +0,0 @@ -import os -import re -import subprocess -import sys - -def replace_version(filename, build_info): - pattern = r'(Current : constant String := "[^+]+\+)([^"]*)(";)' - - # Depending on the context in which this is run, there may be mix-ups with - # line terminators between the environment detected, github runner, etc... - # So just keep them as they are and that should always work. - - with open(filename, 'rb') as file: - content = file.read().decode() - - # The pattern captures the part between '+' and '";', replacing it with our - # new build information - - new_content = re.sub(pattern, r'\g<1>' + build_info + r'\3', content) - - # A few sanity checks and write if needed - - if new_content == content: - if build_info in content: - print(f"Note: version in {filename} already up to date") - else: - print(f"WARNING: failed to update version in {filename}") - else: - # Ensure the content line terminators are not changed - with open(filename, 'wb') as file: - file.write(new_content.encode()) - - -# If a flag exists, skip any updating, just print a message and exit -if "ALR_VERSION_DONT_PATCH" in os.environ: - print("Note: skipping version update") - sys.exit(0) - -# If there is an argument to the script, retrieve it here and use it as the new -# dirty flag after the commit -if len(sys.argv) > 1: - dirty = sys.argv[1] -else: - # Detect whether the current directory contains changes - if subprocess.call(['git', 'diff-index', '--quiet', 'HEAD', '--']) != 0: - dirty = "_dirty" - else: - dirty = "" - -# Find the short git commit of the repository in the current directory -commit = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip() - -# Replace the build version part with the short commit hash plus any extra info -print(f"Updating version in src/alire/alire-version.ads to commit {commit}{dirty}...") -replace_version('src/alire/alire-version.ads', commit+dirty) diff --git a/scripts/version-patcher.sh b/scripts/version-patcher.sh index d377825b0..5f17ac27c 100755 --- a/scripts/version-patcher.sh +++ b/scripts/version-patcher.sh @@ -27,5 +27,6 @@ fi $bin "$@" -echo "Resulting version file:" -cat src/alire/alire-version.ads | grep "Current_Str : constant String" \ No newline at end of file +echo "Resulting build metadata info:" +cat src/alire/alire-meta.ads | grep "Commit" +cat src/alire/alire-meta.ads | grep "Changes" \ No newline at end of file diff --git a/src/alire/alire-meta.ads b/src/alire/alire-meta.ads new file mode 100644 index 000000000..c8d259930 --- /dev/null +++ b/src/alire/alire-meta.ads @@ -0,0 +1,14 @@ +package Alire.Meta with Preelaborate is + + -- Root package for information about Alire itself. Alire.Version could be + -- moved here if at some point there is a reason good enough to expend the + -- energy. + + package Working_Tree is + + Commit : constant String := "unknown"; + Changes : constant String := "unknown"; + + end Working_Tree; + +end Alire.Meta; diff --git a/src/alr/alr-commands-version.adb b/src/alr/alr-commands-version.adb index a72c5ad21..3b9eecc20 100644 --- a/src/alr/alr-commands-version.adb +++ b/src/alr/alr-commands-version.adb @@ -4,6 +4,7 @@ with Alire.Settings.Edit; with Alire.Directories; with Alire.Index; with Alire.Index_On_Disk.Loading; +with Alire.Meta; with Alire.Milestones; with Alire.Origins.Deployers.System; with Alire.Paths.Vault; @@ -91,6 +92,8 @@ package body Alr.Commands.Version is Add ("APPLICATION", ""); Add ("alr version:", Alire.Version.Current.Image); Add ("libalire version:", Alire.Version.Current.Image); + Add ("source commit:", Alire.Meta.Working_Tree.Commit); + Add ("source changes:", Alire.Meta.Working_Tree.Changes); Add ("compilation date:", GNAT.Source_Info.Compilation_ISO_Date & " " & GNAT.Source_Info.Compilation_Time); diff --git a/support/version_patcher/src/version_patcher.adb b/support/version_patcher/src/version_patcher.adb index b91768447..0d1343ce7 100644 --- a/support/version_patcher/src/version_patcher.adb +++ b/support/version_patcher/src/version_patcher.adb @@ -1,4 +1,3 @@ -with Ada.Command_Line; use Ada.Command_Line; with Ada.Directories; with Ada.Environment_Variables; use Ada.Environment_Variables; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; @@ -13,16 +12,23 @@ with GNAT.OS_Lib; procedure Version_Patcher is - --------------------- - -- Replace_Version -- - --------------------- + ------------------ + -- Replace_Info -- + ------------------ - procedure Replace_Version (Filename : String; Build_Info : String) is + procedure Replace_Info + (Filename : String; + -- File in which to look for patterns + Trigger : String; + -- Substring that will trigger replacement in a line. The only text in + -- quotes will be replaced. + Replacement : String) + -- Replacement + is F : Ada.Text_IO.File_Type; O : Ada.Text_IO.File_Type; use Ada.Text_IO; - - Target : constant String := "Current_Str : constant String :="; + Hit : Boolean := False; begin Open (F, In_File, Filename); Create (O, Out_File, Filename & ".new"); @@ -30,26 +36,39 @@ procedure Version_Patcher is declare Line : constant String := Get_Line (F); begin - if (for some I in Line'Range => - I + Target'Length - 1 <= Line'Last and then - Line (I .. I + Target'Length - 1) = Target) + if not Hit and then + (for some I in Line'Range => + I + Trigger'Length - 1 <= Line'Last and then + Line (I .. I + Trigger'Length - 1) = Trigger) then declare - Quotes_Seen : Boolean := False; + Ini : Integer := Line'First - 1; + Fin : Integer := Line'First - 1; begin - for Char of Line loop - if Char = '"' and then not Quotes_Seen then - Quotes_Seen := True; - Put (O, Char); - elsif (Char = '"' and then Quotes_Seen) - or else Char = '+' - then - Put_Line (O, "+" & Build_Info & '"' & ";"); - exit; - else - Put (O, Char); + for I in Line'Range loop + if Line (I) = '"' then + if Ini < Line'First then + Ini := I; + elsif Fin < Line'First then + Fin := I; + else + raise Constraint_Error + with "Too many quotes in line: " & Line; + end if; end if; end loop; + + if Ini < Line'First or else Fin < Line'First then + raise Constraint_Error + with "No quoted string in line: " & Line; + end if; + + Put_Line + (O, + Line (Line'First .. Ini) + & Replacement + & Line (Fin .. Line'Last)); + Hit := True; end; else Put_Line (O, Line); @@ -60,10 +79,15 @@ procedure Version_Patcher is Close (F); Close (O); + if not Hit then + raise Constraint_Error + with "Trigger not found in file: " & Trigger; + end if; + Ada.Directories.Delete_File (Filename); Ada.Directories.Rename (Filename & ".new", Filename); - end Replace_Version; + end Replace_Info; ----------------- -- Git_Command -- @@ -94,14 +118,12 @@ begin declare Dirty : constant String - := (if Argument_Count > 0 then - Argument (1) - elsif Git_Command ("diff-index --quiet HEAD --").Code /= 0 then - "_dirty" + := (if Git_Command ("diff-index --quiet HEAD --").Code /= 0 then + "dirty" else - ""); + "clean"); Commit_Result : constant Result := - Git_Command ("rev-parse --short HEAD"); + Git_Command ("rev-parse HEAD"); Commit : constant String := To_String (Commit_Result.Output); begin if Commit_Result.Code /= 0 then @@ -110,8 +132,13 @@ begin & Commit_Result.Code'Image; end if; Ada.Text_IO.Put_Line - ("Updating version in src/alire/alire-version.ads to commit " - & Commit & Dirty & "..."); - Replace_Version ("src/alire/alire-version.ads", Commit & Dirty); + ("Updating src/alire/alire-meta.ads to commit [" & Commit + & "] with status [" & Dirty & "]"); + Replace_Info ("src/alire/alire-meta.ads", + "Commit", + Commit); + Replace_Info ("src/alire/alire-meta.ads", + "Changes", + Dirty); end; end Version_Patcher;