PowerShell-Profile/RdzeN-Profile.ps1
2024-05-01 14:12:54 +02:00

114 lines
3.4 KiB
PowerShell

# gitea.RdzeN.net
#
# Example Powershell Profile.
#
# TO-DO
# Step 0, precheck for git, notepad++ and vscode installed.
# 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. Check if local GIT repo is up to date
Set-Location $path_gitea_powershell_profile
$RepositoryURL = "https://gitea.rdzen.net/najlepszytomasz/PowerShell-Profile.git"
try {
$git_tmp = git fetch origin
Write-Host "Success
git fetch origin
: $RepositoryURL" -ForegroundColor Green
} catch {
Write-Host "Error occurred while git fetch origin
: $RepositoryURL" -ForegroundColor Red
}
try {
$git_tmp = git status
Write-Host "Success
git status
: $RepositoryURL" -ForegroundColor Green
} catch {
Write-Host "Error occurred while git status
: $RepositoryURL" -ForegroundColor Red
}
# 3. Download GIT REPO
function Pull-Repository {
Set-Location $path_gitea_powershell_profile
try {
git pull -q
Write-Host "Success
git pull
: $RepositoryURL" -ForegroundColor Green
} catch {
Write-Host "Error occurred while git pull for: $RepositoryURL" -ForegroundColor Red
}
}
if ($git_tmp -match "(Your branch is up to date)") {
$git_tmp | ForEach-Object {
Write-Host $_ -ForegroundColor Green
}
} else {
$git_tmp | ForEach-Object {
Write-Host $_ -ForegroundColor Red
}
Pull-Repository
}
# 4. Load functions
$path_to_search = "$path_gitea_powershell_profile_functions"
Get-ChildItem $path_to_search | ForEach-Object {
. $_.FullName
}
# 5. List functions
$found_functions = @()
Get-ChildItem $path_to_search | ForEach-Object {
$file_content = Get-Content $_.FullName -Raw
$matched_functions = [Regex]::Matches($file_content, '(?<=function\s).*?(?={)')
$matched_functions | ForEach-Object {
$found_functions += $_.Value.Trim()
}
}
$found_functions = $found_functions | Sort-Object
for ($i = 0; $i -lt $found_functions.Count; $i++) {
Write-Host "$($i+1)" -NoNewline -ForegroundColor Cyan
Write-Host ")`t" -NoNewline -ForegroundColor Blue
Write-Host "$($found_functions[$i])" -ForegroundColor Cyan
}
# 6. Check if current Profile is equal to repo profile file.
$profile_repo = Get-Content "$path_gitea_powershell_profile\RdzeN-Profile.ps1"
$profile_system = Get-Content $PROFILE
$compare_profile = Compare-Object -ReferenceObject $profile_repo -DifferenceObject $profile_system
# 7. Override actual profile and reload it if needed.
if ($compare_profile.sideindicator.count -gt 0) {
Write-Host "Pulled profile file need to be updated" -ForegroundColor Red
clear-host
Override-Profile
Write-Host "Profile updated" -ForegroundColor Red
Reload-Profile
} else {
# Just override ;-)
Override-Profile
}
# 98. Set lcoation to userprofile folder
Set-Location $env:USERPROFILE
# 99. Prompt profile loaded
New-SimulateTyping "PowerShell User Profile is loaded"