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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ $RECYCLE.BIN/
Network Trash Folder
Temporary Items
.apdisk

test.ps1
.profile.ps1
.git
.vscode
launch.json
.gitignore
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.7.0'
ModuleVersion = '1.8.1'

# ID used to uniquely identify this module
GUID = '2997e240-5cec-4543-a231-573576b78c88'
Expand Down
219 changes: 148 additions & 71 deletions InstallModuleFromGitHub.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Install-ModuleFromGitHub {
function Install-ModuleFromGitHub
{
[CmdletBinding()]
param(
$GitHubRepo,
Expand All @@ -8,104 +9,180 @@ function Install-ModuleFromGitHub {
$DestinationPath,
$SSOToken,
$moduleName,
[ValidateSet('CurrentUser','AllUsers')]
[ValidateSet('CurrentUser', 'AllUsers')]
[string]
$Scope
$Scope = "CurrentUser"
)

Process {
if($PSBoundParameters.ContainsKey("ProjectUri")) {
Process
{
Write-Verbose ("[$(Get-Date)] Powershell : {0}.{1}" -f $PSVersionTable.psVersion.Major, $PSVersionTable.psVersion.Minor)

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]
}
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)
{
$url = "https://api.github.com/repos/{0}/zipball/{1}" -f $GitHubRepo, $Branch
Write-Verbose ("[$(Get-Date)] Repository : {0}" -f "https://api.github.com/repos/$GitHubRepo")
Write-Verbose ("[$(Get-Date)] Branch : {0}" -f "$Branch")
Write-Verbose ("[$(Get-Date)] Retrieving : {0} {1}" -f $GitHubRepo, $Branch)
Write-Verbose ("[$(Get-Date)] Download from : {0}" -f $url)

if ($moduleName)
{
$targetModuleName = $moduleName
}
else
{
$targetModuleName = $GitHubRepo.split('/')[-1]
}
Write-Verbose ("[$(Get-Date)] targetModuleName : {0}" -f $targetModuleName)
Write-Debug "targetModuleName: $targetModuleName"

$url = "https://api.github.com/repos/{0}/zipball/{1}" -f $GitHubRepo, $Branch
$tmpDir = [System.IO.Path]::GetTempPath()

if ($moduleName) {
$targetModuleName = $moduleName
} else {
$targetModuleName=$GitHubRepo.split('/')[-1]
}
Write-Debug "targetModuleName: $targetModuleName"
$OutFile = Join-Path -Path $tmpDir -ChildPath "$($targetModuleName).zip"
Write-Verbose ("[$(Get-Date)] Output File : {0}" -f $OutFile)
Write-Debug "OutFile: $OutFile"

$tmpDir = [System.IO.Path]::GetTempPath()
if ($SSOToken)
{
$headers = @{"Authorization" = "token $SSOToken" }
Write-Verbose ("[$(Get-Date)] SSO Token : {0}" -f $SSOToken)
}
else
{
Write-Verbose ("[$(Get-Date)] SSO Token : N/A")
}

#enable TLS1.2 encryption
if (-not ($IsLinux -or $IsMacOS))
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}
Invoke-RestMethod $url -OutFile $OutFile -Headers $headers

$OutFile = Join-Path -Path $tmpDir -ChildPath "$($targetModuleName).zip"
Write-Debug "OutFile: $OutFile"
if (-not ([System.Environment]::OSVersion.Platform -eq "Unix"))
{
Unblock-File $OutFile
}

if ($SSOToken) {$headers = @{"Authorization" = "token $SSOToken" }}
$fileHash = $(Get-FileHash -Path $OutFile).hash
$tmpDir = "$tmpDir/$fileHash"

#enable TLS1.2 encryption
if (-not ($IsLinux -or $IsMacOS)) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (Test-Path $tmpDir)
{
Write-Warning ("[$(Get-Date)] Checking Folder : {0} already exists, re-using content" -f $tmpDir)
}
else
{
New-Item $tmpDir -ItemType Directory
Write-Verbose ("[$(Get-Date)] Checking Folder : {0} created" -f $tmpDir)
}

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

$unzippedArchive = Get-ChildItem "$tmpDir"
Write-Debug "targetModule: $targetModule"
Write-Verbose ("[$(Get-Date)] targetModuleName : {0}" -f $targetModuleName)

if ([System.Environment]::OSVersion.Platform -eq "Unix")
{
if ($Scope -eq "CurrentUser")
{
$dest = 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
{
$dest = "/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 -eq "CurrentUser") {
$dest = Join-Path -Path $HOME -ChildPath ".local/share/powershell/Modules"
} else {
$dest = "/usr/local/share/powershell/Modules"
}
else
{
Write-Verbose ("[$(Get-Date)] Using Scope : {0}" -f $Scope)
if ($Scope -eq "CurrentUser")
{
# Current User
if ($psVersionTable.PSVersion.Major -eq "5")
{
$scopedPath = [Environment]::GetFolderPath('MyDocuments')
$scopedChildPath = "\WindowsPowerShell\Modules"
}
elseif ($psVersionTable.PSVersion.Major -eq "7")
{
$scopedPath = [Environment]::GetFolderPath('MyDocuments')
$scopedChildPath = "\PowerShell\Modules"
}
}

else {
if ($Scope -eq "CurrentUser") {
$scopedPath = $HOME
$scopedChildPath = "\Documents\PowerShell\Modules"
} else {
else
{
# All Users
if ($psVersionTable.PSVersion.Major -eq "5")
{
$scopedPath = $env:ProgramFiles
$scopedChildPath = "\WindowsPowerShell\Modules"
}
elseif ($psVersionTable.PSVersion.Major -eq "7")
{
$scopedPath = $env:ProgramFiles
$scopedChildPath = "\PowerShell\Modules"
}
$dest = Join-Path -Path $scopedPath -ChildPath $scopedChildPath
}

if($DestinationPath) {
$dest = $DestinationPath
}
$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.Name) -Include *.psd1 -Recurse
}

$sourcePath = $unzippedArchive.FullName

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
$sourcePath = $psd1.DirectoryName
}
$dest = Join-Path -Path $scopedPath -ChildPath $scopedChildPath
}

if ($DestinationPath)
{
$dest = $DestinationPath
}
$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.Name) -Include *.psd1 -Recurse
}

if ([System.Environment]::OSVersion.Platform -eq "Unix") {
$null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $dest -Force -Recurse
} else {
$null = Copy-Item "$sourcePath\*" $dest -Force -Recurse
}
$sourcePath = $unzippedArchive.FullName

if ($psd1)
{
$ModuleVersion = (Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion
Write-Verbose ("[$(Get-Date)] Module version : {0}" -f $ModuleVersion)

$dest = Join-Path -Path $dest -ChildPath $ModuleVersion
Write-Verbose ("[$(Get-Date)] Destination : {0}" -f $dest)

$null = New-Item -ItemType directory -Path $dest -Force
$sourcePath = $psd1.DirectoryName
}

if ([System.Environment]::OSVersion.Platform -eq "Unix")
{
$null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $dest -Force -Recurse
}
else
{
$null = Copy-Item "$sourcePath\*" $dest -Force -Recurse
}
}
Write-Verbose "[$(Get-Date)] Status : Installer finished"
}
}

Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ Not all PowerShell Modules are published to the PowerShellGallery but are hosted

## Changes

## 1.8.1
- Add: Default $Scope to CurrentUser if $Scope is'nt entered
- Add: More verbose output
- Fix: PSModulePath is different between Windows Powershell 5.1 and Powershell 7.0
$HOME can't be used when documents folder is moved with folder redirection

Current User:
5.1 [Environment]::GetFolderPath('MyDocuments')\WindowsPowerShell\Modules
7.0 [Environment]::GetFolderPath('MyDocuments')\PowerShell\Modules

All User:
5.1 $env:ProgramFiles\WindowsPowerShell\Modules
7.0 $env:ProgramFiles\PowerShell\Modules


## 1.8.0
- Add: Create folder first before expanding archive
- Add: Test-Path to see if tmpDir already exist

## 1.7.0
https://github.com/dfinke/InstallModuleFromGitHub/pull/32
- Add tab-completion for -Scope by @cspotcode

https://github.com/dfinke/InstallModuleFromGitHub/pull/25
- fix unzippedArchive path when searching for psd1 file by @joshschmitter

## 1.6.0

via https://github.com/dfinke/InstallModuleFromGitHub/pull/25
Expand Down