From e63f4b2307ecf0c29291c5a47897553e3b678425 Mon Sep 17 00:00:00 2001 From: Sayed Ibrahim Hashimi Date: Sun, 2 Feb 2014 21:43:22 -0800 Subject: [PATCH] Adding debugMode functionality. Related to 15 --- README.md | 40 ++++++++++++++++++++++++++++ src/psbuild.psm1 | 69 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ddd4717..0eb4746 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,46 @@ Most functions have help defined so you can use ```get-help``` on most commands We have not yet developed the NuGet package yet but will be working on it soon. +## Debug mode +In many cases after a build it would be helpful to be able to answer questions like the following. + + - What is the value of x property? + - What is the value of y property? + - What would the expression '@(Compile->'%(FullPath)') be? + +But when you call msbuild.exe the project that is built is created in memory and trashed at the end of the process. ```Invoke-MSBuild``` now has a way that you can invoke your build and then have a _"handle"_ to your project that was built. This allows you to ask questions like the following. To enable this you just need to pass in the ```-debugMode``` switch to ```Invoke-MSBuild``` (_Note: this is actively under development so if you run into an problems please open an issue_). Here are some examples of what you can do. + +``` +PS> $bResult = Invoke-MSBuild .\temp.proj -debugMode + +PS> $bResult.EvalProperty('someprop') +default + +PS> $bResult.EvalItem('someitem') +temp.proj + +PS> $bResult.ExpandString('$(someprop)') +default + +PS> $bResult.ExpandString('@(someitem->''$(someprop)\%(Filename)%(Extension)'')') +default\temp.proj +``` + +You can get full access to the [ProjectInstance](http://msdn.microsoft.com/en-us/library/microsoft.build.execution.projectinstance(v=vs.121).aspx) object with the ProjectInstance property. + +More functionality is available via the ProjectInstance object. +``` +PS> $bResult.ProjectInstance.GetItems('someitem').EvaluatedInclude +temp.proj +``` + +You can get the [BuildResuilt](http://msdn.microsoft.com/en-us/library/microsoft.build.execution.buildresult(v=vs.121).aspx) via the BuildResult parameter. + +``` +PS> $bResult.BuildResult.OverallResult +Failure +``` + # Reporting Issues To report any issues please create an item on the [issues page](https://github.com/sayedihashimi/psbuild/issues/new). diff --git a/src/psbuild.psm1 b/src/psbuild.psm1 index c55e22e..5d52950 100644 --- a/src/psbuild.psm1 +++ b/src/psbuild.psm1 @@ -80,7 +80,7 @@ function Set-MSBuild{ } <# -.SYNOPSIS +.SYNOPSIS Can be used to invoke MSBuild. If the msbuildPath parameter is not passed in the Get-MSBuild function will be called to determine the version of MSBuild which should be used. @@ -333,6 +333,7 @@ function Invoke-MSBuild{ $brd) $psbuildResult = New-PSBuildResult -buildResult $buildResult -projectInstance $projectInstance + $script:lastDebugBuildResult = $psbuildResult return $psbuildResult } } @@ -340,6 +341,22 @@ function Invoke-MSBuild{ } } +<# +.SYNOPSIS + When you call Invoke-MSBuild with the -debugMode flag an object is returned that is the build result. + If you did not save this object you can use this method to reterive that last build result. + +.EXAMPLE + $lastResult = Get-PSBuildLastDebugBuildResult +#> +function Get-PSBuildLastDebugBuildResult{ + [cmdletbinding()] + param() + process{ + return $script:lastDebugBuildResult + } +} + function New-PSBuildResult{ [cmdletbinding()] param( @@ -365,6 +382,56 @@ function New-PSBuildResult{ ProjectInstance = $projectInstance } + $result | Add-Member -MemberType ScriptMethod -Name EvalProperty -Value { + [cmdletbinding()] + param( + [Parameter( + Mandatory=$true)] + [string] + $propName) + if($this.ProjectInstance){ + $this.ProjectInstance.GetPropertyValue($propName) + } + else{ + 'project is null' + } + } + + $result | Add-Member -MemberType ScriptMethod -Name EvalItem -Value { + [cmdletbinding()] + param( + [Parameter( + Mandatory=$true)] + [string] + $propName) + if($this.ProjectInstance){ + # todo: is there a better way to do this? + $expressionToEval = ('@({0})' -f $propName) + return $this.ProjectInstance.ExpandString($expressionToEval) + } + else{ + 'project is null' + } + } + + $result | Add-Member -MemberType ScriptMethod ExpandString -Value { + [cmdletbinding()] + param( + [Parameter( + Mandatory=$true)] + [string] + $unexpandedValue + ) + process{ + if($this.ProjectInstance){ + return $this.ProjectInstance.ExpandString($unexpandedValue) + } + else{ + 'project is null' + } + } + } + return $result } }