Skip to content

Commit

Permalink
Adding debugMode functionality. Related to 15
Browse files Browse the repository at this point in the history
  • Loading branch information
sayedihashimi committed Feb 3, 2014
1 parent e34f8b3 commit e63f4b2
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
69 changes: 68 additions & 1 deletion src/psbuild.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -333,13 +333,30 @@ function Invoke-MSBuild{
$brd)
$psbuildResult = New-PSBuildResult -buildResult $buildResult -projectInstance $projectInstance

$script:lastDebugBuildResult = $psbuildResult
return $psbuildResult
}
}
}
}
}

<#
.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(
Expand All @@ -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
}
}
Expand Down

0 comments on commit e63f4b2

Please sign in to comment.