Skip to content

Commit d049ce7

Browse files
ghvanderwegrjmholt
authored andcommitted
Refactor build logic to avoid saving modules in subfolders (#979)
1 parent fcc89ad commit d049ce7

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

PowerShellEditorServices.build.ps1

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -313,30 +313,53 @@ task RestorePsesModules -After Build {
313313
# Save each module in the modules.json file
314314
foreach ($moduleName in $moduleInfos.Keys)
315315
{
316-
if (Test-Path -Path (Join-Path -Path $submodulePath -ChildPath $moduleName))
316+
$moduleInstallPath = (Join-Path -Path $subModulePath -ChildPath $moduleName)
317+
318+
if (-not (Test-Path -Path $moduleInstallPath))
317319
{
318-
Write-Host "`tModule '${moduleName}' already detected. Skipping"
319-
continue
320-
}
320+
Write-Host "`tInstalling module: ${moduleName}"
321321

322-
$moduleInstallDetails = $moduleInfos[$moduleName]
322+
$moduleInstallDetails = $moduleInfos[$moduleName]
323323

324-
$splatParameters = @{
325-
Name = $moduleName
326-
MinimumVersion = $moduleInstallDetails.MinimumVersion
327-
MaximumVersion = $moduleInstallDetails.MaximumVersion
328-
Repository = if ($moduleInstallDetails.Repository) { $moduleInstallDetails.Repository } else { $DefaultModuleRepository }
329-
Path = $submodulePath
330-
}
324+
$splatParameters = @{
325+
Name = $moduleName
326+
MinimumVersion = $moduleInstallDetails.MinimumVersion
327+
MaximumVersion = $moduleInstallDetails.MaximumVersion
328+
Repository = if ($moduleInstallDetails.Repository) { $moduleInstallDetails.Repository } else { $DefaultModuleRepository }
329+
Path = $submodulePath
330+
}
331331

332-
if ($script:SaveModuleSupportsAllowPrerelease)
332+
if ($script:SaveModuleSupportsAllowPrerelease)
333+
{
334+
$splatParameters += @{ AllowPrerelease = $moduleInstallDetails.AllowPrerelease }
335+
}
336+
337+
Save-Module @splatParameters
338+
}
339+
else
333340
{
334-
$splatParameters += @{ AllowPrerelease = $moduleInstallDetails.AllowPrerelease }
341+
Write-Host "`tModule '${moduleName}' already detected."
335342
}
336343

337-
Write-Host "`tInstalling module: ${moduleName}"
344+
# Version number subfolders aren't supported in PowerShell < 5.0
345+
# If the module is located in a subfolder, move the module files up one level and remove the (empty) subfolder.
346+
$moduleFolderChildItems = Get-ChildItem -Path $moduleInstallPath -Force
338347

339-
Save-Module @splatParameters
348+
if ($moduleFolderChildItems.Count -eq 1 -and $moduleFolderChildItems[0].PSIsContainer)
349+
{
350+
Write-Host "`tMoving module '${moduleName}' out of subfolder."
351+
352+
$moduleSubfolder = $moduleFolderChildItems[0].FullName
353+
354+
Get-ChildItem -Path $moduleSubfolder -Recurse -Force | Move-Item -Destination $moduleInstallPath -Force
355+
356+
if ($null -ne (Get-ChildItem -Path $moduleSubfolder -Recurse -Force))
357+
{
358+
throw "Cannot remove folder $moduleSubfolder because it is not empty!"
359+
}
360+
361+
Remove-Item -Path $moduleSubfolder
362+
}
340363
}
341364
Write-Host "`n"
342365
}

0 commit comments

Comments
 (0)