Last active
November 25, 2016 18:49
-
-
Save mdschweda/0e341d306f5a4bbdeb88bd4458037622 to your computer and use it in GitHub Desktop.
Installing prerequisites for .NET Core projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Global:required = "netcore", "nuget", "docfx" | |
. $PSScriptRoot\tools\prerequisites.ps1 | |
$tooldir = "$env:LOCALAPPDATA\.build" | |
$docs = "$PSScriptRoot\docs\docfx.json" | |
$projects = | |
"$PSScriptRoot\src\projectA", | |
"$PSScriptRoot\src\projectB" | |
$projects.ForEach({ & dotnet build $_ }) | |
& $tooldir\docfx\docfx $docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Converts a size in bytes to its upper most value. | |
.DESCRIPTION | |
Converts a size in bytes to its upper most value. | |
.PARAMETER Size | |
The size in bytes to convert | |
.NOTES | |
Author: Boe Prox | |
Date Created: 22AUG2012 | |
#> | |
function Convert-Size { | |
[CmdletBinding()] | |
Param ( | |
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] | |
[Alias("Length")] | |
[Int64]$Size | |
) | |
Begin { | |
If (-Not $ConvertSize) { | |
Write-Verbose ("Creating signature from Win32API") | |
$Signature = @" | |
[DllImport("Shlwapi.dll", CharSet = CharSet.Auto)] | |
public static extern long StrFormatByteSize( long fileSize, System.Text.StringBuilder buffer, int bufferSize ); | |
"@ | |
$Global:ConvertSize = Add-Type -Name SizeConverter -MemberDefinition $Signature -PassThru | |
} | |
Write-Verbose ("Building buffer for string") | |
$stringBuilder = New-Object Text.StringBuilder 1024 | |
} | |
Process { | |
Write-Verbose ("Converting {0} to upper most size" -f $Size) | |
$ConvertSize::StrFormatByteSize( $Size, $stringBuilder, $stringBuilder.Capacity ) | Out-Null | |
$stringBuilder.ToString() | |
} | |
} | |
<# | |
.Synopsis | |
Download file. | |
.DESCRIPTION | |
Downloads a file from a URL. | |
.PARAMETER Address | |
The URL of the file. | |
.PARAMETER Destination | |
The destination path of the file. | |
.PARAMETER Name | |
An optional download name to display. | |
.NOTES | |
Author: Marcus Schweda | |
#> | |
function Download-File { | |
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$Address, | |
[Parameter(Mandatory=$true)] | |
[string]$Destination, | |
[string]$Name = "file" | |
) | |
Begin{ | |
$activity = "Downloading $Name..." | |
$client = New-Object System.Net.WebClient | |
$src = $client.OpenRead($Address) | |
$completed = 0 | |
$total = $client.ResponseHeaders["Content-Length"] | |
$dest = New-Object System.IO.FileStream -ArgumentList $Destination, Create | |
$buffer = New-Object byte[] 10KB | |
$read = $src.Read($buffer,0, $buffer.length) | |
while ($read -gt 0) { | |
$completedStr = Convert-Size $completed | |
$completedP = ($completed / $total * 100) | |
$dest.Write($buffer, 0, $read) | |
$read = $src.Read($buffer, 0, $buffer.length) | |
Write-Progress -Activity $activity -CurrentOperation $completedStr -PercentComplete $completedP | |
$completed = $completed + $read | |
} | |
$dest.Flush() | |
$dest.Dispose() | |
$src.Dispose() | |
} | |
} | |
<# | |
.Synopsis | |
Latest version of a GitHub release. | |
.DESCRIPTION | |
Retrieves the latest stable version of a GitHub release from the GitHub API. | |
.PARAMETER Owner | |
The owner of the repository. | |
.PARAMETER Repo | |
The name of the repository. | |
.NOTES | |
Author: Marcus Schweda | |
#> | |
function Github-Latest { | |
[CmdletBinding()] | |
[OutputType([string])] | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$Owner, | |
[Parameter(Mandatory=$true)] | |
[string]$Repo | |
) | |
Begin { | |
$url = "https://api.github.com/repos/$Owner/$Repo/releases/latest" | |
Invoke-WebRequest $url | ConvertFrom-Json | Select -ExpandProperty tag_name | |
} | |
} | |
<# | |
.Synopsis | |
Latest download of a GitHub release. | |
.DESCRIPTION | |
Retrieves the download url of the latest stable version of a GitHub release from the GitHub API. | |
.PARAMETER Owner | |
The owner of the repository. | |
.PARAMETER Repo | |
The name of the repository. | |
.NOTES | |
Author: Marcus Schweda | |
#> | |
function Github-LatestUrl { | |
[CmdletBinding()] | |
[OutputType([string])] | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$Owner, | |
[Parameter(Mandatory=$true)] | |
[string]$Repo | |
) | |
Begin { | |
$url = "https://api.github.com/repos/$Owner/$Repo/releases/latest" | |
Invoke-WebRequest $url | ConvertFrom-Json | Select -ExpandProperty assets -first 1 | select -ExpandProperty browser_download_url | |
} | |
} | |
<# | |
.Synopsis | |
Date and time of NuGet GitHub release. | |
.DESCRIPTION | |
Retrieves the publication date of a GitHub release from the GitHub API. | |
.PARAMETER Owner | |
The owner of the repository. | |
.PARAMETER Repo | |
The name of the repository. | |
.NOTES | |
Author: Marcus Schweda | |
#> | |
function Github-When { | |
[CmdletBinding()] | |
[OutputType([datetime])] | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$Owner, | |
[Parameter(Mandatory=$true)] | |
[string]$Repo | |
) | |
Begin { | |
$url = "https://api.github.com/repos/$Owner/$Repo/releases/latest" | |
$dt = Invoke-WebRequest $url | ConvertFrom-Json | Select -ExpandProperty published_at | |
[datetime]::Parse($dt) | |
} | |
} | |
<# | |
.Synopsis | |
Latest version of a NuGet package. | |
.DESCRIPTION | |
Retrieves the latest stable version of a NuGet package from NuGet v3 API. | |
.PARAMETER PackageId | |
The exact Id of the package. The default value is "NuGet.CommandLine" (nuget.exe). | |
.NOTES | |
Author: Marcus Schweda | |
#> | |
function NuGet-Latest { | |
[CmdletBinding()] | |
[OutputType([string])] | |
Param | |
( | |
[Parameter()] | |
[string]$PackageId = "NuGet.CommandLine" | |
) | |
Begin { | |
$url = "https://api-v2v3search-0.nuget.org/query?q=packageid:$PackageId" | |
Invoke-WebRequest $url | ConvertFrom-Json | Select -ExpandProperty data -first 1 | Select -ExpandProperty version | |
} | |
} | |
<# | |
.Synopsis | |
Date and time of NuGet package publication. | |
.DESCRIPTION | |
Retrieves the publication date of a NuGet package from NuGet v3 API. | |
.PARAMETER PackageId | |
The exact Id of the package. The default value is "NuGet.CommandLine" (nuget.exe). | |
.PARAMETER Version | |
The version of the package. | |
.NOTES | |
Author: Marcus Schweda | |
#> | |
function NuGet-When { | |
[CmdletBinding()] | |
[OutputType([datetime])] | |
Param | |
( | |
[Parameter()] | |
[string]$PackageId = "NuGet.CommandLine", | |
[Parameter(Mandatory=$true)] | |
[string]$Version | |
) | |
Begin { | |
$url = "https://api.nuget.org/v3/registration0/$PackageId/$Version.json" | |
$dt = Invoke-WebRequest $url | ConvertFrom-Json | Select -ExpandProperty published | |
[datetime]::Parse($dt) | |
} | |
} | |
<# | |
.Synopsis | |
Create or update timestamp file. | |
.DESCRIPTION | |
Creates or updates a file with a timestamp for version checks. | |
.PARAMETER Directory | |
The directory to create the file in. | |
.PARAMETER Date | |
The date and time to save within the file. | |
.PARAMETER FileName | |
The name of the file. The default value is ".ver". | |
.NOTES | |
Author: Marcus Schweda | |
#> | |
function TimeStamp-Set { | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$Directory, | |
[Parameter(Mandatory=$true)] | |
[datetime]$Date = [datetime]::Now, | |
[string]$FileName = ".ver" | |
) | |
Begin { | |
$path = [System.IO.Path]::Combine($Directory, $FileName) | |
if ([System.IO.File]::Exists($path)) { | |
[System.IO.File]::SetAttributes($path, [System.IO.FileAttributes]::Normal) | |
} | |
[System.IO.File]::WriteAllText($path, $Date.ToString("o")) | |
[System.IO.File]::SetAttributes($path, [System.IO.FileAttributes]::Hidden) | |
} | |
} | |
<# | |
.Synopsis | |
Read timestamp file. | |
.DESCRIPTION | |
Reads a tmestamp from a file previously created with TimeStamp-Set. | |
.PARAMETER Directory | |
The directory of the file. | |
.PARAMETER FileName | |
The name of the file. The default value is ".ver". | |
.NOTES | |
Author: Marcus Schweda | |
#> | |
function TimeStamp-Get { | |
[CmdletBinding()] | |
[OutputType([datetime])] | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$Directory, | |
[string]$FileName = ".ver" | |
) | |
Begin { | |
$path = [System.IO.Path]::Combine($Directory, $FileName) | |
if (Test-Path $path) { | |
$dt = [System.IO.File]::ReadAllText($path) | |
[datetime]::ParseExact($dt, "o", [cultureinfo]::InvariantCulture) | |
} else { | |
[datetime]::MinValue | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ($Global:required -ne $null) { | |
. $PSScriptRoot\download.ps1 | |
$tooldir = "$env:LOCALAPPDATA\.build" | |
if (!(Test-Path $tooldir)) { | |
New-Item $tooldir -ItemType Directory | %{ $_.Attributes = "Hidden" } | |
} | |
## .NET Core ############################################################## | |
if ($Global:required.Contains("netcore")) { | |
if ((Get-Command "dotnet" -ErrorAction SilentlyContinue) -eq $null) { | |
Write-Host .NET Core not instaled. | |
& $PSScriptRoot\tools\dotnet-install.ps1 | |
} | |
} | |
## NuGet ################################################################## | |
if ($Global:required.Contains("nuget")) { | |
$dir = "$tooldir\nuget" | |
if (!(Test-Path $dir)) { | |
New-Item $dir -ItemType Directory | Out-Null | |
} | |
$ver = Nuget-Latest nuget.commandline | |
$published = NuGet-When nuget.commandline $ver | |
$update = $false | |
if (-not(Test-Path $dir\nuget.exe)) { | |
$update = $true | |
Write-Host NuGet not installed. | |
} else { | |
$installed = TimeStamp-Get $dir | |
if ($published -gt $installed) { | |
Write-Host New NuGet version available. | |
$update = $true | |
} | |
} | |
if ($update) { | |
$url = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" | |
Download-File $url $dir\nuget.exe nuget.exe | |
TimeStamp-Set $dir $published | |
} | |
} | |
## DocFX ################################################################## | |
if ($Global:required.Contains("docfx")) { | |
$dir = "$tooldir\docfx" | |
if (!(Test-Path $dir)) { | |
New-Item $dir -ItemType Directory | Out-Null | |
} | |
$published = Github-When dotnet docfx | |
if (-not(Test-Path $dir\docfx.exe)) { | |
$update = $true | |
Write-Host DocFX not installed. | |
} else { | |
$installed = TimeStamp-Get $dir | |
if ($published -gt $installed) { | |
Write-Host New DocFX version available. | |
$update = $true | |
} | |
} | |
if ($update) { | |
Get-ChildItem -Path $dir *.* | Remove-Item -Force | |
$url = Github-LatestUrl dotnet docfx | |
Download-File $url $dir\docfx.zip docfx.zip | |
TimeStamp-Set $dir $published | |
Write-Host Extracting... | |
Add-Type -AssemblyName System.IO.Compression.FileSystem | |
[System.IO.Compression.ZipFile]::ExtractToDirectory("$dir\docfx.zip", "$dir") | |
Remove-Item "$dir\docfx.zip" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment