RamblingCookieMonster / PSDepend

PowerShell Dependency Handler
MIT License
277 stars 75 forks source link

Register-PSRepository or Package Source if not registered #50

Open sheldonhull opened 6 years ago

sheldonhull commented 6 years ago

One tedious setup issue for ensuring portable is making sure any defined providers are available on the target system. I couldn't find any examples, but is PsDepend able to get around this by registering or providing the appropriate setup details in requirements to ensure repository/package source is accessible?

sheldonhull commented 6 years ago

Talking in Slack confirmed this isn't something currently handled. maybe we could enhance this . Right now I'm successfully doing it by adding a custom command, but thinking if a provider is defined PsDepend could do this check instead.

Example of custom step

   'EnsureNugetRepositoryRegistered'    = @{
        DependencyType = 'Command'
        Source         = '
    $NugetURL = "http://www.nuget.org/api/v2"
    $PackageSources = Get-PackageSource
    if(@($PackageSources).Where{$_.location -eq $NugetUrl}.count -eq 0)
    {
      Register-PackageSource -name nugetRespository -ProviderName Nuget -Trusted $true -Location $NugetUrl -WhatIf
      write-output "Success: Register-PackageSource -name nugetRespository"
    }
    else
    {
      write-output "Package Source NugetRepository already exists, no need to register"
    }'
        DependsOn      = 'PackageManagement'
    }
RamblingCookieMonster commented 6 years ago

Hiyo!

This makes sense to me - I could see requiring a Force on the dependency, given that we'll be adding an arbitrary source (we make up name and whether trusted), and presumably we might stick with -Trusted $False.

Alternatively, we could do it without Force, and tear down the new source at the end of the dependency script - depending on the overhead of adding/removing sources, this might be a bit slow (and would happen with each dependency using this type)

Cheers!