2023-11-19 17:42:11
This commit is contained in:
parent
2ad6ae3956
commit
bac39032ec
2 changed files with 60 additions and 27 deletions
|
@ -1,9 +1,9 @@
|
|||
$ModuleManifest = (Get-ChildItem -Path $PSScriptRoot | Where-Object {$_.Extension -eq '.psd1'})
|
||||
$ModuleManifest = (Get-ChildItem -Path $PSScriptRoot | Where-Object { $_.Extension -eq '.psd1' })
|
||||
$CurrentManifest = (Test-ModuleManifest $ModuleManifest)
|
||||
|
||||
$Aliases = @()
|
||||
$PrivateFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Private') | Where-Object {$_.Extension -eq '.ps1'})
|
||||
$PublicFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') | Where-Object {$_.Extension -eq '.ps1'})
|
||||
$PrivateFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Private') | Where-Object { $_.Extension -eq '.ps1' })
|
||||
$PublicFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') | Where-Object { $_.Extension -eq '.ps1' })
|
||||
|
||||
(@($PrivateFunctions) + @($PublicFunctions)) | ForEach-Object {
|
||||
try {
|
||||
|
@ -25,10 +25,10 @@ $PublicFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') | Whe
|
|||
}
|
||||
}
|
||||
|
||||
$FunctionsAdded = ($PublicFunctions | Where-Object {$_.BaseName -notin $CurrentManifest.ExportedFunctions.Keys})
|
||||
$FunctionsRemoved = ($CurrentManifest.ExportedFunctions.Keys | Where-Object {$_ -notin $PublicFunctions.BaseName})
|
||||
$AliasesAdded = ($Aliases | Where-Object {$_ -notin $CurrentManifest.ExportedAliases.Keys})
|
||||
$AliasesRemoved = ($CurrentManifest.ExportedAliases.Keys | Where-Object {$_ -notin $Aliases})
|
||||
$FunctionsAdded = ($PublicFunctions | Where-Object { $_.BaseName -notin $CurrentManifest.ExportedFunctions.Keys })
|
||||
$FunctionsRemoved = ($CurrentManifest.ExportedFunctions.Keys | Where-Object { $_ -notin $PublicFunctions.BaseName })
|
||||
$AliasesAdded = ($Aliases | Where-Object { $_ -notin $CurrentManifest.ExportedAliases.Keys })
|
||||
$AliasesRemoved = ($CurrentManifest.ExportedAliases.Keys | Where-Object { $_ -notin $Aliases })
|
||||
|
||||
if ($FunctionsAdded -or $FunctionsRemoved -or $AliasesAdded -or $AliasesRemoved) {
|
||||
try {
|
||||
|
|
|
@ -1,29 +1,62 @@
|
|||
function Install-CustomModule() {
|
||||
param(
|
||||
[Parameter(Mandatory)][Alias('MN')][string]$ModuleName,
|
||||
[Alias('MP')][string]$ModulePrefix = 'PkgStore',
|
||||
[Alias('MV')][string]$ModuleVersion = 'latest',
|
||||
[Alias('MD')][string]$ModuleDirectory = "$(($Env:PSModulePath -split ';')[0])",
|
||||
[Parameter(Mandatory)][Alias('GHP')][string]$GitHubPath,
|
||||
[Parameter(Mandatory)][Alias('GHT')][string]$GitHubToken
|
||||
)
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Installing a module from GitHub.
|
||||
|
||||
$ModuleFullName = "${ModulePrefix}.${ModuleName}"
|
||||
$ModuleVersion = ($ModuleVersion -eq 'latest') ? $ModuleVersion : "tags/${ModuleVersion}"
|
||||
$ModulePath = (Join-Path $ModuleDirectory $ModuleFullName)
|
||||
$Headers = @{
|
||||
Authorization="Bearer ${GitHubToken}"
|
||||
}
|
||||
.DESCRIPTION
|
||||
Installing a third-party module from GitHub.
|
||||
|
||||
.PARAMETER Name
|
||||
Module name.
|
||||
|
||||
.PARAMETER Prefix
|
||||
Module prefix.
|
||||
|
||||
.PARAMETER Version
|
||||
Module version.
|
||||
|
||||
.PARAMETER Directory
|
||||
Directory for installing the module.
|
||||
|
||||
.PARAMETER GitHubPath
|
||||
Organization and repository of the module on GitHub.
|
||||
|
||||
.PARAMETER GitHubToken
|
||||
Token for GitHub API.
|
||||
|
||||
.EXAMPLE
|
||||
Install-CustomModule -Name 'Kernel' -GitHubPath 'pkgstore/pwsh-kernel'
|
||||
|
||||
.EXAMPLE
|
||||
Install-CustomModule -Name 'Kernel' -GitHubPath 'pkgstore/pwsh-kernel' -Version 'v0.1.0'
|
||||
|
||||
.EXAMPLE
|
||||
Install-CustomModule -Name 'Kernel' -GitHubPath 'pkgstore/pwsh-kernel' -GitHubToken '<TOKEN>' -Version 'v0.1.0'
|
||||
#>
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory)][Alias('N')][string]$Name,
|
||||
[Alias('P')][string]$Prefix = 'PkgStore',
|
||||
[Alias('V')][string]$Version = 'latest',
|
||||
[Alias('D')][string]$Directory = "$(($Env:PSModulePath -split ';')[0])",
|
||||
[Parameter(Mandatory)][Alias('GHP')][string]$GitHubPath,
|
||||
[Alias('GHT')][string]$GitHubToken
|
||||
)
|
||||
|
||||
$FullName = "${Prefix}.${Name}"
|
||||
$Version = ($Version -eq 'latest') ? $Version : "tags/${Version}"
|
||||
$Path = (Join-Path $Directory $FullName)
|
||||
$RestHeaders = ($GitHubToken) ? @{Authorization="Bearer ${GitHubToken}"} : @{}
|
||||
|
||||
try {
|
||||
Invoke-RestMethod -Headers $Headers -Uri "https://api.github.com/repos/${GitHubPath}/releases/${ModuleVersion}"
|
||||
Invoke-RestMethod -Headers $RestHeaders -Uri "https://api.github.com/repos/${GitHubPath}/releases/${Version}"
|
||||
| ForEach-Object 'zipball_url'
|
||||
| ForEach-Object { Invoke-WebRequest $_ -OutFile "${ModulePath}.zip" }
|
||||
| ForEach-Object { Invoke-WebRequest $_ -OutFile "${Path}.zip" }
|
||||
|
||||
Expand-Archive -Path "${ModulePath}.zip" -DestinationPath "${ModuleDirectory}"
|
||||
if (Test-Path -Path "${ModulePath}") { Remove-Item -Path "${ModulePath}" -Recurse -Force}
|
||||
Rename-Item -Path (Join-Path $ModuleDirectory "$($GitHubPath.replace('/','-'))-*") -NewName "${ModulePath}"
|
||||
Remove-Item -Path "${ModulePath}.zip"
|
||||
Expand-Archive -Path "${Path}.zip" -DestinationPath "${Directory}"
|
||||
if (Test-Path -Path "${Path}") { Remove-Item -Path "${Path}" -Recurse -Force }
|
||||
Rename-Item -Path (Join-Path $Directory "$($GitHubPath.replace('/','-'))-*") -NewName "${Path}"
|
||||
Remove-Item -Path "${Path}.zip"
|
||||
} catch {
|
||||
$_ | Write-Error
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue