PowerShell-Profile/RdzeN-Profile.ps1

115 lines
4.0 KiB
PowerShell

# gitea.RdzeN.net
#
# 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"
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_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"
# 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
try {
git pull -q
Write-Host "Success GIT pull for: $RepositoryURL" -ForegroundColor Green
} catch {
Write-Host "Error occurred while GIT pull for: $RepositoryURL" -ForegroundColor Red
}
}
# 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
Get-ChildItem $path_gitea_powershell_profile_functions | ForEach-Object {
. $_.FullName
}
# 97. Override profile
Override-Profile
# 98. Override profile
#Reload-Profile
} 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
Get-ChildItem $path_gitea_powershell_profile_functions | ForEach-Object {
. $_.FullName
}
# 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
Get-ChildItem $path_gitea_powershell_profile_functions | ForEach-Object {
. $_.FullName
}
} else {
Write-Host "Functions are up to date." -ForegroundColor Green
}
# 4. List functions
$functions = Get-ChildItem $path_gitea_powershell_profile_functions | Sort-Object
for ($i = 0; $i -lt $functions.Count; $i++) {
Write-Host "$($i+1)" -NoNewline -ForegroundColor Cyan
Write-Host ")`t" -NoNewline -ForegroundColor Blue
Write-Host "$(($functions[$i].Name).Replace('.ps1',''))" -ForegroundColor Cyan
}
# 99. Prompt profile loaded
New-SimulateTyping "PowerShell User Profile is loaded"
#