2020-07-20 19:02:53 +03:00
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
2020-07-23 23:31:06 +03:00
# This script is used to auto-generate the FactoryOrchestratorClient implementation of IFactoryOrchestratorService (IPCInterface.cs)
2020-07-20 19:02:53 +03:00
Param
(
[ string ] $InterfaceName ,
[ string ] $InterfaceFile ,
[ string ] $TemplateFile ,
2020-11-17 19:58:40 +03:00
[ string ] $OutputFile ,
[ switch ] $Async
2020-07-20 19:02:53 +03:00
)
Write-Host " Autogenerating $OutputFile from $InterfaceName interface in $InterfaceFile "
$file = Get-Item -Path " $InterfaceFile "
$interfaceContent = Get-Content $file . FullName
$inInterface = $false
$indent = " "
[ string ] $outputContent = " "
$currentSummary = @ ( )
# find the interface, then iterate through the APIs in it
foreach ( $line in $interfaceContent )
{
if ( $inInterface -eq $true )
{
if ( $line -like " */// <summary>* " )
{
2020-11-17 19:58:40 +03:00
$currentSummary = " "
2020-07-20 19:02:53 +03:00
# add to methodSummaries
$currentSummary + = " $line `n "
}
elseif ( $line -like " *///* " )
{
# add to methodSummaries
$currentSummary + = " $line `n "
}
elseif ( $line -like " *; " )
{
2020-11-17 19:58:40 +03:00
if ( $Async )
2020-07-20 19:02:53 +03:00
{
2020-11-17 19:58:40 +03:00
$summarySplit = $currentSummary . Split ( " `n " )
for ( $i = 0 ; $i -lt $summarySplit . Count ; $i + + )
{
$summ = $summarySplit [ $i ] ;
if ( $i -eq 1 )
{
2020-11-19 23:52:34 +03:00
$summ = $summ . Replace ( " /// " , " /// Asynchronously " ) ;
2020-11-17 19:58:40 +03:00
}
if ( $i -ne $summarySplit . Count - 1 )
{
$outputContent + = " $summ `n "
}
}
}
else
{
2020-11-19 23:52:34 +03:00
$outputContent + = $currentSummary
2020-07-20 19:02:53 +03:00
}
# line is an API in the interface, parse it
$api = $line
[ regex ] $rx = '\s*(.+)\s(.+)(\(.+);'
$result = $rx . Match ( $api )
$apiRet = $result . Groups [ 1 ] . Value
$apiName = $result . Groups [ 2 ] . Value
$apiArgs = $result . Groups [ 3 ] . Value
Write-Host " Found api: $apiName args: $apiArgs ret: $apiRet "
# Create a new API per interface API which wraps the InvokeAsync call
2020-11-17 19:58:40 +03:00
$outputContent + = " $indent $indent " + " public "
if ( $Async )
2020-07-20 19:02:53 +03:00
{
2020-11-17 19:58:40 +03:00
if ( $apiRet -eq " void " )
{
$outputContent + = " async Task "
}
else
{
2020-11-19 23:52:34 +03:00
$outputContent + = " async Task< $apiRet > "
}
2020-07-20 19:02:53 +03:00
}
else
{
2020-11-17 19:58:40 +03:00
$outputContent + = " $apiRet "
2020-07-20 19:02:53 +03:00
}
2020-11-17 19:58:40 +03:00
$outputContent + = " $apiName $apiArgs "
$outputContent + = " `n $indent $indent { `n $indent $indent $indent "
if ( $Async )
2020-07-20 19:02:53 +03:00
{
2020-11-17 19:58:40 +03:00
$outputContent + = " if (!IsConnected) " + " `n $indent $indent $indent " + " { `n $indent $indent $indent $indent " + " throw new FactoryOrchestratorConnectionException(Resources.ClientNotConnected); `n $indent $indent $indent } "
$outputContent + = " `n `n $indent $indent $indent " + " try `n $indent $indent $indent { "
$outputContent + = " `n $indent $indent $indent $indent "
if ( $apiRet -eq " void " )
{
$outputContent + = " await _IpcClient.InvokeAsync(CreateIpcRequest( `" $apiName `" "
}
else
{
$outputContent + = " return await _IpcClient.InvokeAsync< $apiRet >(CreateIpcRequest( `" $apiName `" "
}
2020-07-20 19:02:53 +03:00
}
else
{
2020-11-17 19:58:40 +03:00
if ( $apiRet -eq " void " )
{
$outputContent + = " AsyncClient. $apiName ( "
}
else
{
$outputContent + = " return AsyncClient. $apiName ( "
2020-11-19 23:52:34 +03:00
}
2020-07-20 19:02:53 +03:00
}
# Split args, remove default values and variable typesd
if ( $apiArgs -like " () " )
{
$outputContent + = " ) "
}
else
{
2020-11-17 19:58:40 +03:00
if ( $Async )
{
$outputContent + = " , "
}
2020-07-20 19:02:53 +03:00
$argsSplit = $apiArgs . Split ( ',' )
2020-11-17 19:58:40 +03:00
$invokeAsyncArgs = " "
2020-07-20 19:02:53 +03:00
for ( $i = 0 ; $i -lt $argsSplit . Count ; $i + + )
{
$arg = $argsSplit [ $i ]
$equalSplit = $arg . Split ( '=' ) [ 0 ]
[ regex ] $rx = '\s*.+\s+(.+)\s?'
$result = $rx . Match ( $equalSplit )
2020-11-17 19:58:40 +03:00
$invokeAsyncArgs + = " $( $result . Groups [ 1 ] . Value ) "
2020-07-20 19:02:53 +03:00
if ( $i -ne $argsSplit . Count - 1 )
{
2020-11-17 19:58:40 +03:00
$invokeAsyncArgs + = " , "
2020-07-20 19:02:53 +03:00
}
elseif ( " $( $result . Groups [ 1 ] . Value ) " -notlike '*)*' )
{
2020-11-17 19:58:40 +03:00
$invokeAsyncArgs + = " ) "
2020-07-20 19:02:53 +03:00
}
}
2020-11-17 19:58:40 +03:00
$outputContent + = $invokeAsyncArgs
}
if ( $Async )
{
2020-11-19 23:52:34 +03:00
$outputContent + = " ) "
2020-11-17 19:58:40 +03:00
}
else
{
if ( $apiRet -eq " void " )
{
$outputContent + = " .Wait() "
}
else
2020-11-19 23:52:34 +03:00
{
$outputContent + = " .Result "
}
2020-07-20 19:02:53 +03:00
}
2020-11-17 19:58:40 +03:00
$outputContent + = " ; "
if ( $Async )
{
$outputContent + = " `n $indent $indent $indent } `n $indent $indent $indent "
$outputContent + = " catch (Exception ex) `n $indent $indent $indent { `n $indent $indent $indent $indent "
$outputContent + = " throw CreateIpcException(ex); `n "
$outputContent + = " $indent $indent $indent } `n "
}
else
{
2020-11-19 23:52:34 +03:00
$outputContent + = " `n "
2020-11-17 19:58:40 +03:00
}
$outputContent + = " $indent $indent } `n `n "
2020-07-20 19:02:53 +03:00
}
elseif ( $line -like " *}* $InterfaceName " )
{
# no longer in interface
break
}
}
elseif ( $line -like " * $InterfaceName " )
{
$inInterface = $true
}
}
$outputContent + = " `n $indent } `n } "
# Save to file
2020-11-19 23:52:34 +03:00
$OutFolder = [ System.IO.Path ] :: GetDirectoryName ( $OutputFile )
if ( ( Test-Path $OutFolder ) -eq $false )
{
$Folder = New-Item -Path $OutFolder -ItemType Directory
}
2020-07-20 19:02:53 +03:00
Copy-Item -Path " $TemplateFile " -Destination " $OutputFile "
2020-09-11 04:54:33 +03:00
Add-Content -Path " $OutputFile " -Value " $outputContent "
if ( $null -ne $env:AGENT_MACHINENAME )
{
# Running in Azure Pipeline. Print entire file for logging.
Write-Host " ------------ $OutputFile contents ------------ "
Get-Content $OutputFile | Write-Host
Write-Host " `n "
}