forked from rbanks54/microcafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-NuGetPackageRestore.psm1
99 lines (83 loc) · 2.83 KB
/
Invoke-NuGetPackageRestore.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#Requires -Version 2.0
function Invoke-NuGetPackageRestore
{
[CmdletBinding(DefaultParameterSetName="Wait")]
param
(
[parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,HelpMessage="The path to the file to restore NuGet packages for (e.g. a .sln or .csproj file).")]
[ValidateScript({Test-Path $_})]
[string] $Path,
[parameter(Mandatory=$false)]
[Alias("Show","S")]
[switch] $ShowNugetWindow
)
BEGIN { }
END { }
PROCESS
{
Set-StrictMode -Version Latest
# Default the ParameterSet variables that may not have been set depending on which parameter set is being used. This is required for PowerShell v2.0 compatibility.
if (!(Test-Path Variable:Private:AutoLaunchBuildLogOnFailure)) { $AutoLaunchBuildLogOnFailure = $false }
if (!(Test-Path Variable:Private:KeepBuildLogOnSuccessfulBuilds)) { $KeepBuildLogOnSuccessfulBuilds = $false }
if (!(Test-Path Variable:Private:PassThru)) { $PassThru = $false }
$buildCrashed = $false;
$windowStyle = if ($ShowNugetWindow) { "Normal" } else { "Hidden" }
$nugetExe = Get-NuGetExePath
Write-Debug "NuGet.exe Path [$nugetExe]"
if ($nugetExe -eq $null)
{
Write-Error "Could not find NuGet.exe"
return $null
}
$argumentList = "restore " + $Path
try
{
if ($PassThru)
{
$process = Start-Process $nugetExe.FullName -ArgumentList $argumentList -Wait -PassThru -WindowStyle $windowStyle
}
else
{
$process = Start-Process $nugetExe.FullName -ArgumentList $argumentList -Wait -PassThru -WindowStyle $windowStyle
$processExitCode = $process.ExitCode
}
}
catch
{
$errorMessage = $_
Write-Error ("Unexpect error occured while building ""$Path"": $errorMessage" );
}
return $processExitCode -eq 0
}
}
function Get-ChildItemToDepth {
param(
[String]$Path = $PWD,
[String]$Filter = "*",
[Byte]$ToDepth = 255,
[Byte]$CurrentDepth = 0,
[Switch]$DebugMode
)
$CurrentDepth++
if ($DebugMode) { $DebugPreference = "Continue" }
Get-ChildItem $Path | ForEach-Object {
$_ | Where-Object { $_.Name -like $Filter }
if ($_.PsIsContainer) {
if ($CurrentDepth -le $ToDepth) {
# Callback to this function
Get-ChildItemToDepth -Path $_.FullName -Filter $Filter -ToDepth $ToDepth -CurrentDepth $CurrentDepth
} else {
Write-Debug $("Skipping GCI for Folder: $($_.FullName) " +
"(Why: Current depth $CurrentDepth vs limit depth $ToDepth)")
}
}
}
}
function Get-NuGetExePath
{
#$nugetExes = Get-ChildItem $PSScriptRoot NuGet.exe -Recurse
$nugetExes = Get-ChildItemToDepth -Path $PSScriptRoot -Filter NuGet.exe -ToDepth 2
if ($nugetExes -eq $null) { return $null }
return $nugetExes[0]
}
Export-ModuleMember -Function Invoke-NuGetPackageRestore