Formatting and function extractions

This commit is contained in:
Andrew Schwartzmeyer 2016-04-01 22:01:05 -07:00
parent f3f0bc0b11
commit 68879655a5

View File

@ -17,6 +17,7 @@ try {
$IsWindows = $true $IsWindows = $true
} }
function Start-PSBuild { function Start-PSBuild {
[CmdletBinding(DefaultParameterSetName='CoreCLR')] [CmdletBinding(DefaultParameterSetName='CoreCLR')]
param( param(
@ -47,20 +48,6 @@ function Start-PSBuild {
[string]$msbuildConfiguration = "Release" [string]$msbuildConfiguration = "Release"
) )
function precheck([string]$command, [string]$missedMessage) {
$c = Get-Command $command -ErrorAction SilentlyContinue
if (-not $c) {
Write-Warning $missedMessage
return $false
} else {
return $true
}
}
function log([string]$message) {
Write-Host -Foreground Green $message
}
# simplify ParameterSetNames # simplify ParameterSetNames
if ($PSCmdlet.ParameterSetName -eq 'FullCLR') { if ($PSCmdlet.ParameterSetName -eq 'FullCLR') {
$FullCLR = $true $FullCLR = $true
@ -209,11 +196,14 @@ function Start-PSBuild {
} }
function Get-PSOutput { function Get-PSOutput {
$script:Output $script:Output
} }
function Start-PSxUnit { function Start-PSxUnit {
[CmdletBinding()]param()
if ($IsWindows) { if ($IsWindows) {
throw "xUnit tests are only currently supported on Linux / OS X" throw "xUnit tests are only currently supported on Linux / OS X"
} }
@ -239,7 +229,7 @@ function Start-PSPackage {
# PowerShell packages use Semantic Versioning http://semver.org/ # PowerShell packages use Semantic Versioning http://semver.org/
# #
# Ubuntu and OS X packages are supported. # Ubuntu and OS X packages are supported.
param( [CmdletBinding()]param(
[string]$Version, [string]$Version,
[int]$Iteration = 1, [int]$Iteration = 1,
[ValidateSet("deb", "osxpkg", "rpm")] [ValidateSet("deb", "osxpkg", "rpm")]
@ -302,8 +292,8 @@ function Start-PSPackage {
"$PSScriptRoot/package/powershell=/usr/local/bin" "$PSScriptRoot/package/powershell=/usr/local/bin"
} }
function Start-DevPSGitHub
{ function Start-DevPSGitHub {
param( param(
[switch]$ZapDisable, [switch]$ZapDisable,
[string[]]$ArgumentList = '', [string[]]$ArgumentList = '',
@ -312,27 +302,23 @@ function Start-DevPSGitHub
[switch]$NoNewWindow [switch]$NoNewWindow
) )
try try {
{ if ($LoadProfile -eq $false) {
if ($LoadProfile -eq $false)
{
$ArgumentList = @('-noprofile') + $ArgumentList $ArgumentList = @('-noprofile') + $ArgumentList
} }
$env:DEVPATH = $binDir $env:DEVPATH = $binDir
if ($ZapDisable) if ($ZapDisable) {
{
$env:COMPLUS_ZapDisable = 1 $env:COMPLUS_ZapDisable = 1
} }
if (-Not (Test-Path $binDir\powershell.exe.config)) if (-Not (Test-Path $binDir\powershell.exe.config)) {
{
$configContents = @" $configContents = @"
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<configuration> <configuration>
<runtime> <runtime>
<developmentMode developerInstallation="true"/> <developmentMode developerInstallation="true"/>
</runtime> </runtime>
</configuration> </configuration>
"@ "@
$configContents | Out-File -Encoding Ascii $binDir\powershell.exe.config $configContents | Out-File -Encoding Ascii $binDir\powershell.exe.config
@ -350,55 +336,14 @@ function Start-DevPSGitHub
} }
Start-Process @startProcessArgs Start-Process @startProcessArgs
} } finally {
finally
{
ri env:DEVPATH ri env:DEVPATH
if ($ZapDisable) if ($ZapDisable) {
{
ri env:COMPLUS_ZapDisable ri env:COMPLUS_ZapDisable
} }
} }
} }
## this function is from Dave Wyatt's answer on
## http://stackoverflow.com/questions/22002748/hashtables-from-convertfrom-json-have-different-type-from-powershells-built-in-h
function Convert-PSObjectToHashtable
{
param (
[Parameter(ValueFromPipeline)]
$InputObject
)
process
{
if ($null -eq $InputObject) { return $null }
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string])
{
$collection = @(
foreach ($object in $InputObject) { Convert-PSObjectToHashtable $object }
)
Write-Output -NoEnumerate $collection
}
elseif ($InputObject -is [psobject])
{
$hash = @{}
foreach ($property in $InputObject.PSObject.Properties)
{
$hash[$property.Name] = Convert-PSObjectToHashtable $property.Value
}
$hash
}
else
{
$InputObject
}
}
}
<# <#
.EXAMPLE Copy-SubmoduleFiles # copy files FROM submodule TO src/<project> folders .EXAMPLE Copy-SubmoduleFiles # copy files FROM submodule TO src/<project> folders
@ -413,8 +358,7 @@ function Copy-SubmoduleFiles {
) )
if (-not (Test-Path $mappingFilePath)) if (-not (Test-Path $mappingFilePath)) {
{
throw "Mapping file not found in $mappingFilePath" throw "Mapping file not found in $mappingFilePath"
} }
@ -422,47 +366,38 @@ function Copy-SubmoduleFiles {
# mapping.json assumes the root folder # mapping.json assumes the root folder
Push-Location $PSScriptRoot Push-Location $PSScriptRoot
try try {
{
$m.GetEnumerator() | % { $m.GetEnumerator() | % {
if ($ToSubmodule) if ($ToSubmodule) {
{
cp $_.Value $_.Key -Verbose:$Verbose cp $_.Value $_.Key -Verbose:$Verbose
} } else {
else
{
mkdir (Split-Path $_.Value) -ErrorAction SilentlyContinue > $null mkdir (Split-Path $_.Value) -ErrorAction SilentlyContinue > $null
cp $_.Key $_.Value -Verbose:$Verbose cp $_.Key $_.Value -Verbose:$Verbose
} }
} }
} } finally {
finally
{
Pop-Location Pop-Location
} }
} }
<# <#
.EXAMPLE Create-MappingFile # create mapping.json in the root folder from project.json files .EXAMPLE Create-MappingFile # create mapping.json in the root folder from project.json files
#> #>
function New-MappingFile function New-MappingFile {
{
param( param(
[string]$mappingFilePath = "$PSScriptRoot/mapping.json", [string]$mappingFilePath = "$PSScriptRoot/mapping.json",
[switch]$IgnoreCompileFiles, [switch]$IgnoreCompileFiles,
[switch]$Ignoreresource [switch]$Ignoreresource
) )
function Get-MappingPath([string]$project, [string]$path) function Get-MappingPath([string]$project, [string]$path) {
{ if ($project -match 'TypeCatalogGen') {
if ($project -match 'TypeCatalogGen')
{
return Split-Path $path -Leaf return Split-Path $path -Leaf
} }
if ($project -match 'Microsoft.Management.Infrastructure') if ($project -match 'Microsoft.Management.Infrastructure') {
{
return Split-Path $path -Leaf return Split-Path $path -Leaf
} }
@ -473,8 +408,7 @@ function New-MappingFile
# assumes the root folder # assumes the root folder
Push-Location $PSScriptRoot Push-Location $PSScriptRoot
try try {
{
$projects = ls .\src\ -Recurse -Depth 2 -Filter 'project.json' $projects = ls .\src\ -Recurse -Depth 2 -Filter 'project.json'
$projects | % { $projects | % {
$project = Split-Path $_.FullName $project = Split-Path $_.FullName
@ -482,8 +416,7 @@ function New-MappingFile
if (-not $IgnoreCompileFiles) { if (-not $IgnoreCompileFiles) {
$json.compileFiles | % { $json.compileFiles | % {
if ($_) { if ($_) {
if (-not $_.EndsWith('AssemblyInfo.cs')) if (-not $_.EndsWith('AssemblyInfo.cs')) {
{
$fullPath = Join-Path $project (Get-MappingPath -project $project -path $_) $fullPath = Join-Path $project (Get-MappingPath -project $project -path $_)
$mapping[$_.Replace('../', 'src/')] = ($fullPath.Replace("$($pwd.Path)\",'')).Replace('\', '/') $mapping[$_.Replace('../', 'src/')] = ($fullPath.Replace("$($pwd.Path)\",'')).Replace('\', '/')
} }
@ -502,38 +435,19 @@ function New-MappingFile
} }
} }
} }
} } finally {
finally
{
Pop-Location Pop-Location
} }
Set-Content -Value ($mapping | ConvertTo-Json) -Path $mappingFilePath -Encoding Ascii Set-Content -Value ($mapping | ConvertTo-Json) -Path $mappingFilePath -Encoding Ascii
} }
function Get-InvertedOrderedMap
{
param(
$h
)
$res = [ordered]@{}
foreach ($q in $h.GetEnumerator()) {
if ($res.Contains($q.Value))
{
throw "Cannot invert hashtable: duplicated key $($q.Value)"
}
$res[$q.Value] = $q.Key
}
return $res
}
<# <#
.EXAMPLE Send-GitDiffToSd -diffArg1 45555786714d656bd31cbce67dbccb89c433b9cb -diffArg2 45555786714d656bd31cbce67dbccb89c433b9cb~1 -pathToAdmin d:\e\ps_dev\admin .EXAMPLE Send-GitDiffToSd -diffArg1 45555786714d656bd31cbce67dbccb89c433b9cb -diffArg2 45555786714d656bd31cbce67dbccb89c433b9cb~1 -pathToAdmin d:\e\ps_dev\admin
Apply a signle commit to admin folder Apply a signle commit to admin folder
#> #>
function Send-GitDiffToSd function Send-GitDiffToSd {
{
param( param(
[Parameter(Mandatory)] [Parameter(Mandatory)]
[string]$diffArg1, [string]$diffArg1,
@ -550,32 +464,88 @@ function Send-GitDiffToSd
$affectedFiles = git diff --name-only $diffArg1 $diffArg2 $affectedFiles = git diff --name-only $diffArg1 $diffArg2
$rev = Get-InvertedOrderedMap $m $rev = Get-InvertedOrderedMap $m
foreach ($file in $affectedFiles) { foreach ($file in $affectedFiles) {
if ($rev.Contains) if ($rev.Contains) {
{
$sdFilePath = Join-Path $pathToAdmin $rev[$file].Substring('src/monad/'.Length) $sdFilePath = Join-Path $pathToAdmin $rev[$file].Substring('src/monad/'.Length)
$diff = git diff $diffArg1 $diffArg2 -- $file $diff = git diff $diffArg1 $diffArg2 -- $file
if ($diff) if ($diff) {
{
Write-Host -Foreground Green "Apply patch to $sdFilePath" Write-Host -Foreground Green "Apply patch to $sdFilePath"
Set-Content -Value $diff -Path $env:TEMP\diff -Encoding Ascii Set-Content -Value $diff -Path $env:TEMP\diff -Encoding Ascii
if ($WhatIf) if ($WhatIf) {
{
Write-Host -Foreground Green "Patch content" Write-Host -Foreground Green "Patch content"
cat $env:TEMP\diff cat $env:TEMP\diff
} } else {
else
{
& $patchPath --binary -p1 $sdFilePath $env:TEMP\diff & $patchPath --binary -p1 $sdFilePath $env:TEMP\diff
} }
} } else {
else
{
Write-Host -Foreground Green "No changes in $file" Write-Host -Foreground Green "No changes in $file"
} }
} } else {
else
{
Write-Host -Foreground Green "Ignore changes in $file, because there is no mapping for it" Write-Host -Foreground Green "Ignore changes in $file, because there is no mapping for it"
} }
} }
} }
function script:log([string]$message) {
Write-Host -Foreground Green $message
}
function script:precheck([string]$command, [string]$missedMessage) {
$c = Get-Command $command -ErrorAction SilentlyContinue
if (-not $c) {
Write-Warning $missedMessage
return $false
} else {
return $true
}
}
function script:Get-InvertedOrderedMap {
param(
$h
)
$res = [ordered]@{}
foreach ($q in $h.GetEnumerator()) {
if ($res.Contains($q.Value)) {
throw "Cannot invert hashtable: duplicated key $($q.Value)"
}
$res[$q.Value] = $q.Key
}
return $res
}
## this function is from Dave Wyatt's answer on
## http://stackoverflow.com/questions/22002748/hashtables-from-convertfrom-json-have-different-type-from-powershells-built-in-h
function script:Convert-PSObjectToHashtable {
param (
[Parameter(ValueFromPipeline)]
$InputObject
)
process {
if ($null -eq $InputObject) { return $null }
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
$collection = @(
foreach ($object in $InputObject) { Convert-PSObjectToHashtable $object }
)
Write-Output -NoEnumerate $collection
} elseif ($InputObject -is [psobject]) {
$hash = @{}
foreach ($property in $InputObject.PSObject.Properties)
{
$hash[$property.Name] = Convert-PSObjectToHashtable $property.Value
}
$hash
} else {
$InputObject
}
}
}