Files
nt8-sdk/tools/preflight/strategy-source-verification.ps1

115 lines
6.7 KiB
PowerShell

function Get-ExpectedStrategySourceFiles {
param([string]$RepositoryRoot)
return @{
'NT8StrategyBase.cs' = 'src\NT8.Adapters\Strategies\NT8StrategyBase.cs'
'SimpleORBNT8.cs' = 'src\NT8.Adapters\Strategies\SimpleORBNT8.cs'
'BaseNT8StrategyWrapper.cs' = 'src\NT8.Adapters\Wrappers\BaseNT8StrategyWrapper.cs'
'SimpleORBNT8Wrapper.cs' = 'src\NT8.Adapters\Wrappers\SimpleORBNT8Wrapper.cs'
}
}
function Get-DeclaredSourceTypeNames {
param([string]$FilePath)
$content = [System.IO.File]::ReadAllText($FilePath)
$namespaceMatch = [regex]::Match($content, '(?m)^\s*namespace\s+([\w.]+)')
if (-not $namespaceMatch.Success) { return @() }
$namespaceName = $namespaceMatch.Groups[1].Value
$typeMatches = [regex]::Matches($content, '(?m)^\s*(?:(?:public|internal|protected|private|abstract|sealed|static|partial|new)\s+)*(?:class|struct|interface|enum)\s+(\w+)')
return @($typeMatches | ForEach-Object { $namespaceName + '.' + $_.Groups[1].Value } | Select-Object -Unique)
}
function Find-DeployedAssemblyTypeCollisions {
param(
[string]$FilePath,
[string]$AssemblyDirectory
)
$declaredTypes = @(Get-DeclaredSourceTypeNames -FilePath $FilePath)
if ($declaredTypes.Count -eq 0 -or -not (Test-Path -LiteralPath $AssemblyDirectory -PathType Container)) { return @() }
$collisions = @()
$assemblies = @(Get-ChildItem -LiteralPath $AssemblyDirectory -File -Filter '*.dll' | Where-Object { $_.Name -in @('NT8.Strategies.dll', 'NinjaTrader.Custom.dll') })
foreach ($assemblyFile in $assemblies) {
try {
$assembly = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($assemblyFile.FullName)
foreach ($typeName in $declaredTypes) {
if ($assembly.GetType($typeName, $false, $false) -ne $null) {
$collisions += ('{0} in {1}' -f $typeName, $assemblyFile.Name)
}
}
}
catch {
# An unavailable dependency must not hide the source-file discrepancy itself.
}
}
return @($collisions | Select-Object -Unique)
}
function Compare-DeployedStrategySources {
param(
[string]$RepositoryRoot,
[string]$DeployedDirectory,
[hashtable]$ExpectedFiles = (Get-ExpectedStrategySourceFiles -RepositoryRoot $RepositoryRoot),
[string[]]$AllowedForeignNames = @('MinimalTestStrategy.cs', 'ORBFibPullbackV1.cs')
)
$issues = New-Object System.Collections.Generic.List[object]
$expectedByDestination = @{}
foreach ($destination in $ExpectedFiles.Keys) {
$expectedByDestination[$destination] = $ExpectedFiles[$destination]
$repoPath = Join-Path $RepositoryRoot $ExpectedFiles[$destination]
$deployedPath = Join-Path $DeployedDirectory $destination
if (-not (Test-Path -LiteralPath $repoPath -PathType Leaf)) {
$issues.Add([pscustomobject]@{ Kind = 'missing-repository'; File = $destination; RepoPath = $ExpectedFiles[$destination]; RepoTimestamp = 'missing'; DeployedTimestamp = if (Test-Path -LiteralPath $deployedPath) { (Get-Item -LiteralPath $deployedPath).LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss') } else { 'missing' }; SameNamedSource = ''; TypeCollisions = @() })
continue
}
$repoFile = Get-Item -LiteralPath $repoPath
if (-not (Test-Path -LiteralPath $deployedPath -PathType Leaf)) {
$issues.Add([pscustomobject]@{ Kind = 'missing-deployed'; File = $destination; RepoPath = $ExpectedFiles[$destination]; RepoTimestamp = $repoFile.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'); DeployedTimestamp = 'missing'; SameNamedSource = ''; TypeCollisions = @() })
continue
}
$deployedFile = Get-Item -LiteralPath $deployedPath
$repoHash = (Get-FileHash -LiteralPath $repoPath -Algorithm SHA256).Hash
$deployedHash = (Get-FileHash -LiteralPath $deployedPath -Algorithm SHA256).Hash
if ($repoHash -ne $deployedHash) {
$issues.Add([pscustomobject]@{ Kind = 'hash-mismatch'; File = $destination; RepoPath = $ExpectedFiles[$destination]; RepoTimestamp = $repoFile.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'); DeployedTimestamp = $deployedFile.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'); SameNamedSource = ''; TypeCollisions = @() })
}
}
if (Test-Path -LiteralPath $DeployedDirectory -PathType Container) {
$deployedSources = @(Get-ChildItem -LiteralPath $DeployedDirectory -Recurse -File -Filter '*.cs')
foreach ($deployedFile in $deployedSources) {
$relativePath = $deployedFile.FullName.Substring($DeployedDirectory.TrimEnd('\').Length).TrimStart('\')
if ($expectedByDestination.ContainsKey($relativePath)) { continue }
$isRootFile = $relativePath.IndexOf('\') -lt 0
if ($isRootFile -and ($deployedFile.Name.StartsWith('@', [System.StringComparison]::Ordinal) -or $AllowedForeignNames -contains $deployedFile.Name)) { continue }
$sameNamed = @(Get-ChildItem -LiteralPath (Join-Path $RepositoryRoot 'src') -Recurse -File -Filter $deployedFile.Name -ErrorAction SilentlyContinue)
$sameNamedPath = if ($sameNamed.Count -gt 0) { $sameNamed[0].FullName.Substring($RepositoryRoot.TrimEnd('\').Length).TrimStart('\') } else { 'none under src' }
$repoTimestamp = if ($sameNamed.Count -gt 0) { $sameNamed[0].LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss') } else { 'not found' }
$collisions = @(Find-DeployedAssemblyTypeCollisions -FilePath $deployedFile.FullName -AssemblyDirectory (Split-Path -Parent $DeployedDirectory))
$issues.Add([pscustomobject]@{ Kind = 'unexpected'; File = $relativePath; RepoPath = ''; RepoTimestamp = $repoTimestamp; DeployedTimestamp = $deployedFile.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'); SameNamedSource = $sameNamedPath; TypeCollisions = $collisions })
}
}
else {
$issues.Add([pscustomobject]@{ Kind = 'missing-deployed-directory'; File = $DeployedDirectory; RepoPath = ''; RepoTimestamp = 'n/a'; DeployedTimestamp = 'missing'; SameNamedSource = ''; TypeCollisions = @() })
}
return @($issues.ToArray())
}
function Format-StrategySourceIssues {
param([object[]]$Issues)
$lines = @()
foreach ($issue in $Issues) {
$line = ('{0}: {1}; repo timestamp={2}; deployed timestamp={3}' -f $issue.Kind, $issue.File, $issue.RepoTimestamp, $issue.DeployedTimestamp)
if ($issue.SameNamedSource) { $line += '; same-named source=' + $issue.SameNamedSource }
if ($issue.TypeCollisions.Count -gt 0) { $line += '; GAP-065 type collision: ' + [string]::Join(', ', $issue.TypeCollisions) }
$lines += $line
}
return [string]::Join([Environment]::NewLine, $lines)
}