Skip to content

Instantly share code, notes, and snippets.

@yipo
Last active April 29, 2021 10:02
Show Gist options
  • Save yipo/e70638aae454d95377cc96f936ba0840 to your computer and use it in GitHub Desktop.
Save yipo/e70638aae454d95377cc96f936ba0840 to your computer and use it in GitHub Desktop.
Update the source file list of CMakeLists.txt under one directory.
$ErrorActionPreference = 'Stop'
function Get-SourceFiles ([String]$Dir) {
Push-Location $Dir
try {
Get-ChildItem . -File -Recurse | Where-Object { $_.Extension -match '^\.(c|cpp|h|hpp)$' } | Resolve-Path -Relative | ForEach-Object { $_.TrimStart('.\').Replace('\', '/') } | Sort-Object
}
finally {
Pop-Location
}
}
function Update-SourceFileList {
param (
[Parameter(ValueFromPipeline)]
[String]$Line,
[String]$Dir = $PWD
)
begin {
$take = $true
}
process {
if ($take) {
$Line
$take = $Line -ne 'set(SOURCES'
}
elseif ($Line -eq ')') {
Get-SourceFiles $Dir | ForEach-Object { " $_" }
')'
$take = $true
}
}
}
function Update-CMakeLists ([String]$Dir) {
$file = Join-Path $Dir -ChildPath 'CMakeLists.txt' -Resolve
$content = Get-Content $file | Update-SourceFileList -Dir $Dir
Set-Content $file -Value $content
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment