Adjusted the build script behavior

Removed some absolute path, and refactored the code so
it handles the nested folder archives
This commit is contained in:
Samuel Vasko 2014-03-02 22:00:04 +01:00 committed by Jack
parent b4a264b1bc
commit 4c1f96c51d

View File

@ -46,6 +46,7 @@ function Ensure-Exists ($item) {
exit 1 exit 1
} }
} }
function Ensure-Executable ($command) { function Ensure-Executable ($command) {
try { Get-Command $command -ErrorAction Stop > $null} try { Get-Command $command -ErrorAction Stop > $null}
catch{ catch{
@ -53,51 +54,54 @@ function Ensure-Executable ($command) {
exit 1 exit 1
} }
} }
function Delete-Existing ($name) {
Write-Verbose "Change directory to $($name.path)"
Push-Location -Path $name.path
Write-Verbose "Remove $($name.name)" function Delete-Existing ($path) {
Remove-Item -Recurse -force $name.name -ErrorAction SilentlyContinue Write-Verbose "Remove $path"
Remove-Item -Recurse -force $path -ErrorAction SilentlyContinue
Pop-Location
} }
function Expand-Download{ function Expand-Download{
[CmdletBinding()] [CmdletBinding()]
Param( Param(
[psobject]$name [psobject]$package
) )
Push-Location -Path $name.path Write-Verbose "Extract $($package.name).tmp"
Write-Verbose "Extract $($name.package)"
# As if 7-zip doesn't have a silent output option. Append > `&null to the end to silence it. # As if 7-zip doesn't have a silent output option. Append > `&null to the end to silence it.
# Also silences the error output # Also silences the error output
Invoke-Expression "7z x -y -o$($name.name) $($name.package)"
Write-Verbose "Delete downloaded archive: $($name.package)" Write-Verbose "Delete downloaded archive: $($package.package)"
Remove-Item $name.package Remove-Item $package.package
Pop-Location
} }
# Check for requirements # Check for requirements
Ensure-Exists $sourcesPath Ensure-Exists $sourcesPath
Ensure-Executable "7z" Ensure-Executable "7z"
Push-Location -Path $saveTo
$sources = Get-Content $sourcesPath | Out-String | Convertfrom-Json $sources = Get-Content $sourcesPath | Out-String | Convertfrom-Json
foreach ($s in $sources) { foreach ($s in $sources) {
$s | Add-Member -MemberType NoteProperty -Name 'path' -Value $saveTo Write-Verbose "Getting $($s.name) from URL $($s.url)"
if( -not $s.package){
$filename = $s.name + '.' + $s.url.Split('.')[-1] # We do not care about the extensions/type of archive
$s | Add-Member -MemberType NoteProperty -Name 'package' -Value $filename $tempArchive = "$($s.name).tmp"
} Delete-Existing $tempArchive
Write-Verbose "URL $($s.url) has package $($s.package)" 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!" Write-Host "All good and done!"