Skip to content

Commit

Permalink
Bugfixes, Wrappers, CI/CD and Test Automation (#5)
Browse files Browse the repository at this point in the history
So I reworked the module to enable CI/CD:

Added Invoke-Build, BuildHelpers, PlatyPS, Pester and PSScriptanalyzer.

I wrote Pester tests for all functionality except the two Invoke functions. (which will be in the next iteration).

Because of these changes i had to re-arrange the Module which will BREAK existing installations. I'm sorry for that but this will make sure the future of this repo will be ensured.

Also wrote some wrappers for calling functions and generally improved the coding quality because of Pester.
  • Loading branch information
tsteenbakkers authored Jun 6, 2018
1 parent 3574cf4 commit b22f304
Show file tree
Hide file tree
Showing 17 changed files with 1,358 additions and 68 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
2 changes: 1 addition & 1 deletion Example/CICD-Kicker.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if(-not(Get-Module MSSQLCICDHelper)){
}
catch{
write-error "something wnet wrong cloning or importing the MSSQL-CICD-helper module. please check and retry"
exit 1;
Throw;
}


Expand Down
1 change: 1 addition & 0 deletions MSSQL-CICD-Helper.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function TaskX($Name, $Parameters) {task $Name @Parameters -Source $MyInvocation
Task Default Clean, Build, Pester, UpdateSource
Task Build CopyToOutput, BuildPSM1, BuildPSD1
Task Pester Build, ImportModule, UnitTests, FullTests
Task LocalPester Build, ImportModule, UnitTests
Task Local Build, Pester, UpdateSource

Task Clean {
Expand Down
2 changes: 1 addition & 1 deletion MSSQL-CICD-Helper/MSSQL-CICD-Helper.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Module manifest for module 'MSSQLCICDHelper'
# Module manifest for module 'MSSQL-CICD-Helper'
#
# Generated by: Tobi Steenbakkers
#
Expand Down
121 changes: 121 additions & 0 deletions MSSQL-CICD-Helper/Private/BuildDeploy/Invoke-Cmd.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
function Invoke-Cmd {
<#
.SYNOPSIS
Executes a Invoke-Expression / CMD.Exe based on the Given Parameters. Allows for re-using part of program code and mocking
.DESCRIPTION
<will add later based on actual code >
.PARAMETER Executable
<not used currently>
.PARAMETER Arguments
Specifies the Arguments for which the Invoked Cmd should use.
.PARAMETER logfile
Specifies the Arguments for which the Invoked Cmd should use.
.PARAMETER errorlogfile
Specifies the Arguments for which the Invoked Cmd should use.
.OUTPUTS
A result set with the outcome of the process along with some metrics.
.EXAMPLE
Invoke-Cmd -Arguments <add some arguments>
Will execute the following command:
.LINK
Project home: https://github.com/tsteenbakkers/MSSQL-CICD-Helper
.NOTES
Name: MSSQLCICDHelper
Author: Tobi Steenbakkers
Version: 1.0.0
#>
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,
HelpMessage='What to find: *.sln, *.dacpac, *.publish.xml *.dtspac or *.sqlproject File. Options are: Solution, DacPac, DTSPac or Project',
Position=0)]
[ValidateNotNullOrEmpty()]
$executable,

[Parameter(Mandatory=$true,
HelpMessage='Path where search for $typetofind should be started',
Position=0)]
[ValidateNotNullOrEmpty()]
$arguments,

[Parameter(Mandatory=$true,
HelpMessage='Determines the basepath where logfiles should be stored. if empty the directory where the script is running will be used',
Position=0)]
[Alias("lf")]
[ValidateNotNullOrEmpty()]
[String] $logfile,

[Parameter(Mandatory=$true,
HelpMessage='Determines the basepath where logfiles should be stored. if empty the directory where the script is running will be used',
Position=0)]
[Alias("elf")]
[ValidateNotNullOrEmpty()]
[String] $errorlogfile
)
$poutput = @{}
$poutput.Output = [string]::Empty
$poutput.ErrorOutput = [string]::Empty
$poutput.Message = [string]::Empty
$poutput.Duration = [TimeSpan]::Zero
$poutput.Succeeded = $null
$poutput.ExitCode = $null

$logbase = Split-Path -path $logfile -Parent
$errorlogbase = Split-Path -path $logfile -Parent

if(-not(Test-Path -Path $logbase) -or -not(Test-Path -Path $errorlogbase)){

$poutput.Message = "Could not find parent of $logfile or $errorlogfile unable to proceed to execute Cmd."

Write-Error "$($poutput.message)"
return $result
throw;
}

try{

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $executable
$pinfo.Arguments = $arguments

#$pinfo.Passthru = $true
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false

if($debug){
$pinfo
}
#executing the command and storing the result inside $p:
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null

$poutput.output = $p.StandardOutput.ReadToEnd()
$poutput.erroroutput = $p.StandardError.read()
$poutput.Duration = $p.ExitTime - $p.StartTime
$poutput.output | Out-file -literalpath $logfile -Force
$poutput.erroroutput | Out-file -literalpath $errorlogfile -Force
$poutput.ExitCode = $p.ExitCode

return $poutput

}catch{
$errorMessage = $_
$poutput.Message = "Unexpected error occurred while processing : $errorMessage"
$poutput.Succeeded = $false
Write-Error ($poutput.Message)
return $poutput
throw;
}
}
2 changes: 1 addition & 1 deletion MSSQL-CICD-Helper/Private/Configuration/ImportConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ Function ImportConfig {
} else {
Write-Warning 'No saved configuration information found. Run Save-MSSQL-CICD-HelperConfiguration.'
Write-Warning "path which was looked for: $configfile"
EXIT 1;
throw;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Get-MSSQLCICDHelperFiletoBuildDeploy {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,
HelpMessage='What to find: *.sln, *.dacpac, *.publish.xml *.dtspac or *.sqlproject File. Options are: Solution, DacPac, DTSPack or Project',
HelpMessage='What to find: *.sln, *.dacpac, *.publish.xml *.dtspac or *.sqlproject File. Options are: Solution, DacPac, DTSPac or Project',
Position=0)]
[ValidateNotNullOrEmpty()]
$typetofind,
Expand Down Expand Up @@ -66,17 +66,22 @@ function Get-MSSQLCICDHelperFiletoBuildDeploy {
}
default {
Write-Error "Invalid option given for input param -typetofind. valid options are: Solution, Project, dacpac, PublishProfile or dtspac"
EXIT 1;
throw;
}
}

