Update-Package.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Author: Miodrag Milic <miodrag.milic@gmail.com>
# Last Change: 12-Aug-2016.

<#
.SYNOPSIS
    Update automatic package
 
.DESCRIPTION
    This function is used to perform necessary updates to the specified files in the package.
    It shouldn't be used on its own but must be part of the script which defines two functions:
 
    - au_SearchReplace
      The function should return HashTable where keys are file paths and value is another HashTable
      where keys and values are standard search and replace strings
    - au_GetLatest
      Returns the HashTable where the script specifies information about new Version, new URLs and
      any other data. You can refer to this variable as the $Latest in the script.
      While Version is used to determine if updates to the package are needed, other arguments can
      be used in search and replace patterns or for whatever purpose.
 
    With those 2 functions defined, calling Update-Package will:
 
    - Call your au_GetLatest function to get remote version. It will also set $nuspec_version.
    - If remote version is higher then the nuspec version:
        - Check the returned URLs and Versions for validity (unless NoCheckXXX variables are specified).
        - Download files and calculate the checksum, (unless ChecksumFor is set to 'none').
        - Update the nuspec with the latest version.
        - Do the necessary file replacements.
        - Pack the files into the nuget package.
 
    You can also define au_BeforeUpdate and au_AfterUpdate functions to integrate your code into the
    update pipeline.
.EXAMPLE
    PS> notepad update.ps1
    # The following script is used to update the package from the github releases page.
    # Once it defines the 2 functions, it calls the Update-Package.
    # Checksums are automatically calculated for 32 bit version (the only one in this case)
    import-module au
 
    function global:au_SearchReplace {
        ".\tools\chocolateyInstall.ps1" = @{
            "(^[$]url32\s*=\s*)('.*')" = "`$1'$($Latest.URL32)'"
            "(^[$]checksum32\s*=\s*)('.*')" = "`$1'$($Latest.Checksum32)'"
        }
    }
 
    function global:au_GetLatest {
        $download_page = Invoke-WebRequest -Uri https://github.com/hluk/CopyQ/releases
 
        $re = "copyq-.*-setup.exe"
        $url = $download_page.links | ? href -match $re | select -First 1 -expand href
        $version = $url -split '-|.exe' | select -Last 1 -Skip 2
 
        return @{ URL32 = $url; Version = $version }
    }
 
    Update-Package -ChecksumFor 32
#>

