Skip to content

Commit

Permalink
Use HTML Agility Pack to work around "Windows Security Warning" when …
Browse files Browse the repository at this point in the history
…using Invoke-WebRequest
  • Loading branch information
flcdrg committed Jul 8, 2017
1 parent 3b8704b commit 2078677
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 39 deletions.
1 change: 1 addition & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ install:
- ps: $PSVersionTable
- git --version
- choco --version
- nuget install -excludeversion
- ps: |
git clone -q https://github.com/majkinetor/au.git $Env:TEMP/au
. "$Env:TEMP/au/scripts/Install-AU.ps1" $Env:au_version
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Update-Force-Test*
update_info.xml
api_key
*.vsix
/packages
6 changes: 6 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="packages" />
</config>
</configuration>
5 changes: 5 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ReverseMarkdown" version="0.1.25" targetFramework="net45" />
<package id="HtmlAgilityPack" version="1.5.1" targetFramework="net45" />
</packages>
91 changes: 52 additions & 39 deletions typescript-vs2015/update.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import-module au

Add-type -Path ..\packages\HtmlAgilityPack\lib\Net45\HtmlAgilityPack.dll

$releases = 'https://www.microsoft.com/en-us/download/details.aspx?id=48593'

function global:au_SearchReplace {
Expand All @@ -18,51 +20,62 @@ function global:au_SearchReplace {

function global:au_GetLatest {

# http://download.microsoft.com/download/6/D/8/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA/2.3.3-TS-release-dev14update3-20170519.1/TypeScript_Dev14Full.exe
$download_page = Invoke-WebRequest -Uri $releases #1

$links = $download_page.links
$url32 = $links | ? href -match '.exe$' | select -First 1 -expand href

$releaseNotes = $links | Where-Object { $_.href.StartsWith("https://github.com/Microsoft/TypeScript/releases/tag/") } | Select-Object -First 1 -ExpandProperty Href

# version in filename isn't always complete, so we search for text description before link
$all = $download_page.ParsedHtml.body.all
$a = $all | Where { $_.tagName -eq 'a' -and $_.href -eq $url32 }
$li = $a.parentElement

# Use HTTPS
$builder = [UriBuilder] $url32
$builder.Scheme = "https"
$builder.Port = 443
$url32 = $builder.Uri.ToString()

# TypeScript 2.3.0 RC for Visual Studio 2015
if ($li.innerText -match "^TypeScript (?<version>(?<digits>\d+\.\d+\.\d+)( \w*)?) for Visual Studio 2015.*$") {

$digits = $Matches.digits # 2.3.0
$installVersion = $Matches.version.Replace($digits, $digits + ".0") #2.3.0.0 RC

# semver 2.3.0-RC
$version = $installVersion.Replace(" ", "-")

$Latest = @{
URL32 = $url32;
Version = $version;
ReleaseNotes = $releaseNotes
InstallVersion = $installVersion
}
} else {
$Latest = @{}
}

try {
# http://download.microsoft.com/download/6/D/8/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA/2.3.3-TS-release-dev14update3-20170519.1/TypeScript_Dev14Full.exe

$htmlWeb = New-Object HtmlAgilityPack.HtmlWeb
$htmlWeb.AutoDetectEncoding = $true
$doc = $htmlWeb.Load($releases)

$links = $doc.DocumentNode.SelectNodes("//a[substring(@href, string-length(@href) - string-length('.exe') +1) = '.exe']")
$link = $links[0]
$url32 = $link.Attributes["href"].Value

$li = $link.ParentNode

# first a should be release notes
$firstA = $li.SelectSingleNode("a")
$releaseNotes = $firstA.Attributes["href"].Value

# version in filename isn't always complete, so we search for text description before link

# Use HTTPS
$builder = [UriBuilder] $url32
$builder.Scheme = "https"
$builder.Port = 443
$url32 = $builder.Uri.ToString()

# TypeScript 2.3.0 RC for Visual Studio 2015
if ($li.InnerText -match "^TypeScript (?<version>(?<digits>\d+\.\d+\.\d+)( \w*)?) for Visual Studio 2015.*$") {

$digits = $Matches.digits # 2.3.0
$installVersion = $Matches.version.Replace($digits, $digits + ".0") #2.3.0.0 RC

# semver 2.3.0-RC
$version = $installVersion.Replace(" ", "-")

Write-Host $version

$Latest = @{
URL32 = $url32;
Version = $version;
ReleaseNotes = $releaseNotes
InstallVersion = $installVersion
}
} else {
$Latest = @{}
}
} catch {
Write-Error $_
$Latest = @{}
}
return $Latest
}

function global:au_AfterUpdate
{
$nuspecFileName = $Latest.PackageName + ".nuspec"
$nu = gc $nuspecFileName -Raw -Encoding UTF8
$nu = Get-Content $nuspecFileName -Raw -Encoding UTF8
$nu = $nu -replace "(?smi)(\<releaseNotes\>).*?(\</releaseNotes\>)", "`${1}$($Latest.ReleaseNotes)`$2"

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
Expand Down

0 comments on commit 2078677

Please sign in to comment.