1+ Param (
2+ [switch ]$Publish
3+ )
4+
5+ $ErrorActionPreference = " Stop"
6+ $ExitCode = 1
7+
8+ function Write-Log {
9+
10+ # region Parameters
11+
12+ [cmdletbinding ()]
13+ Param (
14+ [Parameter (ValueFromPipeline = $true )]
15+ [array ] $Messages ,
16+
17+ [Parameter ()] [ValidateSet (" Error" , " Warn" , " Info" )]
18+ [string ] $Level = " Info" ,
19+
20+ [Parameter ()]
21+ [Switch ] $NoConsoleOut = $false ,
22+
23+ [Parameter ()]
24+ [String ] $ForegroundColor = ' White' ,
25+
26+ [Parameter ()] [ValidateRange (1 , 30 )]
27+ [Int16 ] $Indent = 0 ,
28+
29+ [Parameter ()]
30+ [IO.FileInfo ] $Path = " .\NuGet.log" ,
31+
32+ [Parameter ()]
33+ [Switch ] $Clobber ,
34+
35+ [Parameter ()]
36+ [String ] $EventLogName ,
37+
38+ [Parameter ()]
39+ [String ] $EventSource ,
40+
41+ [Parameter ()]
42+ [Int32 ] $EventID = 1
43+
44+ )
45+
46+ # endregion
47+
48+ Begin {}
49+
50+ Process {
51+
52+ $ErrorActionPreference = " Continue"
53+
54+ if ($Messages.Length -gt 0 ) {
55+ try {
56+ foreach ($m in $Messages ) {
57+ if ($NoConsoleOut -eq $false ) {
58+ switch ($Level ) {
59+ ' Error' {
60+ Write-Error $m - ErrorAction SilentlyContinue
61+ Write-Host (' {0}{1}' -f (" " * $Indent ), $m ) - ForegroundColor Red
62+ }
63+ ' Warn' {
64+ Write-Warning $m
65+ }
66+ ' Info' {
67+ Write-Host (' {0}{1}' -f (" " * $Indent ), $m ) - ForegroundColor $ForegroundColor
68+ }
69+ }
70+ }
71+
72+ if ($m.Trim ().Length -gt 0 ) {
73+ $msg = ' {0}{1} [{2}] : {3}' -f (" " * $Indent ), (Get-Date - Format " yyyy-MM-dd HH:mm:ss" ), $Level.ToUpper (), $m
74+
75+ if ($Clobber ) {
76+ $msg | Out-File - FilePath $Path - Force
77+ } else {
78+ $msg | Out-File - FilePath $Path - Append
79+ }
80+ }
81+
82+ if ($EventLogName ) {
83+
84+ if (-not $EventSource ) {
85+ $EventSource = ([IO.FileInfo ] $MyInvocation.ScriptName ).Name
86+ }
87+
88+ if (-not [Diagnostics.EventLog ]::SourceExists($EventSource )) {
89+ [Diagnostics.EventLog ]::CreateEventSource($EventSource , $EventLogName )
90+ }
91+
92+ $log = New-Object System.Diagnostics.EventLog
93+ $log.set_log ($EventLogName )
94+ $log.set_source ($EventSource )
95+
96+ switch ($Level ) {
97+ " Error" { $log.WriteEntry ($Message , ' Error' , $EventID ) }
98+ " Warn" { $log.WriteEntry ($Message , ' Warning' , $EventID ) }
99+ " Info" { $log.WriteEntry ($Message , ' Information' , $EventID ) }
100+ }
101+ }
102+ }
103+ }
104+ catch {
105+ throw " Failed to create log entry in: '$Path '. The error was: '$_ '."
106+ }
107+ }
108+ }
109+
110+ End {}
111+
112+ <#
113+ . SYNOPSIS
114+ Writes logging information to screen and log file simultaneously.
115+
116+ . DESCRIPTION
117+ Writes logging information to screen and log file simultaneously. Supports multiple log levels.
118+
119+ . PARAMETER Messages
120+ The messages to be logged.
121+
122+ . PARAMETER Level
123+ The type of message to be logged.
124+
125+ . PARAMETER NoConsoleOut
126+ Specifies to not display the message to the console.
127+
128+ . PARAMETER ConsoleForeground
129+ Specifies what color the text should be be displayed on the console. Ignored when switch 'NoConsoleOut' is specified.
130+
131+ . PARAMETER Indent
132+ The number of spaces to indent the line in the log file.
133+
134+ . PARAMETER Path
135+ The log file path.
136+
137+ . PARAMETER Clobber
138+ Existing log file is deleted when this is specified.
139+
140+ . PARAMETER EventLogName
141+ The name of the system event log, e.g. 'Application'.
142+
143+ . PARAMETER EventSource
144+ The name to appear as the source attribute for the system event log entry. This is ignored unless 'EventLogName' is specified.
145+
146+ . PARAMETER EventID
147+ The ID to appear as the event ID attribute for the system event log entry. This is ignored unless 'EventLogName' is specified.
148+
149+ . EXAMPLE
150+ PS C:\> Write-Log -Message "It's all good!" -Path C:\MyLog.log -Clobber -EventLogName 'Application'
151+
152+ . EXAMPLE
153+ PS C:\> Write-Log -Message "Oops, not so good!" -Level Error -EventID 3 -Indent 2 -EventLogName 'Application' -EventSource "My Script"
154+
155+ . INPUTS
156+ System.String
157+
158+ . OUTPUTS
159+ No output.
160+
161+ . NOTES
162+ Revision History:
163+ 2011-03-10 : Andy Arismendi - Created.
164+ #>
165+ }
166+
167+ function Create-Process () {
168+ param ([string ] $fileName , [string ] $arguments )
169+
170+ $pinfo = New-Object System.Diagnostics.ProcessStartInfo
171+ $pinfo.RedirectStandardError = $true
172+ $pinfo.RedirectStandardOutput = $true
173+ $pinfo.UseShellExecute = $false
174+ $pinfo.FileName = $fileName
175+ $pinfo.Arguments = $arguments
176+
177+ $p = New-Object System.Diagnostics.Process
178+ $p.StartInfo = $pinfo
179+
180+ return $p
181+ }
182+
183+ function HandlePublishError {
184+
185+ # Run NuGet Setup
186+ $setupTask = Start-Process PowerShell.exe " -ExecutionPolicy Unrestricted -File .\NuGetSetup.ps1 -Url $url " - Wait - PassThru
187+
188+ if ($setupTask.ExitCode -eq 0 ) {
189+ # Try to push package again
190+ $publishTask = Create- Process .\NuGet.exe (" push " + $_.Name + " -Source " + $url )
191+ $publishTask.Start () | Out-Null
192+ $publishTask.WaitForExit ()
193+
194+ $output = ($publishTask.StandardOutput.ReadToEnd () -Split ' [\r\n]' ) | ? {$_ }
195+ $error = (($publishTask.StandardError.ReadToEnd () -Split ' [\r\n]' ) | ? {$_ })
196+ Write-Log $output
197+ Write-Log $error Error
198+
199+ if ($publishTask.ExitCode -eq 0 ) {
200+ $ExitCode = 0
201+ }
202+ }
203+ else {
204+ $ExitCode = 0
205+ }
206+ }
207+
208+ function Publish {
209+
210+ Write-Log " "
211+ Write-Log " Publishing package..." - ForegroundColor Green
212+
213+ # Get nuget config
214+ [xml ]$nugetConfig = Get-Content .\NuGet.Config
215+
216+ $nugetConfig.configuration.packageSources.add | ForEach-Object {
217+ $url = $_.value
218+
219+ Write-Log " Repository Url: $url "
220+ Write-Log " "
221+
222+ Get-ChildItem * .nupkg | Where-Object { $_.Name.EndsWith (" .symbols.nupkg" ) -eq $false } | ForEach-Object {
223+
224+ # Try to push package
225+ $task = Create- Process .\NuGet.exe (" push " + $_.Name + " -Source " + $url )
226+ $task.Start () | Out-Null
227+ $task.WaitForExit ()
228+
229+ $output = ($task.StandardOutput.ReadToEnd () -Split ' [\r\n]' ) | ? {$_ }
230+ $error = (($task.StandardError.ReadToEnd () -Split ' [\r\n]' ) | ? {$_ })
231+ Write-Log $output
232+ Write-Log $error Error
233+
234+ if ($task.ExitCode -gt 0 ) {
235+ HandlePublishError
236+ }
237+ else {
238+ $ExitCode = 0
239+ }
240+ }
241+ }
242+ }
243+
244+ Write-Log " "
245+ Write-Log " NuGet Packager 2.0.2" - ForegroundColor Yellow
246+
247+ # Make sure the nuget executable is writable
248+ Set-ItemProperty NuGet.exe - Name IsReadOnly - Value $false
249+
250+ # Make sure the nupkg files are writeable and create backup
251+ if (Test-Path * .nupkg) {
252+ Set-ItemProperty * .nupkg - Name IsReadOnly - Value $false
253+
254+ Write-Log " "
255+ Write-Log " Creating backup..." - ForegroundColor Green
256+
257+ Get-ChildItem * .nupkg | ForEach-Object {
258+ Move-Item $_.Name ($_.Name + " .bak" ) - Force
259+ Write-Log (" Renamed " + $_.Name + " to " + $_.Name + " .bak" )
260+ }
261+ }
262+
263+ Write-Log " "
264+ Write-Log " Updating NuGet..." - ForegroundColor Green
265+ Write-Log (Invoke-Command {.\NuGet.exe update - Self} - ErrorAction Stop)
266+
267+ Write-Log " "
268+ Write-Log " Creating package..." - ForegroundColor Green
269+
270+ # Create symbols package if any .pdb files are located in the lib folder
271+ If ((Get-ChildItem * .pdb - Path .\lib - Recurse).Count -gt 0 ) {
272+ Write-Log (Invoke-Command {.\NuGet.exe pack Package.nuspec - Symbol - Verbosity Detailed 2>&1 })
273+ $ExitCode = $LASTEXITCODE
274+ }
275+ Else {
276+ Write-Log (Invoke-Command {.\NuGet.exe pack Package.nuspec - Verbosity Detailed 2>&1 })
277+ $ExitCode = $LASTEXITCODE
278+ }
279+
280+ # Check if package should be published
281+ if ($Publish ) {
282+ Publish
283+ }
284+
285+ Write-Log " "
286+ Write-Log " Exit Code: $ExitCode " - ForegroundColor Gray
287+
288+ $host.SetShouldExit ($ExitCode )
289+ Exit $ExitCode
0 commit comments