From e76c2200a79a28f7918aa44728e4854ce5e0dfcf Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Thu, 4 Jan 2024 15:33:20 +0100
Subject: [PATCH] fix: update ci tools #7

---
 devenv.nix   | 171 +++++++++++++++++++++++++++++++++++++++++----------
 project.nix  |   4 +-
 treefmt.toml |   2 +-
 3 files changed, 142 insertions(+), 35 deletions(-)

diff --git a/devenv.nix b/devenv.nix
index 657251f..7fc282a 100644
--- a/devenv.nix
+++ b/devenv.nix
@@ -46,13 +46,6 @@ tasks:
         cmds:
           - ${gitCommit}/bin/git-commit
         silent: true
-        
-    
-    commit:
-      desc: Commit a feature
-      cmds:
-        - do-git-commit
-      silent: true
     
     default:
       desc: Print this help message
@@ -66,7 +59,7 @@ tasks:
   };
 
   treefmtConfig = pkgs.writeTextFile {
-    name = "Taskfile.yaml";
+    name = "treefmt.toml";
     text = ''
         ## DO NOT EDIT THIS FILE MANUALLY, IT IS GENERATED BY NIX
 
@@ -208,7 +201,12 @@ tasks:
             - windows
           dir: ${config.devenv.root}/source
           ldflags:
-            - -s -w -X main.version={{.Version}} -X 'main.commit={{.Commit}}' -X 'main.build={{.Date}}' -X 'main.mnemonic=${goPkgMnemonic}' -X 'main.name=${goPkgName}'
+            - -s -w 
+            - -X main.version={{.Version}}
+            - -X 'main.commit={{.Commit}}' 
+            - -X 'main.build={{.Date}}' 
+            - -X 'main.mnemonic=${goPkgMnemonic}' 
+            - -X 'main.name=${goPkgName}'
 
 
       ## DO NOT EDIT THIS FILE MANUALLY, IT IS GENERATED BY NIX
@@ -331,33 +329,142 @@ in {
     ${pkgs.git}/bin/git push origin $CI_COMMIT_REF_NAME --tags
 
   '';
+  
+  scripts.common-functions.exec = ''
+
+    if [ -t 1 ]; then
+        RED='\033[0;31m'
+        GREEN='\033[0;32m'
+        BLUE='\033[0;34m'
+        RESET='\033[0m'
+        BOLD='\033[1m'
+    else
+        RED=""
+        GREEN=""
+        RESET=""
+    fi
+
+    is_true() {
+      val="$1"
+      if [ "$val" = 'true' ] || [ "$val" = '1' ]; then
+        return 0
+      else
+        return 1
+      fi
+    }
+
+    get_commit_hash() {
+        hash=""
+        if [[ -n "$CI_COMMIT_SHORT_SHA" ]]; then
+            hash="$CI_COMMIT_SHORT_SHA"
+        else
+            hash="$(git rev-parse --short HEAD)"
+
+            if [[ -n "$(git status --porcelain)" ]]; then
+                hash="''${hash}-dirty"
+            fi
+        fi
+        echo "$hash"
+    }
+    
+    echo_green() {
+        echo -e "$GREEN ✔ $1$RESET"
+    }
+    
+    echo_red() {
+        echo -e "$RED ✖ $1$RESET"
+    }
+    
+    echo_todo() {
+        echo_bold "$BLUE • $1"
+    }
+
+    update_project_version() {
+        if version auto --git --exit-code-if-no-bump --verbose
+        then
+            buildVer=$(version print --git)
+            local project_nix="${config.devenv.root}/project.nix"
+            sed -i "s/^\s*version\s*=.*/  version = \"$buildVer\";/" "$project_nix"
+            echo_green "Project version updated to $buildVer"
+
+            ${pkgs.git}/bin/git add "$project_nix"
+            ${pkgs.git}/bin/git commit -m "chore: Update project version to $buildVer" "$project_nix"
+            ${pkgs.git}/bin/git tag -f -a "$buildVer" -m "Release $buildVer"
+            
+            if is_true "$CI_MODE"; then
+                ${pkgs.git}/bin/git push -o ci.skip origin ''${CI_COMMIT_REF_NAME} --tags
+            fi
+            
+        else
+            echo_green "Project version is up to date"
+        fi
+    }
+
+    get_project_version() {
+        echo "$(version print --git)"
+    }
+
+    if [ -z "$DEBUG_MODE" ]; then
+        DEBUG_MODE='false'
+    fi
+
+    if [ -n "$CI_JOB_TOKEN" ]; then
+        CI_MODE='true'
+    fi
+
+    if is_true "$DEBUG_MODE" || is_true "$CI_MODE"; then
+        set -x
+    fi
+
+
+  '';
+  
+  
   scripts.update-files.exec = ''
     #!${pkgs.bash}/bin/bash
-        update_symlink() {
-            local source_path="$1"
-            local target_path="$2"
-            local file_name="$(basename "$target_path")"
-
-            if [[ -L "$source_path" ]]; then
-                if [[ "$(readlink "$source_path")" != "$target_path" ]]; then
-                    # Link exists but is not up to date
-                    rm "$source_path"
-                    ln -s "$target_path" "$source_path"
-                fi
-            elif [[ -e "$source_path" ]]; then
-                echo "$file_name already exists. Please rename or delete it."
-                exit 1
-            else
-                ln -s "$target_path" "$source_path"
-            fi
-        }
+    source common-functions
+    create_symlink() {
+        if ln -s "$1" "$2" 2>/dev/null; then
+            echo_green "$2 created"
+        else
+            echo_red "$2 already exists"
+        fi
+    }
+    
+    update_symlink() {
+        echo -e " → $1"
+        local source_path="$1"
+        local target_path="$2"
+        local file_name="$(basename "$target_path")"
+        local source_dir="$(dirname "$source_path")"
+        mkdir -p "$source_dir" || true
+        if [[ ! -d "$source_dir" ]]; then
+         echo_red "$RED ✖ $source_dir is not a directory. Please remove it manually and run the script again."
+         exit 1
+        fi
+        
+        if [[ -L "$source_path" ]]; then
+        if [[ "$(readlink "$source_path")" != "$target_path" ]]; then
+             rm "$source_path"
+             create_symlink "$target_path" "$source_path"
+             echo_green "$GREEN ✔ $source_path updated$RESET"
+         else
+                echo_green "$GREEN ✔ $source_path is up to date$RESET"
+         fi
+        elif [[ -e "$source_path" ]]; then
+         echo_red "$RED ✖ $source_path already exists and is not a symlink. Please remove it manually and run the script again."
+         exit 1
+        else
+         create_symlink "$target_path" "$source_path"
+        fi
+    }
 
-        # Usage for Taskfile.yaml
-        update_symlink "${config.devenv.root}/Taskfile.yaml" "${taskfileYaml}"
-        update_symlink "${config.devenv.root}/.goreleaser.yml" "${goReleaserYaml}"
-        update_symlink "${config.devenv.root}/treefmt.toml" "${treefmtConfig}"
+    # Usage for Taskfile.yaml
+    update_symlink "${config.devenv.root}/Taskfile.yaml" "${taskfileYaml}"
+    update_symlink "${config.devenv.root}/.goreleaser.yml" "${goReleaserYaml}"
+    update_symlink "${config.devenv.root}/treefmt.toml" "${treefmtConfig}"
 
-        cat ${gitlabCiYaml} > "${config.devenv.root}/.gitlab-ci.yml"
+    cat ${gitlabCiYaml} > "${config.devenv.root}/.gitlab-ci.yml"
 
   '';
 
diff --git a/project.nix b/project.nix
index 493eaff..7e9dc0a 100644
--- a/project.nix
+++ b/project.nix
@@ -5,6 +5,6 @@
   supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"];
   compileForSystems = ["linux/arm64" "linux/amd64" "darwin/amd64" "windows/amd64"];
   modulePath = "gitlab.schukai.com/oss/utilities/version";
-  version="0.6.26";  ## don't change this line, it is updated automatically by GitLab CI
-  vendorHash="sha256-uyWI6Gnko1J62XDk6jjRdqZ8TSFtwsTOlDwdR9Xc+ic="; ## this is updated automatically by update-hashes
+  version = "0.6.26"; ## don't change this line, it is updated automatically by GitLab CI
+  vendorHash = "sha256-uyWI6Gnko1J62XDk6jjRdqZ8TSFtwsTOlDwdR9Xc+ic="; ## this is updated automatically by update-hashes
 }
diff --git a/treefmt.toml b/treefmt.toml
index 484d9a1..503545c 120000
--- a/treefmt.toml
+++ b/treefmt.toml
@@ -1 +1 @@
-/nix/store/smqjqjy0wa39nvk52jq6x8w1i4bs65g9-Taskfile.yaml
\ No newline at end of file
+/nix/store/wrp2lsy6h8mzr6fa4n71lqnxw5dliwmc-treefmt.toml
\ No newline at end of file
-- 
GitLab