Update samy.ps1
This commit is contained in:
22
samy.ps1
22
samy.ps1
@@ -82,8 +82,8 @@ function Import-SamyModule {
|
|||||||
- Fallback to .\<Name>
|
- Fallback to .\<Name>
|
||||||
|
|
||||||
Remote (iwr | iex):
|
Remote (iwr | iex):
|
||||||
- Try .../module/<Name>?raw=1 first
|
- Try .../module/<Name> first
|
||||||
- If that 404s, fallback to .../<Name>?raw=1
|
- If that 404s, fallback to .../<Name>
|
||||||
#>
|
#>
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
param(
|
param(
|
||||||
@@ -92,8 +92,8 @@ function Import-SamyModule {
|
|||||||
|
|
||||||
# 1) Local dev mode: script saved to disk
|
# 1) Local dev mode: script saved to disk
|
||||||
if ($PSCommandPath) {
|
if ($PSCommandPath) {
|
||||||
$moduleRoot = Join-Path -Path $PSScriptRoot -ChildPath 'module'
|
$moduleRoot = Join-Path -Path $PSScriptRoot -ChildPath 'module'
|
||||||
$localModulePath = Join-Path -Path $moduleRoot -ChildPath $Name
|
$localModulePath = Join-Path -Path $moduleRoot -ChildPath $Name
|
||||||
$localRootPath = Join-Path -Path $PSScriptRoot -ChildPath $Name
|
$localRootPath = Join-Path -Path $PSScriptRoot -ChildPath $Name
|
||||||
|
|
||||||
if (Test-Path $localModulePath) {
|
if (Test-Path $localModulePath) {
|
||||||
@@ -108,19 +108,18 @@ function Import-SamyModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# 2) Remote mode (iwr | iex): pull module from repo
|
# 2) Remote mode (iwr | iex): pull module from repo
|
||||||
$base = "$Script:SamyRepoBase/$Script:SamyBranch"
|
$base = "{0}/{1}" -f $Script:SamyRepoBase, $Script:SamyBranch
|
||||||
|
|
||||||
$primaryUrl = "$base/module/$Name?raw=1"
|
$primaryUrl = "{0}/module/{1}" -f $base, $Name
|
||||||
$fallbackUrl = "$base/$Name?raw=1"
|
$fallbackUrl = "{0}/{1}" -f $base, $Name
|
||||||
|
|
||||||
# Helper to download and load a URL
|
|
||||||
function Invoke-LoadUrl {
|
function Invoke-LoadUrl {
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)][string]$Url,
|
[Parameter(Mandatory)][string]$Url,
|
||||||
[Parameter(Mandatory)][string]$ModuleName
|
[Parameter(Mandatory)][string]$ModuleName
|
||||||
)
|
)
|
||||||
|
|
||||||
$resp = Invoke-WebRequest -Uri $Url -UseBasicParsing -ErrorAction Stop
|
$resp = Invoke-WebRequest -Uri $Url -UseBasicParsing -ErrorAction Stop
|
||||||
$content = $resp.Content
|
$content = $resp.Content
|
||||||
if (-not $content) {
|
if (-not $content) {
|
||||||
Write-Host ("[Error] Module {0} from {1} returned empty content." -f $ModuleName, $Url) -ForegroundColor Red
|
Write-Host ("[Error] Module {0} from {1} returned empty content." -f $ModuleName, $Url) -ForegroundColor Red
|
||||||
@@ -130,7 +129,7 @@ function Import-SamyModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
# Try /module/<Name>?raw=1 first
|
# Try /module/<Name> first
|
||||||
Invoke-LoadUrl -Url $primaryUrl -ModuleName $Name
|
Invoke-LoadUrl -Url $primaryUrl -ModuleName $Name
|
||||||
}
|
}
|
||||||
catch [System.Net.WebException] {
|
catch [System.Net.WebException] {
|
||||||
@@ -141,7 +140,6 @@ function Import-SamyModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($statusCode -eq 404) {
|
if ($statusCode -eq 404) {
|
||||||
# Fallback to root-level file
|
|
||||||
Write-Host ("[Info] Module {0} not found at {1} (404). Trying fallback {2}." -f $Name, $primaryUrl, $fallbackUrl) -ForegroundColor Yellow
|
Write-Host ("[Info] Module {0} not found at {1} (404). Trying fallback {2}." -f $Name, $primaryUrl, $fallbackUrl) -ForegroundColor Yellow
|
||||||
try {
|
try {
|
||||||
Invoke-LoadUrl -Url $fallbackUrl -ModuleName $Name
|
Invoke-LoadUrl -Url $fallbackUrl -ModuleName $Name
|
||||||
@@ -158,7 +156,7 @@ function Import-SamyModule {
|
|||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
if (-not ($_ -is [System.Net.WebException])) {
|
if (-not ($_ -is [System.Net.WebException])) {
|
||||||
Write-Host ("[Error] Failed to load module {0} from {1}: {2}" -f $Name, $primaryUrl, $_.Exception.Message) -ForegroundColor Red
|
Write-Host ("[Error] Failed to load module {0}: {1}" -f $Name, $_.Exception.Message) -ForegroundColor Red
|
||||||
}
|
}
|
||||||
throw
|
throw
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user