Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions PSCMake/Common/CMake.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ function GetCMakePresets {

<#
.Synopsis
Gets names of the 'buildPresets' in the specified CMakePresets.json object.
Gets 'buildPresets' in the specified CMakePresets.json object.
#>
function GetBuildPresetNames {
function GetBuildPresets {
param(
$CMakePresetsJson
)
Expand All @@ -101,15 +101,38 @@ function GetBuildPresetNames {
$null -ne $ConfigurePresetJson
}

$Presets.name
$Presets
}
}

function GetMatchingBuildPresets {
param(
$CMakePresetsJson,
$Preset
)
$BuildPresets = GetBuildPresets $CMakePresetsJson
if (-not $Preset) {
if (-not $BuildPresets) {
Write-Error "No Presets values specified, and one could not be inferred."
}
$BuildPresets | Select-Object -First 1
} else {
foreach ($CandidatePresetName in $Preset) {
$MatchingPresets = $BuildPresets |
Where-Object { ($_.name -like $CandidatePresetName) -or ($_.name -eq $CandidatePresetName) }
if (-not $MatchingPresets) {
Write-Error "Unable to find build preset '$CandidatePresetName' in $script:CMakePresetsPath"
}
$MatchingPresets
}
}
}

<#
.Synopsis
Gets names of the 'configurePresets' in the specified CMakePresets.json object.
Gets the 'configurePresets' in the specified CMakePresets.json object.
#>
function GetConfigurePresetNames {
function GetConfigurePresets {
param(
$CMakePresetsJson
)
Expand All @@ -124,7 +147,7 @@ function GetConfigurePresetNames {
$Presets = $Presets |
Where-Object { EvaluatePresetCondition $_ $CMakePresetsJson.configurePresets }

$Presets.name
$Presets
}
}

Expand All @@ -149,26 +172,12 @@ function GetCMake {
$CMake
}

function ResolvePresets {
function GetConfigurePresetFor {
param(
$CMakePresetsJson,

[ValidateSet('buildPresets', 'testPresets')]
$PresetType,

$PresetName
$Preset
)
$PresetJson = $CMakePresetsJson.$PresetType | Where-Object { $_.name -eq $PresetName }
if (-not $PresetJson) {
Write-Error "Unable to find $PresetType '$Preset' in $(GetCMakePresetsPath)"
}

$ConfigurePresetJson = $CMakePresetsJson.configurePresets | Where-Object { $_.name -eq $PresetJson.configurePreset }
if (-not $ConfigurePresetJson) {
Write-Error "Unable to find configure preset '$($PresetJson.configurePreset)' in $(GetCMakePresetsPath)"
}

$PresetJson, $ConfigurePresetJson
$CMakePresetsJson.configurePresets | Where-Object { $_.name -eq $Preset.configurePreset }
}

<#
Expand Down
91 changes: 33 additions & 58 deletions PSCMake/PSCMake.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ function BuildPresetsCompleter {
$null = $CommandAst
$null = $FakeBoundParameters
$CMakePresetsJson = GetCMakePresets -Silent
GetBuildPresetNames $CMakePresetsJson | Where-Object { $_ -ilike "$WordToComplete*" }
GetBuildPresets $CMakePresetsJson |
Select-Object -ExpandProperty 'name' |
Where-Object { $_ -ilike "$WordToComplete*" }
}

<#
Expand Down Expand Up @@ -112,10 +114,9 @@ function BuildTargetsCompleter {
$null = $ParameterName
$null = $CommandAst
$CMakePresetsJson = GetCMakePresets -Silent
$PresetNames = GetBuildPresetNames $CMakePresetsJson
$PresetName = $FakeBoundParameters['Preset'] ?? $PresetNames |
$BuildPreset = GetMatchingBuildPresets $CMakePresetsJson $FakeBoundParameters['Preset'] |
Select-Object -First 1
$BuildPreset, $ConfigurePreset = ResolvePresets $CMakePresetsJson 'buildPresets' $PresetName
$ConfigurePreset = GetConfigurePresetFor $CMakePresetsJson $BuildPreset
$BinaryDirectory = GetBinaryDirectory $CMakePresetsJson $ConfigurePreset
$CMakeCodeModel = Get-CMakeBuildCodeModel $BinaryDirectory

Expand Down Expand Up @@ -159,10 +160,9 @@ function ExecutableTargetsCompleter {
$null = $ParameterName
$null = $CommandAst
$CMakePresetsJson = GetCMakePresets -Silent
$PresetNames = GetBuildPresetNames $CMakePresetsJson
$PresetName = $FakeBoundParameters['Presets'] ?? $PresetNames |
$BuildPreset = GetMatchingBuildPresets $CMakePresetsJson $FakeBoundParameters['Preset'] |
Select-Object -First 1
$BuildPreset, $ConfigurePreset = ResolvePresets $CMakePresetsJson 'buildPresets' $PresetName
$ConfigurePreset = GetConfigurePresetFor $CMakePresetsJson $BuildPreset
$BinaryDirectory = GetBinaryDirectory $CMakePresetsJson $ConfigurePreset
$CMakeCodeModel = Get-CMakeBuildCodeModel $BinaryDirectory

Expand Down Expand Up @@ -197,7 +197,9 @@ function ConfigurePresetsCompleter {
$null = $CommandAst
$null = $FakeBoundParameters
$CMakePresetsJson = GetCMakePresets -Silent
GetConfigurePresetNames $CMakePresetsJson | Where-Object { $_ -ilike "$WordToComplete*" }
GetConfigurePresets $CMakePresetsJson |
Select-Object -ExpandProperty 'name' |
Where-Object { $_ -ilike "$WordToComplete*" }
}

function ConfigureCMake {
Expand Down Expand Up @@ -262,25 +264,24 @@ function Configure-CMakeBuild {
)
$CMakeRoot = FindCMakeRoot
$CMakePresetsJson = GetCMakePresets
$ConfigurePresetNames = GetConfigurePresetNames $CMakePresetsJson
$ConfigurePresetNames = if (-not $Preset) {
$ConfigurePresetNames | Select-Object -First 1
$ConfigurePresets = GetConfigurePresets $CMakePresetsJson
$ConfigurePresets = if (-not $Preset) {
$ConfigurePresets | Select-Object -First 1
} else {
foreach ($CandidatePreset in $Preset) {
$ExpandedPresets = $ConfigurePresetNames | Where-Object { $_ -like $CandidatePreset }
$ExpandedPresets ?? $CandidatePreset
foreach ($CandidatePresetName in $Preset) {
$MatchingPresets = $ConfigurePresets |
Where-Object { ($_.name -like $CandidatePresetName) -or ($_.name -eq $CandidatePresetName) }
if (-not $MatchingPresets) {
Write-Error "Unable to find configuration preset '$CandidatePresetName' in $script:CMakePresetsPath"
}
$MatchingPresets
}
}

$CMake = GetCMake
Using-Location $CMakeRoot {
foreach ($ConfigurePresetName in $ConfigurePresetNames) {
Write-Output "Preset : $ConfigurePresetName"

$ConfigurePreset = $CMakePresetsJson.configurePresets | Where-Object { $_.name -eq $ConfigurePresetName }
if (-not $ConfigurePreset) {
Write-Error "Unable to find configuration preset '$ConfigurePresetName' in $script:CMakePresetsPath"
}
foreach ($ConfigurePreset in $ConfigurePresets) {
Write-Output "Preset : $($ConfigurePreset.name)"

ConfigureCMake -CMake $CMake $CMakePresetsJson $ConfigurePreset -Fresh:$Fresh
}
Expand Down Expand Up @@ -349,18 +350,7 @@ function Build-CMakeBuild {
)
$CMakeRoot = FindCMakeRoot
$CMakePresetsJson = GetCMakePresets
$BuildPresetNames = GetBuildPresetNames $CMakePresetsJson
$BuildPresetNames = if (-not $Preset) {
if (-not $BuildPresetNames) {
Write-Error "No Presets values specified, and one could not be inferred."
}
$BuildPresetNames | Select-Object -First 1
} else {
foreach ($CandidatePreset in $Preset) {
$ExpandedPresets = $BuildPresetNames | Where-Object { $_ -like $CandidatePreset }
$ExpandedPresets ?? $CandidatePreset
}
}
$BuildPresets = GetMatchingBuildPresets $CMakePresetsJson $Preset

# If;
# * no targets were specified, and
Expand All @@ -370,10 +360,10 @@ function Build-CMakeBuild {
$ScopeLocation = (Get-Location).Path
$CMake = GetCMake
Using-Location $CMakeRoot {
foreach ($BuildPresetName in $BuildPresetNames) {
Write-Output "Preset : $BuildPresetName"
foreach ($BuildPreset in $BuildPresets) {
Write-Output "Preset : $($BuildPreset.name)"

$BuildPreset, $ConfigurePreset = ResolvePresets $CMakePresetsJson 'buildPresets' $BuildPresetName
$ConfigurePreset = GetConfigurePresetFor $CMakePresetsJson $BuildPreset
$BinaryDirectory = GetBinaryDirectory $CMakePresetsJson $ConfigurePreset
$CMakeCacheFile = Join-Path -Path $BinaryDirectory -ChildPath 'CMakeCache.txt'

Expand Down Expand Up @@ -415,7 +405,7 @@ function Build-CMakeBuild {

$CMakeArguments = @(
'--build'
'--preset', $BuildPresetName
'--preset', $BuildPreset.name
if ($ConfigurationName) {
'--config', $ConfigurationName
}
Expand Down Expand Up @@ -451,17 +441,9 @@ function Write-CMakeBuild {
[string] $As = 'dot'
)
$CMakePresetsJson = GetCMakePresets
$PresetNames = GetBuildPresetNames $CMakePresetsJson

if (-not $Preset) {
if (-not $PresetNames) {
Write-Error "No Preset values specified, and one could not be inferred."
}
$Preset = $PresetNames | Select-Object -First 1
Write-Information "No preset specified, defaulting to: $Preset"
}

$BuildPreset, $ConfigurePreset = ResolvePresets $CMakePresetsJson 'buildPresets' $Preset
$BuildPreset = GetMatchingBuildPresets $CMakePresetsJson $Preset |
Select-Object -First 1
$ConfigurePreset = GetConfigurePresetFor $CMakePresetsJson $BuildPreset
$BinaryDirectory = GetBinaryDirectory $CMakePresetsJson $ConfigurePreset
$CodeModel = Get-CMakeBuildCodeModel $BinaryDirectory
$CodeModelDirectory = Get-CMakeBuildCodeModelDirectory $BinaryDirectory
Expand Down Expand Up @@ -521,16 +503,9 @@ function Invoke-CMakeOutput {
[string[]] $Arguments
)
$CMakePresetsJson = GetCMakePresets
$PresetNames = GetBuildPresetNames $CMakePresetsJson

if (-not $Preset) {
if (-not $PresetNames) {
Write-Error "No Presets values specified, and one could not be inferred."
}
$Preset = $PresetNames | Select-Object -First 1
}

$BuildPreset, $ConfigurePreset = ResolvePresets $CMakePresetsJson 'buildPresets' $Preset
$BuildPreset = GetMatchingBuildPresets $CMakePresetsJson $Preset |
Select-Object -First 1
$ConfigurePreset = GetConfigurePresetFor $CMakePresetsJson $BuildPreset
$BinaryDirectory = GetBinaryDirectory $CMakePresetsJson $ConfigurePreset

# Find the 'code model' for the preset. If no code model is found, configure the build and try again.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ BeforeAll {
} }
}

Describe 'GetConfigurePresetNames' {
Describe 'GetConfigurePresets' {
It 'Given CMakePresets.Complex.json it retrieves the correct configuration preset names.' {
$CMakePresetsJson = Get-Content "$PSScriptRoot/ReferencePresets/CMakePresets.Complex.json" | ConvertFrom-Json

GetConfigurePresetNames $CMakePresetsJson |
GetConfigurePresets $CMakePresetsJson |
Select-Object -ExpandProperty 'name' |
Should -Be @('linux-x64')
}
}