From 8461c0c8e47bea84e78f5e084bb1985cb1afaf9f Mon Sep 17 00:00:00 2001 From: Tomasz Kostrzewa Date: Tue, 5 Mar 2024 23:49:58 +0100 Subject: [PATCH] modified: RdzeN-Profile.ps1 --- RdzeN-Profile.ps1 | 104 +++++++++++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 30 deletions(-) diff --git a/RdzeN-Profile.ps1 b/RdzeN-Profile.ps1 index 9562aca..e745884 100644 --- a/RdzeN-Profile.ps1 +++ b/RdzeN-Profile.ps1 @@ -3,61 +3,105 @@ # Example Powershell Profile. # # 1. Check if GITEA folder exists. -$path_gitea = "$($env:USERPROFILE)\GITEA" -$path_gitea_powershell_profile = "$path_gitea\PowerShell-Profile" -$path_gitea_powershell_profile_functions = "$path_gitea_powershell_profile\functions" +$path_gitea = "$($env:USERPROFILE)\GITEA" +$path_gitea_powershell_profile = "$path_gitea\PowerShell-Profile" +$path_gitea_powershell_profile_functions = "$path_gitea_powershell_profile\functions" if (-not (Test-Path $path_gitea)) { New-Item -ItemType Directory -Path $path_gitea | Out-Null } -if (-not (Test-Path $path_gitea_powerShell_profile )) { - New-Item -ItemType Directory -Path $path_gitea_powerShell_profile | Out-Null +if (-not (Test-Path $path_gitea_powershell_profile)) { + New-Item -ItemType Directory -Path $path_gitea_powershell_profile | Out-Null } -if (-not (Test-Path $path_gitea_powershell_profile_functions )) { +if (-not (Test-Path $path_gitea_powershell_profile_functions)) { New-Item -ItemType Directory -Path $path_gitea_powershell_profile_functions | Out-Null } # 2. Download / pull GIT REPO $RepositoryURL = "https://gitea.rdzen.net/najlepszytomasz/PowerShell-Profile.git" -if (-not (Get-ChildItem $path_gitea_powershell_profile_functions).count -gt 0) { - Set-Location $path_gitea - Write-Host "Creating GIT clone for: $RepositoryURL" -ForegroundColor Yellow - try { - git clone $RepositoryURL -q - Write-Host "Success GIT clone for: $RepositoryURL" -ForegroundColor Green - } catch { - Write-Host "Error occured while GIT clone for: $RepositoryURL" -ForegroundColor Red - } -} else { +# Define paths +$path_gitea = "$($env:USERPROFILE)\GITEA" +$path_gitea_powershell_profile = "$path_gitea\PowerShell-Profile" +$path_gitea_powershell_profile_functions = "$path_gitea_powershell_profile\functions" +$path_profile_file = "$path_gitea_powershell_profile\RdzeN-Profile.ps1" + +# Function to pull repository +function Pull-Repository { Set-Location $path_gitea_powershell_profile - Write-Host "Pulling GIT for: $RepositoryURL" -ForegroundColor Yellow try { git pull -q Write-Host "Success GIT pull for: $RepositoryURL" -ForegroundColor Green } catch { - Write-Host "Error occured while GIT pull for: $RepositoryURL" -ForegroundColor Red + Write-Host "Error occurred while GIT pull for: $RepositoryURL" -ForegroundColor Red } } -# 3. dot source functions. +# Check if the profile file exists locally +if (-not (Test-Path $path_profile_file)) { + Write-Host "Profile file not found locally. Pulling from repository..." -ForegroundColor Yellow + Pull-Repository +} else { + # Check if the profile file has been modified + $local_profile_hash = Get-FileHash -Path $path_profile_file + $remote_profile_hash = git hash-object $path_profile_file + + if ($local_profile_hash.Hash -ne $remote_profile_hash.Trim()) { + Write-Host "Local profile file has been modified. Pulling from repository..." -ForegroundColor Yellow + Pull-Repository + # 97. Override profile + Override-Profile + # 98. Override profile + Reload-Profile + } else { + Write-Host "Local profile file is up to date." -ForegroundColor Green + } +} + +# Check if functions have been modified +$local_functions_hashes = @{} +$remote_functions_hashes = @{} + +# Iterate through local functions +Get-ChildItem $path_gitea_powershell_profile_functions | ForEach-Object { + $local_functions_hashes[$_.Name] = (Get-FileHash -Path $_.FullName).Hash +} + +# Get remote functions hashes +Set-Location $path_gitea_powershell_profile_functions +git pull -q +Get-ChildItem | ForEach-Object { + $remote_functions_hashes[$_.Name] = (git hash-object $_.Name).Trim() +} + +# Compare local and remote function hashes +$functions_to_pull = @() +foreach ($function_name in $local_functions_hashes.Keys) { + if ($local_functions_hashes[$function_name] -ne $remote_functions_hashes[$function_name]) { + $functions_to_pull += $function_name + } +} + +if ($functions_to_pull.Count -gt 0) { + Write-Host "Functions have been modified. Pulling from repository..." -ForegroundColor Yellow + Pull-Repository +} else { + Write-Host "Functions are up to date." -ForegroundColor Green +} + + +# 3. Dot source functions. Get-ChildItem $path_gitea_powershell_profile_functions | ForEach-Object { . $_.FullName } -# 4. list functions +# 4. List functions $functions = Get-ChildItem $path_gitea_powershell_profile_functions | Sort-Object for ($i = 0; $i -lt $functions.Count; $i++) { Write-Host "$i" -NoNewline -ForegroundColor Cyan - write-host ")`t" -NoNewline -ForegroundColor Blue - write-host "$(($functions[$i].Name).Replace('.ps1',''))" -ForegroundColor Cyan + Write-Host ")`t" -NoNewline -ForegroundColor Blue + Write-Host "$(($functions[$i].Name).Replace('.ps1',''))" -ForegroundColor Cyan } -# 97. Override profile -Override-Profile - -# 98. Override profile -Reload-Profile - -# 99. Prompt profil loaded -New-SimulateTyping "PowerShell User Profile is loaded" \ No newline at end of file +# 99. Prompt profile loaded +New-SimulateTyping "PowerShell User Profile is loaded"