Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target folder is not created, even with -Force parameter. Git.ps1 #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rcarboneras
Copy link

When using a Git repository, if target doesn't exist, the dependency is downloaded in the current directory and the error is silenced, affecting the path sctructure of your proyect. Unless you check the $Errors variable, you'll never now what is happening.

However, when using PowerShell Gallery, the bellow piece of code does the work in the Save-Module function:
When -Force is specified, Path will be created if not available

    # When -Force is specified, Path will be created if not available.
    if(-not $Force)
    {
        if($Path)
        {
            $destinationPath = Resolve-PathHelper -Path $Path -CallerPSCmdlet $PSCmdlet | Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore

            if(-not $destinationPath -or -not (Microsoft.PowerShell.Management\Test-path $destinationPath))
            {
                $errorMessage = ($LocalizedData.PathNotFound -f $Path)
                ThrowError  -ExceptionName "System.ArgumentException" `
                            -ExceptionMessage $errorMessage `
                            -ErrorId "PathNotFound" `
                            -CallerPSCmdlet $PSCmdlet `
                            -ExceptionObject $Path `
                            -ErrorCategory InvalidArgument
            }

            $PSBoundParameters['Path'] = $destinationPath
        }
        else
        {
            $destinationPath = Resolve-PathHelper -Path $LiteralPath -IsLiteralPath -CallerPSCmdlet $PSCmdlet | Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore

            if(-not $destinationPath -or -not (Microsoft.PowerShell.Management\Test-Path -LiteralPath $destinationPath))
            {
                $errorMessage = ($LocalizedData.PathNotFound -f $LiteralPath)
                ThrowError  -ExceptionName "System.ArgumentException" `
                            -ExceptionMessage $errorMessage `
                            -ErrorId "PathNotFound" `
                            -CallerPSCmdlet $PSCmdlet `
                            -ExceptionObject $LiteralPath `
                            -ErrorCategory InvalidArgument
            }

            $PSBoundParameters['LiteralPath'] = $destinationPath
        }
    }

I propose then to implement the same behaviour in Git.ps1 script with the following lines:

else
{
if ($Force) {New-Item -ItemType Directory -Name $Dependency.Target -Force | Out-Null
Write-Debug "Target folder $($Dependency.Target) created as -Force switch was specified"
$Target = Join-Path $PWD "$($Dependency.Target)"
}
else {
$Target = $PWD.Path
Write-Debug "Target defaulted to current dir: $Target"
}
}

@rcarboneras rcarboneras changed the title Update Git.ps1 Target folder is not created, even with -Force parameter. Git.ps1 May 8, 2019
@rcarboneras
Copy link
Author

I've made a minor change in the way of creating the folder:

else
{
if ($true) {New-Item -ItemType Directory -Name (Split-Path $Dependency.Target -Leaf) -Force | Out-Null
Write-Debug "Target folder $($Dependency.Target) created as -Force switch was specified"
$Target = Join-Path $PWD "$(Split-Path $Dependency.Target -Leaf)"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant