ReCrutch.Build.psm1

function Install-ReCrAssembliesForBuildSharePointSln()
{
    pushd "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\x64"
    try
    {
        Write-Host-New-Line $ReCr.NewLines.beforeHeader1
        Write-Host-Indent -1 "$($MyInvocation.MyCommand):" -ForegroundColor:$ReCr.ConsoleColors.header1

        $vsPath = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community"

        $files = @(
          "$vsPath\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.Modeling.Sdk.15.0.dll"
          "$vsPath\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.SharePoint.Designers.Models.dll"
          "$vsPath\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.SharePoint.Designers.Models.Features.dll"
          "$vsPath\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.SharePoint.dll"
          "$vsPath\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.Shell.15.0.dll"
          "$vsPath\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.Shell.Framework.dll"

          #"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\VisualStudio\v15.0\SharePointTools\Microsoft.VisualStudio.SharePoint.dll"
        )

        ForEach ($file in $files) {
            & ./sn -Vr $file
            if($LASTEXITCODE -ne 0)
            {
                throw New-Object [System.Exception]("Process sn -Vr $file
    Exit code: $($LASTEXITCODE)"
)
            }

            & ./gacutil /i $file
            if($LASTEXITCODE -ne 0)
            {
                throw New-Object [System.Exception]("Process gacutil /i $file
    Exit code: $($LASTEXITCODE)"
)
            }
        }
    }
    catch
    {
        Show-ReCrError "$($MyInvocation.MyCommand)" $_.Exception
        throw new-object System.Exception ("$($MyInvocation.MyCommand) error", $_.Exception)
    }
    finally
    {
        popd
    }
}

