RamblingCookieMonster / PSDepend

PowerShell Dependency Handler
MIT License
285 stars 76 forks source link

FileDownload Dependency Target Path #49

Open doesitscript opened 7 years ago

doesitscript commented 7 years ago

Using the FileDownload dependency is determining that a path is a full filepath if the parent folder exists.

# Options that recreate the scenario
'https://chocolatey.org/install.ps1' = @{
    DependencyType = 'FileDownload'
    Target = 'PackageProviderBootStrap\bin\Chocolatey\'
    AddToPath = $False
}
if ( (Test-Path $TargetParent) -and -not (Test-Path $Target) ) {
    # They gave us a full path, don't parse the file name, use this!

The folder Bin does exist so the webfile fails to download with this folderpath when it tries to download the file with a folderpath: "Exception calling "DownloadFile" with "2" argument(s)"

This is the current work around I'm using however, I used $PWD because I wasn't sure what the default base directory should be. Also, to get this scenario to work, it seemed that another elseif statement to try to rule in directory creation.

if (![io.path]::IsPathRooted($Target)){
        # the path appears to be relative
        $Target = (Join-Path $PWD $Target)
        Write-Verbose "Target [$Target] appears to be relative, appending the path to the current working directory"
    } 
    $PathToAdd = $Target
    if( (Test-Path $TargetParent) -and -not (Test-Path $Target) -and ([System.IO.Path]::GetExtension($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) -and ([System.IO.Path]::GetExtension($Target)))
    {
        # 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
    }
    elseif (-not (Test-Path $Target)) { #somecode}
    else { #something bad happened }