Skip to content

Instantly share code, notes, and snippets.

@jlattimer
Created April 14, 2019 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlattimer/a72c60853d71ba5121142d33db197dce to your computer and use it in GitHub Desktop.
Save jlattimer/a72c60853d71ba5121142d33db197dce to your computer and use it in GitHub Desktop.
Ensures an EasyRepro project's Selenium references match what is supported #blog
Write-Host "Checking EasyRepro package references"
$ProjectPath = ((Get-Item $PSScriptRoot).Parent).FullName
$Parameters = @{
ProjectPath = $ProjectPath
References = @(
[PSCustomObject]@{ Assembly = "Selenium.Support"; Version = '3.11.2' }, # Selenium
[PSCustomObject]@{ Assembly = "Selenium.WebDriver"; Version = '3.11.2' }, # Selenium
[PSCustomObject]@{ Assembly = "Selenium.Chrome.WebDriver"; Version = '2.38' }, # Chrome
[PSCustomObject]@{ Assembly = "Selenium.WebDriver.GeckoDriver.Win64"; Version = '0.16.1' }, # Firefox
[PSCustomObject]@{ Assembly = "Selenium.WebDriver.MicrosoftWebDriver"; Version = '10.0.15063' }, # Edge
[PSCustomObject]@{ Assembly = "WebDriver.IEDriverServer.win64"; Version = '3.4.0' } # IE 11
)
}
Write-Host "Executing with parameters:"
Write-Host ($Parameters | Out-String)
# Fix packages.config
Write-Host "Reading packages.config file"
$PackagesConfigPath = Join-Path -Path "$($Parameters.ProjectPath)" -ChildPath 'packages.config'
Write-Host "PackagesConfigPath: $PackagesConfigPath"
if ((Test-Path $PackagesConfigPath) -eq $false) {
throw "Could not find packages.config file: $PackagesConfigPath"
}
$PackagesFileContent = [xml](Get-Content -Path $PackagesConfigPath)
$PackagesNode = $PackagesFileContent.SelectSingleNode("packages");
if ($PackagesNode -eq $null) {
throw "Could not find packages node"
}
# Update matching package versions
$Updated = $false
foreach ($package in $PackagesNode.ChildNodes | Where-Object { $_ -is [System.Xml.XmlElement] }) {
foreach ($p in $Parameters.References | Where-Object { $_.Assembly -eq $package.id }) {
if ($package.version -ne $p.Version) {
$package.version = $p.Version
Write-Host "Updated: $($package.id)"
$Updated = $true
}
else {
Write-Host "Skipped: $($package.id)"
}
}
}
if ($Updated) {
Write-Host "Updating packages.config file"
$PackagesFileContent.Save($PackagesConfigPath)
Write-Host "Updated packages.config file"
# Fix project file
Write-Host "Reading project file"
$ProjectFilePath = Get-ChildItem -Path "$($Parameters.ProjectPath)" -Filter *.csproj | Select-Object -First 1
$ProjectFilePath = Join-Path -Path "$($Parameters.ProjectPath)" -ChildPath $ProjectFilePath
Write-Host "ProjectFilePath: $ProjectFilePath"
if ((Test-Path $ProjectFilePath) -eq $false) {
throw "Could not find .csproj project file: $ProjectFilePath"
}
$ProjectFileContent = [IO.File]::ReadAllText($ProjectFilePath)
# Update matching package versions
foreach ($p in $Parameters.References) {
$ProjectFileContent = $ProjectFileContent -replace "packages\\$($p.Assembly).[0-9].*?\\build",
"packages\$($p.Assembly).$($p.Version)\build"
}
Write-Host "Updating project file"
[System.IO.File]::WriteAllText($ProjectFilePath, $ProjectFileContent)
Write-Host "Updated project file"
}
else {
Write-Host "No updates required"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment