Last active
October 29, 2015 13:21
-
-
Save nikolay-pshenichny/c28f7d210926352c81b2 to your computer and use it in GitHub Desktop.
PowerShell script to convert filter settings from DotSettings to XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Converts *.sln.DotSettings file to xml format suitable to be used with dotcover from command line | |
.DESCRIPTION | |
Converts DotSettings file (created from DotCover VS Extension) to its xml representation that can be used with dotcover from command line | |
.PARAMETER inputFileath | |
A xxx.sln.DotSettings file | |
.PARAMETER executable | |
Value for the "<Executable>" node in the generated Xml | |
.PARAMETER arguments | |
Value for the "<Arguments>" node in the generated Xml | |
.PARAMETER output | |
Value for the "<output>" node in the generated Xml | |
.NOTES | |
Author : Nikolay Pshenichny | |
.EXAMPLE | |
.\ConvertFrom-DotSettings.ps1 -inputFile MySolution.sln.DotSettings | |
#> | |
param | |
( | |
[string]$inputFile = $(throw "-inputFile is required. Should be a xxx.sln.DotSettings file."), | |
[string]$executable = "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\MSTest.exe", | |
[string]$arguments = "/testcontainer:.\UnitTest\bin\Debug\UnitTest.dll /resultsfile:.\TestResults.trx", | |
[string]$output = "dotCover.dotCoverSnapshot" | |
) | |
function ToFilterEntryNode([System.Xml.XmlDocument]$ownerDocument, [System.Xml.XmlElement]$dotCoverFilterNode) | |
{ | |
$filtersFragment = $ownerDocument.CreateDocumentFragment() | |
$filtersFragment.InnerXml = @" | |
<FilterEntry> | |
<ModuleMask>$($dotCoverFilterNode.ModuleMask)</ModuleMask> | |
<ModuleVersionMask>$($dotCoverFilterNode.ModuleVersionMask)</ModuleVersionMask> | |
<ClassMask>$($dotCoverFilterNode.ClassMask)</ClassMask> | |
<FunctionMask>$($dotCoverFilterNode.FunctionMask)</FunctionMask> | |
<IsEnabled>$($dotCoverFilterNode.IsEnabled)</IsEnabled> | |
</FilterEntry> | |
"@ | |
return $filtersFragment | |
} | |
# Read the xxx.sln.DotSettings file | |
[xml]$dotSettingsFile = Get-Content $inputFile | |
# Get the string resource that has filters and convert it to xml | |
$resource = $dotSettingsFile.ResourceDictionary.String | Where {$_.Key -eq "/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue"} | Select-Object Key, InnerText | |
$filters = [xml]$resource.InnerText | |
# Prepare output "template" | |
[xml]$output = @" | |
<?xml version="1.0" encoding="utf-8"?> | |
<AnalyseParams> | |
<Executable>$executable</Executable> | |
<Arguments>$arguments</Arguments> | |
<Output>$output</Output> | |
<Filters> | |
<IncludeFilters/> | |
<ExcludeFilters/> | |
</Filters> | |
</AnalyseParams> | |
"@ | |
# Convert "IncludeFilters" data | |
$includeFilters = $filters.SelectSingleNode("//data/IncludeFilters") | |
if ($includeFilters.HasChildNodes -eq $True) | |
{ | |
$outputIncludeFiltersNode = $output.SelectSingleNode("//AnalyseParams/Filters/IncludeFilters") | |
foreach($includeFilter in $includeFilters.Filter) | |
{ | |
$filterEntry = ToFilterEntryNode -ownerDocument $output -dotCoverFilterNode $includeFilter | |
$appendedChild = $outputIncludeFiltersNode.AppendChild($filterEntry) | |
} | |
} | |
# Convert "ExcludeFilters" data | |
$excludeFilters = $filters.SelectSingleNode("//data/ExcludeFilters") | |
if ($excludeFilters.HasChildNodes -eq $True) | |
{ | |
$outputIncludeFiltersNode = $output.SelectSingleNode("//AnalyseParams/Filters/ExcludeFilters") | |
foreach($excludeFilter in $excludeFilters.Filter) | |
{ | |
$filterEntry = ToFilterEntryNode -ownerDocument $output -dotCoverFilterNode $excludeFilter | |
$appendedChild = $outputIncludeFiltersNode.AppendChild($filterEntry) | |
} | |
} | |
#Note: Writing to console output directly (ie. $output.Save([Console]::Out)) will change encoding to IBM437 | |
$memoryStream = New-Object System.IO.MemoryStream | |
$output.Save($memoryStream) | |
$memoryStream.Position = 0 | |
$reader = New-Object System.IO.StreamReader $memoryStream | |
$reader.ReadToEnd() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET INPUT=-inputFile MySolution.sln.DotSettings | |
SET EXECUTABLE=-executable "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\MSTest.exe" | |
SET ARGS=-arguments "/testcontainer:.\UnitTest\bin\Debug\UnitTest.dll /resultsfile:.\TestResults.trx" | |
SET OUTPUT=-output dotCover.dotCoverSnapshot | |
SET GIST="https://gist.githubusercontent.com/nikolay-pshenichny/c28f7d210926352c81b2/raw/57b6708f0c284395ac4dfc00ef268c49f27ef132/temp.ps1" | |
ECHO Invoke-WebRequest %GIST% -method GET -outfile ".\temp.ps1" | powershell -ExecutionPolicy Unrestricted -command - | |
powershell -f .\temp.ps1 %INPUT% %EXECUTABLE% %ARGS% %OUTPUT%> dotCoverConfig.xml | |
DEL .\temp.ps1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment