Skip to content

Commit

Permalink
fixes #128 using internal boxstarter function for downloading http re…
Browse files Browse the repository at this point in the history
…sources
  • Loading branch information
mwrock committed Dec 14, 2015
1 parent b582003 commit 481ee16
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 33 deletions.
1 change: 1 addition & 0 deletions BoxStarter.Common/Boxstarter.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Export-ModuleMember Confirm-Choice,`
Enter-BoxstarterLogable,`
Enter-DotNet4,`
Get-CurrentUser,`
Get-HttpResource,`
Get-IsMicrosoftUpdateEnabled,`
Get-IsRemote,`
Invoke-FromTask,`
Expand Down
22 changes: 1 addition & 21 deletions BoxStarter.Common/Enter-DotNet4.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Enable-Net40 {
if(!(test-path "$env:windir\Microsoft.Net\$fx\v4.0.30319")) {
if((Test-PendingReboot) -and $Boxstarter.RebootOk) {return Invoke-Reboot}
Write-BoxstarterMessage "Downloading .net 4.5..."
Get-HttpToFile "http://download.microsoft.com/download/b/a/4/ba4a7e71-2906-4b2d-a0e1-80cf16844f5f/dotnetfx45_full_x86_x64.exe" "$env:temp\net45.exe"
Get-HttpResource "http://download.microsoft.com/download/b/a/4/ba4a7e71-2906-4b2d-a0e1-80cf16844f5f/dotnetfx45_full_x86_x64.exe" "$env:temp\net45.exe"
Write-BoxstarterMessage "Installing .net 4.5..."
if(Get-IsRemote) {
Invoke-FromTask @"
Expand All @@ -59,23 +59,3 @@ Start-Process "$env:temp\net45.exe" -verb runas -wait -argumentList "/quiet /nor
}
}
}

