#**************************************************************************#
#**  QUADARAX DEPLOYMENT SCRIPTS                                         **#
#**  File: 002_library.ps1                                               **#
#**  Description: PowerShell script library of elementary functions to   **#
#**                work with QDS                                         **#
#**  Version: 1.0.                                                       **#
#**  Updated: 29.7.2019                                                  **# 
#**************************************************************************#

# Rebuilds solution file, may calls CleanSolution
Function RebuildSolution
{
    param(
        # Solution file name with full path
        [string] $solutionFileName,
        # Build configuration Debug / Release
        [string] $configurationName = $buildCfgDebug,
        # Defines alternative output directory. If not defined, use default output locations depends on configuration
        [string] $alternativeOutputDirectory,
        # Defines if Clean will be called before build
        [bool] $cleanBeforeBuild = $false,
        # Defines if only show calling commands instead of executes them (just for debugging)
        [bool] $simulate = $false
    )

    $fileExists = Test-Path($solutionFileName)
    if ($fileExists -eq $false){
        Write-Error("Solution file '{0}' doesn't exists." -f $solutionFileName)
        Break
    }

    if (!$alternativeOutputDirectory){
        $alternativeOutputDirectory = ""
    }
    else{
        $alternativeOutputDirectory = " /p:OutputPath='{0}' " -f $alternativeOutputDirectory
    }

    Write-Host("**** BEGIN *** BUILDS SOLUTION/PROJECT : {0} ****" -f $solutionFileName) -ForegroundColor Yellow
    if ($cleanBeforeBuild -eq $true){
        CleanSolution $solutionFileName $configurationName $simulate
    }
    if ($simulate -eq $true){
        Write-Host "msbuild $solutionFileName /t:$buildTrgRebuild /p:VisualStudioVersion=15.0 /p:Configuration=$configurationName /m /clp:Summary /clp:ShowTimestamp $alternativeOutputDirectory"  -ForegroundColor Cyan
    } else{
        msbuild $solutionFileName /t:$buildTrgRebuild /p:VisualStudioVersion=15.0 /p:Configuration=$configurationName /m /clp:Summary /clp:ShowTimestamp $alternativeOutputDirectory
    }    
    Write-Host("**** END *** BUILDS SOLUTION/PROJECT : {0} ****" -f $solutionFileName) -ForegroundColor Yellow
}

# Cleans solution file
Function CleanSolution
{
    param(
        # Solution file name with full path
        [string] $solutionFileName,
        # Build configuration Debug / Release
        [string] $configurationName = $buildCfgDebug,
        # Defines if only show calling commands instead of executes them (just for debugging)
        [bool] $simulate = $false
    )

    $fileExists = Test-Path($solutionFileName)
    if ($fileExists -eq $false){
        Write-Error("Solution file '{0}' doesn't exists." -f $solutionFileName)
        Break
    }

    Write-Host("***** BEGIN *** CLEANS SOLUTION/PROJECT : {0} *****" -f $solutionFileName) -ForegroundColor DarkYellow
    if ($simulate -eq $true){
        Write-Host "msbuild $solutionFileName /t:$buildTrgClean /p:VisualStudioVersion=15.0 /p:Configuration=$configurationName /m" -ForegroundColor Cyan
    } else{
        msbuild $solutionFileName /t:$buildTrgClean /p:VisualStudioVersion=15.0 /p:Configuration=$configurationName /m
    }    
    Write-Host("***** END *** CLEANS SOLUTION/PROJECT : {0} *****" -f $solutionFileName) -ForegroundColor DarkYellow
    #/p:SolutionDir=$location /m
}

# Publish solution file, may calls CleanSolution
Function PublishSolution
{
    param(
        # Solution file name with full path
        [string] $solutionFileName,
        # Build configuration Debug / Release
        [string] $configurationName = $buildCfgDebug,
        # Defines alternative output directory. If not defined, use default output locations depends on configuration
        [string] $alternativeOutputDirectory,
        # Defines if Clean will be called before build
        [bool] $cleanBeforeBuild = $false,
        # Defines if only show calling commands instead of executes them (just for debugging)
        [bool] $simulate = $false
    )

    $fileExists = Test-Path($solutionFileName)
    if ($fileExists -eq $false){
        Write-Error("Solution file '{0}' doesn't exists." -f $solutionFileName)
        Break
    }

    if (!$alternativeOutputDirectory){
        $alternativeOutputDirectory = ""
    }
    else{
        $alternativeOutputDirectory = " /p:PublishDestination='{0}'" -f $alternativeOutputDirectory
    }

    Write-Host("**** BEGIN *** BUILDS SOLUTION/PROJECT : {0} ****" -f $solutionFileName) -ForegroundColor Yellow
    if ($cleanBeforeBuild -eq $true){
        CleanSolution $solutionFileName $configurationName $simulate
    }
    if ($simulate -eq $true){
        Write-Host "msbuild $solutionFileName /t:$buildTrgPublish /p:VisualStudioVersion=15.0 /p:Configuration=$configurationName /m /clp:Summary /clp:ShowTimestamp $alternativeOutputDirectory"  -ForegroundColor Cyan
    } else{
        msbuild $solutionFileName /t:$buildTrgPublish /p:VisualStudioVersion=15.0 /p:Configuration=$configurationName /m /clp:Summary /clp:ShowTimestamp $alternativeOutputDirectory
    }    
    Write-Host("**** END *** BUILDS SOLUTION/PROJECT : {0} ****" -f $solutionFileName) -ForegroundColor Yellow
}


# Returns list of project from solution file. Returns objects with properties: Name, File, Guid, Directory
Function GetProjectsFromSolution
{
    param(
        # Solution file name with full path
        [string] $solutionFileName
    )

    $fileExists = Test-Path($solutionFileName)
    if ($fileExists -eq $false){
        Write-Error("Solution file '{0}' doesn't exists." -f $solutionFileName)
        Break
    }

    return Get-Content $solutionFileName |
        Select-String 'Project\(' |
            ForEach-Object {
                $projectParts = $_ -Split '[,=]' | ForEach-Object { $_.Trim('[ "{}]') };
                New-Object PSObject -Property @{
                Name = $projectParts[1];
                File = $projectParts[2];
                Guid = $projectParts[3];
                Directory = Split-Path -Path "$projectDir\$($projectParts[2])" -Parent
                }
            }
}
Write-Host "QDS library was loaded." -ForegroundColor DarkGray