summaryrefslogtreecommitdiff
path: root/.github/scripts/Build-ChangedSamples.ps1
blob: f03f93f3b74a4b033d14b9e246d890092492a312 (plain)
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
[CmdletBinding()]
param (
    [array]$ChangedFiles
)

$Verbose = $false
if ($PSBoundParameters.ContainsKey('Verbose')) {
    $Verbose = $PsBoundParameters.Get_Item('Verbose')
}

$root = (Get-Location).Path

# Search for samples (directories) to build
$sampleSet = @{}
$buildAll = $false
foreach ($file in $ChangedFiles) {
    if (-not (Test-Path $file)) {
        Write-Verbose "`u{2754} Changed file $file cannot be found"
        continue
    }
    $dir = (Get-Item $file).DirectoryName
    $filename = Split-Path $file -Leaf

    # Files that can affect how every sample is built should trigger a full build
    if ($filename -eq "Build-Samples.ps1" -or $filename -eq "Get-NtTargetVersions.ps1" -or $filename -eq "exclusions.csv" -or $filename -eq "Directory.Build.props" -or $filename -eq "packages.config") {
        $buildAll = $true
    }
    if ($dir -like "$root\.github\scripts" -or $dir -like "$root\.github\scripts\*") {
        $buildAll = $true
    }
    if ($dir -like "$root\.github\workflows" -or $dir -like "$root\.github\workflows\*") {
        $buildAll = $true
    }
    if ($buildAll)
    {
        Write-Verbose "`u{2754} Full build triggered by change in file $file"
        break
    }

    while ((-not ($slnItems = (Get-ChildItem $dir '*.sln'))) -and ($dir -ne $root)) {
        $dir = (Get-Item $dir).Parent.FullName
    }
    if ($dir -eq $root) {
        Write-Verbose "`u{2754} Changed file $file does not match a sample"
        continue
    }
    $sampleName = $dir.Replace($root, '').Trim('\').Replace('\', '.').ToLower()
    Write-Verbose "`u{1F50E} Found sample [$sampleName] at $dir from changed file $file"
    if (-not ($sampleSet.ContainsKey($sampleName))) {
        $sampleSet[$sampleName] = $dir
    }
}

if ($buildAll) {
    .\Build-Samples -Verbose:$Verbose -LogFilesDirectory (Join-Path $root "_logs")
}
else {
    $sampleNames = $sampleSet.Keys | Sort-Object
    .\Build-Samples -Samples $sampleNames -Verbose:$Verbose -LogFilesDirectory (Join-Path $root "_logs")
}