function Get-HttpToFile ($url, $file){
Write-BoxstarterMessage "Downloading $url to $file" -Verbose
Invoke-RetriableScript -RetryScript {
if(Test-Path $args[1]){Remove-Item $args[1] -Force}
$downloader=new-object net.webclient
$wp=[system.net.WebProxy]::GetDefaultProxy()
$wp.UseDefaultCredentials=$true
$downloader.Proxy=$wp
try {
$downloader.DownloadFile($args[0], $args[1])
}
catch{
if($VerbosePreference -eq "Continue"){
Write-Error $($_.Exception | fl * -Force | Out-String)
}
throw $_
}
} $url $file
}
61 changes: 61 additions & 0 deletions BoxStarter.Common/Get-HttpResource.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function Get-HttpResource {
<#
.SYNOPSIS
Downloads the contents from a URL
.DESCRIPTION
Get-HttpResource downloads the contents of an HTTP url.
When -PassThru is specified it returns the string content.
.PARAMETER Url
The url containing the content to download.
.PARAMETER OutputPath
If provided, the content will be saved to this path.
.PARAMETER PassThru
If provided, the string will be output to the pipeline.
.EXAMPLE
$content = Get-HttpResource -Url 'http://my/url' `
-OutputPath 'c:\myfile.txt' `
-PassThru
This downloads the content located at http://my/url and
saves it to a file at c:\myfile.txt and also returns
the downloaded string.
.LINK
http://boxstarter.org
#>
param (
[string]$Url,
[string]$OutputPath = $null,
[switch]$PassThru
)
Write-BoxstarterMessage "Downloading $url" -Verbose
$str = Invoke-RetriableScript -RetryScript {
$downloader=new-object net.webclient
$wp=[system.net.WebProxy]::GetDefaultProxy()
$wp.UseDefaultCredentials=$true
$downloader.Proxy=$wp
try {
$httpString = $downloader.DownloadString($args[0])
if($args[1]) {
Write-BoxstarterMessage "Saving $httpString to $($args[1])" -Verbose
if(Test-Path $args[1]){Remove-Item $args[1] -Force}
$httpString | Out-File -FilePath $args[1] -Encoding utf8
}
$httpString
}
catch{
if($VerbosePreference -eq "Continue"){
Write-Error $($_.Exception | fl * -Force | Out-String)
}
throw $_
}
} $Url $OutputPath

if($PassThru) { Write-Output $str }
}
6 changes: 1 addition & 5 deletions Boxstarter.Chocolatey/New-PackageFromScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ about_boxstarter_chocolatey
Call-Chocolatey
$chocoInstall = [System.Environment]::GetEnvironmentVariable('ChocolateyInstall', 'MACHINE')
}
if(!(test-path function:\Get-WebFile)){
. "$chocoInstall\helpers\functions\Format-FileSize.ps1"
. "$chocoInstall\helpers\functions\Get-WebFile.ps1"
}
if($source -like "*://*"){
try {$text = Get-WebFile -url $Source -passthru } catch{
try {$text = Get-HttpResource -url $Source -passthru } catch{
throw "Unable to retrieve script from $source `r`nInner Exception is:`r`n$_"
}
}
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/nuget/Boxstarter.Azure.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<dependency id="WindowsAzureLibsForNet" version="2.5" />
</dependencies>
<releaseNotes>
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/nuget/Boxstarter.Bootstrapper.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<dependency id="Boxstarter.WinConfig" version="$version$" />
</dependencies>
<releaseNotes>
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/nuget/Boxstarter.Chocolatey.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<dependency id="Boxstarter.Bootstrapper" version="$version$" />
</dependencies>
<releaseNotes>
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/nuget/Boxstarter.Common.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Provides common functions used by multiple Boxstarter Modules.</description>
<releaseNotes xmlns="">
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/nuget/Boxstarter.HyperV.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<dependency id="Boxstarter.Chocolatey" version="$version$" />
</dependencies>
<releaseNotes>
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/nuget/Boxstarter.TestRunner.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<dependency id="Boxstarter.Azure" version="$version$" />
</dependencies>
<releaseNotes>
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/nuget/Boxstarter.WinConfig.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<dependency id="Boxstarter.Common" version="$version$" />
</dependencies>
<releaseNotes xmlns="">
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/nuget/Boxstarter.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<dependency id="Boxstarter.HyperV" version="$version$" />
</dependencies>
<releaseNotes>
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/releaseNotes.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<releaseNotes>
- Use internal function to download http gist scripts.
- No longer vendors a legacy version of Chocolatey. Leverages the current beta Chocolatey library API for all Chocolatey interaction.
</releaseNotes>
6 changes: 3 additions & 3 deletions tests/Chocolatey/New-PackageFromScript.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Describe "New-PackageFromScript" {
Context "When Building a script from a URL" {
MkDir $unzipPath
. "$env:chocolateyinstall\helpers\functions\Get-WebFile.ps1"
Mock Get-WebFile {return "return"}
Mock Get-HttpResource {return "return"}

($result = New-PackageFromScript "file://$($boxstarter.Basedir)/script.ps1") | Out-Null

Expand All @@ -56,7 +56,7 @@ Describe "New-PackageFromScript" {

Context "When http client throws an error" {
. "$env:chocolateyinstall\helpers\functions\Get-WebFile.ps1"
Mock Get-WebFile {throw "blah"}
Mock Get-HttpResource {throw "blah"}
Mock New-BoxstarterPackage

try {($result = New-PackageFromScript "file://$($boxstarter.Basedir)/script.ps1") | Out-Null}catch{}
Expand Down Expand Up @@ -95,4 +95,4 @@ Describe "New-PackageFromScript" {
}
}

}
}
8 changes: 4 additions & 4 deletions tests/Common/Enter-DotNet4.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Describe "Enter-DotNet4" {
Mock Test-Path { $true } -ParameterFilter { $path.EndsWith('v4.0.30319') }
Mock Test-PendingReboot { $false }
Mock Get-IsRemote { $false }
Mock Get-HttpToFile
Mock Get-HttpResource
Mock Start-Process { @{HasExited=$true} } -ParameterFilter { $FilePath.EndsWith("net45.exe") }
$PSVersionTable.CLRVersion = New-Object Version '4.0.0'

Expand All @@ -21,7 +21,7 @@ Describe "Enter-DotNet4" {
$result | should be "PID:$PID"
}
it 'should not download .net' {
Assert-MockCalled Get-HttpToFile -Times 0
Assert-MockCalled Get-HttpResource -Times 0
}
it 'should not install .net' {
Assert-MockCalled Start-Process -Times 0
Expand All @@ -34,7 +34,7 @@ Describe "Enter-DotNet4" {
Enter-Dotnet4 { Write-Output "$($args[0]):$PID" } 'PID' | Out-Null

it 'downloads .net 4' {
Assert-MockCalled Get-HttpToFile
Assert-MockCalled Get-HttpResource
}
it 'installs .net 4' {
Assert-MockCalled Start-Process
Expand All @@ -49,7 +49,7 @@ Describe "Enter-DotNet4" {
Enter-Dotnet4 { Write-Output "$($args[0]):$PID" } 'PID' | Out-Null

it 'downloads .net 4' {
Assert-MockCalled Get-HttpToFile
Assert-MockCalled Get-HttpResource
}
it 'does not installs .net 4 in process' {
Assert-MockCalled Start-Process -Times 0
Expand Down

0 comments on commit 481ee16

Please sign in to comment.