Last active
April 28, 2023 23:11
-
-
Save uyriq/55ce749d4d69347e868b53918aec4677 to your computer and use it in GitHub Desktop.
copy this gist to $PROFILE powershell startup script, to have a shortcut for preferred code editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-IsAdmin { | |
try { | |
$identity = [Security.Principal.WindowsIdentity]::GetCurrent() | |
$principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity | |
return $principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) | |
} | |
catch { | |
throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_ | |
} | |
return false | |
<# | |
.SYNOPSIS | |
Checks if the current Powershell instance is running with elevated privileges or not. | |
.EXAMPLE | |
PS C:\> Test-IsAdmin | |
.OUTPUTS | |
System.Boolean | |
True if the current Powershell is elevated, false if not. | |
#> | |
} | |
Function Test-CommandExists { | |
Param ($command) | |
$oldPreference = $ErrorActionPreference | |
$ErrorActionPreference = 'stop' | |
try { if (Get-Command $command -ErrorAction SilentlyContinue) { RETURN $true } } | |
Catch { Write-Host "$command does not exist"; RETURN $false } | |
Finally { $ErrorActionPreference = $oldPreference } | |
} | |
Function cod { | |
<# | |
.SYNOPSIS | |
Run your codeeditor of choice on file or folder, coult be setup one as environment variable $env:preferCodeEditor as your arbitrary choice, | |
or by default will be code-insders look at script to change this behav | |
.EXAMPLE | |
PS C:\> cod ./project/myproject # open project folder in codeeditor | |
.OUTPUTS | |
no outputs | |
#> | |
Param ($path) | |
if ($path.Length -lt 1) { $path = '.' } | |
$preferCodeEditor = $([System.Environment]::GetEnvironmentVariable('preferCodeEditor', 'Machine')) | |
if ($preferCodeEditor -eq $null) { | |
if (!$(Test-IsAdmin)) { | |
Write-Output "can not set preferCodeEditor env var as simple user $env:UserName, please restart term as Admin with elevated rights"; | |
Break | |
} | |
Write-Output "env var preferCodeEditor not set : $($([System.Environment]::GetEnvironmentVariable('preferCodeEditor','Machine')) -eq $null)" | |
Write-Output "exists fleet : $(Test-CommandExists fleet)" ; if (Test-CommandExists fleet) { $choice = 'fleet' } | |
Write-Output "exists vscode : $(Test-CommandExists code)" ; if (Test-CommandExists code) { $choice = 'code' } | |
Write-Output "exists codium : $(Test-CommandExists codium)" ; if (Test-CommandExists codium) { $choice = 'codium' } | |
Write-Output "exists code-insiders : $(Test-CommandExists code-insiders)" ; | |
# this is the last reassignment of $choice, so this will be actual choice, you can swap line below to change for codium or fleet if you like | |
if (Test-CommandExists code-insiders) { | |
$choice = 'code-insiders' | |
} | |
[Environment]::SetEnvironmentVariable('preferCodeEditor', $choice, 'Machine') | |
Write-Output "preferCodeEditor set to $choice, please close and restart terminal before further use" | |
Break | |
} | |
if ($preferCodeEditor.Length -gt 1) { | |
"$preferCodeEditor $path" | Out-String | Invoke-Expression | |
} | |
} | |
function Count-Loc { | |
<# | |
.SYNOPSIS | |
Using of git stat to count LOC total, inserted, deleted and average | |
.DESCRIPTION | |
Simple helper to use git stat command on the fly named Count-Loc | |
.PARAMETER NumberDays | |
The number of days to count LOC from | |
.PARAMETER Author | |
The author of the git commits | |
.PARAMETER Since | |
The start date or number of days ago of the git commits period | |
.PARAMETER Until | |
The end date or number of days ago of the git commits period | |
.EXAMPLE | |
PS C:\> Count-Loc -NumberDays 5 | |
.EXAMPLE | |
PS C:\> Count-Loc -Since 2023-04-20 -Until 2023-04-28 -Author uyriq | |
.EXAMPLE | |
PS C:\> Count-Loc -Since 5 -Until 1 -Author uyriq | |
.OUTPUTS | |
System.Int | |
Total LOC. | |
#> | |
param ( | |
[Parameter(mandatory = $false, | |
parametersetname = 'DateRange' | |
)] | |
[int]$NumberDays, | |
[Parameter( | |
mandatory = $false, | |
parametersetname = 'DateRange' | |
)] | |
[string]$Author = (git log --format='%an' | sort -u | ForEach-Object { $_.ToString().Split('\n')[0] })[0], | |
[Parameter( | |
mandatory = $false, | |
parametersetname = 'DateRange' | |
)] | |
[string]$Since, | |
[Parameter( | |
mandatory = $false, | |
parametersetname = 'DateRange' | |
)] | |
[string]$Until | |
) | |
if ($NumberDays -eq 0 -and $Since -eq '') { | |
Write-Output 'Numbers of days ago, or since data not given, count default value one week ago' | |
$NumberDays = 7 | |
$Since = "$NumberDays days ago" | |
} | |
else { | |
if ($Since -eq '') { | |
$Since = "$NumberDays days ago" | |
} | |
} | |
if ($Until -eq 0 -or $Until -eq '') { | |
$Until = '0 days ago' | |
} | |
else { | |
if ($Since -match "^[\d\.]+$") { | |
$NumberDays = $Since - $Until | |
$Since = "$Since days ago" | |
$Until = "$Until days ago" | |
} | |
if ($Since -match "^\d{4}-\d{2}-\d{2}$" ) { $NumberDays = (New-TimeSpan -Start "$Since" -End "$Until" ).Days } | |
} | |
$Authors = git log --format='%an' | sort -u | ForEach-Object { $_.ToString().Split('\n')[0] } | |
Write-Output "Count LOC for $Author - please check possible authors of these:" $Authors | |
Write-Output "stat count from $Since up to $Until given $NumberDays" | |
$LinesIns = git log --since="$Since" --until="$Until" --date=local --author $Author --oneline --stat | Select-String -Pattern '\d+ file' | ForEach-Object { $_.ToString().Split(' ')[6] } | Measure-Object -Sum | Select-Object -ExpandProperty Sum | |
$LinesDel = git log --since="$Since" --until="$Until" --date=local --author $Author --oneline --stat | Select-String -Pattern '\d+ file' | ForEach-Object { $_.ToString().Split(' ')[4] } | Measure-Object -Sum | Select-Object -ExpandProperty Sum | |
Write-Output 'Average of LOC per day:' ([Math]::Round((($LinesIns + $LinesDel) / $NumberDays) + 0.00, 2)) | |
Write-Output 'Deleted Lines of code:' ($LinesDel) | |
Write-Output 'Inserted Lines of code:' ($LinesIns) | |
Write-Output 'Total Lines of code:' | |
return ($LinesIns + $LinesDel) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment