forked from ubisoft/Sharpmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunSample.ps1
77 lines (67 loc) · 2.63 KB
/
RunSample.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<#
.SYNOPSIS
Runs the sample with the given name
.DESCRIPTION
Runs all commands for the given sample name, as defined in the SamplesDef.json file.
.PARAMETER sampleName
name of the sample to run. If there is no sample with than name in the SamplesDef.json, an error is returned.
.PARAMETER configuration
valued used as configuration in the sample. All commands in the selected sample will see their "{configuration}" string replaced with this value.
.PARAMETER framework
valued used as framework in the sample. All commands in the selected sample will see their "{framework}" string replaced with this value.
.PARAMETER os
valued used as os in the sample. All commands in the selected sample will see their "{os}" string replaced with this value.
.PARAMETER vsVersionSuffix
valued used as Vs version suffix in the sample. All commands in the selected sample will see their "{vsVersionSuffix}" string replaced with this value.
#>
param ([string] $sampleName, [string] $configuration, [string] $framework, [string]$os = "", [string] $VsVersionSuffix = "")
# description of a sample
class SampleDef
{
[string] $Name
[string[]] $CIs
[string[]] $OSs
[string[]] $Frameworks
[string[]] $Configurations
[string] $TestFolder = ""
[string[]] $Commands
}
# json file that contains all samples
class SamplesDefJson
{
[SampleDef[]] $Samples
}
# read json file
$testDefs = [SamplesDefJson] (Get-Content -Raw -Path "SamplesDef.json" | Out-String | ConvertFrom-Json)
# run all samples with given name and parameters
foreach ($sample in ($testDefs.Samples | Where-Object { $_.Name -eq $sampleName}))
{
$found = $true;
Write-Host "running sample $($sample.Name)"
# run all commands registered in sample
foreach ($command in $sample.Commands)
{
# apply parameters to sample command line
$resolvedCommand = $command.replace('{testFolder}',$sample.testFolder).replace('{configuration}',$configuration).replace('{framework}',$framework).replace('{os}',$os).replace('{VsVersionSuffix}',$vsVersionSuffix)
Write-Host "running : $resolvedCommand"
# add error detection at the end of the command
$resolvedCommand = $resolvedCommand + '; if ( -not $? ) { throw "command returned error $LASTEXITCODE" }'
# run command line
try
{
Invoke-Expression "$resolvedCommand"
}
catch
{
Write-Host "error detected in invoked command"
Write-Error $PSItem.Exception
exit 1
}
}
}
# error if given sample does not exist in json file
if ($found -ne $true)
{
Write-Error "sample $sampleName was not found"
exit 1
}