From 4c1f96c51daec2f45f8996664b2553e39e41bfda Mon Sep 17 00:00:00 2001 From: Samuel Vasko Date: Sun, 2 Mar 2014 22:00:04 +0100 Subject: [PATCH 1/4] Adjusted the build script behavior Removed some absolute path, and refactored the code so it handles the nested folder archives --- scripts/build.ps1 | 56 +++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 72ec6de..abc255c 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -29,10 +29,10 @@ #> [CmdletBinding(SupportsShouldProcess=$true)] Param( - # CmdletBinding will give us; + # CmdletBinding will give us; # -verbose switch to turn on logging and # -whatif switch to not actually make changes - + # Path to the vendor configuration source file [string]$sourcesPath = "..\vendor\sources.json" @@ -46,6 +46,7 @@ function Ensure-Exists ($item) { exit 1 } } + function Ensure-Executable ($command) { try { Get-Command $command -ErrorAction Stop > $null} catch{ @@ -53,51 +54,54 @@ function Ensure-Executable ($command) { exit 1 } } -function Delete-Existing ($name) { - Write-Verbose "Change directory to $($name.path)" - Push-Location -Path $name.path - Write-Verbose "Remove $($name.name)" - Remove-Item -Recurse -force $name.name -ErrorAction SilentlyContinue - - Pop-Location +function Delete-Existing ($path) { + Write-Verbose "Remove $path" + Remove-Item -Recurse -force $path -ErrorAction SilentlyContinue } function Expand-Download{ [CmdletBinding()] Param( - [psobject]$name + [psobject]$package ) - Push-Location -Path $name.path - Write-Verbose "Extract $($name.package)" + Write-Verbose "Extract $($package.name).tmp" # As if 7-zip doesn't have a silent output option. Append > `&null to the end to silence it. # Also silences the error output - Invoke-Expression "7z x -y -o$($name.name) $($name.package)" - Write-Verbose "Delete downloaded archive: $($name.package)" - Remove-Item $name.package - - Pop-Location + Write-Verbose "Delete downloaded archive: $($package.package)" + Remove-Item $package.package } # Check for requirements Ensure-Exists $sourcesPath Ensure-Executable "7z" +Push-Location -Path $saveTo $sources = Get-Content $sourcesPath | Out-String | Convertfrom-Json foreach ($s in $sources) { - $s | Add-Member -MemberType NoteProperty -Name 'path' -Value $saveTo - if( -not $s.package){ - $filename = $s.name + '.' + $s.url.Split('.')[-1] - $s | Add-Member -MemberType NoteProperty -Name 'package' -Value $filename - } - Write-Verbose "URL $($s.url) has package $($s.package)" + Write-Verbose "Getting $($s.name) from URL $($s.url)" + + # We do not care about the extensions/type of archive + $tempArchive = "$($s.name).tmp" + Delete-Existing $tempArchive + Delete-Existing $s.name + + Invoke-WebRequest -Uri $s.url -OutFile $tempArchive + Invoke-Expression "7z x -y -o$($s.name) $tempArchive" + Remove-Item $tempArchive + + # Check for archives that were not extracted correctly + if ((Get-Childitem $s.name).Count -eq 1) { + $child = (Get-Childitem $s.name)[0] + Rename-Item $s.name -NewName "$($s.name)_moving" + Move-Item -Path "$($s.name)_moving\$child" -Destination $s.name + Remove-Item -Recurse "$($s.name)_moving" + } - Delete-Existing $s - Invoke-WebRequest -Uri $s.url -OutFile "H:\src\cmder\vendor\$($s.package)" - Expand-download $s -ErrorAction SilentlyContinue } +Pop-Location Write-Host "All good and done!" From c661b1f300f059674d679da6e9c0ebcc89d0c3b2 Mon Sep 17 00:00:00 2001 From: Samuel Vasko Date: Sun, 2 Mar 2014 22:04:58 +0100 Subject: [PATCH 2/4] Removed unused function --- scripts/build.ps1 | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index abc255c..1d06914 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -60,20 +60,6 @@ function Delete-Existing ($path) { Remove-Item -Recurse -force $path -ErrorAction SilentlyContinue } -function Expand-Download{ - [CmdletBinding()] - Param( - [psobject]$package - ) - Write-Verbose "Extract $($package.name).tmp" - - # As if 7-zip doesn't have a silent output option. Append > `&null to the end to silence it. - # Also silences the error output - - Write-Verbose "Delete downloaded archive: $($package.package)" - Remove-Item $package.package -} - # Check for requirements Ensure-Exists $sourcesPath Ensure-Executable "7z" From 5243d8bb8d1fb437019754e30934fac7568d41b2 Mon Sep 17 00:00:00 2001 From: Jack Bennett Date: Wed, 5 Mar 2014 13:28:18 +0000 Subject: [PATCH 3/4] In powershell you pretty much never want to use Write-Host, its output can't be redirected along the pipeline. You couldn't pipe this output to a logfile. --- scripts/build.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 6a7891d..863cd18 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -79,7 +79,7 @@ Push-Location -Path $saveTo $sources = Get-Content $sourcesPath | Out-String | Convertfrom-Json foreach ($s in $sources) { - Write-Host "Getting $($s.name) from URL $($s.url)" + Write-Output "Getting $($s.name) from URL $($s.url)" # We do not care about the extensions/type of archive $tempArchive = "$($s.name).tmp" @@ -97,4 +97,4 @@ foreach ($s in $sources) { } Pop-Location -Write-Host "All good and done!" +Write-Output "All good and done!" \ No newline at end of file From a9bfd2d2a268e8812c1489a47f3ae6c355dafd72 Mon Sep 17 00:00:00 2001 From: Jack Bennett Date: Wed, 5 Mar 2014 14:00:35 +0000 Subject: [PATCH 4/4] Silencing the output from 7z so I can actually see what else the script is doing. Adding some clearer dubugging info for cleaning up folders --- scripts/build.ps1 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 863cd18..465a784 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -56,8 +56,13 @@ function Ensure-Executable ($command) { } function Delete-Existing ($path) { - Write-Verbose "Remove $path" + Write-Verbose "Removing $path in $(get-location)" Remove-Item -Recurse -force $path -ErrorAction SilentlyContinue + if(test-path $path -IsValid){ + Write-Verbose " Removed: $true" + } else { + Write-Verbose " Removed: $false" + } } # Check for archives that were not extracted correctly @@ -87,7 +92,7 @@ foreach ($s in $sources) { Delete-Existing $s.name Invoke-WebRequest -Uri $s.url -OutFile $tempArchive -ErrorAction Stop - Invoke-Expression "7z x -y -o$($s.name) $tempArchive" + Invoke-Expression "7z x -y -o$($s.name) $tempArchive > `$null" Remove-Item $tempArchive if ((Get-Childitem $s.name).Count -eq 1) {