From 28e7b8029d3b0b55517aabe7ce3adc0c87309df0 Mon Sep 17 00:00:00 2001 From: Gerbrand van der Weg Date: Thu, 20 Jun 2019 21:22:29 +0200 Subject: [PATCH 1/3] Refactor build logic to avoid saving modules in subfolders --- PowerShellEditorServices.build.ps1 | 56 ++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/PowerShellEditorServices.build.ps1 b/PowerShellEditorServices.build.ps1 index 676d63f13..83ab7901b 100644 --- a/PowerShellEditorServices.build.ps1 +++ b/PowerShellEditorServices.build.ps1 @@ -313,30 +313,50 @@ task RestorePsesModules -After Build { # Save each module in the modules.json file foreach ($moduleName in $moduleInfos.Keys) { - if (Test-Path -Path (Join-Path -Path $submodulePath -ChildPath $moduleName)) - { - Write-Host "`tModule '${moduleName}' already detected. Skipping" - continue + $moduleInstallPath = (Join-Path -Path $subModulePath -ChildPath $moduleName) + + try { + $moduleFolderChildItems = Get-ChildItem -Path $moduleInstallPath -Force } + catch { + Write-Host "`tInstalling module: ${moduleName}" - $moduleInstallDetails = $moduleInfos[$moduleName] + $moduleInstallDetails = $moduleInfos[$moduleName] - $splatParameters = @{ - Name = $moduleName - MinimumVersion = $moduleInstallDetails.MinimumVersion - MaximumVersion = $moduleInstallDetails.MaximumVersion - Repository = if ($moduleInstallDetails.Repository) { $moduleInstallDetails.Repository } else { $DefaultModuleRepository } - Path = $submodulePath - } + $splatParameters = @{ + Name = $moduleName + MinimumVersion = $moduleInstallDetails.MinimumVersion + MaximumVersion = $moduleInstallDetails.MaximumVersion + Repository = if ($moduleInstallDetails.Repository) { $moduleInstallDetails.Repository } else { $DefaultModuleRepository } + Path = $submodulePath + } - if ($script:SaveModuleSupportsAllowPrerelease) - { - $splatParameters += @{ AllowPrerelease = $moduleInstallDetails.AllowPrerelease } - } + if ($script:SaveModuleSupportsAllowPrerelease) + { + $splatParameters += @{ AllowPrerelease = $moduleInstallDetails.AllowPrerelease } + } + + Save-Module @splatParameters - Write-Host "`tInstalling module: ${moduleName}" + $moduleFolderChildItems = Get-ChildItem -Path $moduleInstallPath -Force + } - Save-Module @splatParameters + # Version number subfolders aren't supported in PowerShell < 5.0 + # If the module is located in a subfolder, move the module files up one level and remove the (empty) subfolder. + if ($moduleFolderChildItems.Count -eq 1 -and $moduleFolderChildItems[0].Attributes.value__ -eq 16) { + $moduleSubfolder = $moduleFolderChildItems[0].FullName + + Get-ChildItem -Path $moduleSubfolder -Recurse -Force | Move-Item -Destination $moduleInstallPath -Force + + if ($null -eq (Get-ChildItem -Path $moduleSubfolder -Recurse -Force)) + { + Remove-Item -Path $moduleSubfolder + } + else + { + throw "Cannot remove folder $moduleSubfolder because it is not empty!" + } + } } Write-Host "`n" } From e65c18f17e8d2ccee050da56164bb47178018856 Mon Sep 17 00:00:00 2001 From: Gerbrand van der Weg Date: Fri, 21 Jun 2019 12:19:59 +0200 Subject: [PATCH 2/3] Address review comments --- PowerShellEditorServices.build.ps1 | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/PowerShellEditorServices.build.ps1 b/PowerShellEditorServices.build.ps1 index 83ab7901b..dd063bbd7 100644 --- a/PowerShellEditorServices.build.ps1 +++ b/PowerShellEditorServices.build.ps1 @@ -315,10 +315,8 @@ task RestorePsesModules -After Build { { $moduleInstallPath = (Join-Path -Path $subModulePath -ChildPath $moduleName) - try { - $moduleFolderChildItems = Get-ChildItem -Path $moduleInstallPath -Force - } - catch { + if (-not (Test-Path -Path $moduleInstallPath)) + { Write-Host "`tInstalling module: ${moduleName}" $moduleInstallDetails = $moduleInfos[$moduleName] @@ -337,13 +335,20 @@ task RestorePsesModules -After Build { } Save-Module @splatParameters - - $moduleFolderChildItems = Get-ChildItem -Path $moduleInstallPath -Force + } + else + { + Write-Host "`tModule '${moduleName}' already detected." } # Version number subfolders aren't supported in PowerShell < 5.0 # If the module is located in a subfolder, move the module files up one level and remove the (empty) subfolder. - if ($moduleFolderChildItems.Count -eq 1 -and $moduleFolderChildItems[0].Attributes.value__ -eq 16) { + $moduleFolderChildItems = Get-ChildItem -Path $moduleInstallPath -Force + + if ($moduleFolderChildItems.Count -eq 1 -and $moduleFolderChildItems[0].PSIsContainer) + { + Write-Host "`tMoving module '${moduleName}' out of subfolder." + $moduleSubfolder = $moduleFolderChildItems[0].FullName Get-ChildItem -Path $moduleSubfolder -Recurse -Force | Move-Item -Destination $moduleInstallPath -Force From 3a8a0b30486de9459ce9710355c1270333156b2d Mon Sep 17 00:00:00 2001 From: Gerbrand van der Weg Date: Wed, 26 Jun 2019 10:11:18 +0200 Subject: [PATCH 3/3] Swap logic for checking folder is empty --- PowerShellEditorServices.build.ps1 | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/PowerShellEditorServices.build.ps1 b/PowerShellEditorServices.build.ps1 index dd063bbd7..458e88ce1 100644 --- a/PowerShellEditorServices.build.ps1 +++ b/PowerShellEditorServices.build.ps1 @@ -353,14 +353,12 @@ task RestorePsesModules -After Build { Get-ChildItem -Path $moduleSubfolder -Recurse -Force | Move-Item -Destination $moduleInstallPath -Force - if ($null -eq (Get-ChildItem -Path $moduleSubfolder -Recurse -Force)) - { - Remove-Item -Path $moduleSubfolder - } - else + if ($null -ne (Get-ChildItem -Path $moduleSubfolder -Recurse -Force)) { throw "Cannot remove folder $moduleSubfolder because it is not empty!" } + + Remove-Item -Path $moduleSubfolder } } Write-Host "`n"