Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Added new $psake variable called "build_script_dir" that can be use…

…d to quickly get the directory that the build script is executing from

* Fixed issue where a nested build that fails did not re-throw the exception to the parent build

* Updated psake.ps1 so that the try/finally is removed and the call to "remove-module" is the first thing that is done

* Removed $psake variable "use_exit_on_error" - The code now detects if it was launched via a windows service and assumes that the exit() function should be used.
Most of the popular continuous build engines (Hudson, TeamCity, Cruise Control.net etc..) that are in use are hosted by a windows service of some kind (IIS, Apache, etc....)
When psake encounters an exception in the build script it checks using a new private module function called "IsChildOfService()" to determine if it's ancestor's process was a windows service, if it was
then it calls the exit() function with a value of "1".

* Removed the "use_exit_on_error" $psake variable since it is no longer needed

* Updated "specs\writing_psake_variables_should_pass.ps1" to include new "build_script_dir" and removed reference to the "use_exit_on_error" variable

* Changed the $psake variable "suppress_error_messages" to "run_by_psake_build_tester" - this is a control variable used by the psake-BuildTester.ps1 script
  • Loading branch information...
commit 048980ecba13c3569991f5394a003d5d0a0522d2 1 parent 140aa89
unknown authored
View
2  psake-buildTester.ps1
@@ -3,7 +3,7 @@ function Main()
write-host "Running psake build tests" -ForeGroundColor GREEN
remove-module psake -ea SilentlyContinue
import-module .\psake.psm1
- $psake.suppress_error_messages = $true
+ $psake.run_by_psake_build_tester = $true
$results = runBuilds
remove-module psake
View
11 psake.ps1
@@ -19,10 +19,7 @@ param(
[System.Collections.Hashtable]$properties = @{}
)
-try {
- $scriptPath = Split-Path -parent $MyInvocation.InvocationName;
- import-module (join-path $scriptPath psake.psm1)
- invoke-psake $buildFile $taskList $framework $docs $parameters $properties
-} finally {
- remove-module psake -ea 'SilentlyContinue'
-}
+remove-module psake -ea 'SilentlyContinue'
+$scriptPath = Split-Path -parent $MyInvocation.InvocationName
+import-module (join-path $scriptPath psake.psm1)
+invoke-psake $buildFile $taskList $framework $docs $parameters $properties
View
84 psake.psm1
@@ -22,14 +22,14 @@
#The $psake hashtable variable is initialized in the invoke-psake function
$script:psake = @{}
-$psake.use_exit_on_error = $false # determines if psake uses the "exit()" function when an exception occurs
-$psake.build_success = $false # indicates that the current build was successful
-$psake.version = "4.00" # contains the current version of psake
-$psake.build_script_file = $null # contains a System.IO.FileInfo for the current build file
-$psake.framework_version = "" # contains the framework version # for the current build
-$psake.default_build_file_name = 'default.ps1'
-$psake.suppress_error_messages = $false
-$psake.context = new-object system.collections.stack
+$psake.build_success = $false # indicates that the current build was successful
+$psake.version = "4.00" # contains the current version of psake
+$psake.build_script_file = $null # contains a System.IO.FileInfo for the current build file
+$psake.build_script_dir = "" # contains a string with fully-qualified path to current build script
+$psake.framework_version = "" # contains the framework version # for the current build
+$psake.default_build_file_name = 'default.ps1' # default build script file name
+$psake.run_by_psake_build_tester = $false # indicates that build is being run by psake-BuildTester
+$psake.context = new-object system.collections.stack # holds onto the current state of all variables
DATA msgs
{
@@ -61,6 +61,46 @@ convertfrom-stringdata @'
import-localizeddata -bindingvariable msgs -erroraction silentlycontinue
#-- Private Module Functions
+function IsChildOfService
+{
+ param
+ (
+ [int]$currentProcessID = $PID
+ )
+
+ $currentProcess = gwmi -Query "select * from win32_process where processid = '$currentProcessID'"
+
+ if ($currentProcess.ProcessID -eq 0) #System Idle Process
+ {
+ return $false
+ }
+
+ $service = Get-WmiObject -Class Win32_Service -Filter "ProcessId = '$currentProcessID'"
+
+ if ($service) # We are invoked by a windows service
+ {
+ return $true
+ }
+ else
+ {
+ $parentProcess = gwmi -Query "select * from win32_process where processid = '$($currentProcess.ParentProcessID)'"
+ return IsChildOfService $parentProcess.ProcessID
+ }
+}
+
+function InNestedScope
+{
+ try
+ {
+ $vars = get-variable -scope 1
+ return $true
+ }
+ catch
+ {
+ return $false
+ }
+}
+
function Configure-BuildEnvironment
{
if ($framework.Length -ne 3 -and $framework.Length -ne 6) {
@@ -971,7 +1011,7 @@ Invoke-psake .\properties.ps1 -properties @{"x"="1";"y"="2"}
Runs the build script called 'properties.ps1' and passes in parameters 'x' and 'y' with values '1' and '2'
.OUTPUTS
- If there is an exception and '$psake.use_exit_on_error' -eq $true
+ If there is an exception and the build script was invoked by a windows service (directly/indirectly)
then runs exit(1) to set the DOS lastexitcode variable
otherwise set the '$psake.build_success variable' to $true or $false depending
on whether an exception was thrown
@@ -980,14 +1020,12 @@ Runs the build script called 'properties.ps1' and passes in parameters 'x' and '
When the psake module is loaded a variabled called $psake is created it is a hashtable
containing some variables that can be used to configure psake:
-$psake.use_exit_on_error = $false # determines if psake uses the "exit()" function when an exception occurs
$psake.build_success = $false # indicates that the current build was successful
$psake.version = "4.00" # contains the current version of psake
$psake.build_script_file = $null # contains a System.IO.FileInfo for the current build file
+$psake.build_script_dir # contains the fully qualified path to the current build file
$psake.framework_version = "" # contains the framework version # for the current build
-$psake.use_exit_on_error is a boolean variable that can be set before you call Invoke-Psake.
-
You should see the following when you display the contents of the $psake variable right after importing psake
PS projects:\psake> Import-Module .\psake.psm1
@@ -997,11 +1035,11 @@ Name Value
---- -----
version 4.00
build_script_file
-use_exit_on_error False
+build_script_dir
build_success False
framework_version
-After a build is executed the following $psake values are updated (build_script_file, build_success, and framework_version)
+After a build is executed the following $psake values are updated (build_script_file, build_script_dir, build_success, and framework_version)
PS projects:\psake> Invoke-psake .\examples\default.ps1
Executing task: Clean
@@ -1029,7 +1067,7 @@ Name Value
---- -----
version 4.00
build_script_file C:\Users\Jorge\Documents\Projects\psake\examples\default.ps1
-use_exit_on_error False
+build_script_dir C:\Users\Jorge\Documents\Projects\psake\examples
build_success True
framework_version 3.5
@@ -1100,7 +1138,8 @@ Assert
Assert (test-path $buildFile) ($msgs.error_build_file_not_found -f $buildFile)
$psake.build_script_file = Get-Item $buildFile
- set-location $psake.build_script_file.Directory
+ $psake.build_script_dir = $psake.build_script_file.DirectoryName
+ set-location $psake.build_script_dir
. $psake.build_script_file.FullName
if ($docs)
@@ -1181,12 +1220,13 @@ Assert
$error_message += ("-" * 70) + "`n"
$error_message += get-variable -scope script | format-table | out-string
- if (!$psake.suppress_error_messages)
+ #if psake is run by psake-BuildTester then suppress error messages
+ if (!$psake.run_by_psake_build_tester)
{
write-host $error_message -foregroundcolor red
}
-
- if ($psake.use_exit_on_error)
+
+ if ( (IsChildOfService) )
{
exit(1)
}
@@ -1194,6 +1234,12 @@ Assert
{
$psake.build_success = $false
}
+
+ #if we are running in a nested scope then we need to re-throw the exception, unless the current script is run by psake-BuildTester
+ if ( (InNestedScope) -and (!$psake.run_by_psake_build_tester) )
+ {
+ throw $_
+ }
}
finally
{
View
10 specs/writing_psake_variables_should_pass.ps1
@@ -13,17 +13,17 @@ task Verify -description "This task verifies psake's variables" {
Assert (Test-Path "psake") "variable psake was not exported from module"
Assert ($psake.ContainsKey("build_success")) "psake variable does not contain 'build_success'"
- Assert ($psake.ContainsKey("use_exit_on_error")) "psake variable does not contain 'use_exit_on_error'"
Assert ($psake.ContainsKey("version")) "psake variable does not contain 'version'"
Assert ($psake.ContainsKey("build_script_file")) "psake variable does not contain 'build_script_file'"
Assert ($psake.ContainsKey("framework_version")) "psake variable does not contain 'framework_version'"
+ Assert ($psake.ContainsKey("build_script_dir")) "psake variable does not contain 'build_script_dir'"
Assert (!$psake.build_success) 'psake.build_success should be $false'
- Assert (!$psake.use_exit_on_error) 'psake.use_exit_on_error should be $false'
- Assert (![string]::IsNullOrEmpty($psake.version)) 'psake.version was null or empty'
- Assert ($psake.build_script_file -ne $null) '$psake.build_script_file was null'
+ Assert ($psake.version) 'psake.version was null or empty'
+ Assert ($psake.build_script_file) '$psake.build_script_file was null'
Assert ($psake.build_script_file.Name -eq "writing_psake_variables_should_pass.ps1") ("psake variable: {0} was not equal to 'writing_psake_variables_should_pass.ps1'" -f $psake.build_script_file.Name)
- Assert (![string]::IsNullOrEmpty($psake.framework_version)) 'psake variable: $psake.framework_version was null or empty'
+ Assert ($psake.build_script_dir) 'psake variable: $psake.build_script_dir was null or empty'
+ Assert ($psake.framework_version) 'psake variable: $psake.framework_version was null or empty'
Assert ($psake.context.Count -eq 1) '$psake.context should have had a length of one (1) during script execution'
}
Please sign in to comment.
Something went wrong with that request. Please try again.