Skip to content

Commit

Permalink
Add nuget bootstrap, PSGalleryOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
RamblingCookieMonster committed Aug 9, 2016
1 parent 5667e5e commit fa5bdb6
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nuget.exe
6 changes: 6 additions & 0 deletions PSDepend/PSDepend.NugetPath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# You can use the following:
# Absolute paths
# Relative paths
# UNC paths
# $ModuleRoot as "the path to the PSDepend module folder"
$ModuleRoot\nuget.exe
10 changes: 10 additions & 0 deletions PSDepend/PSDepend.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@
}
}


#Get nuget dependecy file
$NuGetPath = Get-Content $ModuleRoot\PSDepend.NugetPath | Where{$_ -notmatch "^\s*#"} | Select -First 1
$NugetPath = $NugetPath -replace '\$ModuleRoot', $ModuleRoot
$NuGetPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($NuGetPath)
If(-not (Test-Path $NuGetPath))
{
BootStrap-Nuget -NugetPath $NuGetPath
}

Export-ModuleMember -Function $Public.Basename
32 changes: 32 additions & 0 deletions PSDepend/Private/Bootstrap-Nuget.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Check for nuget exe. If it doesn't exist, create full path to parent, and download it
function BootStrap-Nuget {
[cmdletbinding()]
param(
$NugetPath = "$env:APPDATA\nuget.exe"
)

if($c = Get-Command 'nuget.exe' -ErrorAction SilentlyContinue)
{
write-verbose "Found Nuget at [$($c.path)]"
return
}

#Don't have it, download it
$Parent = Split-Path $NugetPath -Parent
if(-not (Test-Path $NugetPath))
{
if(-not (Test-Path $Parent))
{
Write-Verbose "Creating parent paths to [$NugetPath]'s parent: [$Parent]"
mkdir $Parent -Force
}
Write-Verbose "Downloading nuget to [$NugetPath]"
Invoke-WebRequest -uri 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe' -OutFile $NugetPath
}

# Add to path
if( ($ENV:Path -split ';') -notcontains $Parent )
{
$ENV:Path = $ENV:Path, $Parent -join ';'
}
}
42 changes: 42 additions & 0 deletions PSDepend/Private/Find-NugetPackage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# All credit and major props to Joel Bennett for this simplified solution that doesn't depend on PowerShellGet
# https://gist.github.com/Jaykul/1caf0d6d26380509b04cf4ecef807355
function Find-NugetPackage {
[CmdletBinding()]
param(
# The name of a package to find
[Parameter(Mandatory)]
$Name,
# The repository api URL -- like https://www.powershellgallery.com/api/v2/ or https://www.nuget.org/api/v2/
$PackageSourceUrl = 'https://www.powershellgallery.com/api/v2/',

#If specified takes precedence over version
[switch]$IsLatest,

[string]$Version
)

#Ugly way to do this. Prefer islatest, otherwise look for version, otherwise grab all matching modules
if($IsLatest)
{
Write-Verbose "Searching for latest [$name] module"
$URI = "${PackageSourceUrl}Packages?`$filter=Id eq '$name' and IsLatestVersion"
}
elseif($PSBoundParameters.ContainsKey($Version))
{
Write-Verbose "Searching for version [$version] of [$name]"
$URI = "${PackageSourceUrl}Packages?`$filter=Id eq '$name' and Version eq '$Version'"
}
else
{
Write-Verbose "Searching for all versions of [$name] module"
$URI = "${PackageSourceUrl}Packages?`$filter=Id eq '$name'"
}

Invoke-RestMethod $URI |
Select-Object @{n='Name';ex={$_.title.('#text')}},
@{n='Author';ex={$_.author.name}},
@{n='Version';ex={$_.properties.NormalizedVersion}},
@{n='Uri';ex={$_.Content.src}},
@{n='Description';ex={$_.properties.Description}},
@{n='Properties';ex={$_.properties}}
}
14 changes: 14 additions & 0 deletions PSDepend/Private/Save-NugetPackage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# All credit and major props to Joel Bennett for this simplified solution that doesn't depend on PowerShellGet
# https://gist.github.com/Jaykul/1caf0d6d26380509b04cf4ecef807355
function Save-NugetPackage {
[CmdletBinding()]
param(
[Parameter(ValueFromPipelineByPropertyName,Mandatory)]$Name,
[Parameter(ValueFromPipelineByPropertyName,Mandatory)]$Uri,
[Parameter(ValueFromPipelineByPropertyName)]$Version="",
[string]$Path = $pwd
)
$Path = (Join-Path $Path "$Name.$Version.nupkg")
Invoke-WebRequest $Uri -OutFile $Path
Get-Item $Path
}
11 changes: 11 additions & 0 deletions PSDepend/Public/Get-Dependency.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function Get-Dependency {
DependsOn : Dependency that must be installed before this
PreScripts : One or more paths to PowerShell scripts to run before the dependency
PostScripts : One or more paths to PowerShell scripts to run after the dependency
PSDependOptions: Hash table of global PSDepend options
Raw : Raw output for this dependency from the PSD1. May include data outside of standard items above.
These are parsed from dependency PSD1 files as follows:
Expand Down Expand Up @@ -115,6 +116,14 @@ function Get-Dependency {
$File = Split-Path $DependencyFile -Leaf
$Dependencies = Import-LocalizedData -BaseDirectory $Base -FileName $File


$PSDependOptions = $null
if($Dependencies.Containskey('PSDependOptions'))
{
$PSDependOptions = $Dependencies.PSDependOptions
$Dependencies.Remove('PSDependOptions')
}

foreach($Dependency in $Dependencies.keys)
{
$DependencyHash = $Dependencies.$Dependency
Expand All @@ -137,6 +146,7 @@ function Get-Dependency {
DependsOn = $null
PreScripts = $null
PostScripts = $null
PSDependOptions = $PSDependOptions
Raw = $null
}
}
Expand Down Expand Up @@ -164,6 +174,7 @@ function Get-Dependency {
DependsOn = $DependencyHash.DependsOn
PreScripts = $DependencyHash.PreScripts
PostScripts = $DependencyHash.PostScripts
PSDependOptions = $PSDependOptions
Raw = $DependencyHash
}
}
Expand Down

0 comments on commit fa5bdb6

Please sign in to comment.