Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 219 additions & 0 deletions PowerShellGitHubDev.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,222 @@ function Start-DevPSGitHub
}
}
}

## 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 -ToSubmodule # copy files FROM src/<project> folders TO submodule
#>
function Copy-SubmoduleFiles {

[CmdletBinding()]
param(
[string]$mappingFilePath = "$PSScriptRoot/mapping.json",
[switch]$ToSubmodule
)


if (-not (Test-Path $mappingFilePath))
{
throw "Mapping file not found in $mappingFilePath"
}

$m = cat -Raw $mappingFilePath | ConvertFrom-Json | Convert-PSObjectToHashtable

# mapping.json assumes the root folder
Push-Location $PSScriptRoot
try
{
$m.GetEnumerator() | % {

if ($ToSubmodule)
{
cp $_.Value $_.Key -Verbose:$Verbose
}
else
{
mkdir (Split-Path $_.Value) -ErrorAction SilentlyContinue > $null
cp $_.Key $_.Value -Verbose:$Verbose
}
}
}
finally
{
Pop-Location
}
}

<#
.EXAMPLE Create-MappingFile # create mapping.json in the root folder from project.json files
#>
function New-MappingFile
{
param(
[string]$mappingFilePath = "$PSScriptRoot/mapping.json",
[switch]$IgnoreCompileFiles,
[switch]$Ignoreresource
)

function Get-MappingPath([string]$project, [string]$path)
{
if ($project -match 'TypeCatalogGen')
{
return Split-Path $path -Leaf
}

if ($project -match 'Microsoft.Management.Infrastructure')
{
return Split-Path $path -Leaf
}

return ($path -replace '../monad/monad/src/', '')
}

$mapping = [ordered]@{}

# assumes the root folder
Push-Location $PSScriptRoot
try
{
$projects = ls .\src\ -Recurse -Depth 2 -Filter 'project.json'
$projects | % {
$project = Split-Path $_.FullName
$json = cat -Raw -Path $_.FullName | ConvertFrom-Json
if (-not $IgnoreCompileFiles) {
$json.compileFiles | % {
if ($_) {
if (-not $_.EndsWith('AssemblyInfo.cs'))
{
$fullPath = Join-Path $project (Get-MappingPath -project $project -path $_)
$mapping[$_.Replace('../', 'src/')] = ($fullPath.Replace("$($pwd.Path)\",'')).Replace('\', '/')
}
}
}
}

if ((-not $Ignoreresource) -and ($json.resource)) {
$json.resource | % {
if ($_) {
ls $_.Replace('../', 'src/') | % {
$fullPath = Join-Path $project (Join-Path 'resources' $_.Name)
$mapping[$_.FullName.Replace("$($pwd.Path)\", '').Replace('\', '/')] = ($fullPath.Replace("$($pwd.Path)\",'')).Replace('\', '/')
}
}
}
}
}
}
finally
{
Pop-Location
}

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
Apply a signle commit to admin folder
#>
function Send-GitDiffToSd
{
param(
[Parameter(Mandatory)]
[string]$diffArg1,
[Parameter(Mandatory)]
[string]$diffArg2,
[Parameter(Mandatory)]
[string]$pathToAdmin,
[string]$mappingFilePath = "$PSScriptRoot/mapping.json",
[switch]$WhatIf
)

$patchPath = Join-Path (get-command git).Source ..\..\bin\patch
$m = cat -Raw $mappingFilePath | ConvertFrom-Json | Convert-PSObjectToHashtable
$affectedFiles = git diff --name-only $diffArg1 $diffArg2
$rev = Get-InvertedOrderedMap $m
foreach ($file in $affectedFiles) {
if ($rev.Contains)
{
$sdFilePath = Join-Path $pathToAdmin $rev[$file].Substring('src/monad/'.Length)
$diff = git diff $diffArg1 $diffArg2 -- $file
if ($diff)
{
Write-Host -Foreground Green "Apply patch to $sdFilePath"
Set-Content -Value $diff -Path $env:TEMP\diff -Encoding Ascii
if ($WhatIf)
{
Write-Host -Foreground Green "Patch content"
cat $env:TEMP\diff
}
else
{
& $patchPath --binary -p1 $sdFilePath $env:TEMP\diff
}
}
else
{
Write-Host -Foreground Green "No changes in $file"
}
}
else
{
Write-Host -Foreground Green "Ignore changes in $file, because there is no mapping for it"
}
}
}
Loading