Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion InstallModuleFromGitHub.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
RootModule = 'InstallModuleFromGitHub.psm1'

# Version number of this module.
ModuleVersion = '1.5.0'
ModuleVersion = '1.5.1'

# ID used to uniquely identify this module
GUID = '2997e240-5cec-4543-a231-573576b78c88'
Expand Down
186 changes: 112 additions & 74 deletions InstallModuleFromGitHub.psm1
Original file line number Diff line number Diff line change
@@ -1,106 +1,144 @@
function Install-ModuleFromGitHub {
function Install-ModuleFromGitHub
{
[CmdletBinding()]
param(
$GitHubRepo,
$Branch = "master",
$Branch = 'master',
[Parameter(ValueFromPipelineByPropertyName)]
$ProjectUri,
$DestinationPath,
$SSOToken,
$moduleName,
$Scope
$ModuleName,
[ValidateSet('CurrentUser', 'AllUsers')]
[string] $Scope = 'AllUsers'
)

Process {
if($PSBoundParameters.ContainsKey("ProjectUri")) {
Process
{
if($PSBoundParameters.ContainsKey('ProjectUri'))
{
$GitHubRepo = $null
if($ProjectUri.OriginalString.StartsWith("https://github.com")) {
if($ProjectUri.OriginalString.StartsWith('https://github.com'))
{
$GitHubRepo = $ProjectUri.AbsolutePath
} else {
$name=$ProjectUri.LocalPath.split('/')[-1]
Write-Host -ForegroundColor Red ("Module [{0}]: not installed, it is not hosted on GitHub " -f $name)
}
else
{
$Name = $ProjectUri.LocalPath.split('/')[-1]
Write-Host -ForegroundColor Red ('Module [{0}]: not installed, it is not hosted on GitHub ' -f $Name)
}
}

if($GitHubRepo) {
Write-Verbose ("[$(Get-Date)] Retrieving {0} {1}" -f $GitHubRepo, $Branch)
if($GitHubRepo)
{
Write-Verbose ("[$(Get-Date)] Retrieving {0} {1}" -f $GitHubRepo, $Branch)

$url = "https://api.github.com/repos/{0}/zipball/{1}" -f $GitHubRepo, $Branch
$url = 'https://api.github.com/repos/{0}/zipball/{1}' -f $GitHubRepo, $Branch

if ($moduleName) {
$targetModuleName = $moduleName
} else {
$targetModuleName=$GitHubRepo.split('/')[-1]
}
Write-Debug "targetModuleName: $targetModuleName"
if ($ModuleName)
{
$targetModuleName = $ModuleName
}
else
{
$targetModuleName = $GitHubRepo.split('/')[-1]
}
Write-Debug "targetModuleName: $targetModuleName"

$tmpDir = [System.IO.Path]::GetTempPath()

$tmpDir = [System.IO.Path]::GetTempPath()
$OutFile = Join-Path -Path $tmpDir -ChildPath "$($targetModuleName).zip"
Write-Debug "OutFile: $OutFile"

$OutFile = Join-Path -Path $tmpDir -ChildPath "$($targetModuleName).zip"
Write-Debug "OutFile: $OutFile"
if ($SSOToken)
{
$headers = @{'Authorization' = "token $SSOToken" }
}

if ($SSOToken) {$headers = @{"Authorization" = "token $SSOToken" }}
#enable TLS1.2 encryption
if (-not ($IsLinux -or $IsMacOS))
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}
Invoke-RestMethod $url -OutFile $OutFile -Headers $headers
if (-not ([System.Environment]::OSVersion.Platform -eq 'Unix'))
{
Unblock-File $OutFile
}

#enable TLS1.2 encryption
if (-not ($IsLinux -or $IsMacOS)) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$fileHash = $(Get-FileHash -Path $OutFile).hash
$tmpDir = "$tmpDir/$fileHash"
Expand-Archive -Path $OutFile -DestinationPath $tmpDir -Force

$unzippedArchive = Get-ChildItem "$tmpDir"
Write-Debug "targetModule: $targetModule"
if($IsLinux -or $IsMacOS)
{
if ($Scope -eq 'CurrentUser')
{
$DestinationPath = Join-Path -Path $HOME -ChildPath '.local/share/powershell/Modules'
}
Invoke-RestMethod $url -OutFile $OutFile -Headers $headers
if (-not ([System.Environment]::OSVersion.Platform -eq "Unix")) {
Unblock-File $OutFile
else
{
$DestinationPath = '/usr/local/share/powershell/Modules'
}

$fileHash = $(Get-FileHash -Path $OutFile).hash
$tmpDir = "$tmpDir/$fileHash"

Expand-Archive -Path $OutFile -DestinationPath $tmpDir -Force

$unzippedArchive = get-childItem "$tmpDir"
Write-Debug "targetModule: $targetModule"

if ([System.Environment]::OSVersion.Platform -eq "Unix") {
if ($Scope = "CurrentUser") {
$dest = Join-Path -Path $HOME -ChildPath ".local/share/powershell/Modules"
} else {
$dest = "/usr/local/share/powershell/Modules"
}
else
{
switch ($Scope)
{
'CurrentUser'
{
$DestinationPath = "$([Environment]::GetFolderPath('MyDocuments'))\WindowsPowerShell\Modules"
}
}

else {
if ($Scope = "CurrentUser") {
$scopedPath = $HOME
$scopedChildPath = "\Documents\PowerShell\Modules"
} else {
$scopedPath = $env:ProgramFiles
$scopedChildPath = "\PowerShell\Modules"
'AllUsers'
{
$DestinationPath = "$($env:ProgramFiles)\WindowsPowerShell\Modules"
}
$dest = Join-Path -Path $scopedPath -ChildPath $scopedChildPath
}
}
$DestinationPath = Join-Path -Path $DestinationPath -ChildPath $targetModuleName
Write-Verbose "The module will be saved to '$DestinationPath'."
if($IsLinux -or $IsMacOS)
{
$psd1 = Get-ChildItem (Join-Path -Path $unzippedArchive -ChildPath *) -Include *.psd1 -Recurse
}
else
{
$psd1 = Get-ChildItem (Join-Path -Path $tmpDir -ChildPath $unzippedArchive) -Include *.psd1 -Recurse
}

if($DestinationPath) {
$dest = $DestinationPath
if($psd1)
{
$ModuleVersion = (Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion
$DestinationPath = Join-Path -Path $DestinationPath -ChildPath $ModuleVersion
try
{
$null = New-Item -ItemType directory -Path $DestinationPath -Force -ErrorAction Stop
}
$dest = Join-Path -Path $dest -ChildPath $targetModuleName
if ([System.Environment]::OSVersion.Platform -eq "Unix") {
$psd1 = Get-ChildItem (Join-Path -Path $unzippedArchive -ChildPath *) -Include *.psd1 -Recurse
} else {
$psd1 = Get-ChildItem (Join-Path -Path $tmpDir -ChildPath $unzippedArchive) -Include *.psd1 -Recurse
}

if($psd1) {
$ModuleVersion=(Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion
$dest = Join-Path -Path $dest -ChildPath $ModuleVersion
$null = New-Item -ItemType directory -Path $dest -Force
catch
{
Write-Error "Unable to create the folder '$DestinationPath'. Try again running as Administrator."
break
}
}

if ([System.Environment]::OSVersion.Platform -eq "Unix") {
$null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $dest -Force -Recurse
} else {
$null = Copy-Item "$(Join-Path -Path $tmpDir -ChildPath $unzippedArchive\*)" $dest -Force -Recurse
if($IsLinux -or $IsMacOS)
{
$null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $DestinationPath -Force -Recurse
}
else
{
try
{
$null = Copy-Item "$(Join-Path -Path $tmpDir -ChildPath $unzippedArchive\*)" $DestinationPath -Force -Recurse -ErrorAction Stop
}
catch
{
Write-Output 'Unable to copy files.'
break
}
}
}
}
}

# Install-ModuleFromGitHub dfinke/nameit
# Install-ModuleFromGitHub dfinke/nameit TestBranch
}