if(-not(Test-Path $rootpath)){
Write-Error "$rootpath was not found"
throw;
}

$results = Get-ChildItem -Path $rootpath -Filter $buildfilextension -Recurse -ErrorAction SilentlyContinue

Write-Verbose "Found $($results.Count) $buildfilextension files in $rootpath"

if($results.Count -lt 1){
Write-Error 'No Files found! Please check path and re-run Get-MSSQLCICDHelperFiletoBuild. Exiting'
EXIT 1;
throw;
}
elseif($results.Count -gt 1){
Write-Verbose 'Found multiple files. will return the file with the most recent writedatetime.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function Invoke-MSSQLCICDHelperMSBuild {
if($UseInvokeMSBuildModule){
if(-not(Get-Module Invoke-MSBuild)){
Write-Error 'Invoke-MSBuild was not found on this system. Make sure it is installed with Install-Module Invoke-MSBuild'
break;
throw;
}

}
Expand All @@ -197,7 +197,13 @@ function Invoke-MSSQLCICDHelperMSBuild {
$filename = Get-MSSQLCICDHelperFiletoBuildDeploy -typetofind 'Solution' -RootPath $curdir | Get-ChildItem
}
else{
$filename = Get-ChildItem $filename
if(-not(Test-Path $filename)){
Write-Error "File $filename not found."
throw;
}else{
$filename = Get-ChildItem $filename
}

}

Write-Verbose "The following file will be built: $($filename.Name) located in path $($filename.DirectoryName)"
Expand All @@ -206,7 +212,9 @@ function Invoke-MSSQLCICDHelperMSBuild {
$logbase = Split-Path -path $logfile -Parent
$result.BuildLogFilePath = $logbase
$result.BuildLogFile = $logfile
$result.FiletoBuild = $filename.FullName
$result.FiletoBuild = $filename.FullName
$processlogfile = "$($filename.FullName).msbuildlog.log"
$processerrorlogfile = "$($filename.FullName).msbuilderrorlog.log"

Write-Verbose "The following build arguments will be used: $MSBuildArguments"
$configfile['MSBuildExe']
Expand All @@ -222,35 +230,56 @@ function Invoke-MSSQLCICDHelperMSBuild {
#Write-Verbose "Command to be Executed is: cmd.exe $commandtoexecute"
$result.CommandUsedToBuild = "Command to be Executed is: cmd.exe $commandtoexecute"

if($hidden){
Write-verbose "Starting MSBuild ..."
$result.MsBuildProcess = Start-Process cmd.exe -ArgumentList $CommandtoExecute -Wait -WindowStyle Hidden -PassThru
}else{
Write-verbose "Starting MSBuild ..."
$result.MsBuildProcess = Start-Process cmd.exe -ArgumentList $CommandtoExecute -Wait -NoNewWindow -PassThru
}
$processoutput = Invoke-Cmd -executable 'cmd.exe' -arguments $CommandtoExecute -logfile $processlogfile -errorlogfile $processerrorlogfile

if(!$hidden){
"Normal Output: "
$processoutput.output
"Error Output:"
$processoutput.erroroutput
}
# if($hidden){
# Write-verbose "Starting MSBuild ..."
# $result.MsBuildProcess = Start-Process cmd.exe -ArgumentList $CommandtoExecute -Wait -WindowStyle Hidden -PassThru
# }else{
# Write-verbose "Starting MSBuild ..."
# $result.MsBuildProcess = Start-Process cmd.exe -ArgumentList $CommandtoExecute -Wait -NoNewWindow -PassThru
# }
}else{
$CommandtoExecute = "Invoke-MSBuild -Path $($filename.FullName) -logdirectory $($logbase)"

$CommandtoExecute += " -KeepBuildLogOnSuccessfulBuilds"

$CommandtoExecute += " -MsBuildParameters ""$($MSBuildArguments)"""
if($MSBuildArguments){

$CommandtoExecute += " -MsBuildParameters ""$($MSBuildArguments)"""
}

if ($InvokeMSBuildParameters){
$CommandtoExecute += " $($InvokeMSBuildParameters)"
}

$result.CommandUsedToBuild = "Command to be Executed is: $commandtoexecute"
Write-verbose "Starting MSBuild ..."
$result.MsBuildProcess = Invoke-Expression $CommandtoExecute

$processoutput = Invoke-Cmd -executable 'powershell.exe' -arguments $CommandtoExecute -logfile $processlogfile -errorlogfile $processerrorlogfile

if(!$hidden){
"Normal Output: "
$processoutput.output
"Error Output:"
$processoutput.erroroutput
}

#$result.MsBuildProcess = Invoke-Expression $CommandtoExecute
}
}catch{
$errorMessage = $_
$result.Message = "Unexpected error occurred while building ""$Path"": $errorMessage"
$result.BuildSucceeded = $false
Write-Error ($result.Message)
return $result
EXIT 1;
throw;
}

Write-verbose "MSBuild Started. Continue Checking results..."
Expand All @@ -262,15 +291,17 @@ function Invoke-MSSQLCICDHelperMSBuild {

Write-Error "$($result.message)"
return $result
EXIT 1;
throw;
}

if($UseInvokeMSBuildModule){
[bool] $buildReturnedSuccessfulExitCode = $result.MsBuildProcess.MsBuildProcess.ExitCode -eq 0
$result.BuildDuration = $result.MsBuildProcess.MsBuildProcess.ExitTime - $result.MsBuildProcess.MsBuildProcess.StartTime
# [bool] $buildReturnedSuccessfulExitCode = $result.MsBuildProcess.MsBuildProcess.ExitCode -eq 0
# $result.BuildDuration = $result.MsBuildProcess.MsBuildProcess.ExitTime - $result.MsBuildProcess.MsBuildProcess.StartTime
[bool] $buildReturnedSuccessfulExitCode = $processoutput.ExitCode -eq 0
$result.BuildDuration = $processoutput.Duration
}else{
[bool] $buildReturnedSuccessfulExitCode = $result.MsBuildProcess.ExitCode -eq 0
$result.BuildDuration = $result.MsBuildProcess.ExitTime - $result.MsBuildProcess.StartTime
[bool] $buildReturnedSuccessfulExitCode = $processoutput.ExitCode -eq 0
$result.BuildDuration = $processoutput.Duration
}

[bool] $buildOutputDoesNotContainFailureMessage = (Select-String -Path $($result.BuildLogFile) -Pattern "Build FAILED." -SimpleMatch) -eq $null
Expand All @@ -294,7 +325,7 @@ function Invoke-MSSQLCICDHelperMSBuild {
$result.Message = "Building ""$($result.FiletoBuild)"" Failed! Please check ""$($result.BuildLogFile)"" "
Write-Error "$($result.message)"
return $result
EXIT 1;
throw;
}

Write-Verbose "MSBuild passed. See results below..."
Expand Down
Loading

0 comments on commit b22f304

Please sign in to comment.