Skip to content

Commit d1456c3

Browse files
committed
Refactor searching for files
Cleanup build directory to avoid conflicts
1 parent 6df9819 commit d1456c3

5 files changed

Lines changed: 65 additions & 11 deletions

File tree

extension/BuildPhpExtension/BuildPhpExtension.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
'Get-PhpDevelBuild',
9696
'Get-PhpSdk',
9797
'Get-PhpSrc',
98+
'Get-RecursiveFilePath',
9899
'Get-TempFiles',
99100
'Get-VsCacheInfo',
100101
'Get-VsInstallPath',

extension/BuildPhpExtension/private/Get-Extension.ps1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,31 @@ function Get-Extension {
106106
}
107107
}
108108

109-
$configW32 = Get-ChildItem (Get-Location).Path -Recurse -Filter "config.w32" -ErrorAction SilentlyContinue | Select-Object -First 1
109+
$configW32 = Get-RecursiveFilePath -Directory (Get-Location).Path -FileName 'config.w32'
110110
if($null -eq $configW32) {
111111
if($LocalSrc) {
112112
throw "No config.w32 found, please make sure you are in the extension source directory and it supports Windows."
113113
} else {
114114
throw "No config.w32 found, please check if the extension supports Windows."
115115
}
116116
}
117-
$subDirectory = $configW32.DirectoryName
117+
$subDirectory = Split-Path -Path $configW32 -Parent
118118
if((Get-Location).Path -ne $subDirectory) {
119119
Copy-Item -Path "${subDirectory}\*" -Destination $BuildDirectory -Recurse -Force
120120
Remove-Item -Path $subDirectory -Recurse -Force
121121
}
122+
123+
$selectedConfigW32 = Join-Path (Resolve-Path $BuildDirectory).Path "config.w32"
124+
if (Test-Path -LiteralPath $selectedConfigW32 -PathType Leaf) {
125+
Get-ChildItem -LiteralPath (Resolve-Path $BuildDirectory).Path -Recurse -Filter "config.w32" -File -ErrorAction SilentlyContinue |
126+
Where-Object {
127+
-not [System.StringComparer]::OrdinalIgnoreCase.Equals(
128+
(Resolve-Path -LiteralPath $_.FullName).Path,
129+
$selectedConfigW32
130+
)
131+
} |
132+
Remove-Item -Force
133+
}
122134
$name = Get-ExtensionName
123135

124136
if(!$patches) {
@@ -132,4 +144,4 @@ function Get-Extension {
132144
}
133145
end {
134146
}
135-
}
147+
}

extension/BuildPhpExtension/private/Get-ExtensionConfig.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ Function Get-ExtensionConfig {
251251
}
252252

253253
# TODO: This should be implemented using composer.json once implemented
254-
$packageXml = Get-ChildItem (Get-Location).Path -Recurse -Filter "package.xml" -ErrorAction SilentlyContinue | Select-Object -First 1
254+
$packageXml = Get-RecursiveFilePath -Directory (Get-Location).Path -FileName 'package.xml'
255255
if($null -ne $packageXml) {
256-
$xml = [xml](Get-Content $packageXml.FullName)
256+
$xml = [xml](Get-Content $packageXml)
257257
$config.docs = $xml.SelectNodes("//*[@role='doc']") | ForEach-Object {
258258
$path = $_.name
259259
$current = $_.ParentNode
@@ -286,4 +286,4 @@ Function Get-ExtensionConfig {
286286
}
287287
end {
288288
}
289-
}
289+
}

extension/BuildPhpExtension/private/Get-ExtensionSource.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ function Get-ExtensionSource {
1919
process {
2020
if($env:GITHUB_ACTIONS -eq "true") {
2121
if ($null -eq $ExtensionUrl -or $ExtensionUrl -eq '') {
22-
$configW32 = $null
23-
try {
24-
$configW32 = [System.IO.Directory]::EnumerateFiles((Get-Location).Path, 'config.w32', [System.IO.SearchOption]::AllDirectories) | Select-Object -First 1
25-
} catch { }
22+
$configW32 = Get-RecursiveFilePath -Directory (Get-Location).Path -FileName 'config.w32'
2623
if($null -eq $configW32) {
2724
$ExtensionUrl = "https://github.com/$env:GITHUB_REPOSITORY"
2825
}
@@ -65,4 +62,4 @@ function Get-ExtensionSource {
6562
}
6663
end {
6764
}
68-
}
65+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Function Get-RecursiveFilePath {
2+
<#
3+
.SYNOPSIS
4+
Get a file path from a directory, preferring the root file over nested matches.
5+
.PARAMETER Directory
6+
Directory to search.
7+
.PARAMETER FileName
8+
File name to search for.
9+
#>
10+
[OutputType([string])]
11+
param (
12+
[Parameter(Mandatory = $true, Position=0, HelpMessage='Directory to search.')]
13+
[ValidateNotNull()]
14+
[ValidateLength(1, [int]::MaxValue)]
15+
[string] $Directory,
16+
[Parameter(Mandatory = $true, Position=1, HelpMessage='File name to search for.')]
17+
[ValidateNotNull()]
18+
[ValidateLength(1, [int]::MaxValue)]
19+
[string] $FileName
20+
)
21+
begin {
22+
}
23+
process {
24+
if (-not (Test-Path -LiteralPath $Directory -PathType Container)) {
25+
return $null
26+
}
27+
28+
$searchRoot = (Resolve-Path -LiteralPath $Directory).Path.TrimEnd('\')
29+
$rootFile = Join-Path $searchRoot $FileName
30+
if (Test-Path -LiteralPath $rootFile -PathType Leaf) {
31+
return $rootFile
32+
}
33+
34+
$filePath = Get-ChildItem -LiteralPath $searchRoot -Recurse -Filter $FileName -File -ErrorAction SilentlyContinue |
35+
Select-Object -First 1
36+
if ($null -eq $filePath) {
37+
return $null
38+
}
39+
40+
return (Resolve-Path -LiteralPath $filePath.FullName).Path
41+
}
42+
end {
43+
}
44+
}

0 commit comments

Comments
 (0)