function Get-ReCrVSPaths()
{
    try
    {
        $r = @{}

        #C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe
        #C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\
        $COMNTOOLS = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\"
        if((Test-Path $COMNTOOLS) -eq $false)
        {
            $COMNTOOLS = "C:\Program Files (x86)\Microsoft Visual Studio\2018\Community\Common7\Tools\"
            if((Test-Path $COMNTOOLS) -eq $false)
            {
                $COMNTOOLS = $null
            }
        }

        if($COMNTOOLS -eq $null)
        {
            throw new-object System.Exception ("�� ��������� ���� � C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\")
        }

        $r.VS_Common7Tools = $COMNTOOLS
        $r.VS_devenv = [System.IO.Path]::Combine($r.VS_Common7Tools, "..\IDE\devenv.exe")

        if($r.VS_devenv -eq $null -or (Test-Path $r.VS_devenv) -eq $false)
        {
            throw new-object System.Exception ("�� ��������� ���� � devenv.exe")
        }

        $r
    }
    catch
    {
        Show-ReCrError "$($MyInvocation.MyCommand)" $_.Exception
        throw new-object System.Exception ("$($MyInvocation.MyCommand) error", $_.Exception)
    }
}

function Set-ReCrVsDevCmdBatVariables()
{
    try
    {
        Write-Host-New-Line $ReCr.NewLines.beforeHeader1
        Write-Host-Indent 0 "$($MyInvocation.MyCommand):" -ForegroundColor:$ReCr.ConsoleColors.header1

        $vsPaths = Get-ReCrVSPaths
        pushd "$($vsPaths.VS_Common7Tools)"
        try
        {
            cmd /c "VsDevCmd.bat&set" |
            foreach {
              if ($_ -match "=") {
                $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
              }
            }

            Write-Host-Indent 1 "$($vsPaths.VS_Common7Tools)VsDevCmd.bat Command Prompt variables set" -ForegroundColor:$ReCr.ConsoleColors.high_add
        }
        finally
        {
            popd
        }
    }
    catch
    {
        Show-ReCrError "$($MyInvocation.MyCommand)" $_.Exception
        throw new-object System.Exception ("$($MyInvocation.MyCommand) error", $_.Exception)
    }
}

function Invoke-ReCrMsBuild($workingDirectory, $arguments)
{
    try
    {
        Write-Host-New-Line $ReCr.NewLines.beforeHeader1
        Write-Host-Indent 0 "$($MyInvocation.MyCommand):" -ForegroundColor:$ReCr.ConsoleColors.header1
        pushd $workingDirectory
        try
        {
            msbuild $arguments
            Write-Host "Process exit code: $($LASTEXITCODE)"
        
            if($LASTEXITCODE -ne 0)
            {
                throw New-Object [System.Exception]("Process msbuild $($arguments -join ' ')
Exit code: $($LASTEXITCODE)"
)
            }
        }
        finally
        {
            popd
        }
    }
    catch
    {
        Show-ReCrError "$($MyInvocation.MyCommand)" $_.Exception
        throw new-object System.Exception ("$($MyInvocation.MyCommand) error", $_.Exception)
    }
}

function Start-ReCrDevenv($arguments)
{
    try
    {
        Write-Host-New-Line $ReCr.NewLines.beforeHeader1
        Write-Host-Indent 0 "$($MyInvocation.MyCommand):" -ForegroundColor:$ReCr.ConsoleColors.header1
        
        $vsPaths = Get-ReCrVSPaths
        $devenv = $vsPaths.VS_devenv

        Write-Host-Indent 1 "$devenv $arguments" -ForegroundColor:$ReCr.ConsoleColors.verbose

        $pinfo = New-Object System.Diagnostics.ProcessStartInfo
        $pinfo.FileName = $devenv
        $pinfo.RedirectStandardError = $true
        $pinfo.RedirectStandardOutput = $true
        $pinfo.UseShellExecute = $false
        $pinfo.Arguments = $arguments

        $p = New-Object System.Diagnostics.Process
        $p.StartInfo = $pinfo
        $p.Start() | Out-Null

        #Write-Host "---Output SqlPackage--- b"
        #while($p.HasExited -eq $false)
        #{
        # $stdout = ConvertEncoding $srcCodePage $dstCodePage $p.StandardOutput.BaseStream
        # Write-Host $stdout -NoNewline
        #}

        #$stdout = ConvertEncoding $srcCodePage $dstCodePage $p.StandardOutput.BaseStream
        #Write-Host $stdout -NoNewline

        #Write-Host "---Output SqlPackage--- e"
        $p.WaitForExit()
        if($p.ExitCode -ne 0)
        {
            throw [System.Exception] "Process $($pinfo.FileName) $($pinfo.Arguments)
    Exit code: $($p.ExitCode)"

        }
    }
    catch
    {
        Show-ReCrError "$($MyInvocation.MyCommand)" $_.Exception
        throw new-object System.Exception ("$($MyInvocation.MyCommand) error", $_.Exception)
    }
}

function Clear-ReCrFolder($path)
{
    if((Test-Path $path) -eq $false)
    {
        $f = New-Item $path -type directory
    }

    Write-Host "������� �������� $path"
    Remove-Item "$($path.TrimEnd("\"))\*" -Recurse:$true
}

function Restore-ReCrNuGetPackage($slnPath)
{
    try
    {
        Write-Host-New-Line $ReCr.NewLines.beforeHeader1
        Write-Host-Indent 0 "$($MyInvocation.MyCommand):" -ForegroundColor:$ReCr.ConsoleColors.header1

        $sfp = Get-ReCrScriptPath $MyInvocation.MyCommand
        $nuget = (Resolve-Path -Path ([System.IO.Path]::Combine($sfp, "nuget.exe"))).Path
        . $nuget restore $slnPath
    }
    catch
    {
        Show-ReCrError "$($MyInvocation.MyCommand)" $_.Exception
        throw new-object System.Exception ("$($MyInvocation.MyCommand) error", $_.Exception)
    }
}

function Restore-ReCrNpmPackage($path)
{
    try
    {
        Write-Host-New-Line $ReCr.NewLines.beforeHeader1
        Write-Host-Indent 0 "$($MyInvocation.MyCommand):" -ForegroundColor:$ReCr.ConsoleColors.header1

        pushd $path
        try
        {
            npm -v
            npm install
            if($LASTEXITCODE -ne 0)
            {
                throw New-Object [System.Exception]("Process npm install
Exit code: $($LASTEXITCODE)"
)
            }
        }
        finally
        {
            popd
        }
    }
    catch
    {
        Show-ReCrError "$($MyInvocation.MyCommand)" $_.Exception
        throw new-object System.Exception ("$($MyInvocation.MyCommand) error", $_.Exception)
    }
}

function Get-ReCrBuildVersions($versionsPath)
{
    $versionsStr = Get-Content $versionsPath | Out-String
    $versions = ConvertFrom-Json $versionsStr

    $replSb = {
        param($pattern)

        $pattern = $pattern -replace "{major}", $versions.major
        $pattern = $pattern -replace "{minor}", $versions.minor
        $pattern = $pattern -replace "{build}", $versions.build
        $pattern = $pattern -replace "{revision}", $versions.revision

        return $pattern
    }

    $v = @{
        AssemblyFileVersion = & $replSb -pattern:$versions.AssemblyFileVersionPattern
        AssemblyInformationalVersion = & $replSb -pattern:$versions.AssemblyInformationalVersionPattern
    }

    return $v
}

function Copy-ReCrBuildVersions($assemblyInfoFiles, $spAddInAppManifestFiles, $packageJsonFiles)
{
    try
    {
        Write-Host-New-Line $ReCr.NewLines.beforeHeader1
        Write-Host-Indent 0 "$($MyInvocation.MyCommand):" -ForegroundColor:$ReCr.ConsoleColors.header1

        $versions = BuildGetVersions

        if($assemblyInfoFiles -ne $null)
        {
            foreach($file in $assemblyInfoFiles)
            {
                BuildReplaceContenFile -file:$file -patternReplacment:@(
                    @{
                        pattern ='(?<attr>\[assembly: AssemblyFileVersion\(\"[^\"]*\"\)\])'
                        replacment = "[assembly: AssemblyFileVersion(`"$($versions.AssemblyFileVersion)`")]"
                    },
                    @{
                        pattern ='(?<attr>\[assembly: AssemblyInformationalVersion\(\"[^\"]*\"\)\])'
                        replacment = "[assembly: AssemblyInformationalVersion(`"$($versions.AssemblyInformationalVersion)`")]"
                    }
                )
            }
        }

        if($spAddInAppManifestFiles -ne $null)
        {
            foreach($file in $spAddInAppManifestFiles)
            {
                BuildReplaceContenFile -file:$file -patternReplacment:@(
                    @{
                        pattern ='(?<v> Version=\"[^\"]*\")'
                        replacment = " Version=`"$($versions.AssemblyFileVersion)`""
                    }
                )
            }
        }

        if($packageJsonFiles -ne $null)
        {
            foreach($file in $packageJsonFiles)
            {
                BuildReplaceContenFile -file:$file -patternReplacment:@(
                    @{
                        pattern ='(?<v>\"version\"\: \"[^\"]*\")'
                        replacment = "`"version`": `"$($versions.AssemblyFileVersion)`""
                    }
                )
            }
        }
    }
    catch
    {
        Show-ReCrError "$($MyInvocation.MyCommand)" $_.Exception
        throw new-object System.Exception ("$($MyInvocation.MyCommand) error", $_.Exception)
    }
}