Skip to content

Commit

Permalink
refactor: Use best practice convention for comparing for equals to null
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlydog committed Feb 1, 2019
1 parent b90a7ea commit 05f3929
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
50 changes: 25 additions & 25 deletions publishTools/HelperScripts/CommonFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [s
<#
.SYNOPSIS
Prompts the user with a multi-line input box and returns the text they enter, or null if they cancelled the prompt.
.DESCRIPTION
Prompts the user with a multi-line input box and returns the text they enter, or null if they cancelled the prompt.
.PARAMETER Message
The message to display to the user explaining what text we are asking them to enter.
.PARAMETER WindowTitle
The text to display on the prompt window's title.
.PARAMETER DefaultText
The default text to show in the input box.
.EXAMPLE
$userText = Read-MultiLineInputDialog "Input some text please:" "Get User's Input"
Shows how to create a simple prompt to get mutli-line input from a user.
.EXAMPLE
# Setup the default multi-line address to fill the input box with.
$defaultAddress = @'
Expand All @@ -48,63 +48,63 @@ function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [s
Some Town, SK, Canada
A1B 2C3
'@
$address = Read-MultiLineInputDialog "Please enter your full address, including name, street, city, and postal code:" "Get User's Address" $defaultAddress
if ($address -eq $null)
if ($null -eq $address)
{
Write-Error "You pressed the Cancel button on the multi-line input box."
}
Prompts the user for their address and stores it in a variable, pre-filling the input box with a default multi-line address.
If the user pressed the Cancel button an error is written to the console.
.EXAMPLE
$inputText = Read-MultiLineInputDialog -Message "If you have a really long message you can break it apart`nover two lines with the powershell newline character:" -WindowTitle "Window Title" -DefaultText "Default text for the input box."
Shows how to break the second parameter (Message) up onto two lines using the powershell newline character (`n).
If you break the message up into more than two lines the extra lines will be hidden behind or show ontop of the TextBox.
.NOTES
Name: Show-MultiLineInputDialog
Author: Daniel Schroeder (originally based on the code shown at http://technet.microsoft.com/en-us/library/ff730941.aspx)
Version: 1.0
#>
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms

# Create the Label.
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10,10)
$label.Location = New-Object System.Drawing.Size(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.AutoSize = $true
$label.Text = $Message

# Create the TextBox used to capture the user's text.
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Size(10,40)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Size(10,40)
$textBox.Size = New-Object System.Drawing.Size(575,200)
$textBox.AcceptsReturn = $true
$textBox.AcceptsTab = $false
$textBox.Multiline = $true
$textBox.ScrollBars = 'Both'
$textBox.Text = $DefaultText

# Create the OK button.
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Size(415,250)
$okButton.Size = New-Object System.Drawing.Size(75,25)
$okButton.Text = "OK"
$okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })

# Create the Cancel button.
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(510,250)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Cancel"
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })

# Create the form.
$form = New-Object System.Windows.Forms.Form
$form = New-Object System.Windows.Forms.Form
$form.Text = $WindowTitle
$form.Size = New-Object System.Drawing.Size(610,320)
$form.FormBorderStyle = 'FixedSingle'
Expand All @@ -114,17 +114,17 @@ function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [s
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.ShowInTaskbar = $true

# Add all of the controls to the form.
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($okButton)
$form.Controls.Add($cancelButton)

# Initialize and show the form.
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() > $null # Trash the text of the button that was clicked.

# Return the text that the user entered.
return $form.Tag
}
12 changes: 6 additions & 6 deletions publishTools/Publish-NewVersionToPowerShellGalleryAndGitHub.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $currentScriptVersionNumberLine = $currentScriptVersionNumberMatch.Value

# Get the manifest's current version number.
$currentManifestVersionNumberMatches = Select-String -Path $manifestFilePath -Pattern $manifestVersionNumberRegexPattern | Select-Object -First 1
if ($currentManifestVersionNumberMatches.Matches.Count -le 0 -or !$currentManifestVersionNumberMatches.Matches[0].Success)
if ($currentManifestVersionNumberMatches.Matches.Count -le 0 -or !$currentManifestVersionNumberMatches.Matches[0].Success)
{ throw "Could not find the manifest's current version number." }
$currentManifestVersionNumberMatch = $currentManifestVersionNumberMatches.Matches[0]
$currentManifestVersionNumber = $currentManifestVersionNumberMatch.Groups['Version'].Value
Expand All @@ -60,7 +60,7 @@ $currentManifestVersionNumberLine = $currentManifestVersionNumberMatch.Value
# We have to get the file contents first so that Select-String will search across multiple lines, since the release notes may span multiple lines.
$manifestFileContents = Get-Content -Path $manifestFilePath -Raw
$currentManifestReleaseNotesMatches = Select-String -InputObject $manifestFileContents -Pattern $manifestReleaseNotesRegexPattern | Select-Object -First 1
if ($currentManifestReleaseNotesMatches.Matches.Count -le 0 -or !$currentManifestReleaseNotesMatches.Matches[0].Success)
if ($currentManifestReleaseNotesMatches.Matches.Count -le 0 -or !$currentManifestReleaseNotesMatches.Matches[0].Success)
{ throw "Could not find the manifests's current release notes." }
$currentManifestReleaseNotesMatch = $currentManifestReleaseNotesMatches.Matches[0]
$currentManifestReleaseNotes = $currentManifestReleaseNotesMatch.Groups['ReleaseNotes'].Value
Expand All @@ -81,9 +81,9 @@ $newVersionNumber = $newVersionNumber.Trim()

# Prompt for the release notes for this version.
$newReleaseNotes = Read-MultiLineInputBoxDialog -WindowTitle 'Release Notes' -Message 'What release notes should be included with this version?' -DefaultText $currentManifestReleaseNotes
if ($newReleaseNotes -eq $null) { throw 'You cancelled out of the release notes prompt.' }
if ($newReleaseNotes.Contains("'"))
{
if ($null -eq $newReleaseNotes) { throw 'You cancelled out of the release notes prompt.' }
if ($newReleaseNotes.Contains("'"))
{
$errorMessage = 'Single quotes are not allowed in the Release Notes, as they break our ability to parse them with PowerShell. Exiting script.'
Read-MessageBoxDialog -Message $errorMessage -WindowTitle 'Single Quotes Not Allowed In Release Notes'
throw $errorMessage
Expand All @@ -105,7 +105,7 @@ Publish-ToPowerShellGallery -moduleDirectoryPath $moduleDirectoryPath -powerShel
Read-MessageBoxDialog -WindowTitle "Commit and push changes to GitHub" -Message "Please commit the changes made by this script and push them to GitHub. This will ensure that the GitHub Release about to be created places the Tag on the correct (latest) commit. Click the OK button once that is done." -Buttons OK > $null

$versionNumberIsAPreReleaseVersion = $newVersionNumber -match '-+|[a-zA-Z]+' # (e.g. 1.2.3-alpha). i.e. contains a dash or letters.
$gitHubReleaseParameters =
$gitHubReleaseParameters =
@{
GitHubUsername = $gitHubUsername
GitHubRepositoryName = $gitHubRepositoryName
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ elseif ($buildResult.BuildSucceeded -eq $false)
{
Write-Output ("Build failed after {0:N1} seconds. Check the build log file '$($buildResult.BuildLogFilePath)' for errors." -f $buildResult.BuildDuration.TotalSeconds)
}
elseif ($buildResult.BuildSucceeded -eq $null)
elseif ($null -eq $buildResult.BuildSucceeded)
{
Write-Output "Unsure if build passed or failed: $($buildResult.Message)"
}
Expand Down
10 changes: 5 additions & 5 deletions src/Invoke-MsBuild/Invoke-MsBuild.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function Invoke-MsBuild
{
Write-Output ("Build failed after {0:N1} seconds. Check the build log file '$($buildResult.BuildLogFilePath)' for errors." -f $buildResult.BuildDuration.TotalSeconds)
}
elseif ($buildResult.BuildSucceeded -eq $null)
elseif ($null -eq $buildResult.BuildSucceeded)
{
Write-Output "Unsure if build passed or failed: $($buildResult.Message)"
}
Expand Down Expand Up @@ -415,7 +415,7 @@ function Invoke-MsBuild
{
$result.MsBuildProcess = Start-Process cmd.exe -ArgumentList $cmdArgumentsToRunMsBuild -WindowStyle $windowStyleOfNewWindow -PassThru
}

Wait-Process -InputObject $result.MsBuildProcess
}

Expand Down Expand Up @@ -445,7 +445,7 @@ function Invoke-MsBuild
}

# Get if the build succeeded or not.
[bool] $buildOutputDoesNotContainFailureMessage = (Select-String -Path $buildLogFilePath -Pattern "Build FAILED." -SimpleMatch) -eq $null
[bool] $buildOutputDoesNotContainFailureMessage = $null -eq (Select-String -Path $buildLogFilePath -Pattern "Build FAILED." -SimpleMatch)
[bool] $buildReturnedSuccessfulExitCode = $result.MsBuildProcess.ExitCode -eq 0
$buildSucceeded = $buildOutputDoesNotContainFailureMessage -and $buildReturnedSuccessfulExitCode

Expand Down Expand Up @@ -539,7 +539,7 @@ function Get-VisualStudioCommandPromptPathForVisualStudio2017AndNewer
$expectedVsCommandPromptPathWithWildcards = "$visualStudioDirectoryPath\*\*\Common7\Tools\VsDevCmd.bat"
$vsCommandPromptPathObjects = Get-Item -Path $expectedVsCommandPromptPathWithWildcards

[bool] $vsCommandPromptWasNotFound = ($vsCommandPromptPathObjects -eq $null) -or ($vsCommandPromptPathObjects.Length -eq 0)
[bool] $vsCommandPromptWasNotFound = ($null -eq $vsCommandPromptPathObjects) -or ($vsCommandPromptPathObjects.Length -eq 0)
if ($vsCommandPromptWasNotFound)
{
# Recurisvely search the entire Microsoft Visual Studio directory for the VS Command Prompt (slower, but will still work if MS changes folder structure).
Expand Down Expand Up @@ -646,7 +646,7 @@ function Get-MsBuildPathForVisualStudio2017AndNewer([bool] $Use32BitMsBuild)
$expected64bitPathWithWildcards = "$visualStudioDirectoryPath\*\*\MsBuild\*\Bin\amd64\MsBuild.exe"
$msBuildPathObjects = Get-Item -Path $expected32bitPathWithWildcards, $expected64bitPathWithWildcards

[bool] $msBuildWasNotFound = ($msBuildPathObjects -eq $null) -or ($msBuildPathObjects.Length -eq 0)
[bool] $msBuildWasNotFound = ($null -eq $msBuildPathObjects) -or ($msBuildPathObjects.Length -eq 0)
if ($msBuildWasNotFound)
{
# Recurisvely search the entire Microsoft Visual Studio directory for MsBuild (slower, but will still work if MS changes folder structure).
Expand Down

0 comments on commit 05f3929

Please sign in to comment.