Skip to content
Merged
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
12 changes: 6 additions & 6 deletions Examples/Install-PSDepend.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# No PowerShellGallery? Run this to install PSDepend.
# No PowerShellGallery? Run this to install PSDepend.
# Downloads nuget to your ~\ home directory
# Creates $Path (and full path to it)
# Downloads module to $Path\PSDepend
Expand All @@ -7,17 +7,17 @@
# Example: .\Install-PSDepend -Path C:\Modules (Installs to C:\Modules\PSDepend)
param( [string]$Path ) # Parent folder PSDepend is installed to. Defaults to Profile\WindowsPowerShell\Modules and creates dir if needed

if(-not $Path) { $Path = Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'WindowsPowerShell\Modules' }
if (-not $Path) { $Path = Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'WindowsPowerShell\Modules' }

# Bootstrap nuget if we don't have it
if(-not ($NugetPath = (Get-Command 'nuget.exe' -ErrorAction SilentlyContinue).Path)) {
if (-not ($NugetPath = (Get-Command 'nuget.exe' -ErrorAction SilentlyContinue).Path)) {
$NugetPath = Join-Path $ENV:USERPROFILE nuget.exe
if(-not (Test-Path $NugetPath)) { Invoke-WebRequest -uri 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe' -OutFile $NugetPath }
if (-not (Test-Path $NugetPath)) { Invoke-WebRequest -Uri 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe' -OutFile $NugetPath }
}

# Bootstrap PSDepend, re-use nuget.exe for the module
if($path) { $null = New-Item $path -ItemType Directory -Force }
if ($path) { $null = New-Item $path -ItemType Directory -Force }
$NugetParams = 'install', 'PSDepend', '-Source', 'https://www.powershellgallery.com/api/v2/',
'-ExcludeVersion', '-NonInteractive', '-OutputDirectory', $Path
'-ExcludeVersion', '-NonInteractive', '-OutputDirectory', $Path
& $NugetPath @NugetParams
Move-Item -Path $NugetPath -Destination "$(Join-Path $Path PSDepend)\nuget.exe" -Force
30 changes: 17 additions & 13 deletions PSDepend/PSDependScripts/Chocolatey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ function Get-ChocoInstalledPackage {
'--local-only'
)
$invokeExternalCommandSplat = @{
Command = 'choco.exe'
Command = 'choco.exe'
Arguments = $chocoParams
PassThru = $true
PassThru = $true
}
$convertFromCsvSplat = @{
Header = 'Name', 'Version'
Header = 'Name', 'Version'
Delimiter = "|"
}
Invoke-ExternalCommand @invokeExternalCommandSplat | ConvertFrom-Csv @convertFromCsvSplat
Expand Down Expand Up @@ -118,12 +118,12 @@ function Get-ChocoLatestPackage {
}

$invokeExternalCommandSplat = @{
Command = 'choco.exe'
Command = 'choco.exe'
Arguments = $chocoParams
PassThru = $true
PassThru = $true
}
$convertFromCsvSplat = @{
Header = 'Name', 'Version'
Header = 'Name', 'Version'
Delimiter = "|"
}
Invoke-ExternalCommand @invokeExternalCommandSplat | ConvertFrom-Csv @convertFromCsvSplat
Expand Down Expand Up @@ -172,7 +172,7 @@ function Invoke-ChocoInstallPackage {
}

$invokeExternalCommandSplat = @{
Command = 'choco.exe'
Command = 'choco.exe'
Arguments = $chocoParams
}
Invoke-ExternalCommand @invokeExternalCommandSplat
Expand Down Expand Up @@ -209,7 +209,8 @@ if (-not (Get-Command -Name 'choco.exe' -ErrorAction SilentlyContinue)) {
try {
Invoke-WebRequest -UseBasicParsing -Uri $ChocoInstallScriptUrl -OutFile $scriptPath
& $scriptPath
} catch {
}
catch {
throw "Unable to install Chocolatey from '$scriptUrl'."
}
}
Expand Down Expand Up @@ -239,14 +240,15 @@ Write-Verbose "Getting package [$Name] version, if it is installed."
$existingVersion = (Get-ChocoInstalledPackage -Name $Name).Version
if ($existingVersion) {
Write-Verbose "Found package [$Name] installed with version [$existingVersion]."
} else {
}
else {
Write-Verbose "Package [$Name] not installed."
}

# Version latest requested, and equal to current
if ($Version -ne 'latest' -and $Version -eq $existingVersion) {
Write-Verbose "You have the requested version [$Version] of [$Name]"
if($PSDependAction -contains 'Test') {
if ($PSDependAction -contains 'Test') {
return $true
}

Expand All @@ -266,7 +268,8 @@ Write-Verbose "Getting latest package [$Name] version from source [$Source]."
$repositoryVersion = (Get-ChocoLatestPackage @repoParams).Version
if ($repositoryVersion) {
Write-Verbose "Found package [$Name] version [$repositoryVersion] on source [$Source]."
} else {
}
else {
Write-Verbose "Package [$Name] not found on source [$Source]. Nothing more can be done."
return # cannot continue
}
Expand All @@ -277,7 +280,7 @@ if (
([System.Version]$repositoryVersion -le [System.Version]$existingVersion)
) {
Write-Verbose "You have the latest version of [$Name], with installed version [$existingVersion] and Source version [$repositoryVersion]"
if($PSDependAction -contains 'Test') {
if ($PSDependAction -contains 'Test') {
return $true
}

Expand All @@ -300,6 +303,7 @@ if ($PSDependAction -contains 'Install') {
}

Invoke-ChocoInstallPackage @params
} elseif ($PSDependAction -contains 'Test' -and $PSDependAction.count -eq 1) {
}
elseif ($PSDependAction -contains 'Test' -and $PSDependAction.count -eq 1) {
return $false
}
18 changes: 6 additions & 12 deletions PSDepend/PSDependScripts/Command.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,18 @@ param (

Write-Verbose "Executing $($Dependency.count) commands"

foreach($Depend in $Dependency)
{
foreach($Command in $Depend.Source)
{
foreach ($Depend in $Dependency) {
foreach ($Command in $Depend.Source) {
Write-Verbose "Invoking command [$($Dependency.DependencyName)]:`n$Command"
$ScriptBlock = [ScriptBlock]::Create($Command)
Try
{
Try {
. $ScriptBlock
}
Catch
{
if($FailOnError)
{
Catch {
if ($FailOnError) {
throw $_
}
else
{
else {
Write-Error $_
continue
}
Expand Down
9 changes: 6 additions & 3 deletions PSDepend/PSDependScripts/DotnetSdk.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ if ($PSDependAction -contains 'Install') {
# If the InstallDir is not set, set it to the 'global' path
if (!$InstallDir) {
$InstallTo = $globalDotnetSdkLocation
} else {
}
else {
$InstallTo = $InstallDir
}
Install-Dotnet -Channel $Channel -Version $Version -InstallDir $InstallTo
Expand All @@ -93,13 +94,15 @@ if ($PSDependAction -contains 'Import') {
# If the InstallDir was specified and the .NET Core SDK exists in it, add it to the path
if ($InstallDir -and (Test-Dotnet -Version $Version -InstallDir $InstallDir)) {
$env:PATH = "$InstallDir$([IO.Path]::PathSeparator)$env:PATH"
} else {
}
else {
# Test if it's in the path already and if it's not check if it's in the 'global' location
$dotnetInPath = Get-Command 'dotnet' -ErrorAction SilentlyContinue
if (!$dotnetInPath) {
if (!(Test-Dotnet -Version $Version -InstallDir $globalDotnetSdkLocation)) {
throw ".NET SDK cannot be located. Try installing using PSDepend."
} else {
}
else {
$env:PATH = "$globalDotnetSdkLocation$([IO.Path]::PathSeparator)$env:PATH"
}
}
Expand Down
154 changes: 68 additions & 86 deletions PSDepend/PSDependScripts/FileDownload.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,116 +46,98 @@ param(
)

function Parse-URLForFile {
[cmdletbinding()]
param($URL)
[cmdletbinding()]
param($URL)
# This will need work. Assume leaf is file. If CGI exists in leaf, assume it is after the file
$FileName = $URL.split('/')[-1]
if($FileName -match '\?')
{
if ($FileName -match '\?') {
$FileName.split('?')[0]
}
else
{
else {
$FileName
}
Write-Verbose "Parsed file name [$FileName] from `$URL"
}

# Extract data from Dependency
$DependencyName = $Dependency.DependencyName
$Name = $Dependency.Name
$Target = $Dependency.Target
$Source = $Dependency.Source

# Pick the URL
if($Source)
{
$URL = $Source
}
else
{
$URL = $DependencyName
}
Write-Verbose "Using URL: $URL"
$DependencyName = $Dependency.DependencyName
$Name = $Dependency.Name
$Target = $Dependency.Target
$Source = $Dependency.Source

# Pick the URL
if ($Source) {
$URL = $Source
}
else {
$URL = $DependencyName
}
Write-Verbose "Using URL: $URL"

# Act on target path....
$ToInstall = $False # Anti pattern
$TargetParent = Split-Path $Target -Parent
$PathToAdd = $Target
if( (Test-Path $TargetParent) -and -not (Test-Path $Target))
{
# They gave us a full path, don't parse the file name, use this!
$Path = $Target
$ToInstall = $True
Write-Verbose "Found parent [$TargetParent], not target [$Target], assuming this is target file path"
$ToInstall = $False # Anti pattern
$TargetParent = Split-Path $Target -Parent
$PathToAdd = $Target
if ( (Test-Path $TargetParent) -and -not (Test-Path $Target)) {
# They gave us a full path, don't parse the file name, use this!
$Path = $Target
$ToInstall = $True
Write-Verbose "Found parent [$TargetParent], not target [$Target], assuming this is target file path"
}
elseif (Test-Path $Target -PathType Leaf) {
# File exists. We should download to temp spot, compare hashes, take action as appropriate.
# For now, skip the file.
Write-Verbose "Skipping existing file [$Target]"
if ($PSDependAction -contains 'Test') {
return $True
}
elseif(Test-Path $Target -PathType Leaf)
{
# File exists. We should download to temp spot, compare hashes, take action as appropriate.
# For now, skip the file.
Write-Verbose "Skipping existing file [$Target]"
if($PSDependAction -contains 'Test')
{
return $True
}
$PathToAdd = Split-Path $Target -Parent
$PathToAdd = Split-Path $Target -Parent
}
elseif (-not (Test-Path $Target)) {
# They gave us something that doesn't look like a new container for a new or existing file. Wat?
Write-Error "Could not find target path [$Target]"
if ($PSDependAction -contains 'Test') {
return $False
}
elseif(-not (Test-Path $Target))
{
# They gave us something that doesn't look like a new container for a new or existing file. Wat?
Write-Error "Could not find target path [$Target]"
if($PSDependAction -contains 'Test')
{
return $False
}
}
else {
Write-Verbose "[$Target] is a container, creating path to file"
# We have a target container, now find the name
If ($Name) {
# explicit name
$FileName = $Name
}
else
{
Write-Verbose "[$Target] is a container, creating path to file"
# We have a target container, now find the name
If($Name)
{
# explicit name
$FileName = $Name
}
else
{
$FileName = Parse-URLForFile -URL $URL
}
$Path = Join-Path $Target $FileName
else {
$FileName = Parse-URLForFile -URL $URL
}
$Path = Join-Path $Target $FileName

if(Test-Path $Path -PathType Leaf)
{
Write-Verbose "Skipping existing file [$Path]"
if($PSDependAction -contains 'Test')
{
return $True
}
}
else
{
$ToInstall = $True
if (Test-Path $Path -PathType Leaf) {
Write-Verbose "Skipping existing file [$Path]"
if ($PSDependAction -contains 'Test') {
return $True
}
}
Write-Verbose "Using [$Path] as `$Target"

#No dependency found, return false if we're testing alone...
if( $PSDependAction -contains 'Test' -and $PSDependAction.count -eq 1)
{
return $False
else {
$ToInstall = $True
}
Write-Verbose "Downloading [$URL] to [$Path]"
}
Write-Verbose "Using [$Path] as `$Target"

#No dependency found, return false if we're testing alone...
if ( $PSDependAction -contains 'Test' -and $PSDependAction.count -eq 1) {
return $False
}
Write-Verbose "Downloading [$URL] to [$Path]"

if($PSDependAction -contains 'Install' -and $ToInstall)
{
if ($PSDependAction -contains 'Install' -and $ToInstall) {
# Future considerations:
# Should we check for existing? And if we find it, still download file, and compare sha256 hash, replace if it does not match?
# We should consider credentials at some point, but PSD1 does not lend itself to securely storing passwords
# Should we check for existing? And if we find it, still download file, and compare sha256 hash, replace if it does not match?
# We should consider credentials at some point, but PSD1 does not lend itself to securely storing passwords
Get-WebFile -URL $URL -Path $Path
}

if($Dependency.AddToPath)
{
if ($Dependency.AddToPath) {
Write-Verbose "Setting PATH to`n$($PathToAdd, $env:PATH -join ';' | Out-String)"
Add-ToItemCollection -Reference Env:\Path -Item $PathToAdd
}
Loading
Loading