Skip to content

Commit 2125789

Browse files
fix dat
1 parent d399da6 commit 2125789

1 file changed

Lines changed: 77 additions & 12 deletions

File tree

.github/scripts/install-dbatools-library.ps1

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function Install-FromGitHubRelease {
155155
}
156156
}
157157

158-
$finalInstallPath = Join-Path $installBasePath $ModuleName $RequiredVersion
158+
$finalInstallPath = Join-Path -Path (Join-Path -Path $installBasePath -ChildPath $ModuleName) -ChildPath $RequiredVersion
159159

160160
# Create installation directory
161161
Write-Log "Installing to: $finalInstallPath"
@@ -171,6 +171,16 @@ function Install-FromGitHubRelease {
171171
# Copy module files
172172
Copy-Item -Path $moduleDir.FullName -Destination $finalInstallPath -Recurse -Force
173173

174+
# Add the modules directory to PSModulePath if not already present
175+
$modulesBasePath = Split-Path $finalInstallPath -Parent
176+
$modulesRootPath = Split-Path $modulesBasePath -Parent
177+
$currentPSModulePath = $env:PSModulePath -split [System.IO.Path]::PathSeparator
178+
179+
if ($modulesRootPath -notin $currentPSModulePath) {
180+
$env:PSModulePath = $modulesRootPath + [System.IO.Path]::PathSeparator + $env:PSModulePath
181+
Write-Log "Added '$modulesRootPath' to PSModulePath for this session" -Level 'Success'
182+
}
183+
174184
# Cleanup
175185
Remove-Item $downloadPath -Force -ErrorAction SilentlyContinue
176186
Remove-Item $extractPath -Recurse -Force -ErrorAction SilentlyContinue
@@ -216,26 +226,81 @@ try {
216226
Write-Log "Force installation requested, will reinstall if already present"
217227
}
218228

219-
# Attempt installation from PowerShell Gallery first
220-
$gallerySuccess = Install-FromPowerShellGallery -ModuleName 'dbatools.library' -RequiredVersion $requiredVersion -InstallScope $Scope -ForceInstall $Force
229+
# Check if this is a preview version - skip Gallery for these
230+
$isPreviewVersion = $requiredVersion -match "preview|main-\d+" -or $requiredVersion -match "\d+\.\d+\.\d+-.*"
221231

222-
if (-not $gallerySuccess) {
223-
Write-Log "PowerShell Gallery installation failed, attempting GitHub releases..."
232+
if ($isPreviewVersion) {
233+
Write-Log "Detected preview version '$requiredVersion'. Skipping PowerShell Gallery and attempting GitHub releases directly." -Level 'Warning'
224234
$githubSuccess = Install-FromGitHubRelease -ModuleName 'dbatools.library' -RequiredVersion $requiredVersion
225235

226236
if (-not $githubSuccess) {
227-
throw "Failed to install dbatools.library version $requiredVersion from both PowerShell Gallery and GitHub releases"
237+
throw "Failed to install preview version $requiredVersion from GitHub releases"
238+
}
239+
} else {
240+
# Attempt installation from PowerShell Gallery first for stable versions
241+
$gallerySuccess = Install-FromPowerShellGallery -ModuleName 'dbatools.library' -RequiredVersion $requiredVersion -InstallScope $Scope -ForceInstall $Force
242+
243+
if (-not $gallerySuccess) {
244+
Write-Log "PowerShell Gallery installation failed, attempting GitHub releases..."
245+
$githubSuccess = Install-FromGitHubRelease -ModuleName 'dbatools.library' -RequiredVersion $requiredVersion
246+
247+
if (-not $githubSuccess) {
248+
throw "Failed to install dbatools.library version $requiredVersion from both PowerShell Gallery and GitHub releases"
249+
}
250+
}
251+
}
252+
253+
# Verify installation and provide debugging information
254+
Write-Log "Verifying installation..."
255+
$installedModules = Get-Module -ListAvailable -Name 'dbatools.library'
256+
257+
if ($installedModules) {
258+
Write-Log "Found dbatools.library installations:" -Level 'Success'
259+
foreach ($module in $installedModules) {
260+
Write-Log " Version: $($module.Version) | Path: $($module.ModuleBase)"
228261
}
262+
263+
# Check if the specific version we wanted is installed
264+
$targetModule = $installedModules | Where-Object { $_.Version -eq $requiredVersion }
265+
if ($targetModule) {
266+
Write-Log "Target version $requiredVersion found and available!" -Level 'Success'
267+
} else {
268+
Write-Log "Target version $requiredVersion not found, but other versions are available. This may be acceptable for preview versions." -Level 'Warning'
269+
}
270+
} else {
271+
throw "No dbatools.library module found after installation"
272+
}
273+
274+
# Test import to ensure the module works
275+
Write-Log "Testing module import..."
276+
try {
277+
Import-Module dbatools.library -Force -ErrorAction Stop
278+
Write-Log "Module import test successful" -Level 'Success'
279+
Remove-Module dbatools.library -Force -ErrorAction SilentlyContinue
280+
} catch {
281+
Write-Log "Module import test failed: $($_.Exception.Message)" -Level 'Warning'
282+
Write-Log "This may be expected if there are version compatibility issues with preview versions"
283+
}
284+
285+
# Output PSModulePath for debugging
286+
Write-Log "Current PSModulePath:"
287+
$env:PSModulePath -split [System.IO.Path]::PathSeparator | ForEach-Object {
288+
Write-Log " $_"
229289
}
230290

231-
<# Verify installation
232-
if (Test-ModuleInstalled -ModuleName 'dbatools.library' -RequiredVersion $requiredVersion) {
233-
Write-Log "Installation verification successful!" -Level 'Success'
234-
Write-Log "dbatools.library version $requiredVersion is now available"
291+
# For CI/CD scenarios, prepare dbatools manifest for version compatibility
292+
$prepareScriptPath = Join-Path $PSScriptRoot "prepare-dbatools-for-ci.ps1"
293+
if (Test-Path $prepareScriptPath) {
294+
Write-Log "Preparing dbatools manifest for CI/CD compatibility..."
295+
& $prepareScriptPath
296+
if ($LASTEXITCODE -eq 0) {
297+
Write-Log "dbatools manifest preparation completed successfully" -Level 'Success'
298+
} else {
299+
Write-Log "dbatools manifest preparation failed, but continuing..." -Level 'Warning'
300+
}
235301
} else {
236-
throw "Installation completed but module verification failed"
302+
Write-Log "CI preparation script not found at: $prepareScriptPath" -Level 'Warning'
237303
}
238-
#>
239304

240305
} catch {
241306
Write-Log "Installation failed: $($_.Exception.Message)" -Level 'Error'

0 commit comments

Comments
 (0)