Skip to content

Commit 0f7181b

Browse files
committed
Fix: Handle the zip file extraction - in a way that allows to handle
No initial folder Folder - but empty Folder - with files, being wiped - prior moving forward If the folder exists - user will be asked to use force, regardless of the folder containing files or not
1 parent 9c29374 commit 0f7181b

File tree

2 files changed

+111
-87
lines changed

2 files changed

+111
-87
lines changed

d365fo.tools/functions/invoke-d365sdpinstall.ps1

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,23 @@ function Invoke-D365SDPInstall {
225225
Unblock-File -Path $Path
226226

227227
$extractedPath = $path.Remove($path.Length - 4)
228-
if (!(Test-Path $extractedPath)) {
229-
Expand-Archive -Path $Path -DestinationPath $extractedPath
230-
231-
#lets work with the extracted directory from now on
232-
$Path = $extractedPath
228+
229+
if (-not $Force) {
230+
if (-not (Test-PathExists -Path $extractedPath -Type Container -ShouldNotExist)) {
231+
Write-PSFMessage -Level Host -Message "The directory at the <c='em'>$extractedPath</c> location already exists. If you want to override it - set the <c='em'>Force</c> parameter to clear the folder and extract the content into it."
232+
Stop-PSFFunction -Message "Stopping because output path was already present."
233+
return
234+
}
233235
}
236+
237+
Get-ChildItem -Path $extractedPath -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -Confirm:$false
238+
239+
# To allow the file system to flush the files
240+
# Allows the human to see the folder being wiped
241+
Start-Sleep -Seconds 2
242+
243+
Expand-Archive -Path $Path -DestinationPath $extractedPath -Force
244+
$Path = $extractedPath
234245
}
235246

236247
# Input is a relative path which needs to be converted to an absolute path.

d365fo.tools/functions/invoke-d365sdpinstallude.ps1

Lines changed: 95 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -30,95 +30,108 @@
3030
3131
#>
3232
function Invoke-D365SDPInstallUDE {
33-
param (
34-
[Parameter(Mandatory = $True, Position = 1 )]
35-
[Alias('Hotfix')]
36-
[Alias('File')]
37-
[string] $Path,
38-
39-
[Parameter(Mandatory = $true, Position = 2 )]
40-
[string] $MetaDataDir,
41-
42-
[Alias('LogDir')]
43-
[string] $LogPath = $(Join-Path -Path $Script:DefaultTempPath -ChildPath "Logs\SdpInstall")
44-
)
33+
param (
34+
[Parameter(Mandatory = $True, Position = 1 )]
35+
[Alias('Hotfix')]
36+
[Alias('File')]
37+
[string] $Path,
38+
39+
[Parameter(Mandatory = $true, Position = 2 )]
40+
[string] $MetaDataDir,
41+
42+
[Alias('LogDir')]
43+
[string] $LogPath = $(Join-Path -Path $Script:DefaultTempPath -ChildPath "Logs\SdpInstall"),
44+
45+
[switch] $Force
46+
)
4547

46-
if ((Get-Process -Name "devenv" -ErrorAction SilentlyContinue).Count -gt 0) {
47-
Write-PSFMessage -Level Host -Message "It seems that you have a <c='em'>Visual Studio</c> running. Please ensure <c='em'>exit</c> Visual Studio and run the cmdlet again."
48-
Stop-PSFFunction -Message "Stopping because of running Visual Studio."
49-
return
50-
}
48+
if ((Get-Process -Name "devenv" -ErrorAction SilentlyContinue).Count -gt 0) {
49+
Write-PSFMessage -Level Host -Message "It seems that you have a <c='em'>Visual Studio</c> running. Please ensure <c='em'>exit</c> Visual Studio and run the cmdlet again."
50+
Stop-PSFFunction -Message "Stopping because of running Visual Studio."
51+
return
52+
}
5153

52-
Invoke-TimeSignal -Start
54+
Invoke-TimeSignal -Start
5355

5456

55-
#Test if input is a zipFile that needs to be extracted first
56-
if ($Path.EndsWith(".zip")) {
57-
Unblock-File -Path $Path
58-
59-
$extractedPath = $path.Remove($path.Length - 4)
60-
if (!(Test-Path $extractedPath)) {
61-
Expand-Archive -Path $Path -DestinationPath $extractedPath
57+
#Test if input is a zipFile that needs to be extracted first
58+
if ($Path.EndsWith(".zip")) {
59+
Unblock-File -Path $Path
60+
61+
$extractedPath = $path.Remove($path.Length - 4)
62+
63+
if (-not $Force) {
64+
if (-not (Test-PathExists -Path $extractedPath -Type Container -ShouldNotExist)) {
65+
Write-PSFMessage -Level Host -Message "The directory at the <c='em'>$extractedPath</c> location already exists. If you want to override it - set the <c='em'>Force</c> parameter to clear the folder and extract the content into it."
66+
Stop-PSFFunction -Message "Stopping because output path was already present."
67+
return
68+
}
69+
}
70+
71+
Get-ChildItem -Path $extractedPath -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -Confirm:$false
6272

63-
#lets work with the extracted directory from now on
73+
# To allow the file system to flush the files
74+
# Allows the human to see the folder being wiped
75+
Start-Sleep -Seconds 2
76+
77+
Expand-Archive -Path $Path -DestinationPath $extractedPath -Force
6478
$Path = $extractedPath
6579
}
66-
}
67-
68-
# Input is a relative path which needs to be converted to an absolute path.
69-
# see https://powershellmagazine.com/2013/01/16/pstip-check-if-the-path-is-relative-or-absolute/
70-
if (-not ([System.IO.Path]::IsPathRooted($Path) -or (Split-Path -Path $Path -IsAbsolute))) {
71-
$currentPath = Get-Location
72-
# https://stackoverflow.com/a/13847304/2720554
73-
$absolutePath = Join-Path -Path $currentPath -ChildPath $Path
74-
$absolutePath = [System.IO.Path]::GetFullPath($absolutePath)
75-
Write-PSFMessage -Level Verbose "Updating path to '$absolutePath' as relative paths are not supported"
76-
$Path = $absolutePath
77-
}
80+
81+
# Input is a relative path which needs to be converted to an absolute path.
82+
# see https://powershellmagazine.com/2013/01/16/pstip-check-if-the-path-is-relative-or-absolute/
83+
if (-not ([System.IO.Path]::IsPathRooted($Path) -or (Split-Path -Path $Path -IsAbsolute))) {
84+
$currentPath = Get-Location
85+
# https://stackoverflow.com/a/13847304/2720554
86+
$absolutePath = Join-Path -Path $currentPath -ChildPath $Path
87+
$absolutePath = [System.IO.Path]::GetFullPath($absolutePath)
88+
Write-PSFMessage -Level Verbose "Updating path to '$absolutePath' as relative paths are not supported"
89+
$Path = $absolutePath
90+
}
7891

79-
Get-ChildItem -Path $Path -Recurse | Unblock-File
80-
$packageDetails = Get-D365SDPDetails -Path $Path
81-
82-
$packagesFolder = "$Path\AOSService\Packages"
83-
$filesFolder = Get-ChildItem -Path $packagesFolder -Directory -Filter "files"
84-
if ($filesFolder.Count -eq 0) {
85-
Write-PSFMessage -Level Host -Message "No /AOSService/Packages/files folder found in the package. Please ensure that the package is extracted correctly."
86-
Stop-PSFFunction -Message "Stopping because of missing files folder."
87-
return
88-
}
89-
90-
$zipFiles = Get-ChildItem -Path $filesFolder.FullName -File -Filter "*.zip"
91-
if ($zipFiles.Count -eq 0) {
92-
Write-PSFMessage -Level Host -Message "No module zip files found in the package. Please ensure that the package is extracted correctly."
93-
Stop-PSFFunction -Message "Stopping because of missing zip files."
94-
return
95-
}
96-
97-
$numberOfInstalledModules = 0
98-
$packageDetails.Modules | ForEach-Object {
99-
$moduleZip = $zipFiles | Where-Object Name -eq "dynamicsax-$($_.Name).$($_.Version).zip"
100-
if (-not $moduleZip) {
101-
Write-PSFMessage -Level Host -Message "No module zip file found for module $($_.Name). Please ensure that the package is extracted correctly."
102-
Stop-PSFFunction -Message "Stopping because of missing module zip file."
103-
return
104-
}
105-
106-
# Delete existing module folder if it exists
107-
$moduleFolderPath = Join-Path -Path $MetaDataDir -ChildPath $($_.Name)
108-
if (Test-Path -Path $moduleFolderPath) {
109-
Remove-Item -Path $moduleFolderPath -Recurse -Force
110-
Write-PSFMessage -Level Verbose -Message "Deleted existing module folder $moduleFolderPath"
111-
}
112-
113-
# Unzip to $MetaDataDir
114-
$moduleZipPath = Join-Path -Path $MetaDataDir -ChildPath $($_.Name)
115-
Expand-Archive -Path $moduleZip.FullName -DestinationPath $moduleZipPath
116-
Write-PSFMessage -Level Verbose -Message "Unzipped module $($_.Name) to $moduleZipPath"
117-
$numberOfInstalledModules++
118-
}
119-
120-
Write-PSFMessage -Level Host -Message "Installed $numberOfInstalledModules module(s) into $MetaDataDir"
121-
122-
Invoke-TimeSignal -End
92+
Get-ChildItem -Path $Path -Recurse | Unblock-File
93+
$packageDetails = Get-D365SDPDetails -Path $Path
94+
95+
$packagesFolder = "$Path\AOSService\Packages"
96+
$filesFolder = Get-ChildItem -Path $packagesFolder -Directory -Filter "files"
97+
if ($filesFolder.Count -eq 0) {
98+
Write-PSFMessage -Level Host -Message "No /AOSService/Packages/files folder found in the package. Please ensure that the package is extracted correctly."
99+
Stop-PSFFunction -Message "Stopping because of missing files folder."
100+
return
101+
}
102+
103+
$zipFiles = Get-ChildItem -Path $filesFolder.FullName -File -Filter "*.zip"
104+
if ($zipFiles.Count -eq 0) {
105+
Write-PSFMessage -Level Host -Message "No module zip files found in the package. Please ensure that the package is extracted correctly."
106+
Stop-PSFFunction -Message "Stopping because of missing zip files."
107+
return
108+
}
109+
110+
$numberOfInstalledModules = 0
111+
$packageDetails.Modules | ForEach-Object {
112+
$moduleZip = $zipFiles | Where-Object Name -eq "dynamicsax-$($_.Name).$($_.Version).zip"
113+
if (-not $moduleZip) {
114+
Write-PSFMessage -Level Host -Message "No module zip file found for module $($_.Name). Please ensure that the package is extracted correctly."
115+
Stop-PSFFunction -Message "Stopping because of missing module zip file."
116+
return
117+
}
118+
119+
# Delete existing module folder if it exists
120+
$moduleFolderPath = Join-Path -Path $MetaDataDir -ChildPath $($_.Name)
121+
if (Test-Path -Path $moduleFolderPath) {
122+
Remove-Item -Path $moduleFolderPath -Recurse -Force
123+
Write-PSFMessage -Level Verbose -Message "Deleted existing module folder $moduleFolderPath"
124+
}
125+
126+
# Unzip to $MetaDataDir
127+
$moduleZipPath = Join-Path -Path $MetaDataDir -ChildPath $($_.Name)
128+
Expand-Archive -Path $moduleZip.FullName -DestinationPath $moduleZipPath
129+
Write-PSFMessage -Level Verbose -Message "Unzipped module $($_.Name) to $moduleZipPath"
130+
$numberOfInstalledModules++
131+
}
132+
133+
Write-PSFMessage -Level Host -Message "Installed $numberOfInstalledModules module(s) into $MetaDataDir"
134+
135+
Invoke-TimeSignal -End
123136

124137
}

0 commit comments

Comments
 (0)