-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from szymonos/feat/repo-modules
feat: repository modules
- Loading branch information
Showing
23 changed files
with
1,029 additions
and
177 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#Requires -PSEdition Core | ||
<# | ||
.SYNOPSIS | ||
Update repository modules from ps-modules. | ||
.EXAMPLE | ||
.assets/scripts/modules_update.ps1 | ||
#> | ||
[CmdletBinding()] | ||
param () | ||
|
||
begin { | ||
$ErrorActionPreference = 'Stop' | ||
|
||
# check if repository is up to date | ||
Write-Host 'refreshing current repository...' -ForegroundColor Cyan | ||
git fetch | ||
$remote = "$(git remote)/$(git branch --show-current)" | ||
if ((git rev-parse HEAD) -ne (git rev-parse $remote)) { | ||
Write-Warning "Current branch is behind remote, performing hard reset.`n`t Run the script again!`n" | ||
git reset --hard $remote | ||
exit 0 | ||
} | ||
|
||
# set location to workspace folder | ||
Push-Location "$PSScriptRoot/../.." | ||
# import InstallUtils for the Invoke-GhRepoClone function | ||
Import-Module (Resolve-Path './modules/InstallUtils') | ||
|
||
# specify update functions structure | ||
$import = @{ | ||
'do-common' = @{ | ||
SetupUtils = @{ | ||
certs = @( | ||
'ConvertTo-PEM' | ||
'Get-Certificate' | ||
) | ||
common = @( | ||
'ConvertFrom-Cfg' | ||
'ConvertTo-Cfg' | ||
'Invoke-ExampleScriptSave' | ||
) | ||
} | ||
} | ||
'psm-windows' = @{ | ||
InstallUtils = @{ | ||
common = @( | ||
'Invoke-CommandRetry' | ||
'Test-IsAdmin' | ||
'Update-SessionEnvironmentPath' | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
process { | ||
# *refresh ps-modules repository | ||
Write-Host 'refreshing ps-modules repository...' -ForegroundColor Cyan | ||
if ((Invoke-GhRepoClone -OrgRepo 'szymonos/ps-modules' -Path '..') -eq 0) { | ||
Write-Error 'Cloning ps-modules repository failed.' | ||
} | ||
|
||
# *perform update | ||
Write-Host 'perform modules update..' -ForegroundColor Cyan | ||
foreach ($srcModule in $import.GetEnumerator()) { | ||
Import-Module (Resolve-Path "../ps-modules/modules/$($srcModule.Key)") | ||
Write-Host "`n$($srcModule.Key)" -ForegroundColor Green | ||
foreach ($dstModule in $srcModule.Value.GetEnumerator()) { | ||
Write-Host $dstModule.Key | ||
foreach ($destFile in $dstModule.Value.GetEnumerator()) { | ||
Write-Host " - $($destFile.Key).ps1" | ||
$filePath = "./modules/$($dstModule.Key)/Functions/$($destFile.Key).ps1" | ||
Set-Content -Value $null -Path $filePath | ||
$builder = [System.Text.StringBuilder]::new() | ||
foreach ($function in $destFile.Value) { | ||
Write-Host " • $function" | ||
$def = " $((Get-Command $function -CommandType Function).Definition.Trim())" | ||
$builder.AppendLine("function $function {") | Out-Null | ||
$builder.AppendLine($def) | Out-Null | ||
$builder.AppendLine("}`n") | Out-Null | ||
} | ||
Set-Content -Value $builder.ToString().Trim().Replace("`r`n", "`n") -Path $filePath -Encoding utf8 | ||
} | ||
} | ||
} | ||
} | ||
|
||
end { | ||
Pop-Location | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
function Invoke-CommandRetry { | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory, Position = 0, HelpMessage = 'The command to be invoked.')] | ||
[scriptblock]$Command, | ||
|
||
[Parameter(HelpMessage = 'The number of retries the command should be invoked.')] | ||
[int]$MaxRetries = 10 | ||
) | ||
|
||
Set-Variable -Name retryCount -Value 0 | ||
do { | ||
try { | ||
Invoke-Command -ScriptBlock $Command | ||
$exit = $true | ||
} catch [System.IO.IOException] { | ||
if ($_.Exception.TargetSite.Name -eq 'MoveNext') { | ||
if ($_.ErrorDetails) { | ||
Write-Verbose $_.ErrorDetails.Message | ||
} else { | ||
Write-Verbose $_.Exception.Message | ||
} | ||
Write-Host "`nRetrying..." | ||
} else { | ||
Write-Verbose $_.Exception.GetType().FullName | ||
Write-Error $_ | ||
} | ||
} catch [System.AggregateException] { | ||
if ($_.Exception.InnerException.GetType().Name -eq 'HttpRequestException') { | ||
Write-Verbose $_.Exception.InnerException.Message | ||
Write-Host "`nRetrying..." | ||
} else { | ||
Write-Verbose $_.Exception.InnerException.GetType().FullName | ||
Write-Error $_ | ||
} | ||
} catch { | ||
Write-Verbose $_.Exception.GetType().FullName | ||
Write-Error $_ | ||
} | ||
$retryCount++ | ||
if ($retryCount -eq $MaxRetries) { | ||
$exit = $true | ||
} | ||
} until ($exit) | ||
} | ||
|
||
function Test-IsAdmin { | ||
$currentIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() | ||
$principal = [System.Security.Principal.WindowsPrincipal]$currentIdentity | ||
$admin = [System.Security.Principal.WindowsBuiltInRole]'Administrator' | ||
|
||
return $principal.IsInRole($admin) | ||
} | ||
|
||
function Update-SessionEnvironmentPath { | ||
# instantiate a HashSet to store unique paths | ||
$auxHashSet = [Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase) | ||
|
||
# get Path env variable from all scopes and build unique set | ||
foreach ($scope in @('Machine', 'User', 'Process')) { | ||
[Environment]::GetEnvironmentVariable('Path', $scope).Split(';').Where({ $_ }).ForEach({ | ||
$auxHashSet.Add($_) | Out-Null | ||
} | ||
) | ||
} | ||
|
||
# build a path string from the HashSet | ||
$pathStr = [string]::Join([System.IO.Path]::PathSeparator, $auxHashSet) | ||
# set the Path environment variable in the current process scope | ||
[System.Environment]::SetEnvironmentVariable('Path', $pathStr, 'Process') | ||
} |
Oops, something went wrong.