Created
June 20, 2016 18:51
-
-
Save Xainey/d286bc2a9ae52820ac521dde2f31478e to your computer and use it in GitHub Desktop.
Require/Install Modules for PSake Tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Work in Progress | |
Sync-Modules -Package ("PSGallery/Pester:3.4.*", "PSGallery/PSake:*") -Scope CurrentUser -Force | |
Module version for Pester must be in the rage: <= 3.4.0 and > 3.5 | |
Module version for PSake: latest | |
#> | |
function Sync-Modules | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string[]] $Package, | |
[Parameter(Mandatory=$false)] | |
[string] $Force = $false, | |
[Parameter(Mandatory=$false)] | |
[string] $Scope = 'All Users' | |
) | |
foreach ($module in $Package) | |
{ | |
$meta = (test-meta -package $module) | |
$repository = ($meta.Clone()) | |
$repository.Remove("Repository") | |
$meta | |
if($meta.RequiredVersion) | |
{ | |
Write-Verbose "specific version required" | |
} | |
elseif($meta.MaximumVersion -and $meta.MinimumVersion) | |
{ | |
Write-Verbose "max/min required" | |
} | |
else | |
{ | |
Write-Verbose "Install latest" | |
} | |
if(!(Get-InstalledModule @repository -ErrorAction SilentlyContinue)) | |
{ | |
Write-Verbose "Correct Version not Installed" | |
Install-Module @module -Repository $meta.Repository -Force $Force | |
} | |
else | |
{ | |
Write-Verbose "Module Installed" | |
} | |
} | |
} | |
# Latest Version | |
#test-version -version "*" | |
# Limit Revision | |
#test-version -version "1.0.*" | |
# Limit Minor | |
#test-version -version "1.*" | |
#Specific Version | |
#test-version -package "PSGallery/Pester:1.1.1" | |
function Test-Meta | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string] $package | |
) | |
$input = $package.Split(":") | |
$meta = $input[0].Split("/") | |
$version = $input[1] | |
$repo = $meta[0] | |
$module = $meta[1] | |
Write-Verbose "Repo: $repo; Module: $module; Version: $version" | |
#split and fill 3-part version | |
$versions = {$version.Split('.')}.Invoke() | |
for($i=$versions.Count; $i -lt 3; $i++) | |
{ | |
$versions.Add("*") | |
} | |
$major = $minor = $revis = $null | |
[int32]::TryParse($versions[0], [ref]$major ) | Out-Null | |
[int32]::TryParse($versions[1], [ref]$minor ) | Out-Null | |
[int32]::TryParse($versions[2], [ref]$revis ) | Out-Null | |
#Specific: Install specific version | |
if (!($versions -match '\*')) | |
{ | |
Write-Verbose "Specific Version" | |
return @{ | |
Repository = $repo | |
Name = $module | |
RequiredVersion = New-Object Version -ArgumentList ("{0}.{1}.{2}" -f $major, $minor, $revis) | |
} | |
} | |
#major: Install Latest | |
if($versions[0] -eq "*") | |
{ | |
Write-Verbose "Major *" | |
return @{ | |
Repository = $repo | |
Name = $module | |
} | |
} | |
#minor | |
if($versions[1] -eq "*") | |
{ | |
Write-Verbose "Minor *" | |
return @{ | |
Repository = $repo | |
Name = $module | |
MinimumVersion = "{0}.{1}" -f $major, $minor | |
MaximumVersion = "{0}.{1}" -f $major, [int32]::maxvalue | |
} | |
} | |
#revis | |
if($versions[2] -eq "*") | |
{ | |
Write-Verbose "Revision *" | |
return @{ | |
Repository = $repo | |
Name = $module | |
MinimumVersion = "{0}.{1}.{2}" -f $major, $minor, 0 | |
MaximumVersion = "{0}.{1}.{2}" -f $major, $minor, [int32]::maxvalue | |
} | |
} | |
} | |
cls | |
Sync-Modules -Package ("PSGallery/Pester:3.4.*", "PSGallery/PSake:*") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment