-
Notifications
You must be signed in to change notification settings - Fork 59
Experimental PowerShell discover extension #1071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gijsreyn
wants to merge
51
commits into
PowerShell:main
Choose a base branch
from
Gijsreyn:implement-powershell-discover
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
dd41634
Init
Gijsreyn 9e04ed5
Use concurrentbag
Gijsreyn dae94e4
Use .NET
Gijsreyn 6ec70d7
Use .NET with enumeration option
Gijsreyn d13045d
Init
Gijsreyn 33b7ab2
Use concurrentbag
Gijsreyn f12038e
Use .NET
Gijsreyn 991f7ce
Use .NET with enumeration option
Gijsreyn 1e80819
Remove undiscovered resource
Gijsreyn 6337045
Merge branch 'implement-powershell-discover' of https://github.com/Gi…
Gijsreyn 69ef042
Init
Gijsreyn f7c606a
Use concurrentbag
Gijsreyn f93e691
Use .NET
Gijsreyn 16bf2f5
Use .NET with enumeration option
Gijsreyn 34375b6
Init
Gijsreyn 16b41c4
Use concurrentbag
Gijsreyn 3037ffe
Use .NET
Gijsreyn 1ef6482
Use .NET with enumeration option
Gijsreyn f15195a
Remove undiscovered resource
Gijsreyn 1caa83a
Merge branch 'implement-powershell-discover' of https://github.com/Gi…
Gijsreyn a7263ba
Fix point 2
Gijsreyn 640c664
Add newline
Gijsreyn 1b58423
Merge branch 'main' into implement-powershell-discover
Gijsreyn 6992c43
Merge branch 'main' into implement-powershell-discover
Gijsreyn 5ec950e
Merge branch 'main' into implement-powershell-discover
Gijsreyn c139f8a
Merge branch 'main' into implement-powershell-discover
Gijsreyn 861a017
Add caching
Gijsreyn 48202c7
Debug tests
Gijsreyn e99c662
Format file
Gijsreyn 0dfe4cd
Revert to working situation
Gijsreyn ff104c2
Create function
Gijsreyn 091e017
Catch empty value
Gijsreyn 9b6c993
Re-add tests
Gijsreyn 5c736a7
Update tests
Gijsreyn 12009a9
Remove Write-DscTrace
Gijsreyn 4438bf9
Move test plus docs fix
Gijsreyn 55bb84b
Merge branch 'main' of https://github.com/Gijsreyn/operation-methods …
Gijsreyn d9082b8
Merge conflict
Gijsreyn 7152173
Merge branch 'main' into implement-powershell-discover
Gijsreyn a8e5dbd
Trying to fix new build
Gijsreyn 451db62
Merge branch 'implement-powershell-discover' of https://github.com/Gi…
Gijsreyn 9b2db9f
Merge branches 'implement-powershell-discover' and 'main' of https://…
Gijsreyn 846359b
Update Copilot suggestions
Gijsreyn f7cedf9
Revert Copilot suggestion
Gijsreyn 5a870ef
Fix discovery test
Gijsreyn c558105
Check if extension is copied
Gijsreyn 8947990
Fix copy files
Gijsreyn cc67131
Merge branch 'main' into implement-powershell-discover
Gijsreyn d471887
Add PowerShell extension project data configuration
Gijsreyn d4ee15d
Merge branch 'implement-powershell-discover' of https://github.com/Gi…
Gijsreyn 9f962b9
Swap order
Gijsreyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "Name": "extensions/powershell", | ||
| "Kind": "Extension", | ||
| "CopyFiles": { | ||
| "All": [ | ||
| "powershell.discover.ps1", | ||
| "powershell.dsc.extension.json" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| powershell.discover.ps1 | ||
| powershell.dsc.extension.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| [CmdletBinding()] | ||
| param () | ||
|
|
||
| function Get-CacheFilePath { | ||
| if ($IsWindows) { | ||
| Join-Path $env:LocalAppData "dsc\PowerShellDiscoverCache.json" | ||
| } else { | ||
| Join-Path $env:HOME ".dsc" "PowerShellDiscoverCache.json" | ||
| } | ||
| } | ||
|
|
||
| function Test-CacheValid { | ||
| param([string]$CacheFilePath, [string[]]$PSPaths) | ||
|
|
||
| if (-not (Test-Path $CacheFilePath)) { | ||
| return $false | ||
| } | ||
|
|
||
| try { | ||
| $cache = Get-Content -Raw $CacheFilePath | ConvertFrom-Json | ||
|
|
||
| foreach ($entry in $cache.PathInfo.PSObject.Properties) { | ||
| $path = $entry.Name | ||
| if (-not (Test-Path $path)) { | ||
| return $false | ||
| } | ||
|
|
||
| $currentLastWrite = (Get-Item $path).LastWriteTimeUtc | ||
| $cachedLastWrite = [DateTime]$entry.Value | ||
|
|
||
| if ($currentLastWrite -ne $cachedLastWrite) { | ||
| return $false | ||
| } | ||
| } | ||
Gijsreyn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $cachedPaths = [string[]]$cache.PSModulePaths | ||
| if ($cachedPaths.Count -ne $PSPaths.Count) { | ||
| return $false | ||
| } | ||
|
|
||
| $diff = Compare-Object $cachedPaths $PSPaths | ||
| if ($null -ne $diff) { | ||
| return $false | ||
| } | ||
|
|
||
| foreach ($manifest in $cache.Manifests) { | ||
| if (-not (Test-Path -LiteralPath $manifest.manifestPath)) { | ||
| return $false | ||
| } | ||
| } | ||
|
|
||
| return $true | ||
| } catch { | ||
| return $false | ||
| } | ||
| } | ||
|
|
||
| function Invoke-DscResourceDiscovery { | ||
| [CmdletBinding()] | ||
| param() | ||
|
|
||
| begin { | ||
| $psPaths = $env:PSModulePath -split [System.IO.Path]::PathSeparator | Where-Object { $_ -notmatch 'WindowsPowerShell' } | ||
|
|
||
| $cacheFilePath = Get-CacheFilePath | ||
| $useCache = Test-CacheValid -CacheFilePath $cacheFilePath -PSPaths $psPaths | ||
| } | ||
| process { | ||
| if ($useCache) { | ||
| $cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json | ||
| $manifests = $cache.Manifests | ||
| } else { | ||
| $manifests = $psPaths | ForEach-Object -Parallel { | ||
| $searchPatterns = @('*.dsc.resource.json', '*.dsc.resource.yaml', '*.dsc.resource.yml') | ||
| $enumOptions = [System.IO.EnumerationOptions]@{ IgnoreInaccessible = $true; RecurseSubdirectories = $true } | ||
| foreach ($pattern in $searchPatterns) { | ||
| try { | ||
| [System.IO.Directory]::EnumerateFiles($_, $pattern, $enumOptions) | ForEach-Object { | ||
| @{ manifestPath = $_ } | ||
| } | ||
| } catch { } | ||
| } | ||
| } -ThrottleLimit 10 | ||
|
|
||
| $pathInfo = @{} | ||
| foreach ($path in $psPaths) { | ||
| $item = Get-Item -LiteralPath $path -ErrorAction Ignore | ||
| if ($item) { | ||
| $pathInfo[$path] = $item.LastWriteTimeUtc | ||
| # Track each module subdirectory so that manifest changes inside an | ||
| # already-known module are detected, even if the parent directory timestamp isn't updated. | ||
| Get-ChildItem -LiteralPath $path -Directory -ErrorAction Ignore | ForEach-Object { | ||
| $pathInfo[$_.FullName] = $_.LastWriteTimeUtc | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $cacheObject = @{ | ||
| PSModulePaths = $psPaths | ||
| PathInfo = $pathInfo | ||
| Manifests = $manifests | ||
| } | ||
|
|
||
| $cacheDir = Split-Path $cacheFilePath -Parent | ||
| if (-not (Test-Path $cacheDir)) { | ||
| New-Item -ItemType Directory -Path $cacheDir -Force | Out-Null | ||
| } | ||
|
|
||
| $cacheObject | ConvertTo-Json -Depth 10 | Set-Content -Path $cacheFilePath -Force | ||
| } | ||
| } | ||
| end { | ||
| if ($manifests.Count -gt 0) { | ||
| $manifests | ForEach-Object { $_ | ConvertTo-Json -Compress } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Invoke-DscResourceDiscovery | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| BeforeAll { | ||
| $fakeManifest = @{ | ||
| '$schema' = "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json" | ||
| type = "Test/FakeResource" | ||
| version = "0.1.0" | ||
| get = @{ | ||
| executable = "fakeResource" | ||
| args = @( | ||
| "get", | ||
| @{ | ||
| jsonInputArg = "--input" | ||
| mandatory = $true | ||
| } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| $manifestPath = Join-Path $TestDrive "fake.dsc.resource.json" | ||
| $fakeManifest | ConvertTo-Json -Depth 10 | Set-Content -Path $manifestPath | ||
| $script:OldPSModulePath = $env:PSModulePath | ||
| $env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive | ||
|
|
||
| $script:discoverScript = Join-Path $PSScriptRoot "powershell.discover.ps1" | ||
|
|
||
| $cacheFilePath = if ($IsWindows) { | ||
| Join-Path $env:LocalAppData "dsc\PowerShellDiscoverCache.json" | ||
| } else { | ||
| Join-Path $env:HOME ".dsc" "PowerShellDiscoverCache.json" | ||
| } | ||
| $script:cacheFilePath = $cacheFilePath | ||
|
|
||
| Remove-Item -Force -ErrorAction SilentlyContinue -Path $script:cacheFilePath | ||
| } | ||
Gijsreyn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| AfterAll { | ||
| $env:PSModulePath = $script:OldPSModulePath | ||
| } | ||
|
|
||
| Describe 'Tests for PowerShell resource discovery' { | ||
| It 'Should create cache file on first run' { | ||
| $script:cacheFilePath | Should -Not -Exist | ||
| $null = & $script:discoverScript 2>&1 | ||
| $script:cacheFilePath | Should -Exist | ||
|
|
||
| $cache = Get-Content -Raw $script:cacheFilePath | ConvertFrom-Json | ||
| $cache.PSModulePaths | Should -Not -BeNullOrEmpty | ||
| $cache.PathInfo | Should -Not -BeNullOrEmpty | ||
| $cache.Manifests | Should -Not -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'Should find DSC PowerShell resources' { | ||
| $out = dsc resource list | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.manifest.type | Should -Contain 'Test/FakeResource' | ||
| } | ||
|
|
||
| It 'Should use cache on subsequent runs' { | ||
| $null = & $script:discoverScript 2>&1 | ||
| $script:cacheFilePath | Should -Exist | ||
|
|
||
| $cacheLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc | ||
|
|
||
| Start-Sleep -Milliseconds 100 | ||
|
|
||
| $null = & $script:discoverScript 2>&1 | ||
|
|
||
| $newLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc | ||
| $newLastWriteTime | Should -Be $cacheLastWriteTime | ||
| } | ||
|
|
||
| It 'Should invalidate cache when PSModulePath changes' { | ||
| $null = & $script:discoverScript 2>&1 | ||
| $script:cacheFilePath | Should -Exist | ||
|
|
||
| $cache = Get-Content -Raw $script:cacheFilePath | ConvertFrom-Json | ||
| $originalPaths = $cache.PSModulePaths | ||
| $cache.PSModulePaths = @($originalPaths[0]) # Remove some paths | ||
| $cache | ConvertTo-Json -Depth 10 | Set-Content -Path $script:cacheFilePath -Force | ||
|
|
||
| $cacheLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc | ||
| Start-Sleep -Milliseconds 100 | ||
|
|
||
| $null = & $script:discoverScript 2>&1 | ||
|
|
||
| $newLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc | ||
| $newLastWriteTime | Should -Not -Be $cacheLastWriteTime | ||
| } | ||
|
|
||
| It 'Should invalidate cache when module directory is modified' { | ||
| $null = & $script:discoverScript 2>&1 | ||
| $script:cacheFilePath | Should -Exist | ||
|
|
||
| $cache = Get-Content -Raw $script:cacheFilePath | ConvertFrom-Json | ||
|
|
||
| $firstPath = $cache.PathInfo.PSObject.Properties | Select-Object -First 1 | ||
| if ($firstPath) { | ||
| $oldTimestamp = [DateTime]$firstPath.Value | ||
| $newTimestamp = $oldTimestamp.AddDays(-1) | ||
| $cache.PathInfo.($firstPath.Name) = $newTimestamp | ||
| $cache | ConvertTo-Json -Depth 10 | Set-Content -Path $script:cacheFilePath -Force | ||
|
|
||
| $cacheLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc | ||
| Start-Sleep -Milliseconds 100 | ||
|
|
||
| $null = & $script:discoverScript 2>&1 | ||
|
|
||
| $newLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc | ||
| $newLastWriteTime | Should -Not -Be $cacheLastWriteTime | ||
| } | ||
| } | ||
|
|
||
| It 'Should rebuild cache if cache file is corrupted' { | ||
| "{ invalid json }" | Set-Content -Path $script:cacheFilePath -Force | ||
| $script:cacheFilePath | Should -Exist | ||
|
|
||
| $null = & $script:discoverScript 2>&1 | ||
|
|
||
| $cache = Get-Content -Raw $script:cacheFilePath | ConvertFrom-Json | ||
| $cache.PSModulePaths | Should -Not -BeNullOrEmpty | ||
| $cache.PathInfo | Should -Not -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'Should include test manifest in discovery results' { | ||
| $out = & $script:discoverScript | ConvertFrom-Json | ||
| $out.manifestPath | Should -Contain $manifestPath | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.