Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
updated SecurityProtocol; Get-RemoteFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
majkinetor committed Dec 19, 2016
1 parent 8829e04 commit 4a0024d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
76 changes: 76 additions & 0 deletions AU/Public/Get-RemoteFiles.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Author: Miodrag Milic <[email protected]>
# Last Change: 19-Dec-2016.

<#
.SYNOPSIS
Get Latest URL32 and/or URL64 into tools directxory.
.DESCRIPTION
This function will download the binaries pointed to by $Latest.URL32 and $Latest.URL34.
The function is used to embed binaries into the Chocolatey package.
The function will keep original remote file name but it will add suffix _x32 or _x64.
This is intentional because you can use those to match particular installer via wildcards,
e.g. `gi *_x32.exe`.
#>
function Get-Embedded {
param (
# Delete existing file having $Latest.FileType extension.
# Otherwise, when state of the package remains after the update, older installers
# will pile up and may get included in the updated package.
[switch] $Purge,

# Override remote file name, use this one as a base. Suffixes _x32/_x64 are added.
# Use this parameter if remote URL doesn't contain file name but generated hash.
[string] $FileNameBase,

# By default last URL part is used as a file name. Use this paramter to skip parts
# if file name is specified earlier in the path.
[int] $FileNameSkip=0
)

function name4url($url) {
if ($FileNameBase) { return $FileNameBase }
$res = $url -split '/' | select -Last 1 -Skip $FileNameSkip
$res -replace '\.[a-zA-Z]+$'
}

function ext() {
if ($Latest.FileType) { return $Latest.FileType }
$url = $Latest.Url32; if (!$url) { $url = $Latest.Url64 }
if ($url -match '(?<=\.)[^.]+$') { return $Matches[0] }
}

$ext = ext
if (!$ext) { throw 'Unknown file type' }

if ($Purge) {
Write-Host 'Purging' $ext
rm -Force "$PSScriptRoot\tools\*.$ext"
}

try {
$client = New-Object System.Net.WebClient

if ($Latest.Url32) {
$base_name = name4url $Latest.Url32
$file_name = "{0}_x32.{1}" -f $base_name, $ext
$file_path = "$PSScriptRoot\tools\$file_name"

Write-Host "Downloading to $file_name -" $Latest.Url32
$client.DownloadFile($Latest.URL32, $file_path)
$Latest.Checksum32 = Get-FileHash $file_path | % Hash
}

if ($Latest.Url64) {
$base_name = name4url $Latest.Url64
$file_name = "{0}_x64.{1}" -f $base_name, $ext
$file_path = "$PSScriptRoot\tools\$file_name"

Write-Host "Downloading to $file_name -" $Latest.Url64
$client.DownloadFile($Latest.URL64, $file_path)
$Latest.Checksum64 = Get-FileHash $file_path | % Hash
}
} finally { $client.Dispose() }
}
3 changes: 2 additions & 1 deletion AU/Public/Update-Package.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Author: Miodrag Milic <[email protected]>
# Last Change: 13-Dec-2016.
# Last Change: 19-Dec-2016.

<#
.SYNOPSIS
Expand Down Expand Up @@ -300,6 +300,7 @@ function Update-Package {
$global:Latest.NuspecVersion = $package.NuspecVersion = '0.0'
}

[System.Net.ServicePointManager]::SecurityProtocol = 'Ssl3,Tls,Tls11,Tls12' #https://github.com/chocolatey/chocolatey-coreteampackages/issues/366
$module = $MyInvocation.MyCommand.ScriptBlock.Module
"{0} - checking updates using {1} version {2}" -f $package.Name, $module.Name, $module.Version | result
try {
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# AU Project Changelog

## NEXT

- New function `Get-RemoteFiles`. See [documentation](https://github.com/majkinetor/au#embedding-binaries).
- `Update-Package`
- Support newer TLS version support by setting the `SecurityProtocol` property of `ServicePointManager`.
- Added new function `Convert-ToEmbedded`.

### Bugfixes

- Fix encoding of nuspec (UTF-8 NO BOM) and ps1 (UTF-8 BOM) files.

## 2016.12.17

**NOTE**: Minimal PowerShell version required to run AU is now 5.0 instead of 4.0. This wont affect AppVeyor builds, but might affect local runs. Please update your local PowerShell version (`cinst powershell`) if you run it locally.
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,26 @@ Metapackages can reuse an AU updater of its dependency by the following way:

This is best understood via example - take a look at the [cpu-z](https://github.com/majkinetor/au-packages/blob/master/cpu-z/update.ps1) AU updater which uses the updater from the [cpu-z.install](https://github.com/majkinetor/au-packages/blob/master/cpu-z.install/update.ps1) package on which it depends. It overrides the `au_SearchReplace` function and the `update` call but keeps the `au_GetLatest`.


### Embedding binaries

Embedded packages do not download software from the Internet but contain binaries inside the package. This makes package way more stable as it doesn't depend on the network for installation. AU function `Get-RemoteFiles` can download files and save them in the package's `tools` directory. It does that by using the `$Latest.URL32` and/or `$Latest.URL64`.

The following example downloads files inside `au_BeforeUpdate` function which is called before the package files are updated with the latest data (function is not called if no update is available):

```powershell
...
function au_BeforeUpdate() {
#Download $Latest.URL32 / $Latest.URL64 in tools directory and remove any older installers.
Get-RemoteFiles -Purge
}
...
```

This function will also set the appropriate `$Latest.ChecksumXX`.

## Updating all packages

You can update all packages and optionally push them to the Chocolatey repository with a single command. Function `Update-AUPackages` (alias `updateall`) will iterate over `update.ps1` scripts and execute each in a separate thread. If it detects that a package is updated it will optionally try to push it to the Chocolatey repository and may also run configured plugins.
Expand Down

0 comments on commit 4a0024d

Please sign in to comment.