function Update-Package {
    [CmdletBinding()]
    param(
        #Do not check URL and version for validity.
        #Defaults to global variable $au_NoCheckUrl if not specified.
        [switch] $NoCheckUrl,

        #Do not check if latest returned version already exists in the Chocolatey repository.
        #Defaults to global variable $au_NoCheckChocoVersion if not specified.
        #Ignored when Force is specified.
        [switch] $NoCheckChocoVersion,

        #Specify for which architectures to calculate checksum - all, 32 bit, 64 bit or none.
        [ValidateSet('all', '32', '64', 'none')]
        [string] $ChecksumFor='all',

        #Timeout for all web operations. The default can be specified in global variable $au_Timeout.
        #If not specified at all it defaults to 100 seconds.
        [int]    $Timeout,

        #Force package update even if no new version is found. This is useful for troubleshooting and updating checksums etc.
        #Defaults to global variable $au_Force if not specified.
        [switch] $Force
    )

    function Load-NuspecFile() {
        $nu = New-Object xml
        $nu.psbase.PreserveWhitespace = $true
        $nu.Load($nuspecFile)
        $nu
    }

    function check_url() {
        $Latest.Keys | ? {$_ -like 'url*' } | % {
            $url = $Latest[ $_ ]
            try
            {
                $response = request $url
                if ($response.ContentType -like '*text/html*') { $res = $false; $err="Latest $packageName URL content type is text/html" }
                else { $res = $true }
            }
            catch {
                $res = $false
                $err = $_
            }

            if (!$res) { throw "Can't validate latest $packageName URL (disable using `$NoCheckUrl) '$url'`n$err" }
        }
    }

    function request( $url ) {
        if ([string]::IsNullOrWhiteSpace($url)) {throw 'The URL is empty'}
        $request = [System.Net.HttpWebRequest]::Create($url)
        if ($Timeout)  { $request.Timeout = $Timeout*1000 }
        $request.GetResponse()
    }

    function check_version() {
        $re = '^(\d+)(\.\d+){0,3}$'
        if ($Latest.Version -notmatch $re) { throw "Latest $packageName version doesn't match the pattern '$re': '$($Latest.Version)'" }
    }

    function updated() {
        #Updated only if nuspec version is lower then online version. That will allow to update package revision manually on package errors.
        [version]($latest_version) -gt [version]($nuspec_version)
    }

    function get_checksum()
    {
        function invoke_installer() {
            Import-Module "$choco_tmp_path\helpers\chocolateyInstaller.psm1" -Force
            $env:chocolateyPackageName = "chocolatey\$packageName"

            if ($ChecksumFor -eq 'none') { "Automatic checksum calculation is disabled"; return }
            if ($ChecksumFor -eq 'all')  { $arch = '32','64' } else { $arch = $ChecksumFor }

            $pkg_path = "$Env:TEMP\chocolatey\$packageName\" + $global:Latest.Version
            $env:ChocolateyPackageVersion = $global:Latest.Version
            $env:ChocolateyAllowEmptyChecksums = 'true'
            foreach ($a in $arch) {
                $Env:chocolateyForceX86 = if ($a -eq '32') { 'true' } else { '' }
                try {
                    rm -force -recurse -ea ignore $pkg_path
                    .\tools\chocolateyInstall.ps1
                } catch {
                    if ( "$_" -notlike 'au_break: *') { throw $_ } else {
                        $filePath = "$_" -replace 'au_break: '
                        if (!(Test-Path $filePath)) { throw "Can't find file path to checksum" }

                        $item = gi $filePath
                        $hash = Get-FileHash $item -Algorithm 'SHA256'| % Hash

                        $global:Latest.Add('Checksum' + $a, $hash)
                        "Package downloaded and hash calculated for $a bit version"
                    }
                }
            }
        }

        function fix_choco {
            # Copy choco modules
            rm -recurse -ea ignore $choco_tmp_path
            cp -recurse -force $Env:ChocolateyInstall\helpers $choco_tmp_path\helpers
            cp -recurse -force $Env:ChocolateyInstall\extensions $choco_tmp_path\extensions

            $fun_path = "$choco_tmp_path\helpers\functions\Get-ChocolateyWebFile.ps1"
            (gc $fun_path) -replace '^\s+return \$fileFullPath\s*$', ' throw "au_break: $fileFullPath"' | sc $fun_path
        }

        "Automatic checksum started"

        # Copy choco powershell functions to TEMP dir and monkey patch the Get-ChocolateyWebFile function
        $choco_tmp_path = "$Env:TEMP\chocolatey\au\chocolatey"
        fix_choco

        # This will set the new URLS before the files are downloaded but will replace checksums to empty ones so download will not fail
        # because those still contain the checksums for the previous version.
        # SkipNuspecFile is passed so that if things fail here, nuspec file isn't updated; otherwise, on next run
        # AU will think that package is most recent
        #
        update_files -SkipNuspecFile | out-null

        # Invoke installer for each architecture to download files
        invoke_installer
    }

    function update_files( [switch]$SkipNuspecFile )
    {
        'Updating files'

        if (!$SkipNuspecFile) {
            " $(Split-Path $nuspecFile -Leaf)"
            if (updated) {
                " updating version: $nuspec_version -> $latest_version"
                $nu.package.metadata.version = "$latest_version"
                $nu.Save($nuspecFile)
            } else { " skipped because update is forced, current version is: $nuspec_version" }
        }

        $sr = au_SearchReplace
        $sr.Keys | % {
            $fileName = $_
            " $fileName"

            $fileContent = gc $fileName
            $sr[ $fileName ].GetEnumerator() | % {
                (' {0} = {1} ' -f $_.name, $_.value)
                if (!($fileContent -match $_.name)) { throw "Search pattern not found: '$($_.name)'" }
                $fileContent = $fileContent -replace $_.name, $_.value
            }

            $fileContent | Out-File -Encoding UTF8 $fileName
        }
    }

    if (!$Timeout) { $Timeout = $global:au_Timeout }
    if ($PSBoundParameters.Keys -notcontains 'NoCheckChocoVersion') { if ($global:au_NoCheckChocoVersion) { $NoCheckChocoVersion = $global:au_NoCheckChocoVersion } }
    if ($PSBoundParameters.Keys -notcontains 'NoCheckUrl')          { if ($global:au_NoCheckUrl) { $NoCheckUrl = $global:au_NoCheckUrl } }
    if ($PSBoundParameters.Keys -notcontains 'Force')               { if ($global:au_Force) { $Force = $global:au_Force } }

    $packageName = Split-Path $pwd -Leaf
    $nuspecFile = gi "$packageName.nuspec" -ea ig
    if (!$nuspecFile) {throw 'No nuspec file' }
    $nu = Load-NuspecFile
    $nuspec_version = $nu.package.metadata.version

    "$packageName - checking updates"
    try {
        $global:Latest = au_GetLatest
    } catch {
        throw "au_GetLatest failed`n$_"
    }
    $latest_version = $Latest.version

    if (!$NoCheckUrl) { check_url }
    check_version

    "nuspec version: $nuspec_version"
    "remote version: $latest_version"

    if (!(updated)) {
        if (!$Force) { 'No new version found'; return }
        else { 'No new version found, but update is forced' }
    }

    if (!($NoCheckChocoVersion -or $Force)) {
        $choco_url = "https://chocolatey.org/packages/{0}/{1}" -f $packageName, $latest_version
        try {
            request $choco_url | out-null
            "New version is available but it already exists in chocolatey (disable using `$NoCheckChocoVersion):`n $choco_url"
            return
        } catch { }
    }

    if (updated) { 'New version is available' }

    if ($ChecksumFor -ne 'none') { get_checksum }

    if (Test-Path Function:\au_BeforeUpdate) { 'Running au_BeforeUpdate'; au_BeforeUpdate }
    update_files
    if (Test-Path Function:\au_AfterUpdate) { 'Running au_AfterUpdate'; au_AfterUpdate }

    cpack
    return 'Package updated'
}

Set-Alias update Update-Package