From e2a0c5432f6af1901b92ea370e6579e9ff6fe5fd Mon Sep 17 00:00:00 2001 From: kieranwalsh Date: Tue, 12 Oct 2021 09:39:14 +0100 Subject: [PATCH 1/3] Works with documents in non-default places. Plus bugfixes. --- InstallModuleFromGitHub.psm1 | 183 +++++++++++++++++++++-------------- 1 file changed, 113 insertions(+), 70 deletions(-) diff --git a/InstallModuleFromGitHub.psm1 b/InstallModuleFromGitHub.psm1 index ea15d76..cc23975 100644 --- a/InstallModuleFromGitHub.psm1 +++ b/InstallModuleFromGitHub.psm1 @@ -1,8 +1,9 @@ -function Install-ModuleFromGitHub { +function Install-ModuleFromGitHub +{ [CmdletBinding()] param( $GitHubRepo, - $Branch = "master", + $Branch = 'master', [Parameter(ValueFromPipelineByPropertyName)] $ProjectUri, $DestinationPath, @@ -11,96 +12,138 @@ function Install-ModuleFromGitHub { $Scope ) - 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 ([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 = "CurrentUser") { - $dest = Join-Path -Path $HOME -ChildPath ".local/share/powershell/Modules" - } else { - $dest = "/usr/local/share/powershell/Modules" - } + } + else + { + if ($Scope -eq 'CurrentUser') + { + $scopedPath = [Environment]::GetFolderPath('MyDocuments') + $scopedChildPath = '\PowerShell\Modules' } - - else { - if ($Scope = "CurrentUser") { - $scopedPath = $HOME - $scopedChildPath = "\Documents\PowerShell\Modules" - } else { - $scopedPath = $env:ProgramFiles - $scopedChildPath = "\PowerShell\Modules" - } - $dest = Join-Path -Path $scopedPath -ChildPath $scopedChildPath + else + { + $scopedPath = $env:ProgramFiles + $scopedChildPath = '\WindowsPowerShell\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) -Include *.psd1 -Recurse + } - if($DestinationPath) { - $dest = $DestinationPath + if($psd1) + { + $ModuleVersion = (Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion + $dest = Join-Path -Path $dest -ChildPath $ModuleVersion + try + { + $null = New-Item -ItemType directory -Path $dest -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 '$dest'. 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 ([System.Environment]::OSVersion.Platform -eq 'Unix') + { + $null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $dest -Force -Recurse + } + else + { + try + { + $null = Copy-Item "$(Join-Path -Path $tmpDir -ChildPath $unzippedArchive\*)" $dest -Force -Recurse -ErrorAction Stop } + catch + { + Write-Output 'Unable to copy files.' + break + } + } } } } # Install-ModuleFromGitHub dfinke/nameit -# Install-ModuleFromGitHub dfinke/nameit TestBranch +# Install-ModuleFromGitHub dfinke/nameit TestBranch \ No newline at end of file From f319efae7dbf1e9b643e881e8b8c01141754d2ed Mon Sep 17 00:00:00 2001 From: kieranwalsh Date: Tue, 12 Oct 2021 13:41:18 +0100 Subject: [PATCH 2/3] Validate scope, bugfixes. --- InstallModuleFromGitHub.psm1 | 63 +++++++++++++++++------------------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/InstallModuleFromGitHub.psm1 b/InstallModuleFromGitHub.psm1 index cc23975..0eb0219 100644 --- a/InstallModuleFromGitHub.psm1 +++ b/InstallModuleFromGitHub.psm1 @@ -8,8 +8,9 @@ function Install-ModuleFromGitHub $ProjectUri, $DestinationPath, $SSOToken, - $moduleName, - $Scope + $ModuleName, + [ValidateSet('CurrentUser', 'AllUsers')] + [string] $Scope = 'AllUsers' ) Process @@ -23,8 +24,8 @@ function Install-ModuleFromGitHub } else { - $name = $ProjectUri.LocalPath.split('/')[-1] - Write-Host -ForegroundColor Red ('Module [{0}]: not installed, it is not hosted on GitHub ' -f $name) + $Name = $ProjectUri.LocalPath.split('/')[-1] + Write-Host -ForegroundColor Red ('Module [{0}]: not installed, it is not hosted on GitHub ' -f $Name) } } @@ -34,9 +35,9 @@ function Install-ModuleFromGitHub $url = 'https://api.github.com/repos/{0}/zipball/{1}' -f $GitHubRepo, $Branch - if ($moduleName) + if ($ModuleName) { - $targetModuleName = $moduleName + $targetModuleName = $ModuleName } else { @@ -71,37 +72,34 @@ function Install-ModuleFromGitHub $unzippedArchive = Get-ChildItem "$tmpDir" Write-Debug "targetModule: $targetModule" - if ([System.Environment]::OSVersion.Platform -eq 'Unix') + if($IsLinux -or $IsMacOS) { if ($Scope -eq 'CurrentUser') { - $dest = Join-Path -Path $HOME -ChildPath '.local/share/powershell/Modules' + $DestinationPath = Join-Path -Path $HOME -ChildPath '.local/share/powershell/Modules' } else { - $dest = '/usr/local/share/powershell/Modules' + $DestinationPath = '/usr/local/share/powershell/Modules' } } else { - if ($Scope -eq 'CurrentUser') - { - $scopedPath = [Environment]::GetFolderPath('MyDocuments') - $scopedChildPath = '\PowerShell\Modules' - } - else + switch ($Scope) { - $scopedPath = $env:ProgramFiles - $scopedChildPath = '\WindowsPowerShell\Modules' + 'CurrentUser' + { + $DestinationPath = "$([Environment]::GetFolderPath('MyDocuments'))\WindowsPowerShell\Modules" + } + 'AllUsers' + { + $DestinationPath = "$($env:ProgramFiles)\WindowsPowerShell\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') + $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 } @@ -113,27 +111,27 @@ function Install-ModuleFromGitHub if($psd1) { $ModuleVersion = (Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion - $dest = Join-Path -Path $dest -ChildPath $ModuleVersion + $DestinationPath = Join-Path -Path $DestinationPath -ChildPath $ModuleVersion try { - $null = New-Item -ItemType directory -Path $dest -Force -ErrorAction Stop + $null = New-Item -ItemType directory -Path $DestinationPath -Force -ErrorAction Stop } catch { - Write-Error "Unable to create the folder '$dest'. Try again running as Administrator." + Write-Error "Unable to create the folder '$DestinationPath'. Try again running as Administrator." break } } - if ([System.Environment]::OSVersion.Platform -eq 'Unix') + if($IsLinux -or $IsMacOS) { - $null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $dest -Force -Recurse + $null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $DestinationPath -Force -Recurse } else { try { - $null = Copy-Item "$(Join-Path -Path $tmpDir -ChildPath $unzippedArchive\*)" $dest -Force -Recurse -ErrorAction Stop + $null = Copy-Item "$(Join-Path -Path $tmpDir -ChildPath $unzippedArchive\*)" $DestinationPath -Force -Recurse -ErrorAction Stop } catch { @@ -143,7 +141,4 @@ function Install-ModuleFromGitHub } } } -} - -# Install-ModuleFromGitHub dfinke/nameit -# Install-ModuleFromGitHub dfinke/nameit TestBranch \ No newline at end of file +} \ No newline at end of file From 89236ce9984c1155e8a984cea85ace58523bb838 Mon Sep 17 00:00:00 2001 From: kieranwalsh Date: Tue, 12 Oct 2021 16:13:30 +0100 Subject: [PATCH 3/3] Updated version. --- InstallModuleFromGitHub.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InstallModuleFromGitHub.psd1 b/InstallModuleFromGitHub.psd1 index 43d0e08..cdf315c 100644 --- a/InstallModuleFromGitHub.psd1 +++ b/InstallModuleFromGitHub.psd1 @@ -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'